SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串
【传送门:SPOJ1811&BZOJ2946】
简要题意:
给出若干个字符串,求出这些字符串的最长公共子串
题解:
后缀自动机
这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不多(代码就贴SPOJ的,因为数据范围大一点)
首先用第一个字符串构造SAM
然后处理其他的每个串在后缀自动机的每个状态能够匹配的子串最大值
然后总的来处理所有串在这个状态能够匹配的最小值
最后将所有状态都枚举一遍,求出其中的最大值就行了
参考代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct SAM
{
int son[],fail,dep;
}tr[];int root,cnt,last;
int a[];
void add(int k)
{
int x=a[k];
int np=++cnt,p=last;
tr[np].dep=k;
while(p!=&&tr[p].son[x]==) tr[p].son[x]=np,p=tr[p].fail;
if(p==) tr[np].fail=root;
else
{
int q=tr[p].son[x];
if(tr[q].dep==tr[p].dep+) tr[np].fail=q;
else
{
int nq=++cnt;tr[nq]=tr[q];
tr[nq].dep=tr[p].dep+;
tr[q].fail=tr[np].fail=nq;
while(p!=&&tr[p].son[x]==q) tr[p].son[x]=nq,p=tr[p].fail;
}
}
last=np;
}
char st[];
int Rsort[],sa[];
int d[],f[];
int main()
{
scanf("%s",st+);
int len=strlen(st+);
cnt=root=last=;
for(int i=;i<=len;i++) a[i]=st[i]-'a'+,add(i);
for(int i=;i<=cnt;i++) Rsort[tr[i].dep]++;
for(int i=;i<=len;i++) Rsort[i]+=Rsort[i-];
for(int i=cnt;i>=;i--) sa[Rsort[tr[i].dep]--]=i;
memset(f,,sizeof(f));
while(scanf("%s",st+)!=EOF)
{
len=strlen(st+);
memset(d,,sizeof(d));
for(int i=;i<=len;i++) a[i]=st[i]-'a'+;
int p=root,s=,ans=;
for(int i=;i<=len;i++)
{
if(tr[p].son[a[i]]!=)
{
s++;
p=tr[p].son[a[i]];
}
else
{
while(p!=&&tr[p].son[a[i]]==) p=tr[p].fail;
if(p==) p=root,s=;
else
{
s=tr[p].dep+,p=tr[p].son[a[i]];
}
}
d[p]=max(d[p],s);
}
for(int i=cnt;i>=;i--) d[tr[sa[i]].fail]=min(tr[tr[sa[i]].fail].dep,max(d[tr[sa[i]].fail],d[sa[i]]));
for(int i=cnt;i>=;i--) f[sa[i]]=min(f[sa[i]],d[sa[i]]);
}
int ans=;
for(int i=;i<=cnt;i++) ans=max(f[i],ans);
printf("%d\n",ans);
return ;
}
SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串的更多相关文章
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(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 < ...
- 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, \(\ ...
- 【刷题】SPOJ 1812 LCS2 - Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- 题解 SP1812 【LCS2 - Longest Common Substring II 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
随机推荐
- ASP.NET-HttpPostedFileBase file为null的问题
MVC使用Ajax.BeginForm上传图片时HttpPostedFileBase file为null,Request.Files获取不到文件,问题分析是页面中存在jquery.unobtrusiv ...
- gdb学习-checkpoint,watch
checkpoint的内容参考: http://blog.chinaunix.net/uid-23629988-id-2943273.html 这一篇主要是checkpoint,在next之前加che ...
- [博弈] hdu 3683 Gomoku
题意: 两个人下五子棋.给你现有棋盘,推断在三步之内的胜负情况. 输出分为几种. 1.棋盘不合法 2.黑或白在第一步赢下在(x,y)点,多个输出x最小的.y最小的. 3.输在第二步 4.黑或白在第三步 ...
- 小贝_mysql三种子查询
mysql三种子查询 简要: 一.三种子查询 二.三种子查询理解模型 一.mysql 三种子查询 where子查询.from子查询.exists子查询 二.理解模型: 2.1.一个好的模型,便于我们去 ...
- Linux网络编程(附1)——封装read、write
原打算实践简单的模型的时候,主要专注于基本的模型,先用UNIX I/O糊弄下,可是未封装的read和write用起来实在心累,还是直接用前辈们已经实现好的高级版本号read.write. UNIX I ...
- GitBlit中出现 error: remote unpack failed: error Missing tree
clu@WASYGSHA01-1020 MINGW64 /d/ChuckLu/Git/Edenred/LISA_5.0.0.0 (local)$ git push origin preaction:p ...
- 11.使用boostregex遭遇无法打开libboost_regex-vc120-mt-sgd-1_62.lib的问题
通过Boost库可以在C++项目中使用正则表达式,配置好环境后链接过程出现”无法打开libboost_regex-vc120-mt-sgd-1_62.lib”的错误. 原因是按照官方生成lib的方法 ...
- sublime配置python运行环境
1.sublime下载与插件管理 1.1 下载 官网地址:https://www.sublimetext.com/3 1.2 安装Package Control管理插件 使用ctrl + ` (感叹后 ...
- HTTP学习记录
title: HTTP学习记录 toc: true date: 2018-09-21 20:40:48 HTTP协议,HyperText Transfer Protocol,超文本传输协议,是因特网上 ...
- sql server Delete误操作后如何恢复数据
声明:本文是根据别人的经验https://blog.csdn.net/dba_huangzj/article/details/8491327写的总结 说明:update和delete时没有加where ...