KMP算法的应用,求最小循环节
#include <iostream>
#include<cstring>
using namespace std;
#define MAX 1000005
char p[MAX];
int next[MAX],len;
void getNext()
{
int j,k;
next[0]=-1;
j=0;
k=-1;
while(j<len)
{
if(k==-1||p[k]==p[j])
{
k++;
j++;
next[j]=k;
}
else
k=next[k];
}
}
int main()
{
while(scanf("%s",p)!=EOF)
{
if(strcmp(p,".")==0)
{
return 0;
}
len=strlen(p);
getNext();
int k=next[len];
if(len%(len-k)==0)
{
cout<<len/(len-k)<<endl;
}
else
cout<<"1"<<endl;
}
return 0;
}