给出一个字符串,找出所有可以作为它循环节的子串长度
利用kmp的失配数组的性质,可以直接做
1 #include<stdio.h>
2 #include<string.h>
3
4 const int maxm=1e6+5;
5
6 char t[maxm];
7 int p[maxm];
8
9 int main(){
10 int m;
11 int cnt=0;
12 while(scanf("%d",&m)!=EOF&&m){
13 int i,j;
14 scanf("%s",t);
15 p[0]=p[1]=0;
16 for(i=1;i<m;i++){
17 j=p[i];
18 while(j&&t[i]!=t[j])j=p[j];
19 p[i+1]=t[i]==t[j]?j+1:0;
20 }
21 printf("Test case #%d
",++cnt);
22 for(int i=1;i<=m;++i){
23 if(p[i]!=0&&!(p[i]%(i-p[i])))printf("%d %d
",i,p[i]/(i-p[i])+1);
24 }
25 printf("
");
26 }
27 return 0;
28 }