KMP算法之三

//poj 2406 Power Strings  
#include <iostream> //KMP算法
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()
{
while(scanf("%s",B+1)&&B[1]!='.')
{
for(m=0;B[m+1]!='\0';++m);
get_next();
if(m%(m-next[m])==0)
printf(
"%d\n",m/(m-next[m]));
else
printf(
"1\n");
}
return 0;
}

  

posted on 2011-08-22 12:18  sysu_mjc  阅读(168)  评论(0编辑  收藏  举报

导航