当前位置:网站首页>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] communication signal modulation general function - low pass filter
- [matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
- 【MATLAB】MATLAB 仿真模拟调制系统 — SSB 系统
- 红队视角下的防御体系突破之第一篇介绍、阶段、方法
- Annex V: briefing on the attack process docx
- appliedzkp zkevm(11)中的EVM Proof
- Share some of my telecommuting experience
- laravel 中获取刚刚插入的记录的id
- 附件六:防守工作简报.docx
- 自动化测试selenium基础篇——webdriverAPI
猜你喜欢

在代碼中使用度量單比特,從而生活更美好

TCP状态转换图

Simple g++ and GDB debugging

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

全国职业院校技能大赛(中职组)网络安全竞赛试题—解析

ADB tools

Unity 接入天气系统

RAC delete damaged disk group

Introduction and application of rampax in unity: optimization of dissolution effect

Developing mqtt access program under QT
随机推荐
全国职业院校技能大赛(中职组)网络安全竞赛试题—解析
Zhongke Panyun - module a infrastructure setting and safety reinforcement scoring standard
力扣 第 300 场周赛
网络设备应急响应指南
在代码中使用度量单位,从而生活更美好
【MATLAB】MATLAB 仿真数字带通传输系统 — ASK、 PSK、 FSK 系统
[技术发展-25]:广播电视网、互联网、电信网、电网四网融合技术
Annex 4: scoring criteria of the attacker docx
中科磐云—D模块解析以及评分标准
[matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
Maui introductory tutorial series (5.xaml and page introduction)
[matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
【MATLAB】通信信号调制通用函数 — 低通滤波器
Useful plug-ins for vscode
Get the ID of the record just inserted from laravel
Secondary vocational group network security - memory Forensics
中科磐云—数据分析与取证数据包flag
Zhongke panyun-2022 Guangdong Trojan horse information acquisition and analysis
Zhongke Panyun - 2022 Guangxi reverse analysis ideas
【MATLAB】MATLAB 仿真数字基带传输系统 — 数字基带传输系统