-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrie.hpp
135 lines (119 loc) · 3.53 KB
/
trie.hpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<bits/stdc++.h>
using namespace std;
class trie{
public:
class node{
public:
bool end_word;
map<int, node*> children;
node():end_word(false){};
~node(){for(pair<const int, node*>& child : children) delete child.second;}
int capacity(){
int sz = sizeof(bool);
sz += ((sizeof(int)+sizeof(node*))*children.size())+sizeof(children);
for(pair<const int, node*>& child : children)
sz += child.second->capacity();
return sz;
}
};
trie(){
root = new node();
}
~trie(){
delete root;
}
void insert(string str){
node* r = root;
int t;
for(int i=0; i<str.length(); ++i){
t = str[i]-'a';
if(r->children.find(t) == r->children.end()){
node* n = new node();
r->children.insert(pair<int, node*>(t, n));
r = n;
}else{
r = r->children[t];
}
}
r->end_word = true;
}
vector<string> search(string q, int tol){
vector<string> matches;
vector<int> row(q.length()+1);
for(int i=0; i<=q.length(); ++i)
row[i] = i;
for(pair<const int, node*>& p : root->children)
_search(p.second, string(1, p.first+'a'), q, tol, row, matches);
return matches;
}
void remove(string str){
_remove(root, str, 0);
}
void print(){
_print(root, 0);
}
int capacity(){
int sz = sizeof(node*);
sz += root->capacity();
return sz;
}
private:
node* root;
void _search(node* r, string w, string q, int tol, const vector<int>& prev_row, vector<string>& vec){
int _min = INT_MAX;
vector<int> curr_row(q.length()+1, 0);
curr_row[0] = prev_row[0]+1;
for(int j=1; j<=q.length(); ++j){
curr_row[j] = min(prev_row[j-1], min(prev_row[j], curr_row[j-1]));
if(q[j-1] != w[w.length()-1]){
curr_row[j]++;
}
if(curr_row[j] < _min)
_min = curr_row[j];
}
if(_min > tol)
return;
if(r->end_word)
vec.push_back(w);
for(pair<const int, node*>& p : r->children)
_search(p.second, w+string(1, p.first+'a'), q, tol, curr_row, vec);
}
bool _remove(node* r, string str, int depth){
if(r == nullptr)
return false;
if(depth == str.length()){
if(r->end_word){
r->end_word = false;
if(r->children.size() > 0)
return false;
return true;
}
return false;
}
int index = (int)(str[depth]-'a');
map<int, node*>::iterator it = r->children.find(index);
if(it == r->children.end())
return false;
node* n = it->second;
if(_remove(n, str, depth+1)){
r->children.erase(index);
delete n;
if(r->end_word || r->children.size() > 0)
return false;
return true;
}
return false;
}
void _print(node* r, const int depth){
for(pair<const int, node*>& p : r->children){
for(int i=0; i<depth; ++i){
printf(" ");
}
printf("%c", char(p.first+'a'));
if(p.second->end_word)
printf("*");
printf("\n");
_print(p.second, depth+1);
}
}
};