利用next数组求解
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1000000 + 10;
char str[maxn];
int next[maxn];
int len;
int cas;
int n;
void getNext()
{
int i = 0, j = -1;
next[0] = -1;
while (i != len){
if (j == -1 || str[i] == str[j]){
next[++i] = ++j;
}
else
j = next[j];
}
}
int main()
{
cas = 0;
while (scanf("%d", &len), len){
scanf("%s", str);
printf("Test case #%d\n", ++cas);
getNext();
for (int i = 2; i <= len; i++){
if (i % (i - next[i]) == 0 && i / (i - next[i]) != 1){
printf("%d %d\n", i, i / (i - next[i]));
}
}
printf("\n");
}
return 0;
}