POJ KMP 题目总汇:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2315188.html
AC代码:
深入了解KMP的next数组:http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<queue>
#define MAXN 10000009
int next[MAXN];
char S[MAXN];
void GetNext(){
next[0]=-1;
int j=0;
int k=-1;
int len=strlen(S);
while(j<len){
if(k==-1||(S[j]==S[k])){
j++;
k++;
next[j]=k;
}else{
k=next[k];
}
}
}
int main(){
memset(S,0,sizeof(S));
while(~scanf("%s",S),(S[0]!='.')){
GetNext();
int m=strlen(S);
if(m%(m-next[m])==0)
printf("%d\n",m/(m-next[m]));
else printf("1\n");
memset(S,0,sizeof(S));
}
return 0;
}