spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】
多串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】的更多相关文章
- 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 ...
- 【SPOJ - LCS2】Longest Common Substring II【SAM】
题意 求出多个串的最长公共子串. 分析 刚学SAM想做这个题的话最好先去做一下那道codevs3160.求两个串的LCS应该怎么求?把一个串s1建自动机,然后跑另一个串s2,然后找出s2每个前缀的最长 ...
- spoj SUBLEX - Lexicographical Substring Search【SAM】
先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...
- 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 ...
- 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 ...
- 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 ...
- spoj NSUBSTR - Substrings【SAM】
先求个SAM,然后再每个后缀的对应点上标记si[nw]=1,造好SAM之后用吧parent树建出来把si传上去,然后用si[u]更新f[max(u)],最后用j>i的[j]更新f[i] 因为每个 ...
- 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 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
随机推荐
- 全国省市区三级联动js
function Dsy(){ this.Items = {}; } Dsy.prototype.add = function(id,iArray){ this.Items[id] = iArray; ...
- Elasticsearch 学习笔记 Elasticsearch及Elasticsearch head安装配置
一.安装与配置 1.到官网下载Elasticsearch,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6. ...
- Intel HEX文件解析
近期有一个需求就是为Arduino开发板做一个基于蓝牙的无线烧录程序.眼下的Arduino程序都是通过USB线连接到电脑的主机上,实际的传输过程是基于USB协议的,这个过程还是比較麻烦的.由于每次的编 ...
- 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划
淘宝数据库OceanBase SQL编译器部分 源码阅读--生成物理查询计划 SQL编译解析三部曲分为:构建语法树,制定逻辑计划,生成物理执行计划.前两个步骤请参见我的博客<<淘宝数据库O ...
- libusb 源码阅读
libusb_init(NULL), 如果传入一个NULL, 则libusb 内部会有一个 usbi_default_context 变量在内部保存上下文. 这样以后调用 libusb 函数时可以不指 ...
- Android研究之游戏开发摄像头更新
游戏中摄像头的原理介绍 在游戏开发中更新摄像头的位置能够决定屏幕显示的内容,尤其是RPG类游戏摄像头有着很关键的数据.我举一个样例 有时候我们在玩RPG游戏的时候进入一个新的场景 ...
- html使用代码大全
<DIV style="FONT-SIZE: 9pt">1)贴图:<img src="图片地址">1)首行缩进2格:<p styl ...
- ajax返回页面停留跳转
ajax返回数据后,页面停留跳转. 原理:利用匿名函数自动运行的特性和定时器来完成. (function(){ ; // 设置停留时间单位秒 var href =data.url; //设置跳转的ur ...
- 文件管理中心iOS APP (国外市场:File Center) 技术支持
文件管理中心iOS APP (国外市场:File Center) 技术支持网址:http://www.cnblogs.com/flychen/邮箱:592802944@qq.com
- [转]XCode中修改缺省公司名称/开发人员名称
本文转载至 http://www.cnblogs.com/zhulin/archive/2011/11/24/2261537.html XCode新建文件后,头部会有开发人员名称,公司名称等信息 ...