目录:
分析:
这道题目只要是学过 KMP K M P 算法的童鞋们,应该做起来都不难,也就是考察了学生的基础 KMP K M P ,这里就不过多赘述了
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
inline LL read() {
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
char s[1000001];
int len,next[1000001],w;
void getnext()
{
int j=-1;
next[0]=-1;
for (int i=1;i<len;i++)
{
while (j>-1 && s[j+1]!=s[i]) j=next[j];
if (s[j+1]==s[i]) j++;
next[i]=j;
}
}
int main()
{
while(scanf("%s",s),s[0]!='.')
{
int j;
memset(next,0,sizeof(next));
len=strlen(s);
getnext();
w=next[len-1]+1;
if(len%(len-w)==0)
printf("%d\n",len/(len-w));
else
printf("1\n");
}
return 0;
}