【题目链接】 http://www.spoj.com/problems/LCS2/

【题目大意】

  求n个串的最长公共子串

【题解】

  对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值,
  取最小值中的最大值即可。由于跑的地方只记录了匹配结尾的状态,
  所以还需要更新parent树上的状态,既然匹配到了子节点,
  那么parent树链上的值就都能够取到l,
  一开始给每个不同状态按照l从小到大分配储存地址,
  这样,我们就可以从匹配长度最长的开始更新parent树的情况。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=500005;
char s[N];
struct sam{
int p,q,np,nq,cnt,last,a[N][26],l[N],f[N],mx[N],x[N];
sam(){cnt=0;last=++cnt;}
void extend(int c){
p=last;np=last=++cnt;l[np]=l[p]+1;
while(!a[p][c]&&p)a[p][c]=np,p=f[p];
if(!p)f[np]=1;
else{
q=a[p][c];
if(l[p]+1==l[q])f[np]=q;
else{
nq=++cnt;l[nq]=l[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
f[nq]=f[q]; f[np]=f[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=f[p];
}
}
}
void build(){
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=1;i<=len;i++)extend(s[i]-'a');
for(int i=1;i<=cnt;i++)mx[l[i]]++;
for(int i=1;i<=len;i++)mx[i]+=mx[i-1];
for(int i=1;i<=cnt;i++)x[mx[l[i]]--]=i;
for(int i=1;i<=cnt;i++)mx[i]=l[i];
}
void doit(){
int len=strlen(s+1),tmp=0,p=1;
static int arr[N];
for(int i=1;i<=len;i++){
int c=s[i]-'a';
if(a[p][c])p=a[p][c],tmp++;
else{
while(p&&!a[p][c])p=f[p];
if(!p)p=1,tmp=0;
else tmp=l[p]+1,p=a[p][c];
}arr[p]=max(arr[p],tmp);
}for(int i=cnt;i;i--){
int t=x[i];
mx[t]=min(mx[t],arr[t]);
if(arr[t]&&f[t])arr[f[t]]=l[f[t]];
arr[t]=0;
}
}
void getans(){
int ans=0;
for(int i=1;i<=cnt;i++)ans=max(ans,mx[i]);
printf("%d\n",ans);
}
}sam;
int main(){
sam.build();
while(~scanf("%s",s+1))sam.doit();
sam.getans();
return 0;
}

  

SPOJ 1812 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. SPOJ LCS2 Longest Common Substring II ——后缀自动机

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

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

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

  4. SPOJ 1812 Longest Common Substring II(后缀自动机)(LCS2)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

  5. SPOJ 1812 Longest Common Substring II

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

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

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

  7. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  8. SPOJ LCS Longest Common Substring(后缀自动机)题解

    题意: 求两个串的最大\(LCS\). 思路: 把第一个串建后缀自动机,第二个串跑后缀自动机,如果一个节点失配了,那么往父节点跑,期间更新答案即可. 代码: #include<set> # ...

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

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

随机推荐

  1. Application Loader下载安装和上传IOS app程序

    如果您安装了最新版的XCode开发环境.对于在4.2及以上版本,Developer/Applications/Utilities目录中已经有ApplicationLoader程序,无需执行以下单独安装 ...

  2. Android checkBox

    checkBox      状态:选中(true),未选中(false)      属性:           checked="true/false"; private Chec ...

  3. jquery结合highcharts插件显示实时数据动态曲线图

    效果如图所示: js代码如下: $(document).ready(function() { Highcharts.setOptions({ global: { useUTC: false }, co ...

  4. codeforces 665E Beautiful Subarrays

    题目链接 给一个数列, 让你找出异或结果大于等于k的子序列的个数. 因为任意一段序列的异或值都可以用前缀异或和来表示, 所以我们先求出前缀异或和. 我们考虑字典树, 对于每一个前缀sum, 我们先查询 ...

  5. 编写一个程序实现strcmp函数的功能

    写自己的strcat函数------→mycmp #include <stdio.h> #include <string.h> #define N 5 int mycmp(ch ...

  6. 使用verilog实现4选1数据选择器的几种方法

    第一种方法module mux( d1, d2, d3, d4, se1, se2, dout ); input d1; input d2; input d3; input d4; input se1 ...

  7. JS中特殊句子-for in

    for(var i=0;i<len;i++)这样的用法一般都可以用for in 来替代. 例如: var a = ["a","b","c&quo ...

  8. Oracle EBS-SQL (SYS-15):查询表空间2.sql

    /*表空间查询*/ SELECT d.status "状态", d.tablespace_name "名称", d.contents "类型" ...

  9. Centos 升级MySQL版本或者Yum安装Mysql5.6

    Centos 升级MySQL版本或者Yum安装Mysql5.6 1.从MySQL Yum仓库下载最新的rpm文件:http://dev.mysql.com/downloads/repo/yum/Cen ...

  10. substr,substring,slice 的区别

    javascript中的三个函数substr,substring,slice都可以用来提取字符串的某一部分(函数名称都是小写,不要写成subStr,subString又或者Substring,记住js ...