主要思想见这篇博客:
https://blog.csdn.net/qq_40679299/article/details/79837560
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N=1e6;
int next[N+5];
char s[N+5];
int n;
void get_next(char t[])
{
//i is hou j is qian
int j=-1;
int i=0;
next[0]=-1;
while(i<n)
{
if(j==-1||t[i]==t[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
int casei=1;
while(scanf("%d",&n)==1&&n)
{
scanf("%s",s);
get_next(s);
// for(int i=1;i<=n;i++)
// printf("%d ",next[i]);
printf("Test case #%d\n",casei++);
for(int i=1;i<=n;i++)
{
if(i%(i-next[i])==0&&i!=i-next[i])
printf("%d %d\n",i,i/(i-next[i]));
}
cout<<endl;
}
return 0;
}