题目链接: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 后缀自动机 多个串的最长公共子串的更多相关文章

  1. 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 ...

  2. 2018.12.15 spoj1812 Longest Common Substring(后缀自动机)

    传送门 后缀自动机模板题. 题意简述:求两个字串的最长公共子串长度. 对其中一个构建后缀自动机,用另外一个在上面跑即可. 代码: #include<bits/stdc++.h> #defi ...

  3. SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机、状压DP)

    手动博客搬家: 本文发表于20181217 23:54:35, 原地址https://blog.csdn.net/suncongbo/article/details/85058680 人生第一道后缀自 ...

  4. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  5. SPOJ LCS2 Longest Common Substring II ——后缀自动机

    后缀自动机裸题 #include <cstdio> #include <cstring> #include <iostream> #include <algo ...

  6. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  7. spoj - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

  8. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  9. SPOJ1812 Longest Common Substring II

    题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...

随机推荐

  1. Android lowmemorykiller

    drivers/staging/android/lowmemorykiller.c lowmemorykiller 在系统空闲内存不足时, 根据一定机制选择某个进程, 然后杀死它. 1. regist ...

  2. Highcharts报表——让你的网页上图表画的飞起

    Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达 ...

  3. bash builtin eval

    1 在开始执行eval后面的命令之前eval主要做了哪些事情 1.1 去掉反斜杠的quoting 比如\$ac_optarg,会变成$ac_optarg. 1.2 去掉单引号的quoting 比如: ...

  4. 设计模式-(14)装饰者模式 (swift版)

    一,概念 装饰者模式(Decorator):动态地为一个对象添加一些额外的职责,若要扩展一个对象的功能,装饰者提供了比继承更有弹性的替代方案. 多组合,少继承 二,UML图 抽象构件类(Compone ...

  5. MTK 电池曲线配置【转】

    本文转载自:http://blog.csdn.net/u010245383/article/details/51006597 硬件平台:MT8382 网上有很多关于电池曲线配置的文档,定位到具体的文件 ...

  6. YTU 2432: C++习题 对象数组输入与输出

    2432: C++习题 对象数组输入与输出 时间限制: 1 Sec  内存限制: 128 MB 提交: 1603  解决: 1152 题目描述 建立一个对象数组,内放n(n<10)个学生的数据( ...

  7. 自定义View分类与流程

    自定义View分类与流程(进阶篇)## 转载出处: http://www.gcssloop.com/customview/CustomViewProcess/ 自定义View绘制流程函数调用链(简化版 ...

  8. android 代码优化:关闭输出日志

    android关闭日志 我们在开发时,经常会输出各种日志来debug代码.但是等到应用发布的apk运行时不希望它输出日志. 关闭输出日志Log.v(),Log.i(),Log.w(),Log.v(), ...

  9. 并不对劲的bzoj4827:loj2020:p3723:[AHOI/HNOI2017]礼物

    题目大意 有两个长度为\(n\)(\(n\leq5*10^4\))的数列\(x_1,x_2,...,x_n\)和\(y_1,y_2,...,y_n\),两个数列里的数都不超过\(m\)(\(m\leq ...

  10. 并不对劲的p3676:小清新数据结构题

    题目大意 有一棵有\(n\)(\(n\leq 2*10^5\))个点的树,要进行\(q\)(\(q\leq 2*10^5\))次操作,每次操作是以下两种中的一种: 1.修改一个点的点权 2.指定一个点 ...