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 ...
随机推荐
- 零基础学习Python数据分析
网上虽然有很多Python学习的教程,但是大多是围绕Python网页开发等展开.数据分析所需要的Python技能和网页开发等差别非常大,本人就是浪费了很多时间来看这些博客.书籍.所以就有了本文,希望能 ...
- 聊聊Postgres中的IPC之SI Message Queue
在 PostgreSQL中,每一个进程都有属于自己的共享缓存(shared cache).例如,同一个系统表在不同的进程中都有对应的Cache来缓存它的元组(对于RelCache来说缓存的是一个Rel ...
- Scala代码开发 metaTable(元表)
使用Scala语言开发时,自动生成get和set方法 不用写return进行返回, 因为它的最后一行就是返回值 先建立四个层(层层递进) domain 表结构的建立 repository(DAO) 实 ...
- VUE源代码调试方法
前两条出自: https://link.zhihu.com/?target=http%3A//www.orzzone.com/vuejs-project-debug.html https://www. ...
- 必须声明表变量 "@P0"
mybatis提示错误 ### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 必须声明表变量 "@P0". ; u ...
- error occurred during initialization of vm
虚拟机无法进行如下分配 : -Xmx2048m -XX:MaxPermSize=512m 原因是我的老爷机总共内存只有3G: settings - > 搜索VM ->找到Compiler ...
- 一款开源免费的办公套件系统:DzzOffice详细部署
一.系统环境 个人建议centos 7 系统 cat /etc/redhat-release CentOS Linux release (Core) 基本更新 yum update -y 基本优化 关 ...
- mono for android 第三课--页面布局(转)
对于C#程序员来说布局不是什么难事,可是对于我这个新手在mono for android 中布局还是有点小纠结的,不会没关系.慢慢学习.好吧我们开始简单的布局.在之前我们拖拽的控件都是自动的去布局,也 ...
- html-css-js基本理解和简单总结
目录 一.对于网页的基本理解 1.网页是一种数据展示和信息交互的载体 2.网页组成部分 3.支撑一个网页的技术模块 二.html的理解和技术笔记 1.html理解 2.html技术笔记-html标签 ...
- php-redis 模块 文档
直接从这位朋友转载过来. 地址 Redis::__construct构造函数$redis = new Redis(); connect, open 链接redis服务参数host: string,服务 ...