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. Django 开发blog未完待续

    [root@sishen simpleblog]# python3.5 Python 3.5.4 (default, Sep 20 2017, 20:37:45) [GCC 4.4.7 2012031 ...

  2. js 获取最后一个字符

    方法一: str.charAt(str.length - 1) 方法二: str.subStr(str.length-1,1) 方法三:    var str = "123456" ...

  3. java只http改成https访问

    目录 生成keystore文件 修改tomcat中的server.xml文件 配置浏览器 生成keystore文件: 1.在tomcat的bin 目录下输入命令:keytool -genkeypair ...

  4. 3. UITest笔记

    1.    XCUIApplication *app = [[XCUIApplication alloc] init]; App为查询的入口,当界面发生变化,查询数也会随之更新. 即使是先前存储的XC ...

  5. Java URL 中文乱码解决办法

    一. 统一所有的编码格式 (1)JSP页面设置:<%@ page language="java" import="java.util.*" pageEnc ...

  6. 洛谷 P1802 5倍经验日

    题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却无奈的看着那一些比他等级高的好友,想着能否把他们干掉.干掉能拿不少经验的. 题目描述 现在absi2011拿出了x个迷你装药物 ...

  7. Android(java)学习笔记164:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)

    1.首先我们看看下面这个需求: 这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到" ...

  8. Fire Air(华科校赛 网络赛)

    题目 原题链接:https://www.nowcoder.com/acm/contest/106/L 在100000 * 10000的空地上,有n个时间点,每个时间点会在(xi,yi)上种一棵树. 定 ...

  9. uva1628 Pizza Delivery

    fixing great wall 的变形dp(i,j,k,p)不考虑i-j的客人,还要送k个人,目前位置在p起点i和总数量k都要枚举dp(i,j,k,p)=max(dp(m,j,k-1,p)+val ...

  10. STL || HDU 1263 水果

    map可以映射map…… 然后在map里面会自己排序 惊了 注意输出格式 回车的输出 #include <iostream> #include <cstdio> #includ ...