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 ...
随机推荐
- Freemarker生成HTML静态页面
这段时间的工作是做一个网址导航的项目,面向用户的就是一个首页,于是就想到了使用freemarker这个模板引擎来对首页静态化. 之前是用jsp实现,为了避免用户每次打开页面都查询一次数据库,所以使用了 ...
- 牛客网暑期ACM多校训练营(第一场)I Substring
题意:给你一个只有abc的字符串,求不相同的子串,(不同构算不同,例如aba和bab算同构) 题解:很显然,如果不考虑同构的问题,我们直接上sa/sam即可,但是这里不行,我们考虑到只有abc三种字符 ...
- 关于一些逗逼函数//atoi,itoa,strtok,strupr,
reverse(begin,end) 反转容器内容可以是string,char数组,也可以是int型数组...用于反转其中的内容: char * strtok(cahr *,const char ...
- oracle增加表空间
select tablespace_name, sum(bytes)/1024/1024 from dba_data_files group by tablespace_name; select ta ...
- Error: Chunk.entry was removed. Use hasRuntime()错误解决
Error: Chunk.entry was removed. Use hasRuntime()错误解决 执行如下命令 npm uninstall --save-dev ext ...
- HDU 4791 Alice's Print Service 思路,dp 难度:2
A - Alice's Print Service Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- 51nod1210
题解: 二维树状数组,再矩阵推一下 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; int ...
- java 引用传递和值传递
1.为什么要分值传递和引用传递: 基本类型存在在栈中,复合类型(对象)存在堆中.操作栈的速度要快于堆,且对象的复制相比基本类型不仅浪费内存而且速度比较慢. 从这里就可以看出来:对象是按照引用传递(数据 ...
- JProfiler性能分析
之前已经介绍过如何调试本地的JBoss.现在额外一篇文章关于如何远程调试Tomcat的,其实远程和本地的区别不大,主要区别只是,JProfiler的GUI运行在你本地,而JProfiler的Agent ...
- 驱动程序多线程 PsCreateSystemThread
内核函数PsCreateSystemThread负责创建新线程.该函数可以创建两种线程,一种是用户线程,它属于当前进程中的线程.另一种是系统线程,系统线程不属于当前用户进程,而是属于系统进程,一般PI ...