多串LCS很适合SA但是我要学SAM

对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c[p][c]的p,然后len=dis[p]+1,p=ch[p][c],否则就p=root,len=0(这个len每到一个节点就更新这个节点的f)

然后注意到在parent树上,因为每个节点代表的right集合是儿子的并集,所以f[u]是可以更新f[fa[u]]的,所以从底向上更新一遍(注意先更新!!)

最终点u的f就是每个串的f与dis[u]取min,然后ans在这些点上取max







#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=500005;
int n,m=1,fa[N],ch[N][27],dis[N],cur=1,con=1,la,f[N][15],c[N],a[N],ans;
char s[N];
void ins(int c,int id)
{
la=cur,dis[cur=++con]=id;
int p=la;
for(;p&&!ch[p][c];p=fa[p])
ch[p][c]=cur;
if(!p)
fa[cur]=1;
else
{
int q=ch[p][c];
if(dis[q]==dis[p]+1)
fa[cur]=q;
else
{
int nq=++con;
dis[nq]=dis[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[q]=fa[cur]=nq;
for(;ch[p][c]==q;p=fa[p])
ch[p][c]=nq;
}
}
}
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
ins(s[i]-'a',i);
for(int i=1;i<=con;i++)
f[i][1]=dis[i];
while(~scanf("%s",s+1))
{
n=strlen(s+1),m++;
for(int i=1,p=1,len=0;i<=n;i++)
{
int c=s[i]-'a';
if(ch[p][c])
len++,p=ch[p][c],f[p][m]=max(f[p][m],len);
else
{
for(;p&&!ch[p][c];p=fa[p]);
if(!p)
p=1,len=0;
else
len=dis[p]+1,p=ch[p][c],f[p][m]=max(f[p][m],len);
}
}
}
for(int i=1;i<=con;i++)
c[dis[i]]++;
for(int i=1;i<=con;i++)
c[i]+=c[i-1];
for(int i=1;i<=con;i++)
a[c[dis[i]]--]=i;
for(int j=2;j<=m;j++)
for(int i=con;i>=1;i--)
f[fa[a[i]]][j]=max(f[fa[a[i]]][j],f[a[i]][j]);
for(int i=1;i<=con;i++)
{
for(int j=2;j<=m;j++)
f[i][1]=min(f[i][1],f[i][j]);
ans=max(ans,f[i][1]);
}
printf("%d\n",ans);
return 0;
}

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

  1. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

  2. 【SPOJ - LCS2】Longest Common Substring II【SAM】

    题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...

  3. spoj SUBLEX - Lexicographical Substring Search【SAM】

    先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...

  4. Java for LeetCode 030 Substring with Concatenation of All Words【HARD】

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  5. HDU-1423-Greatest Common Increasing Subsequence-最长公共上升子序列【模版】

    This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...

  6. 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. spoj NSUBSTR - Substrings【SAM】

    先求个SAM,然后再每个后缀的对应点上标记si[nw]=1,造好SAM之后用吧parent树建出来把si传上去,然后用si[u]更新f[max(u)],最后用j>i的[j]更新f[i] 因为每个 ...

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

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

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

随机推荐

  1. redux 及 相关插件 项目实战

    目录结构 +-- app | +-- actions | +-- index.js | +-- components | +-- content.js | +-- footer.js | +-- se ...

  2. weexapp 开发流程(三)其他页面创建

    1.首页 (1)轮播图 步骤一:创建 轮播图 组件(Slider.vue) src / assets / components / Slider.vue <!-- 轮播图 组件 --> & ...

  3. Linux C++的多线程编程(转)

    1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...

  4. 读取xml生成lua測试代码

    #include <iostream> #include <string> #include <fstream> #include "tinyxml2.h ...

  5. 源代码方式向openssl中加入新算法完整具体步骤(演示样例:摘要算法SM3)【非engine方式】

    openssl简单介绍 openssl是一个功能丰富且自包括的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算. ...

  6. ASI和AFN实现POST异步请求的相同功能的代码

    I'm a newbie in obj-c and have been using asihttp for some of my projects. When doing a post request ...

  7. 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划

    淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划 SQL编译解析三部曲分为:构建语法树,制定逻辑计划,生成物理执行计划.前两个步骤请参见我的博客<<淘宝数据库O ...

  8. JAVA WEB学习笔记(三):简单的基于Tomcat的Web页面

    注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...

  9. sanic官方文档解析之静态文件和版本

    1,静态文件 就向图片文件一样,静态文件和指导性的文件,当通过Sanic服务端用app.static()方法注册的时候,这种方法采用端点url和文件名称获得.这样的文件的指定,将会通过指定的端点访问. ...

  10. CentOS笔记-常用网络命令

    1.curl & wget 使用curl或wget命令,不用离开终端就可以下载文件.如你用curl,键入curl -O后面跟一个文件路径.wget则不需要任何选项.下载的文件在当前目录. cu ...