Description
给定一个只包含小写字母的字符串 S S , 请你求出 的所有出现次数不为 1 1 的子串的出现次数乘上该子串长度的最大值。
Solution
SAM模板题。
建出SAM后按拓扑序依次更新,
maxisizei×leni
max
i
s
i
z
e
i
×
l
e
n
i
即是答案。
Source
/************************************************
* Au: Hany01
* Date: Mar 31st, 2018
* Prob: [Luogu3804] SAM Template
* Email: hany01@foxmail.com
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1000005;
struct Node
{
int fa, ch[26], len;
}t[maxn << 1];
int n, sz[maxn << 1], tot = 1, las = 1, per[maxn << 1], c[maxn << 1];
char s[maxn];
LL Ans;
inline void extend(short c)
{
int p = las, np = ++ tot;
las = np;
t[np].len = t[p].len + 1;
while (p && !t[p].ch[c]) t[p].ch[c] = np, p = t[p].fa;
if (!p) t[np].fa = 1;
else {
int q = t[p].ch[c];
if (t[q].len == t[p].len + 1) t[np].fa = q;
else {
int nq = ++ tot;
t[nq] = t[q], t[nq].len = t[p].len + 1;
t[q].fa = t[np].fa = nq;
while (p && t[p].ch[c] == q) t[p].ch[c] = nq, p = t[p].fa;
}
}
sz[np] = 1;
}
int main()
{
#ifdef hany01
File("sam");
#endif
//Init
scanf("%s", s + 1), n = strlen(s + 1);
For(i, 1, n) extend(s[i] - 97);
//Sort by topological order
For(i, 1, tot) ++ c[t[i].len];
For(i, 1, tot) c[i] += c[i - 1];
For(i, 1, tot) per[c[t[i].len] --] = i;
//cout << tot << endl;
//Calc
Fordown(i, tot, 1)
{
int u = per[i];
sz[t[u].fa] += sz[u];
if (sz[u] > 1) chkmax(Ans, (LL)sz[u] * (LL)t[u].len);
}
printf("%lld\n", Ans);
return 0;
}
//最是秋风管闲事,红他枫叶白人头。
// -- 赵翼《野步》