●SPOJ LCS2Longest Common Substring II
题链:
http://www.spoj.com/problems/LCS2/
题解:
后缀自动机。
对第一个串建立后缀自动机,
然后把后面的每个串分别与该串的自动机去匹配,求出相应的数组val*[s]:
表示第*个串与第一个串的自动机的状态s的最大匹配长度,
(求法就是两个串用后缀自动机求LCS这一过程):
对于当前已经匹配的子串T,长度为now,此刻在状态s,现在要匹配第i个字符x,
若trans(s,x)!=0,则s=trans(s,x),now++,i++,并更新val*[s]=max(val*[s],now);
否则,s=parent[s],直到trans(s,x)!=0或者s=0。
由于我们在每一步更新s时,没有更新到其祖先,所以最后再桶排之后去依次更新父亲。
而最后的状态s和每个串的最大匹配长度就是min(min(val*[s]),maxs[s](这个是状态s所允许的最大长度));
代码:
#include<bits/stdc++.h>
#define MAXN 100005
#define INF 0x3f3f3f3f
using namespace std;
struct SAM{
int size,last,lens;
int maxs[MAXN*3],trans[MAXN*3][26],parent[MAXN*3],minmatch[MAXN*3];
int Newnode(int a,int b){
++size; maxs[size]=a; minmatch[size]=INF;
memcpy(trans[size],trans[b],sizeof(trans[b]));
return size;
}
void Extend(int x){
static int p,np,q,nq;
p=last; last=np=Newnode(maxs[p]+1,0);
for(;p&&!trans[p][x];p=parent[p]) trans[p][x]=np;
if(!p) parent[np]=1;
else{
q=trans[p][x];
if(maxs[p]+1!=maxs[q]){
nq=Newnode(maxs[p]+1,q);
parent[nq]=parent[q];
parent[q]=parent[np]=nq;
for(;p&&trans[p][x]==q;p=parent[p]) trans[p][x]=nq;
}
else parent[np]=q;
}
}
void Build(char *S){
memset(trans[0],0,sizeof(trans[0]));
size=0; last=Newnode(0,0); lens=strlen(S);
for(int i=0;i<lens;i++) Extend(S[i]-'a');
}
void Update(int *val){
static int tmp[MAXN],order[MAXN*3];
memset(tmp,0,sizeof(tmp));
for(int p=1;p<=size;p++) tmp[maxs[p]]++;
for(int i=1;i<=lens;i++) tmp[i]+=tmp[i-1];
for(int p=1;p<=size;p++) order[tmp[maxs[p]]--]=p;
for(int i=size,p;i;i--)
p=order[i],val[parent[p]]=max(val[parent[p]],val[p]);
}
void Match(char *T){
static int val[MAXN*3],p,i,len,now;
for(p=1;p<=size;p++) val[p]=0;
p=1; i=0; now=0; len=strlen(T);
while(i<len){
if(!p) p=1,now=0,i++;
else if(!trans[p][T[i]-'a']) p=parent[p],now=maxs[p];
else now++,p=trans[p][T[i]-'a'],i++;
val[p]=max(val[p],now);
}
Update(val);
for(p=1;p<=size;p++) minmatch[p]=min(minmatch[p],val[p]);
}
}SUF;
int main(){
static char S[MAXN];
scanf("%s",S); SUF.Build(S);
while(~scanf("%s",S)) SUF.Match(S);
int ans=0;
for(int p=1;p<=SUF.size;p++)
ans=max(ans,min(SUF.minmatch[p],SUF.maxs[p]));
printf("%d\n",ans);
return 0;
}
●SPOJ LCS2Longest Common Substring II的更多相关文章
- 后缀自动机(SAM):SPOJ Longest Common Substring II
Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...
- 2018.12.15 spoj Longest Common Substring II(后缀自动机)
传送门 后缀自动机基础题. 给出10个串求最长公共子串. 我们对其中一个建一个samsamsam,然后用剩下九个去更新范围即可. 代码: #include<bits/stdc++.h> # ...
- SPOJ Longest Common Substring II
题目连接:戳我 题目大意:求n个字符串的最长公共子串. 它的简化版--这里 当然我们可以用SA写qwq,也可以用广义SAM写qwq 这里介绍纯SAM的写法...就是对其中一个建立后缀自动机,然后剩下的 ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- 【SPOJ】Longest Common Substring II
[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 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 ...
- Longest Common Substring II SPOJ - LCS2 (后缀自动机)
Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...
随机推荐
- bug终结者 团队作业第八周
bug终结者 团队作业第八周 本次任务 素材提供及编辑:20162328 蔡文琛 博客修改完善:20162322 朱娅霖 "bug终结者" 宏伟蓝图 UML 手绘底稿 用例图 选项 ...
- Git学习使用
1.注册码云并建立远程仓库 2.安装git 3.使用eclipse egit 推送以及克隆 建立本地仓库,成功后如图 推送项目至本地仓库与远程仓库 使用右键菜单team-share 选项,与仓库关联后 ...
- 在Apache中运行Python WSGI应用
我们介绍如何使用Apache模块mod_wsgi来运行Python WSGI应用. 安装mod_wsgi 我们假设你已经有了Apache和Python环境,在Linux或者Mac上,那第一步自然是安装 ...
- 同样是IT培训,为什么人家月薪过万,你才几千,问题在哪?!
听过一句话"360行,行行转IT",虽然有些夸张,但也不难看出IT行业的火爆程度.从李总理提的"互联网+大数据"开始,中国的这场"互联网+" ...
- 在wamp集成环境下安装laravel5.2.*框架
虽然官方一直强烈推荐使用homestead,但是这个相对麻烦一点,所以我还是选择使用wamp集成开发环境.还有这里我只讲解windows系统下的安装,其他例如mac或linux就不写了,此文章是面向刚 ...
- c语言中宏定义和常量定义的区别
他们有共同的好处就是"一改全改,避免输入错误"哪两者有不同之处吗?有的. 主要区别就在于,宏定义是在编译之前进行的,而const是在编译阶段处理的 宏定义不占用内存单元而const ...
- Mego开发文档 - 加载关系数据
加载关系数据 Mego允许您使用模型中的导航属性来加载相关数据对象.目前只支持强制加载数据对象.只有正确配置了关系才能加载关系数据,相关内容可参考关系配置文档. 加载对象属性 您可以使用该Includ ...
- SQL执行计划分析
explain执行计划中的字段以及含义在下面的博客中有详细讲述: https://blog.csdn.net/da_guo_li/article/details/79008016 执行计划能告诉我们什 ...
- POJ-1258 Agri-Net---MST裸题Prim
题目链接: https://vjudge.net/problem/POJ-1258 题目大意: 求MST 思路: 由于给的是邻接矩阵,直接prim算法 #include<iostream> ...
- hdu1222&hdu1014 循环群的生成元
hdu1222 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1222 题目大意: 大灰狼追小白兔.小白兔可以躲起来的洞绕成一个圈,大灰狼从0这个点出 ...