KMP 求循环节
next[i]表示前i个字符中最长的相同前缀后缀,因而i %(i-next[i]) == 0时代表前i个字符构成了循环节,
但是next[i]不能为0,例如abcde,next[5] = 0 ,单很明显该字符串不是循环的串
#include <cstdio>
int n,next[1010101],cas;
char a[1010101];
void getN(){
int i,j;
i = 0;
j = next[0] = -1;
while(i<n){
while(j != -1&&a[i] != a[j]) j = next[j];
next[++i] = ++j;
}
}
void cal(){
getN();
for(int i = 2;i<=n;i++){
if(i %(i-next[i]) == 0 && i != i - next[i] )
printf("%d %d \n",i,i/(i-next[i]) );
}
}
int main(){
while(~scanf("%d",&n)&&n){
scanf("%s",a);
printf("Test case #%d\n",++cas);
cal();
printf("\n");
}
return 0;
}