Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16580 Accepted Submission(s): 7717

Problem 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 AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input
The input file 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

题目大意:
给你一个长度为n的字符串,问你它的前缀字符串中相同的字符串个数是多少个,以及总长度。
思路:
利用kmp的next[]数组求每个前后缀最大字串长度

通过next数组可以发现我们要的答案满足这样一个条件,我们将子串当前长度用pos表示,pos%(pos-next[pos])==0这样就把其他错误答案排除了最后输出一下就行,一开始我老是想着用%去取循环节长度,然后用总长度做除法,公式就是:(i+1)/(i+1)%next[i]
(长度从0开始) 然后aabaab让我头疼,长度=6,6/(6%3),分母为0了。。。。因为我怕有的循环节个数很少,长度很低,减法做不出来,最后发现减法也是可行的。。。。。因为(i+1)%(i+1)-next[i],实际上相当于以及做了模运算了,然后答案的特殊性刚好满足上面的公式。。。。

代码:

#include<iostream>
#include<vector>
using namespace std;

const int maxn=1e6+10;
int n;
char str[maxn];
vector<int> getnext(char str[]){
	vector<int>next(n);
	for(int j=1,k=0;j<n;j++){
		while(k>0&&str[j]!=str[k]){
			k=next[k-1];
		}
		if(str[j]==str[k])
		next[j]=++k;
		else
		next[j]=k;
	}
	return next;
}
int main(){
	int T=1;
	while(scanf("%d",&n)&&n){
		printf("Test case #%d\n",T++);
		for(int i=0;i<n;i++){
			scanf(" %c",&str[i]);
		}
		vector<int>next=getnext(str);
		//for(int i=0;i<n;i++)printf("%c ",str[i]);puts("");
		for(int i=0;i<n;i++){
			if(!next[i])continue;
			else{
				int l=(i+1)-next[i];
				if((i+1)%l==0){
					printf("%d %d\n",(i+1),(i+1)/l);
				}
			}
		}
		printf("\n");
	}
}

代码2:

#include<iostream>
#include<vector>
using namespace std;

const int maxn=1e6+10;
int n;
char str[maxn];
vector<int> getnext(char str[]){
    vector<int>next(n+1);
    int j,k;
    j=0;k=-1;
    next[0]=-1;
    while(j<n){
        if(k==-1||str[j]==str[k]){
            j++;k++;
            next[j]=k;
        }else{
            k=next[k];
        }
    }
    
    
    return next;
}
int main(){
    int T=1;
    while(scanf("%d",&n)&&n){
        printf("Test case #%d\n",T++);
        for(int i=0;i<n;i++){
            scanf(" %c",&str[i]);
        }
        vector<int>next=getnext(str);
        for(int i=0;i<=n;i++){
            if(next[i]==0||next[i]==-1)continue;
                int l=i-next[i];
                if(i%l==0)
                printf("%d %d\n",i,i/l);
        }
        printf("\n");
    }
}