【题目链接】 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. 郁闷的Delphi新闻

    Embarcadero closes down their spanish R&D office putting some 80 people on the street and dimini ...

  2. Android 检测SD卡应用

    Android 检测SD卡应用 //                                    Environment.MEDIA_MOUNTED // sd卡在手机上正常使用状态  // ...

  3. javascript数组去重算法-----4

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Redis数据结构及相应的命令

    Redis可以存储键(key)与5种不同类型值(value)之间的映射,5中不同类型的值分别为字符串(string),列表(list),散列(hash),集合(set)和有序集合(sorted set ...

  5. Introduction the naive“scull” 《linux设备驱动》 学习笔记

    Introduction the naive "scull" 首先.什么是scull? scull (Simple Character Utility for Loading Lo ...

  6. [置顶] android 自定义圆角ImageView以及锯齿的处理

    看到很多人开发过程中要使用圆角图片时,解决方法有: 1.重新绘制一张图片 2.通过布局来配置 3.通过重写View来实现 其中1,2在这里就不讲了,重点讲讲方法三的实现. 实现一:通过截取画布一个圆形 ...

  7. Extjs 4 生成饼状图的例子

    前台: //远程抄表设备下落图表数据 var Store1 = new Ext.data.Store({ <span style="white-space:pre"> ...

  8. 核心基础以及Fragment与Activity传递数据完整示例

    MainActivity如下: package cc.testsimplefragment0; import android.os.Bundle; import android.app.Activit ...

  9. Lambda表达式图解

    internal delegate int MyDel(int x); public class Lambda { ; };//匿名方法 ; };//Lambda表达式 ; };//Lambda表达式 ...

  10. Mock.js 与 fiddler 前端模拟数据与拦截请求

    最近 工作需要  接触了Mock.js. Mock.js 是一款模拟数据生成器,旨在帮助前端攻城师独立于后端进行开发,帮助编写单元测试.提供了以下模拟功能: 根据数据模板生成模拟数据 模拟 Ajax ...