http://acm.hdu.edu.cn/showproblem.php?pid=1358
求某个前缀含几个循环 用kmp的next数组
#include <cstdio>
#include <cstring>
using namespace std;
int Next[1000005];
char c[1000005];
int main()
{
int len;
int cnt=1;
while(scanf("%d",&len)&&len)
{
memset(Next,0,sizeof(Next));
scanf("%s",&c);
printf("Test case #%d\n",cnt);
int i = 0,j=-1;
Next[0]=-1;
while(i<len)
{
if(j==-1||c[i]==c[j])
{
i++;j++;
Next[i]=j;
}
else
j=Next[j];
}
for(int i = 1; i<=len; i++)
if(i%(i-Next[i])==0&&Next[i])
printf("%d %d\n",i,i/(i-Next[i]));
printf("\n");
cnt++;
}
}