洛谷3804 【模板】后缀自动机
2018-01-16 13:35
393 查看
题目
给定一个只包含小写字母的字符串SS,
请你求出 SS 的所有出现次数不为 11 的子串的出现次数乘上该子串长度的最大值。
输入格式
一行一个仅包含小写字母的字符串SS
输出格式
一个整数,为 所求答案
输入样例
abab
输出样例
4
提示
对于\(10\%10%\)的数据,|S|<=1000∣S∣<=1000
对于\(100\%100%\)的数据,|S|<=\(10^6\)∣S∣<=\(10^6\)
题解
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define LL long long int #define REP(i,n) for (int i = 1; i <= (n); i++) #define Redge(u) for (int k = h[u]; k; k = ed[k].nxt) using namespace std; const int maxn = 2000005,maxm = 100005,INF = 1000000000; inline int RD(){ int out = 0,flag = 1; char c = getchar(); while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();} while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();} return out * flag; } int ch[maxn][26],pre[maxn],step[maxn],n,cnt,last; int b[maxn],sz[maxn],a[maxn]; LL ans = 0; char s[maxn]; void ins(int u){ int p = last,np = ++cnt; last = np; step[np] = step[p] + 1; while (p && !ch[p][u]) ch[p][u] = np,p = pre[p]; if (!p) pre[np] = 1; else { int q = ch[p][u]; if (step[q] == step[p] + 1) pre[np] = q; else { int nq = ++cnt; step[nq] = step[p] + 1; for (int i = 0; i < 26; i++) ch[nq][i] = ch[q][i]; pre[nq] = pre[q]; pre[q] = pre[np] = nq; while (ch[p][u] == q) ch[p][u] = nq,p = pre[p]; } } sz[np] = 1; } void solve(){ REP(i,cnt) b[step[i]]++; REP(i,cnt) b[i] += b[i - 1]; REP(i,cnt) a[b[step[i]]--] = i; for (int i = cnt; i; i--){ sz[pre[a[i]]] += sz[a[i]]; if (sz[a[i]] > 1) ans = max(ans,1ll * step[a[i]] * sz[a[i]]); } } int main(){ scanf("%s",s + 1); cnt = last = 1; n = strlen(s + 1); for (int i = 1; i <= n; i++) ins(s[i] - 'a'); solve(); printf("%lld",ans); return 0; }
相关文章推荐
- 洛谷.3804.[模板]后缀自动机
- 【Luogu3804】【模板】后缀自动机(后缀自动机)
- 【Luogu3804】【模板】后缀自动机(后缀自动机)
- 洛谷P3804 - 【模板】后缀自动机
- [Luogu3804]【模板】后缀自动机
- 回文(后缀)自动机模板
- [后缀自动机 模板题] SPOJ 1811 Longest Common Substring
- 后缀自动机 模板 【Poi2000】 公共串 bzoj 2946
- [后缀自动机 模板题] SPOJ 1812 Longest Common Substring II & BZOJ 2946 [Poi2000]公共串
- SAM(后缀自动机)模板
- poj1509 后缀自动机模板题
- hdu4622 后缀自动机 模板
- [后缀自动机 模板题] SPOJ 8222 Substrings
- 后缀自动机模板题
- UVALive-4670 Dominating Patterns / 洛谷 3796 【模板】AC自动机
- Sam后缀自动机模板
- SPOJ 8222. Substrings(后缀自动机模板)
- HDU5558后缀自动机模板题
- [最小循环表示 后缀自动机 模板题] BZOJ 2882 工艺
- noi2015模板-后缀自动机