\(\color{purple}{Link}\)

\(\text{Solution:}\)

题目要求找到两个串的最长公共子串。\(LCP\)

我们将两个串中间和末尾插入终止符,并弄到一棵后缀树上去。

然后我们发现,对于一个叶子节点,它属于哪个子串,我们只需要找到它的父边上第一个出现的终止符属于哪个边即可。

这里,我们可以用个奇技淫巧——前缀和实现。

介于\(\text{Suffix Tree}\)的边都是压缩的,所以维护信息变得不是很容易,所以可以采用一个在插入外面进行预处理前缀和的方式维护。

然后,我们只需要找到一个最深的非叶子节点,使得它的子树中既含有第一个串串的终止符,也含有第二个串串的终止符即可。

此时它的答案就是这个点的深度。

\(\text{Suffix Tree}\)的主要问题就在于边上信息的维护。如果找不到一个好的方法去维护,\(\text{Suffix Tree}\)还是很麻烦的。

最近做题,题解区域都没有\(\text{Suffix Tree}\)的题解,做起来真的挺累……我太菜了。

#include<bits/stdc++.h>
#include<ctime>
using namespace std;
const int MAXN=1.2e6+10;
string Z,z;
int n,val[MAXN],ans,tot;
int sum[MAXN],sum2[MAXN];
const int inf=(1<<30);
struct SuffixTree {
int link[MAXN],ch[MAXN][28],now,rem,n;
int start[MAXN],len[MAXN],tail,s[MAXN];
SuffixTree() {
tail=now=1;
rem=n=0;
len[0]=inf;
}
inline int build(int a,int b) {
link[++tail]=1;
start[tail]=a;
len[tail]=b;
return tail;
}
void Extend(int x) {
s[++n]=x;
++rem;
for(int last=1; rem;) {
while(rem>len[ch[now][s[n-rem+1]]])
rem-=len[now=ch[now][s[n-rem+1]]];
int &v=ch[now][s[n-rem+1]];
int c=s[start[v]+rem-1];
if(!v||x==c) {
link[last]=now;
last=now;
if(!v)v=build(n,inf);
else break;
} else {
int u=build(start[v],rem-1);
ch[u][c]=v;
ch[u][x]=build(n,inf);
start[v]+=rem-1;
len[v]-=rem-1;
link[last]=v=u;
last=u;
}
if(now==1)--rem;
else now=link[now];
}
}
} T;
void predfs(int u,int dep) {
if(dep>=inf) {
int L=T.start[u];
int R=L+T.len[u]-1;
R=min(R,T.n);
int V=sum[R]-sum[L-1];
if(V)val[u]=1;
else{
V=sum2[R]-sum2[L-1];
if(V)val[u]=2;
}
return;
}
for(int i=0; i<28; ++i) {
if(T.ch[u][i]) {
predfs(T.ch[u][i],dep+T.len[T.ch[u][i]]);
val[u]|=val[T.ch[u][i]];
}
}
if(val[u]>=3)ans=max(ans,dep);
}
char buf[1<<21],*p1=buf,*p2=buf;
string read(){
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
string s="";
char ch=gc();
while(ch=='\n')ch=gc();
while(ch!='\n')s+=ch,ch=gc();
return s;
}
int main() {
// freopen("1.in","r",stdin);
// freopen("SP.out","w",stdout);
clock_t ST,ET;
ST=clock();
z=read();Z=read();
z+=(char)'a'+26;
z+=Z;z+=(char)'a'+27 ;
for(int i=0;i<z.size();++i)z[i]-='a',T.Extend(z[i]);
tot=z.size();
for(int i=1; i<=tot; ++i) {
sum[i]=sum[i-1]+(T.s[i]==26);
sum2[i]=sum2[i-1]+(T.s[i]==27);
}
predfs(1,0);
printf("%d\n",ans);
ET=clock();
// cout<<(double)(ET-ST)/CLOCKS_PER_SEC<<"s"<<endl;
return 0;
}

注意,如果在\(dfs\)里面来根据这条边的起点和终点暴力处理的话,这就是个\(n^2\)暴力。观察到,我们只需要在叶子的节点处理,而在叶子节点暴力处理的复杂度也不够优秀。

观察到,第一个字符串的终止符一定先于第二个字符串的终止符出现(如果有的话)。那么,根据前缀和,先判断第一个终止符,再判断第二个终止符即可。

最后时间复杂度是:

\(\text{We let D show the constant,then the complexity is O(D*N).N is the length of these strings.}\)

【题解】SP1811 LCS - Longest Common Substring的更多相关文章

  1. SP1811 LCS - Longest Common Substring

    \(\color{#0066ff}{ 题目描述 }\) 输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串.如果没有公共子串则输出0 . \(\color{#0066ff}{输 ...

  2. 「双串最长公共子串」SP1811 LCS - Longest Common Substring

    知识点: SAM,SA,单调栈,Hash 原题面 Luogu 来自 poj 的双倍经验 简述 给定两字符串 \(S_1, S_2\),求它们的最长公共子串长度. \(|S_1|,|S_2|\le 2. ...

  3. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  4. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  5. spoj1811 LCS - Longest Common Substring

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

  6. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  7. LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  8. SPOJ 1811 LCS - Longest Common Substring

    思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...

  9. SPOJ1811 LCS - Longest Common Substring(后缀自动机)

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...

随机推荐

  1. 为什么golang中不存在三元运算符

    三元运算符广泛存在于其他语言中,比如: python: val = trueValue if expr else falseValue javascript: const val = expr ? t ...

  2. IntPtr to bytes

    byte[] managedArray = new byte[size]; Marshal.Copy(pnt, managedArray, 0, size);

  3. python学习笔记回忆录02

    1.for循环 依次按顺序从列表中取出值,直到遍历完整个列表为止 the_count =[1,2,3,4,5] for number in the_count: print "this is ...

  4. Opencv+Yolov3算法实现社交距离安全检测讲解和实战(Social Distance Detector)

    在我们进行交流谈话时,人与人之间总要保持一定的距离,尤其是在疫情的情况下,人与人之间更要保持一定的安全距离,今天给大家来介绍一个检测社交距离的项目,实现社交距离检测器. 社交距离(Social Dis ...

  5. c++基础 写二进制文件

    问题描述 有许多数据待拟合,需要从 root 中提取出来,写成文本文件数据量过大,想转成二进制文件. 解决 #include "TString.h" #include " ...

  6. 在Apache服务器上安装SSL证书

    参考:链接 前提条件 1.您的Apache服务器上已经开启了443端口(HTTPS服务的默认端口) // 开通443端口 firewall-cmd --zone=public --add-port=4 ...

  7. ssm框架spring-mvc.xml和spring-mybatis.xml报错项目无法正常启动问题

    报错信息 Multiple annotations found at this line: - Referenced file contains errors (http://www.springfr ...

  8. PHP程序十点未来的建议

    1. Composer 第一点就要提 Composer ,自从 Composer 出现后,PHP 的依赖管理可以变得非常简单.程序内依赖一些类库和框架,直接使用 Composer 引入即可,通过使用 ...

  9. 线上问题排查-HBase写数据出现NotServingRegionException(Region ... is not online)异常

    今天线上遇到一个问题:有一台服务器的cpu持续冲高,排查发现是我们的一个java应用进程造成的,该进程在向hbase中写入数据时,日志不断地打印下面的异常: org.apache.hadoop.hba ...

  10. yum管理——linux字符界面安装图形化及两种界面的切换(3)

    1.查看yum软件包组 yum groups list 2.选择安装带 GUI 的服务器 yum groups install "带 GUI 的服务器" 3.字符界面切换为图形化界 ...