【Codeforces 1037H】Security(SAM & 线段树合并)
Description
给出一个字符串 \(S\)。
给出 \(Q\) 个操作,给出 \(L, R, T\),求字典序最小的 \(S_1\),使得 \(S^\prime\) 为\(S[L..R]\) 的子串,且 \(S^\prime\) 的字典序严格大于 \(T\)。输出这个 \(S^\prime\) ,如果无解输出 -1。
Hint
- \(1\le |S|\le 10^5\)
- \(1\le Q\le 2\times 10^5\)
- \(1\le L\le R\le |S|\)
- \(1\le \sum |T| \le 2\times 10^5\)
Solution
看到各种“子串”,考虑 SAM。
要求“字典序严格大于 \(T\) 的字典序最小子串”,那么有一个 贪心 的方法:找一个 \(T\) 的 前缀,后面加一个字典序稍大的字符。
这样的话直接把 \(T\) 放到 \(S\) 的 SAM 上跑,求出 每一位如果替换掉的话可以换的最小字符 \(\text{dir}\)。没有的话就是 \(-1\)。
然后整出 \(\text{dir}\) 之后,倒着 看看有没有 \(\text{dir}\ne -1\) 的位置,有就换掉这一位然后输出,否则答案就是 -1。
注意答案长度可能会比 \(T\) 大一,所以 \(\text{dir}\) 要算到 \(|T| + 1\) 位。
还有一个问题,就是怎么处理区间限制?
这就需要 \(\text{end-pos}\) 了。我指 处理出整个集合。 可以用树上主席树或线段树合并维护。这样可以快速判断 \(\text{end-pos}\) 中 是否含有某个区间中的值。 这样在用 \(T\) 在 SAM 上跑的时候就可以只走区间中的点,替换字符也可以只换可以到达区间中的点。
时空复杂度 \(O(n\log n)\),这里将 \(\Sigma = 26\) 记为常数。
Code
实现比较复杂,注意细节。
线段树合并。
/*
* Author : _Wallace_
* Source : https://www.cnblogs.com/-Wallace-/
* Problem : Codeforces 1037H Security
*/
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int Len = 1e5 + 5;
namespace segt {
const int S = Len << 6;
int lc[S], rc[S];
int total = 0;
#define mid ((l + r) >> 1)
void insert(int& x, int p, int l = 1, int r = Len) {
if (!x) x = ++total;
if (l == r) return;
if (p <= mid) insert(lc[x], p, l, mid);
else insert(rc[x], p, mid + 1, r);
}
int merge(int x, int y, int l = 1, int r = Len) {
if (l == r || !x || !y) return x | y;
int z = ++total;
lc[z] = merge(lc[x], lc[y], l, mid);
rc[z] = merge(rc[x], rc[y], mid + 1, r);
return z;
}
bool find(int u, int v, int x, int l = 1, int r = Len) {
if (!x) return false;
if (u <= l && r <= v) return true;
if (u <= mid && find(u, v, lc[x], l, mid)) return true;
if (v > mid && find(u, v, rc[x], mid + 1, r)) return true;
return false;
}
};
namespace SAM {
const int T = Len << 1;
struct Node {
map<char, int> ch;
int link, len, eprt;
} t[T];
int total;
int last;
void extend_char(char c) {
int p = last, np = last = ++total;
t[np].len = t[p].len + 1;
for (; p && !t[p].ch[c]; p = t[p].link)
t[p].ch[c] = np;
if (!p) {
t[np].link = 1;
} else {
int q = t[p].ch[c];
if (t[p].len + 1 == t[q].len) {
t[np].link = q;
} else {
int nq = ++total;
t[nq] = t[q], t[nq].len = t[p].len + 1;
t[np].link = t[q].link = nq;
for (; p && t[p].ch[c] == q; p = t[p].link)
t[p].ch[c] = nq;
}
}
segt::insert(t[np].eprt, t[np].len);
}
int b[T], c[T];
void topo_sort() {
for (register int i = 1; i <= total; i++) ++c[t[i].len];
for (register int i = 1; i <= total; i++) c[i] += c[i - 1];
for (register int i = 1; i <= total; i++) b[c[t[i].len]--] = i;
}
void init_end_pos() {
for (register int i = total; i; i--) {
int x = b[i], f = t[x].link;
if (f) t[f].eprt = segt::merge(t[x].eprt, t[f].eprt);
}
}
void init_data(string& s) {
total = last = 1;
for (string::iterator it = s.begin(); it != s.end(); it++)
extend_char(*it);
topo_sort();
init_end_pos();
}
int dir[Len];
string query(int l, int r, string str);
};
string SAM::query(int l, int r, string str) {
int x = 1, y, i;
for (i = 1; ; i++) {
dir[i] = -1;
char c = (i > str.size()) ? 'a' : str[i - 1] + 1;
map<char, int>::iterator it = t[x].ch.lower_bound(c);
for (; it != t[x].ch.end(); it++) {
y = it->second;
if (segt::find(l + i - 1, r, t[y].eprt)) {
dir[i] = it->first;
break;
}
}
c = (i > str.size()) ? 0 : str[i - 1];
y = t[x].ch[c];
if (!y || i == str.size() + 1 || !segt::find(l + i - 1, r, t[y].eprt))
break;
x = y;
}
for (; i && dir[i] == -1; i--);
if (!i) return "-1";
string ret;
for (register int j = 1; j < i; j++)
ret += str[j - 1];
ret += dir[i];
return ret;
}
signed main() {
ios::sync_with_stdio(false);
string str;
cin >> str;
SAM::init_data(str);
int q;
cin >> q;
while (q--) {
int L, R;
cin >> L >> R >> str;
cout << SAM::query(L, R, str) << '\n';
}
return 0;
}
【Codeforces 1037H】Security(SAM & 线段树合并)的更多相关文章
- CF1037H Security——SAM+线段树合并
又是一道\(SAM\)维护\(endpos\)集合的题,我直接把CF700E的板子粘过来了QwQ 思路 如果我们有\([l,r]\)对应的\(SAM\),只需要在上面贪心就可以了.因为要求的是字典序比 ...
- CodeForces - 1037H: Security(SAM+线段树合并)
题意:给定字符串S: Q次询问,每次询问给出(L,R,T),让你在S[L,R]里面找一个字典序最小的子串,其字典序比T大. 没有则输出-1: 思路:比T字典序大,而且要求字典最小,显然就是在T的尾巴 ...
- Codeforces 1276F - Asterisk Substrings(SAM+线段树合并+虚树)
Codeforces 题面传送门 & 洛谷题面传送门 SAM hot tea %%%%%%% 首先我们显然可以将所有能够得到的字符串分成六类:\(\varnothing,\text{*},s, ...
- Codeforces 700E. Cool Slogans 字符串,SAM,线段树合并,动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF700E.html 题解 首先建个SAM. 一个结论:对于parent树上任意一个点x,以及它所代表的子树内任 ...
- 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree
原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...
- UOJ#395. 【NOI2018】你的名字 字符串,SAM,线段树合并
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ395.html 题解 记得同步赛的时候这题我爆0了,最暴力的暴力都没调出来. 首先我们看看 68 分怎么做 ...
- loj#2059. 「TJOI / HEOI2016」字符串 sam+线段树合并+倍增
题意:给你一个子串,m次询问,每次给你abcd,问你子串sa-b的所有子串和子串sc-d的最长公共前缀是多长 题解:首先要求两个子串的最长公共前缀就是把反过来插入变成最长公共后缀,两个节点在paren ...
- 2019.02.27 bzoj4556: [Tjoi2016&Heoi2016]字符串(二分答案+sam+线段树合并)
传送门 题意:给一个字符串SSS. 有mmm次询问,每次给四个参数a,b,c,da,b,c,da,b,c,d,问s[a...b]s[a...b]s[a...b]的所有子串和s[x...y]s[x... ...
- CF700E:Cool Slogans(SAM,线段树合并)
Description 给你一个字符串,如果一个串包含两个可有交集的相同子串,那么这个串的价值就是子串的价值+1.问你给定字符串的最大价值子串的价值. Input 第一行读入字符串长度$n$,第二行是 ...
随机推荐
- logback怎么写?分类输出日志到不同的文件
此appender有顺序,最好不要乱调顺序,输出日志如下: drwxr-xr-x 2 root root 4096 Dec 3 00:00 2019-12-02drwxr-xr-x 2 root ro ...
- mds/journal.cc: 2929: FAILED assert解决
前言 在处理一个其他双活MDS无法启动环境的时候,查看mds的日志看到了这个错误mds/journal.cc: 2929: FAILED assert(mds->sessionmap.get_v ...
- linux服务器远程网络开机(wake on lan)
通过网络可以远程开关机,某些时候比较方便管理机器 检查服务器是否支持远程网络开机 [root@lab5101 ~]# ethtool eth0 Settings for eth0: Supported ...
- ubuntu服务器启动过程中重启卡死的问题解决
在grub默认参数当中添加 GRUB_RECORDFAIL_TIMEOUT=0 写于: 2014年07月23日 更新于: 2015年03月24日
- 限流10万QPS、跨域、过滤器、令牌桶算法-网关Gateway内容都在这儿
一.微服务网关Spring Cloud Gateway 1.1 导引 文中内容包含:微服务网关限流10万QPS.跨域.过滤器.令牌桶算法. 在构建微服务系统中,必不可少的技术就是网关了,从早期的Zuu ...
- 03、MyBatis 映射文件
1.XML映射器 2.select Select元素来定义查询操作 Id:唯一标识符 - 用来引用这条语句,需要和接口的方法名一致 parameterType:参数类型 - 可以不传,MyBatis会 ...
- Single Depth peeling 顺序无关渲染(OIT)
什么是顺序无关渲染 在3D渲染中,物体的渲染是按一定的顺序渲染的,这也就可能导致半透明的物体先于不透明的物体渲染,结果就是可能出现半透明物体后的物体由于深度遮挡而没有渲染出来.对于这种情况通常会先渲染 ...
- windbg 分析cpu异常
1. !threadpool 查看当前CPU状况 线程数等等 2. !runaway 查看那几个线程使用的高 建议多抓几个dump 然后确定到底是哪个线程 3. ~线程IDs 跳转到那个 ...
- 13.java设计模式之模板模式
基本需求: 制作豆浆的流程 选材--->添加配料--->浸泡--->放到豆浆机打碎 通过添加不同的配料,可以制作出不同口味的豆浆 选材.浸泡和放到豆浆机打碎这几个步骤对于制作每种口味 ...
- Zabbix监控笔记
了解zabbix,有必要了聊一下监控系统相关内容 企业中常用的开源监视系统目前有 cacti.Nagios.Open-Falcon.zabbix.prometheus等 使用监控系统的目的在于 /1. ...