CF1037H Security题解
根据字典序的定义,位置大的大于长度长的,长度长的大于长度短的。
所以我们贪心,先追求长度长的,再追求后面的位置大的,再追求前面的位置大的。
我们要一个能遍历子串的结构,就选 SAM 得了。
还有个限制:为 S[L...R] 的字串。正好 SAM 有个东西叫做 endpos,用线段树合并求一下就求出来了。
upd on 2023.2.3:我觉得还是得细嗦一下线段树合并。具体原因是我突然发现这好像已经是一个常用套路(君の名は)
parent树上统计元素,权值线段树+动态开点这一套组合拳应该没啥问题。但假如手玩一棵 endpos 树就会发现有一些元素分着分着就不见了。原因是长度不允许这个点作最后一个字符。
于是我们按前缀插入SAM时就在这 \(n\) 个点上建 \(n\) 颗只有当前下标的线段树(在它的子树中这个元素由于长度原因不会再出现),然后正常合并即可。
这里我们是不能直接合并的,因为之后需要再次合并,直接合并会破坏其中一棵线段树,我们采用新开节点的方式即可。
看着空间不对,但实际上一次 merge 函数只会加一个点,所以空间复杂度与时间复杂度同阶,可以通过。
注意当当前字符串长度为 \(len\) 时,endpos 要有 S[L+len...R] 才行。因为 endpos统计的是结尾。
再提一嘴,要一直保证 endpos 的条件成立,不行就退出。否则会超时。
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define mp make_pair
#define forp(i, a, b) for(int i = (a);i <= (b);i ++)
using namespace std;
const int maxn = 2e7 + 5;
int n;
string T;int L, R;
namespace linetree
{
int ch[8000005][2], rt[200005], tot;
void make(int p, int i)
{
rt[p] = ++ tot;int u = rt[p], l = 1, r = n;
while(1)
{
int mid = l + r >> 1;
if(l == r) break;
(mid >= i) ? (r = mid, u = ch[u][0] = ++ tot) : (l = mid + 1, u = ch[u][1] = ++ tot);
}
// cout << l << ' ' << r << ' ' << i << ' ' << u << endl;
}
int merge(int u, int v, int len) {
if (!u || !v) return u += v;
int p = ++ tot;
if (len != 1) ch[p][0] = merge(ch[u][0], ch[v][0], len + 1 >> 1), ch[p][1] = merge(ch[u][1], ch[v][1], len >> 1);
return p;
}
int query(int p, int l, int r, int fl, int fr)
{
if(fl <= l && fr >= r) return 1;
int mid = l + r >> 1, ans = 0;
if(fl <= mid && ch[p][0]) ans |= query(ch[p][0], l, mid, fl, fr);
if(fr >= mid + 1 && ch[p][1]) ans |= query(ch[p][1], mid + 1, r, fl, fr);
return ans;
}
}
int c[100005], fin;
namespace SAM
{
struct point{ int ch[26], fa, len; }a[200005];
int las = 1, tot = 1;
ll f[200005];
void add(int s, int i)
{
int p = las, np = las = ++ tot;f[np] = 1;
linetree::make(np, i);
a[np].len = a[p].len + 1;
for(;p && a[p].ch[s] == 0;p = a[p].fa) a[p].ch[s] = np;
if(p == 0){ a[np].fa = 1; return ; }
int q = a[p].ch[s];
if(a[q].len == a[p].len + 1){ a[np].fa = q; return ; }
int nq = ++ tot;a[nq] = a[q];
a[q].fa = a[np].fa = nq;
a[nq].len = a[p].len + 1;
for(;p && a[p].ch[s] == q;p = a[p].fa) a[p].ch[s] = nq;
}
bool run(int i, int u, int pd)
{
if(u == 0) return 0;
if(linetree::query(linetree::rt[u], 1, n, L + (i - 1) - 1, R) == 0) return 0;
if(pd == 1) return 1;
if(i == T.size() + 1)
{
forp(j, 0, 25){ c[i] = j; if(run(i + 1, a[u].ch[j], 1)){ fin = i; return 1; } }
return 0;
}
c[i] = T[i - 1] - 'a'; if(run(i + 1, a[u].ch[T[i - 1] - 'a'], 0)) return 1;
forp(j, T[i - 1] - 'a' + 1, 25){ c[i] = j; if(run(i + 1, a[u].ch[j], 1)){fin = i; return 1;} }
return 0;
}
vector<int> e[200005];
void init(){ forp(i, 2, tot) e[a[i].fa].push_back(i); }
void build(int u)
{
for(auto v : e[u])
{
build(v);
linetree::rt[u] = linetree::merge(linetree::rt[u], linetree::rt[v], n);
}
}
}
signed main()
{
freopen("text.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
string s;
cin >> s;
n = s.size();
forp(i, 1, s.size()) SAM::add(s[i - 1] - 'a', i);
SAM::init();SAM::build(1);
int q;
cin >> q;
// cout << "#### " << linetree::query(linetree::rt[4], 1, n, 3, 3) << endl;
while(q --)
{
cin >> L >> R >> T;
if(SAM::run(1, 1, 0))
{
// if(c[3] == 'm' - 'a' && c[2] == 'y' - 'a' && c[1] == 'o' - 'a' && fin == 3) cout << "#### " << T << endl;
forp(i, 1, fin) cout << char(c[i] + 'a');
cout << endl;
}
else cout << -1 << endl;
}
return 0;
}
CF1037H Security题解的更多相关文章
- CF1037H Security 后缀自动机 + right集合线段树合并 + 贪心
题目描述: 给定一个字符串 $S$ 给出 $Q$ 个操作,给出 $L,R,T$,求出字典序最小的 $S_{1}$ 为 $S[L...R]$的子串,且 $S_{1}$ 的字典序严格大于 $T$. 输出这 ...
- CF1037H Security 线段树合并 SAM
LINK:Security 求一个严格大于T的字符串 是原字符串S[L,R]的子串. 容易想到尽可能和T相同 然后再补一个尽可能小的字符即可. 出于这种思想 可以在SAM上先跑匹配 然后枚举加哪个字符 ...
- [CF1037H] Security
题目链接 codeforces. 洛谷. Solution 按照套路,可以\(SAM\)上线段树合并求出\(endpos\)集合,然后随便贪心一下就好了. #include<bits/stdc+ ...
- CF1037H Security——SAM+线段树合并
又是一道\(SAM\)维护\(endpos\)集合的题,我直接把CF700E的板子粘过来了QwQ 思路 如果我们有\([l,r]\)对应的\(SAM\),只需要在上面贪心就可以了.因为要求的是字典序比 ...
- SAM 做题笔记(各种技巧,持续更新,SA)
SAM 感性瞎扯. 这里是 SAM 做题笔记. 本来是在一篇随笔里面,然后 Latex 太多加载不过来就分成了两篇. 标 * 的是推荐一做的题目. trick 是我总结的技巧. I. P3804 [模 ...
- [笔记] 后缀自动机 (SAM)
实现 void ins(int c){ int np = ++dcnt, p = lst; lst = np; t[np].len = t[p].len + 1, t[np].eps = 1; whi ...
- UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解
题意:给一个r*c的矩阵开关(初始全打开的),每次按下一个开关都会改变3*3范围内的有*的地方的状态,问你最少几步能让开关全闭上,按升序输出按哪些按钮 思路:每个按钮至多按一下,按按钮的顺序和结果无关 ...
- dfs | Security Badges
Description You are in charge of the security for a large building, with n rooms and m doors between ...
- Codeforces 948D Perfect Security(字典树)
题目链接:Perfect Security 题意:给出N个数代表密码,再给出N个数代表key.现在要将key组排序,使key组和密码组的亦或所形成的组字典序最小. 题解:要使密码组里面每个数都找到能使 ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- 攻防世界Web进阶篇——warmup
打开链接,发现是一张滑稽 查看页面源代码,发现文件 于是打开source.php,发现 打开hint.php,根据提示得知flag在ffffllllaaaagggg文件中 回到source.php,检 ...
- C# 以管理员方式运行程序
让你的程序以管理员方式运行 在Program.cs文件中添加如下代码 /// <summary> /// 应用程序的主入口点. /// </summary> [STAThrea ...
- Kubernetes--管理Pod对象的容器(1)
Pod是Kubernetes系统的基础单元,是资源对象模型中可由用户创建或部署的最小组件,也是在Kubernetes系统上运行容器化应用的资源对象.其他的大多数资源对象都是用于支撑和扩展Pod对象功能 ...
- [419] C1 Harbingers Of War OpCodez
[419] C1 Harbingers Of War Client 00 SendProtocolVersion 01 MoveBackwardToLocation 02 Say 03 Request ...
- 记录一次echarts 中bar 定时跳跃并显示内容
查看echarts api ----- https://www.echartsjs.com/zh/api.html#echarts 搜索 1.dispatchAction 执行的关键 2.hi ...
- Mysql-------查询各科成绩前三名的记录
查询各科成绩前三名的记录 分析如下: 1 select s.* from (select distinct Student.*,a.c_id, a.s_score, count( distinct b ...
- 【BOOK】解析库—XPath
XPath-XML Path Language 1.安装 lxml库 2.XPath常用规则 3.XPath解析页面 from lxml import etree text = ''' <div ...
- ie8 不支持 trim方法
那就自己写一个trim() String.prototype.trim = function() { return this.replace(/(^\s*)|(\s* ...
- 08 学生课程分数的Spark SQL分析
读学生课程分数文件chapter4-data01.txt,创建DataFrame. 用DataFrame的操作或SQL语句完成以下数据分析要求,并和用RDD操作的实现进行对比: 每个分数+5分. 总共 ...
- Collectors类的静态工厂方法-《Java 8实战》笔记