forked from elfreki/CompetetiveCodingSols-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_search_engine.cpp
More file actions
96 lines (88 loc) · 2.01 KB
/
trie_search_engine.cpp
File metadata and controls
96 lines (88 loc) · 2.01 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
//Inshallah , Boys played well..!!
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pb push_back
#define pf push_front
#define mk make_pair
#define f(i,j,n) for(ll i=j;i<n;i++)
#define r(i,n,j) for(ll i=n-1;i>=j;i--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define Test int t;cin>>t;while(t--)
#define mod 1000000007
#define md 998244353
#define MIN INT_MIN
#define MAX INT_MAX
#define inf LLONG_MAX
#define all(container) container.begin() , container.end()
#define rall(container) container.rbegin() , container.rend()
#define sz(container) (int)container.size()
#define v vector<long long>
#define pii pair <int , int>
#define fi first
#define se second
#define setp(x) setprecision(x)
#define meme(x,i) memset(x,i,sizeof(x))
const int N = 100005;
struct trie {
trie *bache[26];
bool isend;
};
trie* getnode(void) {
trie *nn = new (struct trie);
nn->isend = false;
for (int i = 0; i < 26; i++)
nn->bache[i] = NULL;
return nn;
}
void insert(trie *root, string s) {
trie *pp = root;
for (int i = 0; i < s.size(); i++) {
if (!pp->bache[s[i] - 'a']) {
pp->bache[s[i] - 'a'] = getnode();
}
pp = pp->bache[s[i] - 'a'];
}
pp->isend = true;
}
bool find(trie *root, string s) {
trie *pp = root;
f(i, 0, sz(s)) {
if (!pp->bache[s[i] - 'a']) {
return false;
}
pp = pp->bache[s[i] - 'a'];
}
if (pp->isend)
return true;
return false;
}
void solve() {
trie *root = getnode();
int n;
string s;
cin >> n;
while (n--) {
cin >> s;
insert(root, s);
}
cin >> s;
if (find(root, s))
cout << 1 << endl;
else
cout << "0\n";
}
int main() {
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
cin >> t;
while (t--) solve();
}