当前位置:网站首页>字典树笔记(自用)
字典树笔记(自用)
2022-07-29 14:23:00 【阐上】
字典树,顾名思义跟字典有关
建树,每个结点连接其他点的时候,这个连接有字典的性质。
插入insert
二进制字典树
int tr[N * 31][2], tdx;
void insert(int x) {
int p = 0;
for (int j = 30; j >= 0; j--) {
int u = (x >> j & 1);
if (!tr[p][u])tr[p][u] = ++tdx;
p = tr[p][u];
}
}
字母、有限数字字典树
int tr[N * 26][26], tdx;
void insert(int x, string & s) {
int p = 0;
for (int i = 0; i < s.size(); i++) {
int u = s[i] - 'a';
if (!tr[p][u])tr[p][u] = ++tdx;
p = tr[p][u];
}
//cnt[p]++ 记录该位置有一个串
}
第一维的大小取决于给定字母、数字个数
dfs遍历
//step是传入的深度,配合获取信息
int dfs(int x, int step) {
int l = tr[x][0], r = tr[x][1];
if (!l && !r)return 0;
else if (!l) return dfs(r, step - 1);
else if (!r)return dfs(l, step - 1);
else {
return min(dfs(l, step - 1), dfs(r, step - 1)) + (1 << step);
}
}
int find(int x) {
int p = 0, res = 0;
for (int j = 31; j >= 0; j--) {
int u = x >> j & 1;
if (son[p][u ^ 1]) {
res += 1 << j;
p = son[p][u ^ 1];
}
else p = son[p][u];
}
return res;
}
作为ac自动机的前置技能,熟练掌握是有必要的呐
例题:
最终boss
CF:字典树统计逆序对 + 贪心维护最小逆序对数量
这题神中神
题解博客
边栏推荐
- Realization of Online Chat System Based on SSM
- Nine kinds of way, teach you to read the resources files in the directory path
- 打卡广汽本田喜悦安全驾驶中心,体验最刁钻的场地训练
- 软件测试架构师的工作日常
- Nacos基础教程
- Children's programming electronics (graphical programming Scratch secondary level exam parsing (choice) in June 2022
- kubernetes cks strace etcd
- dedecms编辑器支持pdf导入
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验19 MQTT 协议通信实验(学习笔记)
- 唯物辩证法-矛盾论(普遍性+特殊性+斗争性+同一性)
猜你喜欢
随机推荐
Pinia状态持久化
工业设备数字孪生技术,解决方案系统平台案例
AVH部署实践 (一) | 在Arm虚拟硬件上部署飞桨模型
《外太空的莫扎特》
【模板引擎】微服务学习笔记六:freemarker模板引擎的常用命令介绍
部门例会上做测试分享,不知道分享什么内容?
马尔可夫跳变线性系统最优控制的研究现状与进展
MySQL 是如何实现 ACID 的?
leetcode linked list topic
双非渣渣的上岸之路!备战60天,三战滴滴侥幸收获Offer
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
【C语言】AI三子棋的成长之路
rk3399驱动添加电池adc开机检测功能
潘多拉 IOT 开发板学习(RT-Thread)—— 实验19 MQTT 协议通信实验(学习笔记)
暴力递归到动态规划 02 (绝顶聪明的人的纸牌游戏)
一篇适合新手的深度学习综述!
Children's programming electronics (graphical programming Scratch secondary level exam parsing (choice) in June 2022
【FreeSwitch开发实践】自定义模块创建与使用
The reason for Apple's official price reduction has been found, and it is also facing declining sales and even inventory problems
什么是异构计算









