当前位置:网站首页>字典树笔记(自用)
字典树笔记(自用)
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:字典树统计逆序对 + 贪心维护最小逆序对数量
这题神中神
题解博客
边栏推荐
猜你喜欢

【Postman】下载与安装(新手图文教程)

第4章_1——SQL语句实现MySQL增删改查

【微信小程序】全局配置

EA&UML日拱一卒-活动图::Feature和StuctualFeature

何为擦除机制,泛型的上界?

479-82(54、11)

工业设备数字孪生技术,解决方案系统平台案例

Chinese Internet technology companies were besieged by wolves. Google finally suffered a severe setback and its profits fell sharply. It regretted promoting the development of Hongmeng...

力扣541. 反转字符串 II ----双指针解法

WOLFLAB一方老师带你解读虚拟云网络《VMware NSX-T卷2》-1
随机推荐
蚂蚁三面滑铁卢!遭分布式截胡,靠这些笔记潜修30天,挺进京东
leetcode linked list topic
换掉 UUID,更快、更安全!
没遇到过这三个问题都不好意思说用过Redis
通过二维顺序表实现杨辉三角
求教一下 现在最新版的flinkcdc能获取到oracle的ddl变更信息吗?
建议尽快优化搜索体验
企业需要知道的5个 IAM 最佳实践
kubernetes cks strace etcd
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
The new technical director, who is in the form of a isXxx Boolean type definition, tomorrow need not come!
PAT serie a A1021 Deepest Root
带你搞懂 Redis 中的两个策略
Interfaces and Abstractions
广州消防:高温天气火灾频发 消防安全不容忽视
Guangzhou Emergency Management Bureau released the top ten safety risks of hazardous chemicals in summer
兆骑科创海外高层次人才引进平台,企业项目对接,赛事活动路演
苹果官方降价的原因找到了,它也面临销量下滑乃至出现库存问题
城市污水处理过程模型预测控制研究综述
【Postman】Download and installation (novice graphic tutorial)