您的位置:首页 > 其它

poj 2406 Power Strings(KMP)

2016-05-02 09:18 363 查看
题目链接:http://poj.org/problem?id=2406

#include <stdio.h>
#include <string.h>

char S[1000010];
int next[1000010];
int main()
{
int i,j,length,len;
while(scanf("%s",S)&&S[0] != '.')
{
i = 0;
j = -1;
next[0] = -1;
len = strlen(S);
while(i < len)
{
if(j == -1 || S[i] == S[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}

length = len - next[len];
if(len%length == 0)
printf("%d",i/length);
else
printf("1");

printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: