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 之 爬普房网
from bs4 import BeautifulSoupimport reimport requestsimport pandas## pa pufangwangclass down(object) ...
- 阿里巴巴前端面试分享-社招(p6)
借鉴了朋友的阿里面试经:(社招前端2年经验) 电话面 简单自我介绍, 做过哪些项目, 使用哪些技术栈 ? 如何看待前端框架选型 ? vue的如何实现双向绑定的 ? react 虚拟DOM 是什么? 如 ...
- 递归实现进制转换(C++版)
上次呢,我们留下了一道题,今天我们来一起看一看: 题目链接:https://www.cnblogs.com/gaozirong/p/10547434.html 这是我写的程序,大家可以对照参考一下(C ...
- QuantLib 金融计算——基本组件之 DateGeneration 类
目录 QuantLib 金融计算--基本组件之 DateGeneration 类 QuantLib 金融计算--基本组件之 DateGeneration 类 许多产品的估值依赖于对未来现金流的分析,因 ...
- C#(同步调用、异步调用、异步回调)
Review: 原作者虽然使用了汉字的类名,看起来十分蹩脚,但是,还是把同步调用.异步调用.异步回调的使用讲解的很详细的.原理讲解的很清晰. ------ 本文将主要通过“同步调用”.“异步调用”.“ ...
- 2. C++11 构造函数相关
1. 继承构造函数 派生类如果要使用基类的成员函数,可以通过using声明来完成. #include <iostream> using namespace std; class Base ...
- CODEVS-1018单词接龙
单词接龙 原题:传送门 解题思路: 此题是典型的深搜题目,首先确定递归变量,表示字母的数量,每当满足一定条件,就往下一层递归,否则回溯 判断由哪个单词开始(因为可能字母首位可能相同),再确定之后所连单 ...
- 基于python的几种排序算法的实现
#!usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2019/3/28 10:26 # @Author : Yosef-夜雨声烦 # @Email ...
- 04-树5 Root of AVL Tree (25 分)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- (转)Centos7上部署openstack ocata配置详解
原文:http://www.cnblogs.com/yaohong/p/7601470.html 随笔-124 文章-2 评论-82 Centos7上部署openstack ocata配置详解 ...