PortalPortal to 洛谷

Description

给出\(n(n\leq10)\)个仅包含小写字母的字符串\(s_1..s_n(|s_i|\leq10^5)\),求这些字符串的最长公共子串长度。

Solution

对第一个串建立SAM,然后依次跑剩下的串。

记录\(maxL[i]\)表示对于目前做过的所有串,状态\(i\)最长能匹配的长度。跑一个串\(x\)时,记录\(tmp[i]\)表示对于串\(x\),状态\(i\)最长能匹配的长度,跑完之后将\(maxL\)与\(tmp\)取\(min\)。答案就是\(maxL\)中的最大值。

如何在SAM上跑一个串呢?如果当前处于状态\(p\),下一个字符是\(x\),那么若有\(ch[p][x]\)则转移,否则在\(parent\)树上回溯\(p\)直至存在\(ch[p][x]\)并转移。如果直到根也不存在\(ch[p][x]\),那么令\(p=rt\),从头开始匹配。在\(parent\)上回溯就相当于不断减小匹配长度\(len\)。

不过只是跑一遍可并不能求出所有的\(tmp\),有的状态因为不够优而被跳过了。所以跑完之后,我们应当在\(parent\)树上从底向上更新出所有状态的\(tmp\),即tmp[fa[p]]=max(tmp[fa[p]],min(len[fa[p],tmp[p]))。但由于\(len[fa[p]] < tmp[p] \leq len[p], tmp[fa[p]]\leq len[fa[p]]\),所以后面的一堆在\(p\)被匹配到(存在\(tmp[p]\))的情况下必然等于\(len[fa[p]]\)。

时间复杂度\(O(\sum|s|)\)。

Code

//Longest Common Substring
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int const N=2e5+10;
int const INF=0x7FFFFFFF;
char s0[N>>1],s[N>>1];
int ndCnt,rt,last;
int fa[N],ch[N][26],len[N];
void ins(int x)
{
int p=last,np=++ndCnt;
last=np,len[np]=len[p]+1;
for(p;p&&!ch[p][x];p=fa[p]) ch[p][x]=np;
if(!p) {fa[np]=rt; return;}
int q=ch[p][x];
if(len[q]==len[p]+1) {fa[np]=q; return;}
int nq=++ndCnt; len[nq]=len[p]+1;
for(int i=0;i<26;i++) ch[nq][i]=ch[q][i];
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
for(p;p&&ch[p][x]==q;p=fa[p]) ch[p][x]=nq;
}
int cnt[N],ord[N];
void buildSAM(char s[])
{
last=rt=++ndCnt;
for(int i=1;s[i];i++) ins(s[i]-'a');
memset(cnt,0,sizeof cnt);
for(int i=1;i<=ndCnt;i++) cnt[len[i]]++;
for(int i=ndCnt-1;i>=0;i--) cnt[i]+=cnt[i+1];
for(int i=ndCnt;i>=1;i--) ord[cnt[len[i]]--]=i;
}
int maxL[N],tmp[N];
void query(char s[])
{
memset(tmp,0,sizeof tmp);
for(int p=rt,L=0,i=1;s[i];i++)
{
int x=s[i]-'a';
if(ch[p][x]) L++,p=ch[p][x];
else
{
while(p&&!ch[p][x]) p=fa[p];
if(!p) L=0,p=rt;
else L=len[p]+1,p=ch[p][x];
}
tmp[p]=max(tmp[p],L);
}
for(int i=1;i<=ndCnt;i++)
{
int p=ord[i]; maxL[p]=min(maxL[p],tmp[p]);
if(fa[p]&&tmp[p]) tmp[fa[p]]=len[fa[p]];
//等价于tmp[fa[p]]=max(tmp[fa[p]],min(len[fa[p],tmp[p]))
}
}
int main()
{
scanf("%s",s0+1); buildSAM(s0);
for(int p=1;p<=ndCnt;p++) maxL[p]=len[p];
while(scanf("%s",s+1)!=EOF) query(s);
int ans=0;
for(int p=1;p<=ndCnt;p++) ans=max(ans,maxL[p]);
printf("%d\n",ans);
return 0;
}

P.S.

昨天就A了今天才发题解真是对不起...

SPOJ1812 - Longest Common Substring II(LCS2)的更多相关文章

  1. SPOJ1812 Longest Common Substring II

    题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...

  2. [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串

    题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...

  3. spoj1812 LCS2 - Longest Common Substring II

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

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

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

  5. SPOJ LCS2 - Longest Common Substring II

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

  6. 【SP1812】LCS2 - Longest Common Substring II

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

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

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

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

  9. Longest Common Substring II SPOJ - LCS2 (后缀自动机)

    Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...

随机推荐

  1. RedHat改yum源免费使用CentOS源

    linux默认是安装了yum软件的,但是由于激活认证的原因让redhat无法直接进行yum安装一些软件 如果我们需要在redhat下直接yum安装软件,我们只用把yum的源修改成CentOS的就好了, ...

  2. Unity Mesh 初体验

    什么是Mesh Mesh是Unity中的一个组件,称为网格组件.通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而一个复杂的多边形,实际上是由多个三角面拼接而成.所以一个3D模型的表面是 ...

  3. P2629 好消息,坏消息

    题目描述 uim在公司里面当秘书,现在有n条消息要告知老板.每条消息有一个好坏度,这会影响老板的心情.告知完一条消息后,老板的心情等于之前老板的心情加上这条消息的好坏度.最开始老板的心情是0,一旦老板 ...

  4. android开发学习 ------- Error:Failed to open zip file.

    我们用Android Studio   Sync Project项目的时候,会出现如下的错误: 解决方案: Project视图下, 这块 https 改为 http 就可以了.

  5. TextView、EditText

    1.TextView     显示文本信息 常用属性: layout_width/height    控件的宽/高 width/heigth    文本区域的宽/高 text 显示的文本 textSi ...

  6. 设计模式(3)-- 原型模式 (clone分析)

    原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型来创建对象. 在java中有语言级别的支持:clone 在java中使用原型模式是非常简单的事情,实现Cloneable接口,调用Objec ...

  7. Haproxy+Rabbitmq中的问题

    问题一.Rabbitmq集群搭建完成 某个集群节宕机后 无法添加失败 解决办法:停掉所有Rabbitmq服务 并删除集群文件C\Users\Administrator\AppData\Roaming\ ...

  8. Android原生系统API自带dp、px、sp单位转换

    Android系统中自带的Api中可以使用TypedValue进行单位转换 1,调用系统api转换单位 // 获得转换后的px值 float pxDimension = TypedValue.appl ...

  9. Sql Server 2008R2升级 Sql Server 2012 问题

    环境: Windows server 2008 r2 Standard +SqlServer2008R2  内网环境需要升级为SQL server 2012 升级安装时提示版本不支持 网上查询相关问题 ...

  10. Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL

    一.IPC inter process communication  进程间通讯 二.AIDL android  interface  defination  language  安卓接口定义语言 满 ...