KMP 求最小循环节,power string
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int M=1e6+10;
char s[M];
int n,nex[M];
void getnext()
{
int k=-1,j=0;
nex[0]=-1;
while(j<n)
if(k==-1||s[j]==s[k]) nex[++j]=++k;
else k=nex[k];
}
int main(){
while(~scanf("%s",s)){
if(s[0]=='.')break;
n=strlen(s);
getnext();
int k=n-nex[n];
if(n%k==0)printf("%d\n",n/k);
else puts("1");
}
return 0;
}
还没有评论,来说两句吧...