SPOJ1812 - Longest Common Substring II(LCS2)
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)的更多相关文章
- SPOJ1812 Longest Common Substring II
题意 A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is th ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- spoj1812 LCS2 - Longest Common Substring II
地址:http://www.spoj.com/problems/LCS2/ 题面: LCS2 - Longest Common Substring II no tags A string is fi ...
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 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 ...
- spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)
spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 < ...
- Longest Common Substring II SPOJ - LCS2 (后缀自动机)
Longest Common Substring II \[ Time Limit: 236ms\quad Memory Limit: 1572864 kB \] 题意 给出\(n\)个子串,要求这\ ...
随机推荐
- redis过期事件
背景:目前在商城项目,订单有过期逻辑,小伙伴提议用redis做,经讨论分析,redis有key的过期事件,貌似可以实现,但是咨询大神,好像不建议这样用,可能会丢数据 随便写了段python代码测试 i ...
- python工具之myql数据库操作
import pymysql import config ''' 1.0 简单封装 1.1 添加了insert_id属性,返回insert时返回的主键 1.2 添加了column属性,返回查询的col ...
- macOS 的 JDK 安装问题 (Homebrew)
Homebrew 介绍 Homebrew 是 macOS 下的一个非常好用的包管理工具, caskroom 则是基于 Homebrew 构建的一个强大的应用程序管理器. 具体用法可以餐参考 像 Mac ...
- 关于ES6的Promise的使用深入理解
ES6的promise对象研究 什么叫promise? Promise对象可以理解为一次执行的异步操作,使用promise对象之后可以使用一种链式调用的方式来组织代码:让代码更加的直观. 那我们为什么 ...
- spring中junit 提示Failed to load ApplicationContext
错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext 2:Error creating bean wit ...
- Mysql无法启动服务解决办法
只需要输入:mysqld --initialize 进行初始化,即可启动
- COGS 1439. [NOIP2013]货车运输
★★☆ 输入文件:truck.in 输出文件:truck.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] [来源] CCF全国信息学奥林匹克联赛(NOIP201 ...
- 【整理】treeGrid 树形表格
treeGrid 树形表格 https://fly.layui.com/extend/treeGrid/
- 这就是那个feature map256 256向量
http://blog.csdn.net/XZZPPP/article/details/51582810 在这个特征图上使用3*3的卷积核(滑动窗口)与特征图进行卷积,那么这个3*3的区域卷积后可以获 ...
- 基于Ubuntu 14.04 LTS编译Android4.4.2源代码
转载自:雨水:http://blog.csdn.net/gobitan/article/details/24367439 基于Ubuntu 14.04 LTS编译Android4.4.2源代码 ...