-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_implementaition_in_c++.cpp
More file actions
83 lines (73 loc) · 1.3 KB
/
trie_implementaition_in_c++.cpp
File metadata and controls
83 lines (73 loc) · 1.3 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
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
///In the name of ALLAH ///
#include <bits/stdc++.h>
using namespace std;
class Trie {
private :
struct node {
bool mark;
node *next[27];
node () {
for (int i = 0; i < 26; i++) {
next[i] = NULL;
}
mark = false;
}
};
node *root = new node ();
public :
void insert__ (string ss) {
node *temp = root ;
int len = ss.size();
for (int i = 0; i < len; i++) {
int id = ss[i] - 'a';
if (temp->next[id] == NULL) {
temp->next[id] = new node ();
}
temp = temp->next[id];
}
temp->mark = true;
}
bool search__ (string ss) {
node *temp = root;
int len = ss.size();
for (int i = 0; i < len; i++) {
int id = ss[i] - 'a';
if (temp->next[id] == NULL) {
return false;
}
temp = temp->next[id];
}
return temp->mark;
}
private :
void delete_ (node *curr) {
for (int i = 0; i < 26; i++) {
if (curr->next[i]) {
delete_ (curr->next[i]);
}
}
delete (curr);
}
public :
void delete__ () {
delete_ (root);
}
};
int main () {
int n;
cin >> n;
Trie tt;
for (int i = 0; i < n; i++) {
string ssss;
cin >> ssss;
tt.insert__(ssss);
}
int m;
cin >> m;
for (int i = 0; i < m; i++) {
string sss;
cin >> sss;
cout << tt.search__(sss) << endl;
}
tt.delete__();
}