https://vjudge.net/problem/UVA-1328
KMP求循环节的问题
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int n,l,j,cnt,et; 7 char s[1000010]; 8 int nt[1000010]; 9 int main(){ 10 while(scanf("%d",&n)==1&&n){ 11 scanf("%s",s+1); 12 j=0; 13 for(int i=2;i<=n;i++){ 14 while(j&&s[j+1]!=s[i]) j=nt[j]; 15 j+=(s[j+1]==s[i]); 16 nt[i]=j; 17 } 18 printf("Test case #%d ",++cnt); 19 for(int i=2;i<=n;i++) 20 if(nt[i]>0&&(i%(i-nt[i])==0)) printf("%d %d ",i,i/(i-nt[i])); 21 printf(" "); 22 } 23 return 0; 24 }