-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie (Prefix Tree or Radix Tree).cpp
More file actions
143 lines (104 loc) · 2.12 KB
/
Copy pathTrie (Prefix Tree or Radix Tree).cpp
File metadata and controls
143 lines (104 loc) · 2.12 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
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
136
137
138
139
140
141
142
143
// Trie (Prefix Tree/Radix Tree).cpp
/// Template by Zayed ///
///************************************************************///
/// #include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <map>
//#include<unordered_map>
#include <set>
//#include<unordered_set>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
///************************************************************///
using namespace std;
int caseno = 1;
///************************************************************///
#define NL '\n'
#define SF scanf
#define PF printf
#define PC() printf("Case %d: ", caseno++)//NOTES:printf
//#define PC cout << "Case "//NOTES:cout
//#define CN cout << caseno++ << ": "//NOTES:cout
#define CLR(ar) memset(ar, 0, sizeof(ar))
#define SET(ar) memset(ar, -1, sizeof(ar))
#define READ() freopen("input.txt", "r", stdin)
#define WRITE() freopen("output.txt", "w", stdout)
string S;
struct node
{
bool endmark;
node *next[26 + 1];
/// Constructor
node()
{
endmark = false;
int I;
for(I = 0; I < 26; I++)
next[I] = NULL;
}
};
node *root;
void Insert(string S, int len)
{
node *curr = root;
int I;
for(I = 0; I < len; I++)
{
int id = S[I] - 'a';
if(curr -> next[id] == NULL)
curr -> next[id] = new node();
curr = curr -> next[id];
}
curr -> endmark = true;
}
bool Search(string S, int len)
{
node *curr = root;
int I;
for(I = 0; I < len; I++)
{
int id = S[I] - 'a';
if(curr -> next[id] == NULL)
return false;
curr = curr -> next[id];
}
return curr -> endmark;
}
int main()
{
///READ();
///WRITE();
int tcases, I, J, K, N, n, m, cnt = 0, len, a, b, cost = 0, q;
root = new node();
cout << "ENTER NUMBE OF WORDS: ";
cin >> n;
for(I = 0; I < n; I++)
{
cin >> S;
len = S.length();
Insert(S, len);
}
cout << "ENTER THE NUMBER OF QUERY: ";
cin >> q;
for(I = 1; I <= q; I++)
{
cin >> S;
len = S.length();
if(Search(S, len))
cout << "FOUND\n";
else
cout << "NOT FOUND\n";
}
return 0;
}