●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\)个子串,要求这\ ...
随机推荐
- 12-TypeScript总结
从前面的文章大家可以看出,TypeScript具有先天的优势,建议前端开发人员使用TypeScript进行开发,提升自己的面向对象开发思想与能力.: 1.微软开源的客户端脚本语言,是JavaScrip ...
- C#之Socket通信
0.虽然之前在项目中也有用过Socket,但始终不是自己搭建的,所以对Server,Clinet端以及心跳,断线重连总没有很深入的理解,现在自己搭建了一遍加深一下理解. 服务端使用WPF界面,客户端使 ...
- 扩展Microsoft Graph数据结构 - 架构扩展
前言 此前我有一篇 文章 讲解了Microsoft Graph的一种数据扩展技术-- 开发扩展(Open Extensions),它可以实现在支持的对象(例如用户,组等)上面附加任意的数据.但开放扩展 ...
- Mybatis的mapper代理开发dao方法
看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...
- Python内置函数(46)——format
英文文档: format(value[, format_spec]) Convert a value to a "formatted" representation, as con ...
- 阿里云API网关(7)开发指南-API参考
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- Homebrew update error not work on OSX
brew update 错误是这样的 chown: /usr/local: Operation not permitted 然后网上osx 10.11, 10.12的解决方法这样的 The probl ...
- Bootstrap 做一个简单的母版页
随便搭的一个母版页,不太好看,只是为了看效果....请勿吐槽. 效果如图: 一.新建母版页,引入Bootstrap相关js文件 <link href="../css/bootstrap ...
- spring4——IOC之基于注解的依赖注入(DI )
spring容器对于Bean的创建和对象属性的依赖注入提供了注解的支持,让我们在开发中能够更加便捷的实现对象的创建和对象属性的依赖注入.一,对于Bean的创建spring容器提供了以下四个注解的支持: ...
- JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...