题目链接: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. OO模式-Composite

    组合模式也叫做"部分-总体"模式,这样事实上定义也就非常明显了,正好和数据结构的知识相相应.把对象组合成树形结构以表示"部分-总体"的层次结构. 先看类图: w ...

  2. 理解Paxos Made Practical

    Paxos Made Practical 当一个组中一台机器提出一个值时,其它成员机器通过PAXOS算法在这个值上达成一致. Paxos分三个阶段. 第一阶段: 提出者会选出一个提议编号n(n> ...

  3. Eclipse+Maven(webapp)+Jetty+JReBel的配置方法

    maven配置 省略 jrebel配置 jrebel毋须繁琐的配置,把jrebel-5.6.3-crack.zip解压放在磁盘文件夹就可以.(笔者路径为:D:\coding-life\IDE\jreb ...

  4. 发布Java桌面程序

    我拿了一份桌面工具的开源代码,修修改改,在elipse上运行,感觉良好,但到了发布应用程序,就傻眼了.我居然不知道咋发布! 呵呵,不愧是Java小白! 如果是微软阵营,直接就编译成exe了.但java ...

  5. Codeforces 709E. Centroids 树形DP

    题目链接:http://codeforces.com/contest/709/problem/E 题意: 给你一棵树,你可以任删一条边和加一条边,只要使得其仍然是一棵树,输出每个点是否都能成为重心 题 ...

  6. 交换分区 在dd命令执行期间 top 其消耗系统约14%的cpu,而mem占比约为0

    [资源不友好代码] from pyltp import * d_dir = '/usr/local/ltp_data_v3.4.0/' def gen_one_sentence_part(paragr ...

  7. RFC函数设置外部断点

  8. UESTC 982质因子分解

    读入一个自然数,将nn分解为质因子连乘的形式输出. Input 有多组测试数据.输入的第一行是整数TT(0<T≤10000),表示测试数据的组数.每一组测试数据只有一行,包含待分解的自然数nn. ...

  9. YTU 2906: 多重继承 日期与时间

    2906: 多重继承 日期与时间 时间限制: 1 Sec  内存限制: 128 MB 提交: 240  解决: 190 题目描述 在空缺的地方补全代码,只需提交补全部分即可 请用C++方式提交 #in ...

  10. JS处理空格

    JS处理空格 2010-10-27 11:48:32|  分类: 技术-JS |  标签:js  空格  |字号 订阅     /*删除两侧空格*/ function trim(ui){        ...