原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html

题目传送门 - SPOJ LCS2

题意

  求若干$(若干<10)$个字符串的最长公共连续子串长度。

  串长$\leq 100000$

题解

  建议在做本题之前,先去做SPOJ LCS,本题是其升级版。

  题解链接 - SPOJ LCS - http://www.cnblogs.com/zhouzhendong/p/8982392.html

  对于本题,我们只需要保持一下之后每一个串在第一个串的$SAM$的每一个状态上的最大匹配长度,然后最后对于每一个状态,取$min(该状态的Max值,其他所有字符串在该状态上面的最大匹配长度的最小值)$即可。

  于是,我们先像SPOJ LCS一样,让所有串都走一遍,然后记录一下值。

  注意到,每一个状态的结果都会对其$fa$做贡献。

  于是我们可以基数排序预处理拓扑序,然后逆序更新即可。

代码

#include <bits/stdc++.h>
using namespace std;
const int N=200005;
int n,m=0,last=1,size=1,Max[15][N];
int id[N],tax[N];
char s[N];
struct SAM{
int Next[26],fa,Max;
}t[N];
void expend(int c){
int p=last,np=++size,q,nq;
t[np].Max=t[p].Max+1;
for (;!t[p].Next[c];p=t[p].fa)
t[p].Next[c]=np;
q=t[p].Next[c];
if (t[q].Max==t[p].Max+1)
t[np].fa=q;
else {
nq=++size;
t[nq]=t[q],t[nq].Max=t[p].Max+1;
t[q].fa=t[np].fa=nq;
for (;t[p].Next[c]==q;p=t[p].fa)
t[p].Next[c]=nq;
}
last=np;
}
int main(){
t[0].Max=-1;
for (int i=0;i<26;i++)
t[0].Next[i]=1;
gets(s);
n=strlen(s);
for (int i=0;i<n;i++)
expend(s[i]-'a');
for (int i=1;i<=size;i++)
tax[t[i].Max]++;
for (int i=1;i<=size;i++)
tax[i]+=tax[i-1];
for (int i=1;i<=size;i++)
id[tax[t[i].Max]--]=i;
while (gets(s)&&strlen(s)){
n=strlen(s);
for (int i=0,now=1,len=0;i<n;i++){
int c=s[i]-'a';
if (t[now].Next[c]){
len++;
now=t[now].Next[c];
Max[m][now]=max(Max[m][now],len);
continue;
}
while (!t[now].Next[c])
now=t[now].fa;
len=t[now].Max+1;
now=t[now].Next[c];
Max[m][now]=max(Max[m][now],len);
}
for (int i=size;i>=1;i--)
Max[m][t[id[i]].fa]=max(Max[m][t[id[i]].fa],Max[m][id[i]]);
m++;
}
int ans=0;
for (int i=1;i<=size;i++){
int now=t[i].Max;
for (int j=0;j<m;j++)
now=min(now,Max[j][i]);
ans=max(ans,now);
}
printf("%d",ans);
return 0;
}

UPD(2018-05-07):

  听说有人要用hash过这题??

看这个:

来自陈立杰的后缀自动机课件

SPOJ LCS2 - Longest Common Substring II 字符串 SAM的更多相关文章

  1. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...

  2. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

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

  4. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  5. spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】

    多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...

  6. Virtual Judge SPOJ - LCS2 Longest Common Substring II

    https://vjudge.net/problem/SPOJ-LCS2 SPOJ注册看不到验证码,气到暴毙,用vjudge写的. 注意!(对拍的时候发现)这份代码没有对只有一个字符串的情况进行处理! ...

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

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

  8. SPOJ - LCS2 Longest Common Substring II(后缀自动机)题解

    题意: 求\(n\)个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,然后枚举所有串.对于每个串,求出这个串在\(i\)节点的最大匹配为\(temp[i]\)(当前串在这个节点最多取多少), ...

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

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

随机推荐

  1. Laravel-Excel 导入 Excel 文件----为什么只获取到最后一行数据?

    ### 今天使用了Laravel-Excel 到类文件,想做一个excel  文件到导入和导出,但是看了 官方到文档示例,自己做了一下,发现 只取到到最后一行到数据, 有点摸不着头脑! 网上找了一下, ...

  2. 5分钟搞定Nginx安装

      1. 安装gcc(centos 7之后一般已自带,可以在第6步失败后再安装) yum install gcc gcc-c++   2. 安装pcre yum install -y pcre pcr ...

  3. Java二维码生成与解码

      基于google zxing 的Java二维码生成与解码   一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...

  4. Spring加载加密的配置文件

    一.继承并实现自己的属性文件配置器类 /** * 带加密的Spring属性配置文件扩展类 * 加密方式:AES * @author simon * */ public class EncryptPro ...

  5. php 汉字首字母和全拼

    <?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...

  6. 设置 Confluence 6 外部索引站点

    Confluence 并不能比较容易的对外部站点进行搜索,这个是因为 Confluence 使用的是 Lucene 内部查找,但是你还是有下面 2 个可选的方案: 嵌入外部页面到 Confluence ...

  7. bzoj 1415

    莫名互测题... 这题一看就是期望dp,可是不会转移,所以考试写50分暴力走人... #include <cstdio> #include <cmath> #include & ...

  8. Nginx详解三:Nginx基础篇之yum安装

    Nginx快速搭建 Mainline version ----开发版:具有最新功能的版本,用于测试.研究.学习,不用于企业生成环境 Stable version----稳定版:官方认可,且通过测试的 ...

  9. python 全栈开发,Day71(模型层-单表操作)

    昨日内容回顾 1. {% include '' %} 2. extend base.html: <html> ..... ..... ..... {% block content%} {% ...

  10. C# 不使用递归遍历目录树中的文件和文件夹

    public class StackBasedIteration { static void Main(string[] args) { // Specify the starting folder ...