-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRIE.cpp
More file actions
70 lines (59 loc) · 1.41 KB
/
TRIE.cpp
File metadata and controls
70 lines (59 loc) · 1.41 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
#include <bits/stdc++.h>
using namespace std;
/************************************************************************************************/
struct trie {
trie *child[26];
bool isEnd;
trie() {
memset(child, 0, sizeof(child));
isEnd = false;
}
};
trie *root = new trie();
void append(string str) {
trie *current_node = root;
for (char ch : str) {
int indx = ch - 'a';
if (current_node->child[indx] == NULL) {
current_node->child[indx] = new trie();
}
current_node = current_node->child[indx];
}
current_node->isEnd = true;
}
bool search(string str) {
trie *current_node = root;
for (char ch : str) {
int indx = ch - 'a';
if (current_node->child[indx] == NULL) {
return false;
}
current_node = current_node->child[indx];
}
return current_node->isEnd;
}
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
append(str);
}
int q;
cin >> q;
while (q--) {
string str;
cin >> str;
bool ans = search(str);
if (ans) {
cout << "Word is present" << endl;
}
else {
cout << "Word is not present" << endl;
}
}
}
int main() {
solve();
}