KMP的失配函数有一个特点,i表示前i个字符,若i%(i-f[i])==0,则前i个字符,最短循环节的长度为i-f[i]。。
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
const int maxn=1000100;
int next[maxn],n,ca=1;
string a;
void getnext()
{
int j=-1,i=0;
next[0]=-1;
while(i<n)
{
if(j==-1||a[i]==a[j])
{
j++,i++;
next[i]=j;
}
else j=next[j];
}
}
void Print()
{
printf("Test case #%d\n",ca++);
for(int i=2; i<=n; i++)
if(next[i]>0&&i%(i-next[i])==0)
printf("%d %d\n",i,i/(i-next[i]));
puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(cin>>n,n)
{
cin>>a;
getnext();
Print();
}
return 0;
}