3 aaa 12 aabaabaabaab 0
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
using namespace std;
#define mod 1000000007
#define inf 0x7f7f7f7f
int Next[1000005];
char s[1000005]; //看好题目,,数组开100W,我开了10WRE了好多次。。
int l;
void getNext()
{
int i,j;
i=0;
j=-1;
Next[i]=j;
while(i<l)
{
if(j==-1 || s[i]==s[j])
{
i++;
j++;
Next[i]=j;
}
else
j=Next[j];
}
}
int main()
{
int n;
int Case=0;
while(cin>>n,n)
{
scanf("%s",s);
l=strlen(s);
memset(Next,0,sizeof(Next));
getNext();
printf("Test case #%d\n",++Case);
int k;
// for(k=0;k<l;k++)
// cout<<Next[k]<<endl;
// cout<<Next[l]<<endl;
for(k=1; k<l; k++)
{
if(Next[k+1]!=0 && (k+1)%(k+1-Next[k+1])==0) //只有next数组不为0时才有循环节出现,,循环节的长度就是当前的长度减去next数组每次移动的长度。
cout<<k+1<<" "<<(k+1)/(k+1-Next[k+1])<<endl;
}
cout<<endl;
}
return 0;
}
原文:http://blog.csdn.net/sky_miange/article/details/44945957