找到一个最短的循环串,看通过几次的循环得到的这个字符串!
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int nex[1000005];
char str[1000005];
void getnex()
{
nex[0]=-1;
int lenth=strlen(str),i=0,j=-1;
while(i<lenth)
{
if(str[i]==str[j]||j==-1)
{
++i,++j;
nex[i]=j;
}
else
j=nex[j];
}
}
int main()
{
while(scanf("%s",str)&&str[0]!='.')
{
int lenth=strlen(str);
getnex();
if(lenth%(lenth-nex[lenth])==0)
cout<<lenth/(lenth-nex[lenth])<<endl;
else
cout<<"1"<<endl;
}
return 0;
}