LG3804 【模板】后缀自动机
题意
给定一个只包含小写字母的字符串\(S\),
请你求出 \(S\) 的所有出现次数不为 \(1\) 的子串的出现次数乘上该子串长度的最大值。
对于\(100\%\) 的数据,\(|S| \leq 10^6\)
分析
后缀自动机和拓扑序的裸题。
对一个节点而言,其后每可转移到一个可接受的节点,标志着该节点对应的子串出现次数加1。
处理顺序需要可转移到的节点都被处理到,所以拓扑排序就好了。
时间复杂度\(O(|S|)\)
代码
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#define rg register
#define il inline
#pragma GCC optimize ("O3")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
const int MAXN=1e6+7;
int n;
char s[MAXN];
const int CHARSET_SIZE=26;
struct node
{
int ch[CHARSET_SIZE],fa,len;
};
struct SufAuto
{
node t[MAXN<<1];
int cnt[MAXN<<1];
int sz,root,last;
il int newnode(rg const int&x)
{
t[++sz].len=x;
return sz;
}
il void init()
{
memset(cnt,0,sizeof cnt);
sz=0;
root=last=newnode(0);
}
il void extend(rg const int&c)
{
rg int v = last,u = newnode(t[v].len + 1);
for(;v && !t[v].ch[c];v = t[v].fa)
t[v].ch[c] = u;
if(!v)
{
t[u].fa=root;
}
else
{
rg int o = t[v].ch[c];
if(t[o].len == t[v].len+1)
{
t[u].fa = o;
}
else
{
rg int n = newnode(t[v].len + 1);
copy(t[o].ch,t[o].ch+CHARSET_SIZE,t[n].ch);
t[n].fa = t[o].fa;
t[o].fa = t[u].fa = n;
for(;v && t[v].ch[c] == o;v = t[v].fa)
t[v].ch[c] = n;
}
}
cnt[u]=1;
last = u;
}
int a[MAXN<<1],c[MAXN];
il void rdxsort()
{
memset(c,0,sizeof c);
for(rg int i=1;i<=sz;++i)
++c[t[i].len];
for(rg int i=1;i<=n;++i)
c[i]+=c[i-1];
for(rg int i=sz;i>=1;--i)
a[c[t[i].len]--]=i;
}
il int solve()
{
rg ll ans=0;
for(rg int i=sz;i>=1;--i)
{
rg int p=a[i];
cnt[t[p].fa]+=cnt[p];
if(cnt[p]>1)
ans=max(ans,(ll)cnt[p]*t[p].len);
}
return ans;
}
}T;
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
scanf("%s",s);
n=strlen(s);
T.init();
for(rg int i=0;i<n;++i)
T.extend(s[i]-'a');
T.rdxsort();
printf("%d\n",T.solve());
// fclose(stdin);
// fclose(stdout);
return 0;
}
LG3804 【模板】后缀自动机的更多相关文章
- [模板] 后缀自动机&&后缀树
后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...
- Luogu3804:[模板]后缀自动机
题面 luogu Sol \(sam\)然后树形\(DP\) 当时还不会拓扑排序的我 # include <bits/stdc++.h> # define IL inline # defi ...
- 洛谷 P3804 [模板] 后缀自动机
题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include ...
- SPOJ LCS Longest Common Substring 和 LG3804 【模板】后缀自动机
Longest Common Substring 给两个串A和B,求这两个串的最长公共子串. no more than 250000 分析 参照OI wiki. 给定两个字符串 S 和 T ,求出最长 ...
- 【Luogu3804】【模板】后缀自动机(后缀自动机)
[Luogu3804][模板]后缀自动机(后缀自动机) 题面 洛谷 题解 一个串的出现次数等于\(right/endpos\)集合的大小 而这个集合的大小等于所有\(parent\)树上儿子的大小 这 ...
- 字符串数据结构模板/题单(后缀数组,后缀自动机,LCP,后缀平衡树,回文自动机)
模板 后缀数组 #include<bits/stdc++.h> #define R register int using namespace std; const int N=1e6+9; ...
- P3804 【模板】后缀自动机
P3804 [模板]后缀自动机 后缀自动机模板 详情可见luogu题解板块 #include<iostream> #include<cstdio> #include<cs ...
- 2018.07.17 后缀自动机模板(SAM)
洛谷传送门 这是一道后缀自动机的模板题,这道题让我切身体会到了后缀自动机的方便与好写. 代码如下: #include<bits/stdc++.h> #define N 2000005 #d ...
- 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题
链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...
随机推荐
- English trip -- Phonics 5 元音字母 o
Vowel 元音 元音 O Consonant 辅音 清辅音 h wh 浊辅音 m wh n ng y oa:[əʊ] # 字母本身音 coat boat load co ...
- Vue.js 渲染函数, JSX(未掌握,未学完)
渲染函数 , JSX(没完成学习) 基础: 实例属性:vm.$slots default 属性包括了所有没有被包含在具名插槽中的节点. 渲染函数: render: function(createEle ...
- Recursive Queries CodeForces - 1117G (线段树)
题面: 刚开始想复杂了, 还以为是个笛卡尔树.... 实际上我们发现, 对于询问(l,r)每个点的贡献是$min(r,R[i])-max(l,L[i])+1$ 数据范围比较大在线树套树的话明显过不了, ...
- bzoj2763: [JLOI2011]飞行路线 最短路
题意:求最多可以有k条路免费的最短路 题解:用dis[x][k]表示从s开始用了k次免费机会到x的最短路,然后dij跑的时候优先队列里多维护一个k就好了 /********************** ...
- 43. Multiply Strings 字符串表示的大数乘法
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 管道与popen函数与重定向
转自:http://www.tldp.org/LDP/lpg/node12.html Pipes the Easy Way! LIBRARY FUNCTION: popen(); PROTOTYPE: ...
- EL表达式、 jstl标签
https://www.cnblogs.com/zhaotiancheng/p/6391894.html https://blog.csdn.net/zdwzzu2006/article/detail ...
- tensorflow安装相关问题
安装步骤 要求:Python必须是64位根据TensorFlow的计算方式,TensorFlow的安装分为CPU版本和GPU版本对于Python3.5或者Python3.6,可以使用pip insta ...
- linux processes identifiers
Linux, like all Unix uses user and group identifiers to check for access rights to files and images ...
- learning uboot source command
reference: http://www.denx.de/wiki/DULG/UBootCmdGroupExec => help source source - run script from ...