这题感觉不是很难,但是既然放在 \(\texttt{EDU}\) 的 \(\texttt{G}\) 题,那么还是写写题解吧。

\(\texttt{Solution}\)

首先看到 "子串",那么想到 \(\texttt{ACAM}\) 和 \(\texttt{SAM}\)。本篇题解就使用 \(\texttt{ACAM}\)。

然后我们先对于最开始给定的 \(n\) 个串建立一个 \(\texttt{ACAM}\)。

然后考虑对于每一个串在 \(\texttt{ACAM}\) 上对应的位置赋上这个串的值。

查询其实就是在 \(\texttt{fail}\) 树上查询一些节点到根的最大值。

这个其实就是要求在 \(\texttt{fail}\) 树上单点修改,求链上最大值。这个东西树剖很好维护。

然后注意以下一个细节:一个串可能出现多次。这个 \(\texttt{multiset}\) 维护一下最大值就好了

\(\texttt{Code}\)

#include<bits/stdc++.h>
using namespace std;
#define L(i, j, k) for(int i = (j), i##E = (k); i <= i##E; i++)
#define R(i, j, k) for(int i = (j), i##E = (k); i >= i##E; i--)
#define ll long long
#define db double
#define mp make_pair
const int N = 3e5 + 7;
const int M = N * 4;
const int inf = 1e9;
int n, m, endid[N], VAL[N];
int ch[N][26], fa[N], tot = 1;
multiset<int> st[N];
int insert(char *s, int n) {
int now = 1;
L(i, 0, n - 1) {
if(!ch[now][s[i] - 'a']) ch[now][s[i] - 'a'] = ++tot;
now = ch[now][s[i] - 'a'];
}
return now;
}
void bfs() {
queue<int> q;
L(i, 0, 25) if(ch[1][i]) fa[ch[1][i]] = 1, q.push(ch[1][i]); else ch[1][i] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
L(i, 0, 25) {
int v = ch[u][i];
if(!v) ch[u][i] = ch[fa[u]][i];
else fa[v] = ch[fa[u]][i], q.push(v);
}
}
}
int head[N], edge_id;
struct node { int to, next; } e[N << 1];
void add_edge(int u, int v) {
++edge_id, e[edge_id].next = head[u], e[edge_id].to = v, head[u] = edge_id;
}
int siz[N], maxto[N], dep[N], uid[N], idtot, heavy[N];
void dfs1(int x) {
siz[x] = 1;
for(int i = head[x]; i; i = e[i].next) {
int v = e[i].to;
fa[v] = x, dep[v] = dep[x] + 1, dfs1(v), siz[x] += siz[v];
if(siz[v] > siz[heavy[x]]) heavy[x] = v;
}
}
void dfs2(int x) {
uid[x] = ++idtot;
if(heavy[x]) maxto[heavy[x]] = maxto[x], dfs2(heavy[x]);
for(int i = head[x]; i; i = e[i].next) {
int v = e[i].to;
if(v == heavy[x]) continue;
maxto[v] = v, dfs2(v);
}
}
int maxn[M];
void build(int x, int l, int r) {
maxn[x] = -1;
if(l == r) return;
int mid = (l + r) / 2;
build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
}
void add(int id, int L, int R, int wz, int val) {
// if(id == 1) cout << wz << " is : " << val << endl;
if(L == R) return maxn[id] = val, void();
int mid = (L + R) / 2;
if(wz <= mid) add(id << 1, L, mid, wz, val);
else add(id << 1 | 1, mid + 1, R, wz, val);
maxn[id] = max(maxn[id << 1], maxn[id << 1 | 1]);
}
int query(int id, int L, int R, int l, int r) {
if(l <= L && R <= r) return maxn[id];
int mid = (L + R) / 2, res = -1;
if(l <= mid) res = max(res, query(id << 1, L, mid, l, r));
if(r > mid) res = max(res, query(id << 1 | 1, mid + 1, R, l, r));
// if(id == 1) cout << l << " to " << r << "'s max = " << res << endl;
return res;
}
int get(int x) {
int res = -1;
while(x) res = max(res, query(1, 1, tot, uid[maxto[x]], uid[x])), x = fa[maxto[x]];
return res;
}
char s[N];
int main() {
scanf("%d%d", &n, &m);
L(i, 1, n) scanf("%s", s), endid[i] = insert(s, strlen(s));
bfs();
L(i, 2, tot) add_edge(fa[i], i);
dfs1(1), maxto[1] = 1, dfs2(1);
build(1, 1, tot);
L(i, 1, n) add(1, 1, tot, uid[endid[i]], 0), st[endid[i]].insert(0);
while(m--) {
int opt, x, val;
scanf("%d", &opt);
if(opt == 1) {
scanf("%d%d", &x, &val);
int now = endid[x];
st[now].erase(st[now].lower_bound(VAL[x]));
st[now].insert(val), VAL[x] = val;
add(1, 1, tot, uid[now], *st[now].rbegin());
}
else {
int maxn = -1, now = 1;
scanf("%s", s);
L(i, 0, strlen(s) - 1) now = ch[now][s[i] - 'a'], maxn = max(maxn, get(now));
printf("%d\n", maxn);
}
}
return 0;
}

题解 CF1437G Death DBMS的更多相关文章

  1. CF1437G Death DBMS

    题面传送门. 题意简述:给出 \(n\) 个字符串 \(s_i\),每个 \(s_i\) 初始权值为 \(0\).\(q\) 次操作:修改 \(s_i\) 的权值:查询给出字符串 \(q\) 能匹配的 ...

  2. ACAM 题乱做

    之前做了不少 ACAM,不过没怎么整理起来,还是有点可惜的. 打 * 的是推荐一做的题目. I. *CF1437G Death DBMS 见 我的题解. II. *CF1202E You Are Gi ...

  3. Educational Codeforces Round 97 (Rated for Div. 2)

    补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...

  4. Death to Binary? (模拟)题解

    思路: 除去前导0,注意两个1不能相邻(11->100),注意 0 *** 或者*** 0或者0 0情况 用string的reverse()很舒服 代码: #include<cstdio& ...

  5. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. 字符串(AC自动机):HDU 5129 Yong Zheng's Death

    Yong Zheng's Death Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/O ...

  7. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  8. Vulnhub靶场题解

    Vulnhub简介 Vulnhub是一个提供各种漏洞环境的靶场平台,供安全爱好者学习渗透使用,大部分环境是做好的虚拟机镜像文件,镜像预先设计了多种漏洞,需要使用VMware或者VirtualBox运行 ...

  9. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

随机推荐

  1. MySQL时间类型datetime、bigint及timestamp的查询效率

    前期数据准备 通过程序往数据库插入 50w 数据 数据表: CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `time_dat ...

  2. innodb之线程及IO相关参数介绍

    引用链接:http://www.cnblogs.com/henglxm/p/4284504.html   1.IO THREAD: 负责IO的相关线程IO THREAD 1. 参数innodb_wri ...

  3. Java编码和字符集(详解)

    [1]什么是编码? [2]通过生活案例: [3]由权威机构形成的编码表才可以称之为:字符集 ASCII 英文字符集 用一个字节的7位表示 IOS8859-1 西欧字符集 用一个字节的8位表示 GB23 ...

  4. Python_科学计算平台__pypi体系的numpy、scipy、pandas、matplotlib库简介

    1.numpy--基础,以矩阵为基础的数学计算模块,纯数学 存储和处理大型矩阵. 这个是很基础的扩展,其余的扩展都是以此为基础. 快速学习入口 https://docs.scipy.org/doc/n ...

  5. appium-appium的等待时间

    #三种appium设置等待时间的方法 #第一种 sleep(): 设置固定休眠时间. python 的 time 包提供了休眠方法 sleep() , 导入 time包后就可以使用 sleep()进行 ...

  6. 最全Linux搭建SVN服务端教程

    文章首推 支付宝接口对接 高德地图调用 验证码登录 QQ邮箱登录 今日主题:Linux搭建SVN服务端 简介 相信程序员对SVN还是不陌生的,虽然现在用Git比较多,但是SVN也是用的,SVN可以做代 ...

  7. IGH_Master主站配置驱动伺服电机和变频器总结

    IGH_Master主站配置驱动伺服电机和变频器总结 Ethercat是倍福公司提出的一种工业现场总线协议,具有很好的实时性,IGH是一种开源的Ethercat主站实现协议,本文总结了一下使用IGH_ ...

  8. (msf使用)msfconsole - meterpreter

    [msf] msfconsole meterpreter 对于这款强大渗透测试框架,详情介绍可看这里:metasploit 使用教程 对于msfconsole, Kali Linux 自带.只需用命令 ...

  9. 面试必看!靠着这份字节和腾讯的面经,我成功拿下了offer!

    准备 敲定了方向和目标后就开始系统准备,主要分为以下几个方面来准备. 算法题 事先已经看过别人的社招面经知道头条每轮技术面都有算法题,而这一块平时练习的比较少,校招时刷的题也忘记了很多.因此系统复习的 ...

  10. RayFire的下载与安装方法

    RayFire的下载与安装方法 发布时间:2020/10/12 近几年,电影中融入了越来越多的动画元素,其中的爆炸场景更是十分吸引眼球.小编不禁好奇,什么样的插件能做出来如此好玩的特效,上网搜索一番发 ...