您的位置:首页 > 其它

hdu1358 循环节

2013-03-16 19:12 120 查看
题意:给定一个字符串,求该串长度为i的前缀的最大循环周期(前提是该前缀是循环串)。
#include<stdio.h>

#define N 1000005
char s
;
int next
,n;

void getnext()
{
int i,j;
next[0]=0;
for(i=1,j=0;i<n;i++)
{
while(j>0&&s[i]!=s[j])
j=next[j-1];
if(s[i]==s[j])
j++;
next[i]=j;
}
}

int main()
{
int i,Case;
Case=0;
while(scanf("%d",&n),n)
{
printf("Test case #%d\n",++Case);
scanf("%s",s);
getnext();
for(i=2;i<=n;i++)
{
if(i%(i-next[i-1])==0&&i/(i-next[i-1])>1)
{
printf("%d %d\n",i,i/(i-next[i-1]));
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: