题目链接:点击打开链接
给你一个字符串,要你从小到大输出重复子串的长度。
利用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;
}