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. wp跳转到评价界面代码

    wp跳转到评价界面代码(仅适用于wp8.0) MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask(); ma ...

  2. AJPFX关于java的依赖 关联 聚合的关系解释

    依赖:  两个相对独立的系统,当一个系统要构筑另一个系统的实例,或者依赖另一的服务时,这两个就是依赖关系.比如自行车和打气筒之间就是依赖关系.代码表现形式如下:    public class A{  ...

  3. (5)《Head First HTML与CSS》学习笔记---布局与定位

    层叠与CSS的权重判断 1.要理解层叠,除了前面的内容外还差最后一个知识点.你已经知道如何使用多个样式表来更好地组织你的样式,或者支持不同类型的设备.不过实际上用户访问你的页面时还有另外一些样式表. ...

  4. FragmentTabHost实现标签卡效果

    转载请注明原文链接:http://www.cnblogs.com/yanyojun/p/8099523.html 代码已上传到github:https://github.com/YanYoJun/Fr ...

  5. 【java基础】Java锁机制

    在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁(广义) 互斥锁/读写锁(独享锁/共享锁的实现) 乐观锁 ...

  6. (转)新手学习System Verilog & UVM指南

    从刚接触System Verilog以及后来的VMM,OVM,UVM已经有很多年了,随着电子工业的逐步发展,国内对验证人才的需求也会急剧增加,这从各大招聘网站贴出的职位上也可以看出来,不少朋友可能想尽 ...

  7. array_keys

    <?php$array = array(0 => 100, "color" => "red");print_r(array_keys($arr ...

  8. 51nod 1096 距离之和最小(水题日常)

    基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 X轴上有N个点,求X轴上一点使它到这N个点的距离之和最小,输出这个最小的距离之和.   Input 第1行:点的数量 ...

  9. $.noconflict() 有什么用处

    jQuery默认使用"$"操作符,prototype等其他框架也是是使用"$",于是,如果jQuery在其他库之后引入,那么jQuery将获得"$&q ...

  10. swift学习——枚举

    swift枚举 1. 枚举基本语法 enum Method { case Add case Sub case Mul case Div } 也可以使用一种更简单的写法 enum Method1{ ca ...