令失配函数为f[i],由失配函数的定义可知i-f[i]就是最小重复子串的长度。
完整代码:
/*0.066s*/
#include<cstdio>
const int maxn = 1000005;
char P[maxn];
int f[maxn];
int main()
{
int n, kase = 0;
while (scanf("%d", &n), n)
{
getchar();
gets(P);
f[0] = f[1] = 0;
for (int i = 1; i < n; i++)
{
int j = f[i];
while (j && P[i] != P[j]) j = f[j];
f[i + 1] = (P[i] == P[j] ? j + 1 : 0);
}
printf("Test case #%d\n", ++kase);
for (int i = 2; i <= n; i++)
if (f[i] && i % (i - f[i]) == 0)
printf("%d %d\n", i, i / (i - f[i]));
putchar(10);
}
return 0;
}