-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.h
More file actions
49 lines (46 loc) · 1.01 KB
/
trie.h
File metadata and controls
49 lines (46 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef _Trie_20241001_
#define _Trie_20241001_
#include<bits/stdc++.h>
class trie{
std::unordered_map<char,trie*> next;
bool isEnd=false;
size_t count=0;
public:
bool insert(const std::string& str){
trie* node=this;
std::vector<trie*> update_nodes;
update_nodes.reserve(str.size());
for(char c:str){
if(!node->next.count(c))node->next[c]=new trie();
node=node->next[c];
update_nodes.push_back(node);
}
if(!node->isEnd){
node->isEnd=true;
count++;
for(trie* update_node:update_nodes)update_node->count++;
return true;
}
return false;
}
bool contain(const std::string& str){
trie* node=this;
for(char c:str){
if(!node->next.count(c))return false;
node=node->next[c];
}
return node->isEnd;
}
size_t prefix_count(const std::string& str){
trie* node=this;
for(char c:str){
if(!node->next.count(c))return 0;
node=node->next[c];
}
return node->count;
}
~trie(){
for(auto& p:next)delete p.second;
}
};
#endif