[SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/
其实两个串的LCS会了,多个串的LCS也就差不多了。
我们先用一个串建立后缀自动机,然后其它的串在上面跑。跑的时候算出每一个位置能往左扩展的最大长度也就是LCS。
于是对于每一个状态维护mx数组,表示当前串与SAM在此状态的LCS值。对于一个状态取所有mx中的最小值,然后答案就是所有状态最小值中的最大值,证明显然。
两个串的时候不用拿一个状态更新其祖先状态,但是这里需要。SAM是一个DAG图,我们通过l数组来基数排序,从叶子开始一层一层向上更新就行了。实现有点妙,具体看代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[];
int sz=,la,rt,len;
int l[],ch[][],fa[],mn[];
void Extend(int c){
int end=++sz,tmp=la;
l[end]=mn[end]=l[tmp]+;
while(tmp&&!ch[tmp][c]){
ch[tmp][c]=end;
tmp=fa[tmp];
}
if(!tmp) fa[end]=rt;
else{
int ne=ch[tmp][c];
if(l[tmp]+==l[ne]) fa[end]=ne;
else{
int np=++sz;
memcpy(ch[np],ch[ne],sizeof(ch[ne]));
l[np]=mn[np]=l[tmp]+;
fa[np]=fa[ne];
fa[end]=fa[ne]=np;
while(tmp&&ch[tmp][c]==ne){
ch[tmp][c]=np;
tmp=fa[tmp];
}
}
}
la=end;
}
int c[],a[];
int mx[];
void Radixsort(){
for(int i=;i<=sz;i++) c[l[i]]++;
for(int i=;i<=len;i++) c[i]+=c[i-];
for(int i=sz;i>=;i--) a[c[l[i]]--]=i;
}
void Solve(){
while(scanf("%s",s+)!=EOF){
int o=rt,lcs=;
for(int i=;s[i];i++){
int c=s[i]-'a';
if(ch[o][c]){
o=ch[o][c];
mx[o]=max(mx[o],++lcs);
}
else{
while(o&&!ch[o][c]) o=fa[o];
if(!o){
o=rt;
lcs=;
}
else{
lcs=l[o]+;
o=ch[o][c];
mx[o]=max(mx[o],lcs);
}
}
}
for(int i=sz;i>=;i--){
int o=a[i];
mn[o]=min(mn[o],mx[o]);
if(fa[o]) mx[fa[o]]=max(mx[fa[o]],mx[o]);
mx[o]=;
}
}
}
int main(){
rt=la=++sz;
scanf("%s",s+);
len=strlen(s+);
for(int i=;i<=len;i++) Extend(s[i]-'a');
Radixsort();
Solve();
int ans=;
for(int i=;i<=sz;i++) ans=max(ans,mn[i]);
printf("%d\n",ans);
return ;
}
[SPOJ1812]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 ...
- 2018.12.15 spoj1812 Longest Common Substring(后缀自动机)
传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...
- SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)
手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- SPOJ LCS2 Longest Common Substring II ——后缀自动机
后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- spoj - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- SPOJ1812 Longest Common Substring II
题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...
随机推荐
- Android项目之HomeHealth基础学习2:Service
一. Service简单介绍 Service是android 系统中的四大组件之中的一个(Activity.Service.BroadcastReceiver.ContentProvider),它跟A ...
- Linux—read
read:将信息读入一个或多个Shell变量 语法格式:read [-r] 变量名 选项: -r:原始读入,不做任何处理,不将结尾结尾处的反斜杠解释为续行字符 行为模式 ...
- 解决Install failed uid changed
出现此问题的原因大多是apk冲突造成,解决的办法如下: 将apk相关文件和相关内容删除 (1) 将手机root,Window上装root大师等工具root成功率较高 (2) 删除可能相关的文件:/da ...
- github的提交源码到服务器
github是现代的代码库,各种牛人,各种开源,也是现在大公司招聘的一个考察点, 这里介绍一下怎样把本地源码提交到github上. 首先我们需要在github上创建一个respository. 2,输 ...
- Spring简单实现数据源的动态切换
Spring简单实现数据源的动态切换: 1. 创建一个数据源切换类: 2. 继承AbstractRoutingDataSource,创建多数据源路由类,并注入到spring的配置文件中: 3. AOP ...
- 设计模式-(7)桥接(swift版)
一,概念 桥接模式为把抽象层次结构从实现中分离出来,使其可以独立变更,抽象层定义了供客户端使用的上层抽象接口,实现层次结构定义了供抽象层次使用的底层接口,实现类的引用被封装于抽象层的实例中,桥接就形成 ...
- mysql08---优化01
Mysql数据库的优化技术 对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索引(什么都不写).主键索引(有一个主键 ...
- 织梦DEDE系统跨站跨数据库调用数据显示
调用方法 本标签的调用格式为: {dede:sql sql="一条完整的SQL语句" appname="数据库配置参数"}您的底层模板{/dede:sql} 稍 ...
- MySQL-业务优化——说的就是变
前言 通过上次发布的业务优化不是一步到位的有不少网友问我许多关于业务优化和Web方面的问题.在这里表示感谢和支持.在期间有些回答不到位的还请谅解,并且个人经验有限. 百牛信息技术bainiu.ltd整 ...
- 【135】NoteExpress使用中的问题
NoteExpress主要是用来管理文献,然后可以方便管理,方便插入,各种方便吧! 关于NoteExpress的下载可以直接搜索进入官网下载,为了不用破解之类的,可以选择大学版的! 引文显示上标!ht ...