KMP next 数组实际上就是最长的i 使得的str[1~i] = str[n-i+1~n]
#include <iostream>
#include <string.h>#include <stdlib.h>
#include <stdio.h>
using namespace std;
int next[1000010];
void COMPUTE_PREFIX_FUNCTION(char P[], int m)
{
next[1]=0;
int k = next[1];
for(int q=2; q<=m; q++)
{
while(k>0&&P[k+1]!=P[q]) k=next[k];
if(P[k+1]==P[q]) k++;
next[q]=k;
}
}
int main()
{
char s[1000010];
int n, cases = 1;
while(scanf("%d",&n) != EOF && n)
{
getchar();
printf("Test case #%d\n",cases++);
scanf("%s",s+1);
COMPUTE_PREFIX_FUNCTION(s,n);
int len = strlen(s+1);
for( int i = 2; i <= len; i++ )
{
if((i - next[i]) == 0)
printf("%d %d\n",i,next[i]);
else if(i%(i-next[i]) == 0 && i/(i-next[i]) != 1)
printf("%d %d\n",i,i/(i-next[i]));
}
printf("\n");
}
}