SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解
题意:
求\(n\)个串的最大\(LCS\)。
思路:
把第一个串建后缀自动机,然后枚举所有串。对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少),然后我们求出最终所有串在\(i\)节点的匹配最小值\(mn[i]\)(即为所有串在\(i\)节点都能取到多少),答案即为\(max\{min[i]\}\)。
但是我们能发现,如果我们更新了\(temp[i]\),那么其实\(fa[i]\)的\(temp[fa[i]]\)也应该要更新,因为父节点是我的后缀子串,只是我没有走过去而已,并且\(temp[fa[i]] = max(temp[fa[i]], \ max(temp[i],\ mxlen[fa[i]]))\),因为父节点的匹配长度不能超过他本身长度。
为了能线性实现如上操作,我们按照\(mxlen\)大小桶排,因为父节点的\(mxlen\)一定小于子节点,那么我直接倒着更新就能保证我更新父节点时自己一定已经更新过了。
黄某讲的挺好,点击前往
代码:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 10;
typedef long long ll;
const ll MOD = 1e9 + 7;
int x[maxn << 1], rk[maxn << 1];
//桶排,按照len从小到大排序节点
struct SAM{
int node[maxn << 1][26], fa[maxn << 1], mxlen[maxn << 1];
int mn[maxn << 1];
int sz, last;
int newnode(){
++sz;
memset(node[sz], 0, sizeof(node[sz]));
fa[sz] = mxlen[sz] = 0;
return sz;
}
void init(){
sz = 0;
last = newnode();
}
void insert(int k){
int p = last, np = last = newnode();
mxlen[np] = mxlen[p] + 1;
for(; p && !node[p][k]; p = fa[p])
node[p][k] = np;
if(p == 0){
fa[np] = 1;
}
else{
int t = node[p][k];
if(mxlen[t] == mxlen[p] + 1){
fa[np] = t;
}
else{
int nt = newnode();
memcpy(node[nt], node[t], sizeof(node[t]));
fa[nt] = fa[t];
mxlen[nt] = mxlen[p] + 1;
fa[np] = fa[t] = nt;
for(; p && node[p][k] == t; p = fa[p])
node[p][k] = nt;
}
}
}
void Sort(int len){
//桶排,按照len从小到大排序节点
for(int i = 1; i <= sz; i++) x[mxlen[i]]++;
for(int i = 1; i <= len; i++) x[i] += x[i - 1];
for(int i = 1; i <= sz; i++) rk[x[mxlen[i]]--] = i;
for(int i = 1; i <= sz; i++) mn[i] = mxlen[i];
}
int tmp[maxn << 1];
void build(char *s){
for(int i = 0; i <= sz; i++) tmp[i] = 0;
int len = strlen(s);
int pos = 1;
int ret = 0;
for(int i = 0; i < len; i++){
int c = s[i] - 'a';
while(pos && node[pos][c] == 0){
pos = fa[pos];
ret = mxlen[pos];
}
if(pos == 0){
ret = 0;
pos = 1;
}
else{
pos = node[pos][c];
ret++;
}
tmp[pos] = max(tmp[pos], ret);
}
for(int i = sz; i >= 1; i--){
int c = rk[i];
tmp[fa[c]] = min(max(tmp[fa[c]], tmp[c]), mxlen[fa[c]]);
}
for(int i = 1; i <= sz; i++){
mn[i] = min(mn[i], tmp[i]);
}
}
void query(){
int ret = 0;
for(int i = 1; i <= sz; i++) ret = max(ret, mn[i]);
printf("%d\n", ret);
}
}sam;
char s[maxn];
int main(){
sam.init();
scanf("%s", s);
int len = strlen(s);
for(int i = 0; i < len; i++) sam.insert(s[i] - 'a');
sam.Sort(len);
sam.build(s);
while(~scanf("%s", s)){
sam.build(s);
}
sam.query();
return 0;
}
SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解的更多相关文章
- SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS
LCS2 - Longest Common Substring II no tags A string is finite sequence of characters over a non-emp ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- SPOJ LCS2 - Longest Common Substring II 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- SPOJ LCS Longest Common Substring(后缀自动机)题解
题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...
随机推荐
- 2021年1月15日【深度学习DeepLearning(python)实战班】
深度学习网络课程QQ群群号: 1057802989(加群备注:杨春娇邀请) 强化学习QQ交流群群号: 872395038(加群备注:杨春娇邀请)
- YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. data = yaml.load(file_data)
YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsa ...
- 正向代理 forward proxy、反向代理 reverse proxy、透明代理 transparent proxy
https://zh.wikipedia.org/wiki/反向代理 反向代理在计算机网络中是代理服务器的一种.服务器根据客户端的请求,从其关系的一组或多组后端服务器(如Web服务器)上获取资源,然后 ...
- 竞态条件 race condition data race
竞态条件 race condition Race condition - Wikipedia https://en.wikipedia.org/wiki/Race_condition A race c ...
- Jenkins部署web项目到Tomcat(热部署)
使用这个方式的话需要tomcat中有初始时Manage这个项目,本质上是通过http://ip:port/manager/html这个地址的上传接口进行上传,进行热部署(需要远程tomcat 必须开启 ...
- springboot开启多线程配置
一.配置线程池参数 @EnableAsync @Configuration public class TaskExecutorConfig { @Bean public TaskExecutor ta ...
- XCTF-phoenix100
前期工作 查壳无壳,界面是普通的输入flag点击验证 逆向分析 文件结构只有一个MainActively,查看MainActively代码 public class MainActivity exte ...
- GeoMesa Spark
GeoMesa Spark 一.Spark JTS 1.1 示例 1.2配置 1.3 地理空间用户定义的类型和功能 1.4 geojson输出 1.5 Building 二.Spark Core 2. ...
- Python3 注释、运算符、数字、字符串
文章目录 注释 单引号(''') 双引号(""") 运算符 数字(Number) Python 数字类型转换 数学函数 随机数函数 三角函数 数学常量 数字与字符,列表之 ...
- vmware打开虚拟级断电情况下,无法找到虚拟机文件
1.此时会在建立的虚拟机目录下,有一些 %虚拟机名字%.vmx.lck 或者别的 %虚拟机名字%.***.lck 删除这些文件夹 2.虚拟文件 是一个后缀名为vmx的文件,发现断电后 变成了v ...