您的位置:首页 > 其它

POJ2752 Seek the Name, Seek the Fame(kmp)

2015-10-08 01:28 435 查看
题目链接:点击打开链接

给你一个字符串,要你从小到大输出重复子串的长度。

利用nxt数组性质,将长度从大到小保存到ans数组中,最后逆序输出答案。

AC代码:

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 4e5 + 5;
char s[MAXN];
int nxt[MAXN], ans[MAXN], len;
void get_nxt()
{
nxt[1] = 0;
int j = 0;
for(int i = 2; i <= len; ++i) {
while(j > 0 && s[j + 1] != s[i])
j = nxt[j];
if(s[j + 1] == s[i]) j++;
nxt[i] = j;
}
}
int main(int argc, char const *argv[])
{
while(scanf("%s", s + 1) != EOF) {
memset(nxt, 0, sizeof(nxt));
memset(ans, 0, sizeof(ans));
len = strlen(s + 1);
get_nxt();
int x = nxt[len], num = 0;
if(x) ans[num++] = x;
while(nxt[x] > 0) {
x = nxt[x];
ans[num++] = x;
}
for(int i = num - 1; i >= 0; --i)
printf("%d ", ans[i]);
printf("%d\n", len);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: