题意

求出多个串的最长公共子串。

分析

 刚学SAM想做这个题的话最好先去做一下那道codevs3160。求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长公共后缀。那么多个的时候,我们也用这种类似的方法,但是我们求最长公共后缀的时候要求第一个串的。我们把其中一个串建SAM,然后把其他的串都在上面跑,维护两个值,Max[u]和Min[u]。自动机中每个状态u的Right存的是结尾集合。那么对于一个字符串,我们可以求出他和自动机中每个状态的最长公共后缀。然后,我们通过Max[fa[u]]=max(Max[fa[u]],Max[u])来确定左右状态的最长公共后缀,然后更新Min[o]。

下面的代码没有AC,但是我找了好久BUG没找到··如果有人看完并且找到了bug麻烦跟我说一下万分感谢!

  

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn=+;
char s[maxn];
struct state{
int len,link;
int next[];
}st[*maxn];
int n,last,cur,sz;
int Min[*maxn],Max[*maxn],c[*maxn]; void init(){
sz=;
cur=last=;
st[].link=-;
st[].len=;
} void build_sam(int c){
cur=sz++;
st[cur].len=st[last].len+;
Min[cur]=st[cur].len;
int p;
for(p=last;p!=-&&st[p].next[c]==;p=st[p].link)
st[p].next[c]=cur;
if(p==-)
st[cur].link=;
else{
int q=st[p].next[c];
if(st[q].len==st[p].len+)
st[cur].link=q;
else{
int clone=sz++;
st[clone].len=st[p].len+;
Min[clone]=st[clone].len;
st[clone].link=st[q].link;
for(int i=;i<;i++)
st[clone].next[i]=st[q].next[i];
for(;p!=-&&st[p].next[c]==q;p=st[p].link)
st[p].next[c]=clone;
st[cur].link=st[q].link=clone;
}
}
last=cur;
} int cmp(int a,int b){
return st[a].len<st[b].len;
} int main(){
scanf("%s",s);
n=strlen(s);
init(); for(int i=;i<n;i++)
build_sam(s[i]-'a');
for(int i=;i<sz;i++)
c[i]=i;
sort(c,c+sz,cmp); while(scanf("%s",s)!=EOF){
n=strlen(s);
int u=,len=;
for(int i=;i<n;i++){
int c=s[i]-'a';
if(u!=-&&st[u].next[c]==)
u=st[u].link,len=st[u].len;
if(u==-)
u=,len=;
else{
u=st[u].next[c];
len++;
Max[u]=max(Max[u],len);
}
} for(int i=sz-;i>=;i--){
int o=c[i];
Min[o]=min(Min[o],Max[o]);
if(st[o].link!=-){
Max[st[o].link]=max(Max[st[o].link],Max[o]);
}
Max[o]=;
}
}
int ans=;
for(int i=;i<sz;i++){
ans=max(ans,Min[i]);
}
printf("%d\n",ans); return ;
}

【SPOJ - LCS2】Longest Common Substring II【SAM】的更多相关文章

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

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

  2. 【SPOJ】Longest Common Substring II

    [SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...

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

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

  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

    http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...

  6. SPOJ LCS2 - Longest Common Substring II 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/8982484.html 题目传送门 - SPOJ LCS2 题意 求若干$(若干<10)$个字符串的最长公共 ...

  7. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  8. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

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

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

随机推荐

  1. pymongo和mongodbengine之间的区别

    pymongo是一个mongo driver,可以用来连接数据库以及对数据库进行操作,但是是用mongo自己的用来操作数据库的语句进行操作数据库,而mongodbengine就像是sqlalchemy ...

  2. FastAdmin 无刷新地址改变

    FastAdmin 无刷新地址改变 群里有人问 FastAdmin 是不是用了 pjax? 之前有看到 Karson 回复过,其实 FastAdmin 用的是 HTML5 的一个History API ...

  3. 相关TableLayoutPanel分页显示自定义控件

    public partial class AcrossGrid : UserControl { /// <summary> /// 一页数量 /// </summary> ; ...

  4. jeecg中树形显示的用法

    1.GoodsController中显示的方法如下: @RequestMapping(params = "goodsgrid") @ResponseBody public Obje ...

  5. 基于STL优先队列和邻接表的dijkstra算法

    首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...

  6. python学习 (三十三) Modules

    1: 方法一: 导入整个模块 import math class ModulesDemo(): def builtin_modules(self): print(math.sqrt()) m = Mo ...

  7. Mybatis动态构建Sql(无实体类)

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 例如,sql语句where条件中,需要一些安全判断,例如按某一条件查询时如果传入的参数是空,此时查询 ...

  8. 【UVa】1601 The Morning after Halloween(双向bfs)

    题目 题目     分析 双向bfs,对着书打的,我还调了好久.     代码 #include<cstdio> #include<cstring> #include<c ...

  9. php 无限分类

    <?php function genTree($items,$id='id',$pid='pid',$son = 'children'){ $tree = array(); $tmpMap = ...

  10. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能

    权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...