1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 int nextt[1100000]; 6 int l; 7 char s[1100000]; 8 void getnext(){ 9 int i=0,j=-1; 10 nextt[0]=-1; 11 while(i<l){ 12 if(j<0||s[i]==s[j]){ 13 nextt[++i]=++j; 14 } 15 else j=nextt[j]; 16 } 17 } 18 19 int main() 20 { 21 while(~scanf("%s",s)&&s[0]!=‘.‘){ 22 l=strlen(s); 23 getnext(); 24 int ans=1; 25 if(l%(l-nextt[l])==0) 26 ans=l/(l-nextt[l]); 27 printf("%d\n",ans); 28 } 29 }
原文:https://www.cnblogs.com/csx-zzh/p/13672517.html