转自http://blog.youkuaiyun.com/alongela/article/details/8196915#
我的代码TLE了,
但是他的代码虽然对了,不好理解。我改了一丁点
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int Next[400005];
char str[400005];
int ans[400005];
int cnt;
int len;
void getNext()
{
Next[0] = -1;
int i = 0, j = -1;
while (i < len)
{
if (j == -1 || str[i] == str[j])
{
++i;
++j;
Next[i] = j;
}
else j = Next[j];
}
}
int main()
{
while (scanf("%s", str) != EOF)
{
len = strlen(str);
getNext();
cnt = 0;
int t = Next[len];
while (t != -1)
{
if (str[t-1] == str[len - 1]) ans[cnt++] = t;//改了这里的部分
t = Next[t];//类似二分似得缩小判断的范围 还是挺难想到的。
}
/*
for(int i=1;i<=len;++i)
cout<<Next[i]<<' ';cout<<'\n';
*/
for (int i = cnt - 1; i >= 0; --i)
{
printf("%d ", ans[i]);
}
printf("%d\n", len);
}
return 0;
}
687

被折叠的 条评论
为什么被折叠?



