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 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
随机推荐
- 6.between...and...
6.在WHERE中使用between...and... 用于区间值的条件判断(包含边界值) //查询工资在2000(包含)到3000(包含)之间的员工信息 select empno,e ...
- cocos2d-x学习笔记(18)--游戏打包(windows平台)
cocos2d-x学习笔记(18)--游戏打包(windows平台) 之前做好的游戏,都是在vs2008下编译执行的.假设说想把游戏公布到网上或者和其它人一起分享游戏,那就得对游戏 ...
- POJ 3744
矩阵快速乘求概率,不难.但有注意的一点是,一定要注意地雷连着的情况,一旦出现两个雷相邻,就必定为0了. #include <iostream> #include <algorithm ...
- HDU 4331 Contest 4
一个很直观的想法是,求出每个点上下左右能到达的最大长度.然后枚举其斜边...没想到过了.... 当然,题解有一个很巧妙的优化,利用树状数组,那个太巧妙了. #include<iostream&g ...
- Python——异常基础
异常基础 在Python中,异常会依据错误自己主动地被触发.也能由代码触发和截获.异常由五个语句处理: 1.[try/except]:捕捉由Python或你引起的异常并恢复. 2.[try/final ...
- [JZOJ 5875] [NOIP2018提高组模拟9.20] 听我说,海蜗牛 解题报告(BFS+二分)
题目链接: http://172.16.0.132/senior/#main/show/5875 题目: 题解: 注意这题只能经过开放的港口 我们考虑用vector存下每个点不能到的点,并把并让vec ...
- web.xml配置详解(转载)
一.web.xml配置文件常用元素及其意义预览 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <display-name></di ...
- ES6 | class类的基本语法总结
类和模块的内部,默认就是严格模式,所以不需要使用use strict指定运行模式.只要你的代码写在类或模块之中,就只有严格模式可用. 考虑到未来所有的代码,其实都是运行在模块之中,所以 ES6 实际上 ...
- (转载) Android开发mac /dev/kvm is not found
Android开发mac /dev/kvm is not found 标签: KVMAndroid开发KVM is not found芒果Android芒果iOS 2016-10-29 16:31 2 ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...