正题
链接:
http://poj.org/problem?id=2406
大意
一个字符串,求最小循环节。
解题思路
用KMP求循环节。
因为如果有循环节那么一定是l-next[l]。只需要判断一下l-next[l]是否可以被l整除就好了
代码
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000051];
int l,p[1000051];
void ycl()//KMP自我匹配
{
p[0]=-1;
for (int i=1,j=-1;i<l;i++)
{
while (j>=0&&s[i]!=s[j+1]) j=p[j];
if (s[i]==s[j+1]) j++;
p[i]=j;
}
}
int main()
{
while (true)
{
scanf("%s",s);
if (s[0]=='.') break;
l=strlen(s);
ycl();
if (l%(l-p[l-1]-1)) printf("1");
else printf("%d",l/(l-p[l-1]-1));//输出
printf("\n");
}
}