poj 1961 Period 把主串的每一种前缀当作小主串,如果小主串的子串在小主串中叠加次数大于1,输出小主串长度及叠加次数。

  1. #include<stdio.h>  
  2. #define M 1000010  
  3. int i,n,next[M];  
  4. char s[M];  
  5. void getNext()  
  6. {  
  7.         int j=-1;  
  8.         next[0]=-1;  
  9.         for(i=1;s[i];i++){  
  10.                 while(j!=-1&&s[j+1]!=s[i])j=next[j];  
  11.                 if(s[j+1]==s[i])j++;  
  12.                 next[i]=j;  
  13.         }  
  14. }  
  15. int main()  
  16. {  
  17.         int k=0;  
  18.         while(scanf("%d",&n),n){  
  19.                 scanf("%s",s);  
  20.                 printf("Test case #%d\n",++k);  
  21.                 getNext();  
  22.                 for(i=1;i<=n;i++)  
  23.                         if(i%(i-next[i-1]-1)==0&&next[i-1]>-1)  
  24.                                 printf("%d %d\n",i,i/(i-next[i-1]-1));  
  25.                 puts("");  
  26.         }  
  27.         return 0;  
  28. }  
posted @ 2017-11-11 23:05  can丶  阅读(124)  评论(0编辑  收藏  举报