BZOJ3145 [Feyat cup 1.5]Str 后缀树、启发式合并
考虑两种情况:
1、答案由一个最长公共子串+可能的一个模糊匹配位置组成。这个用SAM求一下最长公共子串,但是需要注意只出现在\(S\)的开头和\(T\)的结尾的子串是不能够通过额外的一个模糊匹配得到更长的子串的,而对于其他的子串来说都可以。
2、答案由模糊位置两遍的子串构成。暴力就是枚举\(S\)和\(T\)中模糊匹配的位置\(i,j\),那么长度就是\(LCS(i-1,j-1)+LCP(i+1,j+1)+1\)。
注意到\(LCS(i,j)\)是对正串建SAM得到的前缀树上\(S[:i]\)和\(T[:j]\)对应的点的LCA的Longest;\(LCP(i,j)\)是对反串建SAM得到的后缀树上的\(S[i:]\)和\(T[j:]\)对应节点的LCA的Longest,所以我们可以把这个问题变为类似于两棵树上LCA深度和最大值的一个问题。
对于这个问题,我们考虑在前缀树上dfs,对于每个节点用set维护其子树内所有的合法前缀在后缀树上的dfs序,每一次加入一个子树的时候用启发式合并,用dfs序相邻的两个点的LCA更新当前点的答案,最后用当前点的Longest加上当前点的答案更新总答案。
#include<bits/stdc++.h>
using namespace std;
const int _ = 4e5 + 7;
struct SAM{
int trs[_][27] , Lst[_] , fa[_] , pos[_] , cnt = 1; bool flg[_];
int extend(int p , int l , int c , bool f = 1){
int t = ++cnt; Lst[t] = pos[t] = l; flg[t] = f;
while(p && !trs[p][c]){trs[p][c] = t; p = fa[p];} if(!p){fa[t] = 1; return t;}
int q = trs[p][c]; if(Lst[q] == Lst[p] + 1){fa[t] = q; return t;}
int k = ++cnt; memcpy(trs[k] , trs[q] , sizeof(trs[q]));
fa[k] = fa[q]; fa[q] = fa[t] = k; Lst[k] = Lst[p] + 1;
while(trs[p][c] == q){trs[p][c] = k; p = fa[p];} return t;
}
vector < int > ch[_]; int dfn[_] , to[_][20] , ts , dep[_];
void dfs(int x){
dfn[x] = ++ts; dep[x] = dep[fa[x]] + 1; to[x][0] = fa[x];
for(int i = 1 ; to[x][i - 1] ; ++i) to[x][i] = to[to[x][i - 1]][i - 1];
for(auto t : ch[x]){dfs(t); flg[x] |= flg[t];}
}
void build(){for(int i = 2 ; i <= cnt ; ++i) ch[fa[i]].push_back(i); dfs(dep[1] = 1);}
int LCA(int p , int q){
if(dep[p] < dep[q]) swap(p , q);
for(int i = 18 ; i >= 0 ; --i) if(dep[p] - (1 << i) >= dep[q]) p = to[p][i];
if(p == q) return Lst[p];
for(int i = 18 ; i >= 0 ; --i) if(to[p][i] != to[q][i]){p = to[p][i]; q = to[q][i];}
return Lst[to[p][0]];
}
}sam[3]; char str[_]; int id[2][_] , mx[_] , LS , LT , L , ans;
struct cmp{bool operator ()(int a , int b){return sam[1].dfn[a] < sam[1].dfn[b];}};
set < int , cmp > n1[_] , n2[_];
void merge(int p , int q){
if(n1[p].size() + n2[p].size() < n1[q].size() + n2[q].size()){n1[p].swap(n1[q]); n2[p].swap(n2[q]);}
for(auto t : n1[q]){
auto it = n2[p].lower_bound(t); if(it != n2[p].end()) mx[p] = max(mx[p] , sam[1].LCA(*it , t));
if(it != n2[p].begin()) mx[p] = max(mx[p] , sam[1].LCA(*--it , t));
}
for(auto t : n2[q]){
auto it = n1[p].lower_bound(t); if(it != n1[p].end()) mx[p] = max(mx[p] , sam[1].LCA(*it , t));
if(it != n1[p].begin()) mx[p] = max(mx[p] , sam[1].LCA(*--it , t));
}
for(auto t : n1[q]) n1[p].insert(t); for(auto t : n2[q]) n2[p].insert(t);
}
void dfs(int x){
if(sam[0].pos[x] && sam[0].pos[x] <= LS - 2) n1[x].insert(id[1][sam[0].pos[x] + 2]);
if(sam[0].pos[x] >= LS + 2 && sam[0].pos[x] <= L - 2) n2[x].insert(id[1][sam[0].pos[x] + 2]);
for(auto t : sam[0].ch[x]){dfs(t); merge(x , t);}
if(mx[x]) ans = max(ans , mx[x] + sam[0].Lst[x] + 1);
}
int main(){
scanf("%s" , str + 1); LS = strlen(str + 1); str[LS + 1] = 'z' + 1;
scanf("%s" , str + LS + 2); LT = strlen(str + LS + 2); L = strlen(str + 1);
id[0][0] = id[1][L + 1] = 1;
for(int i = 1 ; i <= L ; ++i) id[0][i] = sam[0].extend(id[0][i - 1] , i , str[i] - 'a');
for(int i = L ; i ; --i) id[1][i] = sam[1].extend(id[1][i + 1] , L - i + 1 , str[i] - 'a');
int pre = 1; for(int i = 1 ; i <= LS ; ++i) pre = sam[2].extend(pre , i , str[i] - 'a' , i != LS);
int cur = 1 , len = 0; sam[2].build();
for(int i = LS + 2 ; i <= L ; ++i){
while(cur && !sam[2].trs[cur][str[i] - 'a']) len = sam[2].Lst[cur = sam[2].fa[cur]];
if(!cur) cur = 1; else{cur = sam[2].trs[cur][str[i] - 'a']; ++len;}
ans = max(ans , len + !sam[2].flg[cur]);
}
sam[0].build(); sam[1].build(); dfs(1); cout << min(ans , min(LS , LT)); return 0;
}
BZOJ3145 [Feyat cup 1.5]Str 后缀树、启发式合并的更多相关文章
- BZOJ3145 : [Feyat cup 1.5]Str
如果不存在模糊点,那么答案就是两个串的最长公共子串. 如果模糊点是某个串的开头或者结尾,那么可以暴力枚举另一个串中的某个前后缀更新答案. 否则,假设模糊点在第一个串里是$i$,在第二个串里是$j$,那 ...
- [BZOJ 3145][Feyat cup 1.5]Str 解题报告
[Feyat cup 1.5]Str DescriptionArcueid,白姬,真祖的公主.在和推倒贵看电影时突然对一个问题产生了兴趣:我们都知道真祖和死徒是有类似的地方.那么从现代科学的角度如何解 ...
- Bzoj 3145 - [Feyat cup 1.5]Str
bzoj 3145 - [Feyat cup 1.5]Str Description 给你两个长度\(10^5\)级别的串\(S, T\) 求\(S,T\)的最长模糊匹配公共子串 模糊匹配 : 至多一 ...
- Bzoj2534:后缀自动机 主席树启发式合并
国际惯例的题面:考虑我们求解出字符串uvu第一个u的右端点为i,第二个u的右端点为j,我们需要满足什么性质?显然j>i+L,因为我们选择的串不能是空串.另外考虑i和j的最长公共前缀(也就是说其p ...
- 【bzoj3123】[Sdoi2013]森林 倍增LCA+主席树+启发式合并
题目描述 输入 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负 ...
- P3302 [SDOI2013]森林(主席树+启发式合并)
P3302 [SDOI2013]森林 主席树+启发式合并 (我以前的主席树板子是错的.......坑了我老久TAT) 第k小问题显然是主席树. 我们对每个点维护一棵包含其子树所有节点的主席树 询问(x ...
- 【BZOJ-3123】森林 主席树 + 启发式合并
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2738 Solved: 806[Submit][Status] ...
- [bzoj3123] [SDOI2013]森林 主席树+启发式合并+LCT
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【主席树 启发式合并】bzoj3123: [Sdoi2013]森林
小细节磕磕碰碰浪费了半个多小时的时间 Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M ...
随机推荐
- android studio学习----gradle配置
转载地址:http://blog.csdn.net/loongggdroid/article/details/47037413 1.gradle的简单介绍 Gradle是可以用于Android开发的新 ...
- 分布式CAP定理
根据百度百科的定义,CAP定理又称CAP原则,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),最 ...
- mysql系列2 权限相关
mysql授权认证 请注意(大坑):mysql8.0以前的版本可以使用grant在授权的时候隐式的创建用户,8.0以后已经不支持,所以必须先创建用户,然后再授权!! 例子: 在170mysql主机上授 ...
- XGBoost使用教程(纯xgboost方法)一
一.导入必要的工具包# 导入必要的工具包import xgboost as xgb # 计算分类正确率from sklearn.metrics import accuracy_score二.数据读取X ...
- php观察者模式(observer pattern)
... <?php /* The observer pattern implements a one-too-many dependency between objects. The objec ...
- opencart 3 配置阿里邮箱smtp实测可用
最近ytkah在做一个客户的opencart项目时,配置阿里邮箱smtp一直收不到邮件,修改了很多配置文件也不起作用,今天再继续调试终于成功了,下面把所有步骤都记录下来,希望能帮到碰到同样问题的朋友们 ...
- hdu1873-看病要排队-(结构体优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1873 #include<stdio.h> #include<iostream> #inc ...
- 修改/etc/docker/daemon.json中的log-opts配置发现无效 docker 限制日志大小
https://colobu.com/2018/10/22/no-space-left-on-device-for-docker/ 在/etc/docker/daemon.json中修改或添加log- ...
- org.springframework.beans.NotWritablePropertyException:Bean property 'xxxService' is not writable or has an invalid setter method.
完整报错提示信息:Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'blogDe ...
- Windows_pycharm下安装numpy
https://blog.csdn.net/haishu_zheng/article/details/77489309 一.下载在网站https://pypi.python.org/pypi/nump ...