LA3026 POJ1961 period 字符串周期(KMP) POJ2406 Power Strings

POJ 1961 http://poj.org/problem?id=1961  Period


Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the 
number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3
aaa
12
aabaabaabaab
0

Sample Output

Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4

题意及思路:

//求出所有前缀(满足是两个或两个以上的循环节组成的字符串)
//输出前缀的位置[0-i-1]的i,以及循环节的个数
//KMP求解fail数组,i-f[i]就是循环节长度

//求出所有前缀(满足是两个或两个以上的循环节组成的字符串)
//输出前缀的位置[0-i-1]的i,以及循环节的个数
//KMP求解fail数组,i-f[i]就是循环节长度
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1000000 + 10;
char s[maxn];
int f[maxn];
int main()
{
    int n,cas=1;
    while(scanf("%d",&n)==1&&n){
        scanf("%s",s);

        //求fail数组
        f[0]=f[1]=0;
        for(int i=1;i0&&i%length==0) printf("%d %d\n",i,i/length);
        }
        printf("\n");
    }
    return 0;
}












POJ2406 http://poj.org/problem?id=2406  Power Strings


Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

题意及思路:

//比上一题简单,只需判断最后一个即可
//有则输出n/(n-f[n]),表示原串的都是次幂,就是几个原串的组成
//无则输出1,表示无循环就是原串的1次幂


//比上一题简单,只需判断最后一个即可
//有则输出n/(n-f[n]),表示原串的都是次幂,就是几个原串的组成
//无则输出1,表示无循环就是原串的1次幂

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1000000 + 10;
char s[maxn];
int f[maxn];
int main()
{
    int n,cas=1;
    while(scanf("%s",s)&&s[0]!='.'){
        n=strlen(s);
        
        //求fail数组
        f[0]=f[1]=0;
        for(int i=1;i0&&n%(n-f[n])==0) //满足上题的条件即可
            printf("%d\n",n/(n-f[n]));
        else printf("1\n");
    }
    return 0;
}






你可能感兴趣的:(字符串)