poj 1961 Period

#include <iostream>        //KMP算法,参考poj 2406 Power Strings
using namespace std;
char B[2000000];
int m,next[2000000];
void get_next()
{
next[
1]=0;
int j=0;
for(int i=2;i<=m;++i)
{
while(j>0&&B[j+1]!=B[i])
j
=next[j];
if(B[j+1]==B[i])
j
=j+1;
next[i]
=j;
}
}
int main()
{
int num=1;
while(scanf("%d",&m)&&m)
{
scanf(
"%s",B+1);
get_next();
printf(
"Test case #%d\n",num++);
for(int i=2;i<=m;++i)
if(next[i]>0&&i%(i-next[i])==0)
printf(
"%d %d\n",i,i/(i-next[i]));
printf(
"\n");
}
return 0;
}

  

posted on 2011-07-18 11:30  sysu_mjc  阅读(103)  评论(0编辑  收藏  举报

导航