给出一个字符串,对于它的每个(each)前缀(prefix)长度 i (2<=i),求能由一个子串构成这个前缀的最大字串数量。注意用 i-next[j]获得反复字串的长度
#include<stdio.h>
const int Max = 1000005;
char str[Max];
int len, next[Max];
void get_next()
{
int j=1, k=0;
next[0] = -1;
next[1]=0;
while(j<len)
{
if(str[j] == str[k])
{
next[j+1]=k+1;
j++;
k++;
}
else if (k==0)
{
next[j+1]=0;
j++;
}
else k=next[k];
}
}
int main()
{
int i, testCase = 1;
while(scanf("%d", &len) && len)
{
scanf("%s", str);
get_next();
printf("Test case #%d\n", testCase++);
for(i = 2; i <= len; i ++)
if(next[i] != 0 && i % (i-next[i]) == 0)
printf("%d %d\n", i, i / (i-next[i]));
printf("\n");
}
return 0;
}