【题解】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 ...
随机推荐
- 在Spring中拦截器的使用
Filter Filter是Servlet容器实现的,并不是由Spring 实现的 下面是一个例子 import java.io.IOException; import javax.servlet.F ...
- iOS NSNotification通知
通知中心(NSNotificationCenter) 通知(NSNotification) 一个完整的通知一般包含3个属性:(注意顺序) - (NSString *)name; 通知的名称 - (i ...
- 尝试MatCap类型shader
听说MatCap能在低端机上做出很漂亮的pbr效果,就尝试了一下. MatCap全称MaterailCapture,里面存的是光照信息,通过法线的xy分量去采样matcap,得到在该方向法线的光照信息 ...
- nginx如何限制并发连接请求数?
简介 限制并发连接数的模块为:http_limit_conn_module,地址:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.ht ...
- I - 乓 (BFS+邻接表)
USTC campus network is a huge network. There is a bi-directional link between every pair of computer ...
- 深入了解Kafka【五】Partition和消费者的关系
1.消费者与Partition 以下来自<kafak权威指南>第4章. 假设主题T1有四个分区. 1.1.一个消费者组 1.1.1.消费者数量小于分区数量 只有一个消费者时,消费者1将收到 ...
- Splay 记录
luogu 模板 P3391 [模板]文艺平衡树(Splay). 知识点:1.splay模板题,练习splay,rotate顺序:x变成z的儿子,x的一个儿子变为y的一个儿子(具体哪个看代码),y变为 ...
- Q20200511-01 翻转字符串
需求:做一函数将字符串倒转过来 程序: package test4; public class Reverse { public static String reverse(String origin ...
- Spring Boot与日志
目录 1.日志框架 2.市面上的日志框架 2.1 下表行间无任何对应关系 2.2 日志门面:slf4j 2.3 日志实现:logback 2.4 Spring Boot怎么做的呢? 3.slf4j的使 ...
- unzip命令笔记
unzip命令 文件压缩与解压 unzip命令用于解压缩由zip命令压缩的".zip"压缩包. 语法 unzip(选项)(参数) 选项 -c:将解压缩的结果显示到屏幕上,并对字符做 ...