# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int len; char a[1000010]; int next[1000010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[j]==a[i]) i++,j++,next[i]=j; else j=next[j]; } } int main() { while(~scanf("%s",a),a[0]!='.') { len=strlen(a); Getnext(); int ans=len-next[len];//最小循环节 if(len%ans)//不是最小循环节的倍数,没有循环 printf("1\n"); else { int flag=0; for(int i=ans*2;i<=len;i+=ans*2)//ans*2的倍数的next必是最小循环结的倍数,可以Y几个案例 { if(next[i]%ans) { flag=1; break; } } if(flag) printf("1\n"); else printf("%d\n",len/ans); } } return 0; }
poj 2406 Power Strings (KMP),布布扣,bubuko.com
原文地址:http://blog.csdn.net/lp_opai/article/details/38541931