poj 2406 Power Strings求子串在主串中最多叠加次数

  1. #include<stdio.h>  
  2. #define M 1000010  
  3. int n,next[M];  
  4. char s[M];  
  5. void getNext()  
  6. {  
  7.         int i=1,j=-1;  
  8.         next[0]=-1;  
  9.         for(;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.         n=i;  
  15. }  
  16. int main()  
  17. {  
  18.         while(scanf("%s",s),s[0]!='.'){  
  19.                 getNext();  
  20.                 if(n%(n-next[n-1]-1))puts("1");  
  21.                 else printf("%d\n",n/(n-next[n-1]-1));  
  22.         }  
  23.         return 0;  
  24. }  
posted @ 2017-11-10 22:50  can丶  阅读(171)  评论(0编辑  收藏  举报