当前位置:网站首页>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;
}
边栏推荐
- 【MATLAB】通信信号调制通用函数 — 带通滤波器
- When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
- STM32F1与STM32CubeIDE编程实例-74HC595驱动4位7段数码管
- 中科磐云—2022广西逆向解析思路
- 我们认为消费互联网发展到最后,依然会局限于互联网行业本身
- Encryption and decryption
- Create ASM disk through DD
- Annex II: confidentiality agreement for offensive and defensive drills docx
- Notes on the paper "cross view transformers for real time map view semantic segmentation"
- Technology Management - learning / practice
猜你喜欢

抓包整理外篇fiddler———— 会话栏与过滤器

每日刷题记录 (十二)

Zhengzhou zhengqingyuan Culture Communication Co., Ltd.: seven marketing skills for small enterprises

Headache delayed double deletion

2022广东省赛——编码信息获取 解析flag

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

中職組網絡安全—內存取證

National vocational college skills competition (secondary vocational group) network security competition questions - Analysis

Capturing and sorting out external Fiddler -- Conversation bar and filter

Customize a pager needed in your project
随机推荐
Using jsts in esmodule environment
【MATLAB】MATLAB 仿真 — 窄带高斯白噪声
Developing mqtt access program under QT
中科磐云—数据分析与取证数据包flag
在代碼中使用度量單比特,從而生活更美好
Zkevm (12) state proof of appliedzkp
When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
Utiliser des unités de mesure dans votre code pour une vie meilleure
Zhongke panyun-d module analysis and scoring standard
June 2022 summary
[matlab] matlab simulation of modulation system - power spectrum and coherent demodulation of AM modulated signal
We believe that the development of consumer Internet will still be limited to the Internet industry itself
Exercise bubble sort
Can closed data be deleted by DBCA? can
Error response from daemon: You cannot remove a running container 8d6f0d2850250627cd6c2acb2497002fc3
KMP匹配字符串
The second case analysis of the breakthrough of defense system from the perspective of the red team
Qt QTableView数据列宽度自适应
[go] database framework Gorm
Maui introductory tutorial series (5.xaml and page introduction)