【题目链接】 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. Flink资料(5) -- Job和调度

    该文档翻译自Jobs and Scheduling ----------------------------------------------- 该文档简单描述了Flink是如何调度Job的,以及如 ...

  2. hdu 4676 Sum Of Gcd 莫队+数论

    题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...

  3. MySQLdb callproc 方法

    MySQLdb执行存储过程时就要调用 callproc 方法.它返回的是调用时的参数列表. MySQL 中存储过程的定如下: delimiter // create procedure proc_in ...

  4. SAR 图像

    http://www.dlr.de/hr/en/desktopdefault.aspx/tabid-2326/3776_read-5679/

  5. Oracle EBS-SQL (SYS-2): sys_在线用户查询.sql

    SELECT fs.USER_NAME,       fu.description,       fs.RESPONSIBILITY_NAME,       fs.USER_FORM_NAME,    ...

  6. poj2583---Series Determination

    #include <stdio.h> #include <stdlib.h> int main() { int x,y,z,a,b,c; while(scanf("% ...

  7. 练习一下linux中的list函数。

    所有的list函数见 include/linux/list.h 自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下. 没有别的目的,就是想熟练一下 ...

  8. 一步一步学android之布局管理器——RelativeLayout

    今天开始学习RelativeLayout(相对布局),相对布局在平时布局的时候用的较多,因为Android适配方面的原因.相对布局可以控制组件摆放的位置(放在任一组件的上下左右等位置),下面来看看类的 ...

  9. 素数距离问题_ny_24.java

    素数距离问题 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 2   描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距 ...

  10. JavaScript之面向对象学习三原型语法升级

    1.到目前为止,我们是时候分析下前面的使用原型语法来定义对象有哪些不足的地方,代码如下: function Person(){ } Person.prototype.name="张三&quo ...