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. MongoDB管理练习

    一.索引 1.插入10W条数据 文档内容为:{name:zs-i,age:1} 2016-06-07T14:35:57.041+0800 I CONTROL [initandlisten] > ...

  2. Asp.Net MVC中捕捉错误路由并设置默认Not Found页面。

    在Global中写一个Application_Error捕捉错误路由并重定向到Not Found页面.这里是全局性抓取错误路由,此处还可以写由错误路由导致访问失败的日志记录. protected vo ...

  3. 拦截@RequestBody的请求数据

    要拦截首先想到的是拦截器,@RequestBody只能以流的方式读取,流被读过一次后,就不在存在了,会导致会续无法处理,因此不能直接读流 为了解决这个问题,思路如下: 1.读取流前先把流保存一下 2. ...

  4. 解决Android Studio安装过程中“SDK tools directory is missing”的问题

    "SDK tools directory is missing",这是因为安装时你的计算机无法连接到google的服务器(对google服务器的域名地址解析出问题了),无法从goo ...

  5. SQL Server 2008还原数据库的具体方法

    俗话说“好记性不如烂笔头”,在相隔较长的时间段内,每次还原客户的数据库都记不清完全的步骤,为此mark一下. SQL Server 2008一般默认备份的文件格式是bak,即后缀名为.bak.bak文 ...

  6. 在CentOS上把Git从1.7.1升级到1.7.12.4

    在CentOS上把Git从1.7.1升级到1.7.12.4 摘要:本文记录了在CentOS 6.3上,把Git从1.7.1升级到1.7.12.4的过程. 1. 概述 在我做的一个项目中,最近我对生产服 ...

  7. Hibernate核心接口和工作原理

    Hibernate核心接口和工作原理 Hibernate有五大核心接口,分别是:Session .Transaction .Query .SessionFactory .Configuration . ...

  8. jsp <%@ include %> 例子

    <%@ include %>:所有代码包含进来之后一起进行处理,最终编译成一个servlet. jsp文件中添加top和bottom.jsp页面 empList.jsp <%@ pa ...

  9. 原创 :单刷深渊 在Linux中系统安装mysql实战直播

    [root@web108 tools]# ###开始装mysql 1添加用户 [root@web108 tools]# useradd -s /sbin/nologin -M mysql 2解压 [r ...

  10. jeecms

    ===标签=== <!-- 显示一级栏目对应的二级栏目 --> <!-- [@cms_channel_list parentId=c.id] [#if tag_list?size&g ...