题目地址:http://poj.org/problem?id=2752
题目大意:求一个字符串中满足:前缀等于后缀的可能的子串的长度,串的长度小于等于400000。
解题思路:kMP,貌似发现了清华的数据结构书中的一点错误,明个再研究研究。。。
程序代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define N 400010
int next[N];
int res[N];
char str[N];
void GetNext()
{
next[0] = -1;
int i = 0;
int j = -1;
while (i < strlen(str))
{
if (j == -1 || (str[i] == str[j]))
{
i++;
j++;
// if (str[i] == str[j]) next[i] = next[j];
// else
next[i] = j;
}
else
{
j = next[j];
}
}
}
void getNext()
{
int i=1;
int j=-1;
next[0]=-1;
while (i < strlen(str))
{
while (j != -1 && str[j+1] != str[i])
j = next[j];
if (str[j+1] == str[i])j++;
next[i] = j;
i++;
}
}
int main()
{
while (~scanf("%s", str))
{
//GetNext();
getNext();
int j = strlen(str) - 1;
int cnt = 0;
while (j != -1)
{
res[cnt] = j + 1;
cnt++;
j = next[j];
}
for (int i = cnt - 1; i >= 0; i--)
{
printf("%d ", res[i]);
}
printf("\n");
}
return 0;
}