【传送门: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. 2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem

    MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  2. 利用VisualVM监视远程JVM

    VisualVM介绍 VisualVM是集成了多个JDK命令工具的一个可视化工具,它主要用来监控JVM的运行情况,可以用它来查看和浏览Heap Dump.Thread Dump.内存对象实例情况.GC ...

  3. angular-事件

    ng-click事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <div ng-app="myApp ...

  4. MyBATIS插件原理第一篇——技术基础(反射和JDK动态代理)(转)

    在介绍MyBATIS插件原理前我们需要先学习一下一些基础的知识,否则我们是很难理解MyBATIS的运行原理和插件原理的. MyBATIS最主要的是反射和动态代理技术,让我们首先先熟悉它们. 1:Jav ...

  5. Spring中基于Java的配置@Configuration和@Bean用法 (转)

    spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  6. 使用malloc分别分配2KB的空间,然后用realloc调整为6KB的内存空间,打印指针地址

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...

  7. oracle 10g/11g 命令对照,日志文件夹对照

     oracle 10g/11g  命令对照,日志文件夹对照 oracle 11g 中不再建议使用的命令 Deprecated Command Replacement Commands crs_st ...

  8. System.getProperty()方法可以获取的值

    /** 获得当前类的完整路径.最后一句 */ package org.outman.dms.server; import java.net.MalformedURLException; import ...

  9. 根据数据库表结构生成java类

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWrit ...

  10. Hadoop解析--MapReduce

    从本篇博客開始咱们一起来具体了解Hadoop的每一个部分.我们在上篇博客中介绍了HDFS,MapReduce,MapReduce为了更有效率事实上是建立在HDFS之上的.有了分布式的文件系统,我们就能 ...