题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358
还是KMP中nex数组的运用。
1 #include<cstdio> 2 #include<cstring> 3 const int maxn=1000010; 4 char p[maxn]; 5 int nex[maxn]; 6 int n; 7 void getnex() 8 { 9 10 int i=0,j=-1; 11 nex[0]=-1; 12 while(i<n) 13 { 14 if(j==-1||p[i]==p[j]) 15 nex[++i]=++j; 16 else j=nex[j]; 17 } 18 } 19 20 void KMP() 21 { 22 getnex(); 23 int i=2; 24 while(i<=n) 25 { 26 if(i%(i-nex[i])==0&&i!=i-nex[i]) 27 { 28 printf("%d %d ",i,i/(i-nex[i])); 29 } 30 i++; 31 } 32 } 33 34 int main() 35 { 36 37 int cas=0; 38 while(scanf("%d",&n)&&n) 39 { 40 scanf("%s",p); 41 printf("Test case #%d ",++cas); 42 KMP(); 43 printf(" "); 44 } 45 return 0; 46 }