【题解】SP1811 LCS - Longest Common Substring
\(\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的更多相关文章
- SP1811 LCS - Longest Common Substring
\(\color{#0066ff}{ 题目描述 }\) 输入2 个长度不大于250000的字符串,输出这2 个字符串的最长公共子串.如果没有公共子串则输出0 . \(\color{#0066ff}{输 ...
- 「双串最长公共子串」SP1811 LCS - Longest Common Substring
知识点: SAM,SA,单调栈,Hash 原题面 Luogu 来自 poj 的双倍经验 简述 给定两字符串 \(S_1, S_2\),求它们的最长公共子串长度. \(|S_1|,|S_2|\le 2. ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- LCS - Longest Common Substring(spoj1811) (sam(后缀自动机)+LCS)
A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...
- SPOJ 1811 LCS - Longest Common Substring
思路 和SPOJ 1812 LCS2 - Longest Common Substring II一个思路,改成两个串就有双倍经验了 代码 #include <cstdio> #includ ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
随机推荐
- 初学WebGL引擎-BabylonJS:第3篇-方向纹理与相机
[playground]-rotatuib abd scaling(方向) 源码 var createScene = function () { var scene = new BABYLON.Sce ...
- EventDispatcher
事件分发类,提供事件注册.移除.触发功能 采用delegate.dictionary实现 支持自定义事件.事件采用字符串方式标识 支持 0,1,2,3,4 等5种不同参数个数的回调函数 // 路由 ...
- java初探(1)之登录终探
上一章讲了表单验证,数据验证和加密.这一章,将研究服务器和数据库的交互过程. 后端服务器有两种主流的形式,SQL数据库和NOSQL数据库.其中MYSQL属于SQL数据库,REDIS属于非SQL数据库. ...
- [BUUOJ记录] [极客大挑战 2019]RCE ME
前面考察取反或者异或绕过,后面读Flag那里我用脏方法过了,没看出来考察啥 进入题目给出源码: <?php error_reporting(0); if(isset($_GET['code']) ...
- 写给.NET开发者的Python教程(三):运算符、条件判断和循环语句
本节会介绍Python中运算符的用法,以及条件判断和循环语句的使用方法. 运算符 运算符包括算术运算符.赋值运算符.比较运算符.逻辑运算符等内容,大部分用法和C#基本一致,下面我们来看一下: 算数运算 ...
- 封装React AntD的dialog弹窗组件
前一段时间分享了基于vue和element所封装的弹窗组件(封装Vue Element的dialog弹窗组件),今天就来分享一个基于react和antD所封装的弹窗组件,反正所使用的技术还是那个技术, ...
- 获取JSO字符串的key和value值
import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.HashMap; import java. ...
- SpringMVC-08-整合SSM之CRUD
查询书籍功能 完善Controller:BookController @Controller @RequestMapping("/book") public class BookC ...
- css常用属性之绝对定位、相对定位、滚动条属性、背景图属性、字体、鼠标、超链接跳转页面
1.绝对定位position: fixed(比如广告页面向下滑动的时候,页面最上方有个标题不能随之滑动,就需要用到position: fixed,同时还需要用到一个标签(标签高度很高才会出现滚动的情况 ...
- oracle之数据限定与排序
数据限定与排序 6.1 简单查询语句执行顺序 from, where, group by, having, order by, select where限定from后面的表或视图,限定的选项只能是表的 ...