-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.cpp
73 lines (64 loc) · 1.4 KB
/
Trie.cpp
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
/*
* Link To Question : http://www.practice.geeksforgeeks.org/problem-page.php?pid=1619
* Hint : Trie Implementation
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N = 20+1;
struct Trie{
struct Trie *child[26];
bool isLast;
};
struct Trie *getNode(){
struct Trie * node = new Trie();
if(node){
for (int i = 0 ; i < 26 ; i++){
node->child[i] = NULL;
}
node->isLast = 0;
}
return node;
}
void insert(struct Trie * root, string s){
struct Trie * temp = root;
for (int i = 0 ; i < s.length() ; i++){
if(!temp->child[s[i]-'a']){
temp->child[s[i]-'a'] = getNode();
}
temp = temp->child[s[i] - 'a'];
}
temp->isLast = 1;
}
bool search(struct Trie * root, string s){
struct Trie * temp = root;
for (int i = 0 ; i < s.size() ; i++){
if(!temp->child[s[i]-'a']) {
return 0;
}
temp = temp->child[s[i]-'a'];
}
return (temp!=NULL && temp->isLast);
}
int main(){
ll t;
cin>>t;
while(t--){
struct Trie * root = getNode();
ll n;
cin>>n;
for(int i = 0 ; i < n ; i ++){
string s;
cin>>s;
insert(root,s);
}
string s;
cin>>s;
if(search(root,s)){
printf("1\n");
}else
{
printf("0\n");
}
}
}