Luogu3804:[模板]后缀自动机
题面
Sol
\(sam\)然后树形\(DP\)
当时还不会拓扑排序的我
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
template <class Int>
IL void Input(RG Int &x){
RG int z = 1; RG char c = getchar(); x = 0;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= z;
}
const int maxn(2e6 + 5);
int n, trans[26][maxn], fa[maxn], len[maxn], tot = 1, last = 1, size[maxn];
ll ans;
vector <int> edge[maxn];
char s[maxn];
IL void Extend(RG int c){
RG int np = ++tot, p = last; last = np;
len[np] = len[p] + 1, size[np] = 1;
while(p && !trans[c][p]) trans[c][p] = np, p = fa[p];
if(!p) fa[np] = 1;
else{
RG int q = trans[c][p];
if(len[p] + 1 == len[q]) fa[np] = q;
else{
RG int nq = ++tot;
len[nq] = len[p] + 1, fa[nq] = fa[q];
for(RG int i = 0; i < 26; ++i) trans[i][nq] = trans[i][q];
fa[q] = fa[np] = nq;
while(p && trans[c][p] == q) trans[c][p] = nq, p = fa[p];
}
}
}
IL void Dfs(RG int x){
for(RG int l = edge[x].size(), e = 0; e < l; ++e)
Dfs(edge[x][e]), size[x] += size[edge[x][e]];
if(size[x] != 1) ans = max(ans, 1LL * size[x] * len[x]);
}
int main(RG int argc, RG char* argv[]){
scanf(" %s", s), n = strlen(s);
for(RG int i = 0; i < n; ++i) Extend(s[i] - 'a');
for(RG int i = 2; i <= tot; ++i) edge[fa[i]].push_back(i);
Dfs(1);
printf("%lld\n", ans);
return 0;
}
Luogu3804:[模板]后缀自动机的更多相关文章
- [模板] 后缀自动机&&后缀树
后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...
- 洛谷 P3804 [模板] 后缀自动机
题目:https://www.luogu.org/problemnew/show/P3804 模仿了一篇题解,感觉很好写啊. 代码如下: #include<cstdio> #include ...
- 【Luogu3804】【模板】后缀自动机(后缀自动机)
[Luogu3804][模板]后缀自动机(后缀自动机) 题面 洛谷 题解 一个串的出现次数等于\(right/endpos\)集合的大小 而这个集合的大小等于所有\(parent\)树上儿子的大小 这 ...
- Luogu3804 【模板】后缀自动机
题面 题解 一个串的出现次数等于$endpos$的大小,也是$parent$树上节点的$size$大小, 构建出后缀自动机,按拓补序,模拟即可. 代码 #include<cstdio> # ...
- 字符串数据结构模板/题单(后缀数组,后缀自动机,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 ...
- SPOJ 8222. Substrings(后缀自动机模板)
后缀自动机+dp. 后缀自动机主要是在functioner大牛那里学习的:http://blog.sina.com.cn/s/blog_70811e1a01014dkz.html 这道题是在No_st ...
随机推荐
- iOS 时区获取问题
时区缩写 UTC, CST, GMT, CEST 以及转换 UTC是协调世界时(Universal Time Coordinated)英文缩写,是由国际无线电咨询委员会规定和推荐,并由国际时间局(BI ...
- string、char *的转换
string转char* 主要有三种方法可以将str转换为char*类型,分别是:data(); c_str(); copy(); data()方法 string str = "hello& ...
- 工具篇-大数据组件的一些快捷Shell操作
一.Hbase 1. HBase shell窗口进入 执行hbase shell 2. HBase表的创建 # 语法:create <table>, {NAME => <fam ...
- Exponentiation POJ-1001
http://poj.org/problem?id=1001 //10000000 100000 #include<iostream> #include<cstring> us ...
- 【GIS新探索】算法实现在不规则区域内均匀分布点
1 概要 在不规则区域内均匀分布点,这个需求初看可能不好理解.如果设想一下需求场景就比较简单了. 场景1:在某个地区范围内,例如A市区有100W人口,需要将这100W人口在地图上面相对均匀的标识出来. ...
- dede修改文章页命名规则
一.DEDEcms 修改默认文章命名规则 1.单独添加分类默认修改,修改文件:include/common.inc.php. 大概在251行文档的命名规则 $cfg_df_namerule = '{ ...
- Sweep Line
391. Number of Airplanes in the Sky https://www.lintcode.com/problem/number-of-airplanes-in-the-sky/ ...
- excel 快速填充所有非连续空白单元格
工作中经常会碰到这样的表,需要把空白的单元格填充完成.变成后图.下面就是方法. 工具/原料 EXCEL 方法/步骤 首先选中你要填充的区域, 按“F5"或者 Ctrl + ...
- solr实时更新mysql数据的方法
第一步:创建core core是solr的特有概念,每个core是一个查询数据,.索引等的集合体,你可以把它想象成一个独立数据库,我们创建一个新core:名字[core1] 进入linux命令行,进入 ...
- 【数组】word search
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...