题目链接: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开发pool解析xml

    xml在开发中的作用不可小觑,很多时候我们都要用到这种文件,所以学习它的解析方式很是必要. 我们都知道java中xml的解析有:dom,SAX,但是Android下我们使用pool解析,是更为方便,而 ...

  2. 使用 C# 开发智能手机软件:推箱子(三)

    这是"使用 C# 开发智能手机软件:推箱子"系列文章的第三篇.在这篇文章中,介绍 Common/Block.cs 源程序文件.   1 namespace Skyiv.Ben.Pu ...

  3. [办公自动化]企业网IE多版本引发的网页无法访问

    今天同事的某个网页无法打开,但是在我的计算机上该网站确实又能打开. 去看了一下,他的其他网站都正常.确认网络本身没有问题. 最后,看了一下IE版本,IE11. 只好尝试一下兼容性视图的设置. 设置了一 ...

  4. Ant 打包 问题

    Ant 打包问题及解决归纳总结: 1.build.xml注意设置basedir: <project name="s2si"  default="dist" ...

  5. Android图片载入缓存框架Glide

    Glide开源框架是Google推荐的图片载入和缓框架,其在Github上的开源地址是:https://github.com/bumptech/glide 当然一个Google推荐的框架肯定就是Vol ...

  6. ABAP 读取FTP文件

    VALUE '172.168.1.250'. VALUE 'username'. VALUE 'password'. ."密钥 CONSTANTS: cns_rfcdest LIKE rfc ...

  7. iOS SHA加密算法的实现

    - (NSString *)SHAStringWithSourceData:(NSData *)data type:(SHAType)type{ int shaDigestLength; switch ...

  8. 建立自己的私有docker(ssl&login auth)

    建立私有docker需要先建立ssl证书,然后建立htpass的登陆证书 最后使用nginx配置docker-compose.yml 参考: https://www.digitalocean.com/ ...

  9. YTU 2959: 代码填充--雨昕学矩阵

    2959: 代码填充--雨昕学矩阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 112  解决: 50 题目描述 雨昕开始学矩阵了.矩阵数乘规则:一个数k乘一个矩阵A还是一个矩阵,行数 ...

  10. linux系统无法上外网,路由器可以上网,可以ping通路由器,ping不通外网IP

    临时生效方法(添加路由网关),执行: #route add default gw 192.168.92.1   #根据实际网关IP填写 如果不行,使用下面方法: 一:使用 route 命令添加使用ro ...