您的位置:首页 > 其它

POJ 1961 Period(KMP)

2012-07-20 13:44 441 查看
题目链接

和POJ2406差不多,不过用暴力去找,会超时。

#include <stdio.h>
#include <string.h>
#include <math.h>
#define N 1000001
char str
;
int next
;
int main()
{
int i,j,len,d,a = 0;
while(scanf("%d%*c",&len)!=EOF)
{
a ++;
if(len == 0)break;
scanf("%s",str);
memset(next,0,sizeof(next));
if(str[0] == '.')break;
next[0] = -1;
j = -1;
for(i = 1; i <= len-1; i ++)
{
while(j >= 0&&str[j+1] != str[i])
j = next[j];
if(str[j+1] == str[i]) j++;
next[i] = j;
}
printf("Test case #%d\n",a);
for(j = 1; j <= len; j ++)
{
d = j-1-next[j-1];
if(j % d == 0&&j/d != 1)
printf("%d %d\n",j,j/d);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: