【传送门: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]公共串的更多相关文章

  1. spoj1812 LCS2 - Longest Common Substring II

    地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags  A string is fi ...

  2. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...

  3. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  4. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  5. 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 ...

  6. spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

    spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...

  7. 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, \(\ ...

  8. 【刷题】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 ...

  9. 题解 SP1812 【LCS2 - Longest Common Substring II 】

    对于本题这样的多字符串的子串匹配问题,其实用广义后缀自动机就可以很好的解决,感觉会比普通的后缀自动机做法方便一些. 首先记录出每个节点被多少个字符串更新,也就是记录每个节点有多少个字符串能到达它,可以 ...

随机推荐

  1. Lock-less and zero copy messaging scheme for telecommunication network applications

    A computer-implemented system and method for a lock-less, zero data copy messaging mechanism in a mu ...

  2. 极路由设置共享磁盘密码、跨网访问samba服务

    极路由插上移动硬盘后会自动建立samba服务器,但我们没法去配置哪些盘符需要密码,这样只要在同一个wifi下的电脑都能去访问这些东西了,比较弱智.另外我还想再公司中去读写这个移动硬盘. 设置密码 首先 ...

  3. [using_microsoft_infopath_2010]Chapter1 介绍InfoPath2010

    本章提要 1.列举对于就SharePoint2010来说使用InfoPath2010的好处 2.使用Office后台函数创建InfoPath表单 3.使用InfoPath接口 4.创建基于XML的文件 ...

  4. Hibernate中的HQL

    一.查询所有的时候 List<Company> list=session.createQuery("from Company as c order by c.cid desc&q ...

  5. OpenCV基础篇之读取显示图片

    程序及分析 /* * FileName : read.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Tue 13 May ...

  6. bzoj3732: Network(最小生成树+LCA)

    3732: Network 题目:传送门 题解: 第一眼就看到最大边最小,直接一波最小生成树. 一开始还担心会错,问了一波肉大佬,任意两点在最小生成树上的路径最大边一定是最小的. 那么事情就变得简单起 ...

  7. su和sudo的区别与使用,su命令,linux命令

    su和sudo的区别与使用 一.   使用 su 命令临时切换用户身份 1. su 的适用条件和威力 su命令就是切换用户 的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务, ...

  8. ubuntu下创建文件夹快捷方式

    title: ubuntu下创建文件夹快捷方式 toc: false date: 2018-09-01 17:22:28 categories: methods tags: ubuntu 快捷方式 s ...

  9. 【DNN】 安装问题

    http://blog.csdn.net/hwt0101/article/details/9153083 这是IIS 注册的问题  IIS 在安装VS 之前就装上了,所以 没有注册是上 F4 从新卸载 ...

  10. POJ 1852 Ants O(n)

    题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...