Luogu3804 【模板】后缀自动机
题面
题解
一个串的出现次数等于$endpos$的大小,也是$parent$树上节点的$size$大小,
构建出后缀自动机,按拓补序,模拟即可。
代码
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int maxn(1e6 + 10), maxm(maxn << 1);
int last = 1, cnt = 1;
char s[maxn];
long long ans;
int size[maxm], c[maxm], a[maxm];
struct node { int son[26], fa, len; } t[maxm];
void extend(int c)
{
node *p = t + last, *newp = &t[++cnt]; last = cnt;
newp -> len = p -> len + 1;
while(p - t && !p -> son[c]) p -> son[c] = newp - t, p = &t[p -> fa];
if(!(p - t)) newp -> fa = 1;
else
{
node *q = &t[p -> son[c]];
if(p -> len + 1 == q -> len) newp -> fa = q - t;
else
{
node *newq = &t[++cnt]; *newq = *q;
newq -> len = p -> len + 1;
q -> fa = newp -> fa = newq - t;
while(p - t && p -> son[c] == q - t)
p -> son[c] = newq - t, p = &t[p -> fa];
}
}
size[newp - t] = 1;
}
int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
scanf("%s", s + 1);
for(RG int i = 1, l = strlen(s + 1); i <= l; i++) extend(s[i] - 97);
for(node *i = t + 1; i != t + cnt + 1; i++) ++c[i -> len];
for(RG int i = 1; i <= cnt; i++) c[i] += c[i - 1];
for(node *i = t + 1; i != t + cnt + 1; i++) a[c[i -> len]--] = i - t;
for(RG int i = cnt; i; i--)
{
node *now = t + a[i];
size[now -> fa] += size[a[i]];
if(size[a[i]] > 1) ans = std::max(ans, 1ll * size[a[i]] * now -> len);
}
printf("%lld\n", ans);
return 0;
}