
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXn = 1e6;
const int MAXnd = MAXn * 2;
const int MAXegl = MAXnd;
int head[MAXnd + 10], cntnex, nex[MAXegl + 10], to[MAXegl + 10];
inline void Insert(int u, int v) {
nex[++cntnex] = head[u];
head[u] = cntnex;
to[cntnex] = v;
}
int cntnd, last, link[MAXnd + 10], son[MAXnd + 10][26], cntep[MAXnd + 10], mxlen[MAXnd + 10];
void Init() {
cntnd = last = 1;
}
void Extend(int ch) {
int a, b, c;
a = c = last, b = last = ++cntnd;
cntep[b] = 1;
mxlen[b] = mxlen[a] + 1;
for (; c && !son[c][ch]; c = link[c]) son[c][ch] = b;
if (!c) {
link[b] = 1;
} else {
int d = son[c][ch];
if (mxlen[d] == mxlen[c] + 1) {
link[b] = d;
} else {
int e = ++cntnd;
link[e] = link[d];
// memcpy(son[e], son[d], sizeof(son[e]));
copy(begin(son[d]), end(son[d]), begin(son[e]));
cntep[e] = cntep[d];
mxlen[e] = mxlen[c] + 1;
link[d] = link[b] = e;
for (; c && son[c][ch] == d; c = link[c]) son[c][ch] = e;
}
}
}
int ans;
void Dfs(int cur) {
for (int i = head[cur]; i; i = nex[i]) {
Dfs(to[i]);
cntep[cur] += cntep[to[i]];
}
if (cntep[cur] > 1) ans = max(ans, cntep[cur] * mxlen[cur]);
}
char str[MAXn + 10]; int n;
signed main() {
Init();
scanf("%s", str + 1);
n = strlen(str + 1);
for (int i = 1; i <= n; ++i) {
Extend(str[i] - 'a');
}
for (int i = 2; i <= cntnd; ++i) {
Insert(link[i], i);
}
Dfs(1);
printf("%lld\n", ans);
return 0;
}
过不去的数据:
aabab
正确输出:
4
我的输出:
6