当前位置:网站首页>Trie number dictionary tree
Trie number dictionary tree
2022-07-04 05:03:00 【sheep. ice】
One 、 Preface
Dictionary tree is a magical thing . Imagine if we want to use a program to save some strings , But the same characters cannot be placed in different spaces , For example, there are two strings abc,abf, We found that ab It's the same , We need to store them in an array space ? How should we do it ? Then there is why it is called a dictionary tree , In fact, it vividly illustrates his storage method , Is the dictionary . When we look up the dictionary , Will first look up the first letter of a word , Then continue to look up the following letters in the corresponding places . That is, we can use arrays to complete such storage
However, due to the limited space of general arrays , The dictionary tree will occupy a lot of space , One abc It takes up 3 Unit space , So you need to pay attention to the data range when using , Whether it can not explode space !
Two 、 The relevant operation
① Pre variable definition

The dictionary tree needs a two-dimensional array space , be called son, Why is it so called . Because we use the root node of our whole tree idx=0 To express . This point is the head of the whole tree , He has countless sons , Sons have countless sons . So we affectionately call the subsequent node a son
and son The definition of is actually every x There will be corresponding y A son , Whether we are inserting or querying , In order to meet the requirement of not wasting extra space , We all need to make the first judgment , As a first x Whether your father already exists y This node , If it doesn't exist , Just give him a son , If there is , Just treat them equally as one son .
② Insert a string
Suppose the strings in our tree are all lowercase letters , Then our son can only have 26 individual , So when you open the array, you can open : int s o n [ N ] [ 26 ] son[N][26] son[N][26], And our later examples mainly focus on this .

I think the insertion is more clear , So just post a code
void insert(string ss) {
int sz = ss.size();
int p = 0;
for(int i = 0; i < sz; i ++ ) {
int u = ss[i] - 'a';
// If you don't have this son, give him a son
if(son[p][u] == 0) son[p][u] = ++idx;
// With or without a son ,p The pointer will go towards the son or the new point
// once p Go to the new point , In fact, it means that all the following points are new
p = son[p][u];
}
}
③ Query a string
Query is actually similar to build , It depends on whether the string we want to query can completely complete the constructed string , As for inquiry , Combined with the following topics , You can understand , Directly post a small code .
int query(string ss) {
int sz = ss.size();
int p = 0;
for(int i = 0; i < sz; i ++ ) {
int u = ss[i] - 'a';
if(son[p][u] == 0) return 0;
p = son[p][u];
}
return cnt[p];
}
3、 ... and 、 Related topics
Topic 1

The above topic is a classic , Query the number of occurrences of the string , When we build trees , Let's open another cnt Array , This problem can be solved by recording how many letters appear in the position of the last letter of a string . You can take a direct look at the complete code .
complete AC Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100005;
int son[N][26], cnt[N], idx;
void insrt(string ss) {
int sz = ss.size();
int p = 0;
for(int i = 0; i < sz; i ++ ){
int u = ss[i] - 'a';
if(son[p][u] == 0) son[p][u] = ++ idx;
p = son[p][u];
}
cnt[p] ++ ;
}
int query(string ss) {
int sz = ss.size();
int p = 0;
for(int i = 0; i < sz; i ++ ) {
int u = ss[i] - 'a';
if(son[p][u] == 0) return 0;
p = son[p][u];
}
return cnt[p];
}
int main() {
int n;
cin >> n;
while (n -- ) {
string op, in;
cin >> op >> in;
if(op == "I") {
insrt(in);
}
else {
cout << query(in) << endl;
}
}
return 0;
}
Topic two
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kVxj0Ul6-1656845350472)(C:/Users/0604520/AppData/Roaming/Typora/typora-user-images/image-20220703165405990.png)]
This topic is more interesting , In fact, this topic is about a binary tree , Specific ideas are illustrated

We all know that a binary number XOR up , Same as 0, Different for 1, That's actually telling us again , We are constantly making achievements , You can use the element to be inserted , Compare with the elements in the tree , Every time I go to see if there is a son in the same position who is different from me , If any , We can forget that son's path and go .
For example, below :

Insert... In turn 5,3,4 And find the element with the largest XOR ,
Insert again 4 When , First find the maximum XOR , We found that 4 The binary of is 100
about 1 This one , Look for it first 0 son , just 3 The first one in the world is 0, One analogy , He and 3 Or you can get the maximum XOR value 7(111);
complete AC Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100050, M = 31 * N;
// Because a son can only have 0,1 Two options
int son[M][2], idx = 0;
void insert(int num) {
int p = 0;
// Weight is the most 31 position
for(int i = 30; i >= 0; i -- ) {
int u = (num >> i) & 1;
if(son[p][u] == 0) son[p][u] = ++idx;
p = son[p][u];
}
}
int query(int num) {
int p = 0, ret = 0;
for(int i = 30; i >= 0; i -- ) {
int u = (num >> i) & 1;
// If there are different sons
if(son[p][!u] != 0) {
ret = ret * 2 + !u;
p = son[p][!u];
}
// If there were no different sons
else {
ret = ret * 2 + u;
p = son[p][u];
}
}
return ret;
}
int main() {
int m, num;
cin >> m;
int ans = 0;
while (m -- ) {
cin >> num;
insert(num);
ans = max(num ^ query(num), ans);
}
cout << ans << endl;
return 0;
}
边栏推荐
- STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
- [go] database framework Gorm
- YoloV6实战:手把手教你使用Yolov6进行物体检测(附数据集)
- Zhongke panyun-d module analysis and scoring standard
- Can closed data be deleted by DBCA? can
- Trie数-字典树
- 6-4 vulnerability exploitation SSH banner information acquisition
- The first introduction, stages and methods of defense system breakthrough from the perspective of the red team
- 如何构建属于自己的知识引擎?社群开放申请
- Exercise bubble sort
猜你喜欢

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

Technology Management - learning / practice

Download kicad on Alibaba cloud image station

TCP状态转换图

抓包整理外篇fiddler———— 会话栏与过滤器
![[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术](/img/87/e0469e280365ed0261e2b551ebd888.png)
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术

分享一些我的远程办公经验

Customize a pager needed in your project

Notes on the paper "cross view transformers for real time map view semantic segmentation"

Unity is connected to the weather system
随机推荐
Sample template of software design document - learning / practice
Create ASM disk through DD
Exercise bubble sort
CRS-4013: This command is not supported in a single-node configuration.
[untitled]
RPC - gRPC简单的demo - 学习/实践
Simple g++ and GDB debugging
Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
如何构建属于自己的知识引擎?社群开放申请
自动化测试selenium基础篇——webdriverAPI
Technology Management - learning / practice
The first introduction, stages and methods of defense system breakthrough from the perspective of the red team
《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
[matlab] matlab simulates digital bandpass transmission systems - QPSK and OQPSK systems
[matlab] matlab simulation - narrow band Gaussian white noise
【MATLAB】MATLAB 仿真数字基带传输系统 — 双极性基带信号(第 I 类部分响应波形)的眼图
Binary search tree
中科磐云—模块A 基础设施设置与安全加固 评分标准
【MATLAB】通信信号调制通用函数 — 傅里叶变换
RPC - grpc simple demo - learn / practice