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 ...
随机推荐
- Yum项目上线实战 (网站运维)-Linux从入门到精通第十一天(非原创)
文章大纲 一.编译安装与卸载Nginx二.关于LAMP三.LAMP环境部署四.学习资料下载五.参考文章 一.编译安装与卸载Nginx Nginx:是一款比较流行的web服务器软件,类似于Apach ...
- OPATCH在线补丁
如果补丁中有online目录就是在线补丁,不需要数据库停机,在线的又分集群和非集群,如下 查看readme可以得知在线补丁打法 $ cat README.txt Oracle Database 11g ...
- Candies POJ - 3159
题目链接:https://vjudge.net/problem/POJ-3159 思路: 能看出是差分约束的题, 我们想假设一个人是 p(1),另一个人是p(2),他们之间糖果差为w, 那么需要满足的 ...
- NVIDIA-GPU归入K8S集群管理的安装文档--第二版
一,nvidia K80驱动安装 1, 查看服务器上的Nvidia(英伟达)显卡信息,命令lspci |grep NVIDIA 2, 按下来,进行显卡驱动程序的安装,驱动程序可到nvidia的官网 ...
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.qingmu.mybaitsplus.mapper.UserMapper' available:
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- Cantor表-(模拟)
链接:https://ac.nowcoder.com/acm/contest/1069/I来源:牛客网 题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一 ...
- Edit Video
Vsco: the app function is very basic. To what extent. That is to say, there are several more options ...
- localStorage二次封装-----设置过期时间
export default{ set(key,data,time){ let obj={ data=data, ctime:(new Date()).getTime(),//时间戳,同Date.no ...
- luoguP2039 [AHOI2009]跳棋 巧妙的dp
设\(f[i]\)表示在第\(i\)个格子上弄一个棋子的最小代价,前后扫两遍dp后统计答案即可. 代码 #include<bits/stdc++.h> using namespace st ...
- Codeforces Round 564 题解
很抱歉让标题把您骗进来了. 这是一场打得最失败的div1. 作为一个橙名一题都不会…… 旁边紫名的PB怒切3题,div2的也随便玩玩出了div1b/div2d…… 这名字颜色也太有水分了. 也就只会2 ...