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 】
对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...
随机推荐
- a.WHERE使用中单行子查询(适用于>,<,=,>=,<=等条件)
a.单行子查询(适用于>,<,=,>=,<=等条件) //查询工资最高的员工编号和员工名 select empno,ename from emp where ...
- ASP.NET-RedirectToAction只能使用get方法
两个同名Action共同使用return View() return RedirectToAction("test", new { ls = list.Fct_OrderList ...
- 洛谷 P2071 座位安排 seat.cpp/c/pas
P2071 座位安排 seat.cpp/c/pas 题目背景 公元二零一四年四月十七日,小明参加了省赛,在一路上,他遇到了许多问题,请你帮他解决. 题目描述 已知车上有N排座位,有N*2个人参加省赛, ...
- POJ 3696
这里面的一个转换的小技巧很重要,把888...8转换成(10^x-1)/9*8.神来之笔,佩服. 这样有(10^x-1)/9*8=L*p得10^x-1=L*p*9/8,设m=9*L/gcd(L,8). ...
- HDU 1131
N个节点的不同的树的数目.这样 随便取一个节点作为根,那么他左边和右边的儿子节点个数就确定了,假定根节点标号为x,那么左子树的标号就从1到x-1,共x-1个,右子树的标号就从x+1到n,共n-x个,那 ...
- 暑假NOIP期末考试【1】—— Phantom
Phantom •题目名称: phantom •时间限制:1 秒 •空间限制:256 MiB 题目描写叙述 在一个无限大的棋盘上.排列着 n * n 枚棋子,形成一个 n 行 n 列的方阵.棋子能够横 ...
- Android程序之全国天气预报查询(聚合数据开发)
一.项目演示效果例如以下: 项目源码下载地址: http://pan.baidu.com/s/1pL6o5Mb password:5myq 二.使用 聚合数据SDK: (1)聚合数据官网地址:http ...
- atitit。流程图的设计与制作 attilax 总结
atitit.流程图的设计与制作 attilax 总结 1. 流程图的规范1 2. 画图语言2 2.1. atitit.CSDN-markdown编辑器2 2.2. js-sequence-diagr ...
- android学习笔记(5)Activity生命周期学习
每一个activity都有它的生命周期,开启它,关闭它,跳转到其他activity等等,都会自己主动调用下面某种方法.对这些个方法覆写后观察学习. protected void onCreate(Bu ...
- 从零開始学android<TabHost标签组件.二十九.>
TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...