-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.cpp
115 lines (106 loc) · 1.36 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
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
#include<bits/stdc++.h>
using namespace std;
#define MAX_INT 0x3f3f3f3f
#define acc(x) setprecision(x)
#define boost ios_base::sync_with_stdio(false); cin.tie(NULL);
struct Trie
{
Trie* arr[26];
bool eoc;
Trie(): eoc(false){};
};
Trie* Node()
{
Trie* temp = new Trie();
for(int i=0;i<26;i++)
{
temp->arr[i]=NULL;
}
return temp;
}
void insert(string p, Trie* r)
{
Trie* t = r;
for(int i=0;i<p.length();i++)
{
int q = p[i]-'a';
if(t->arr[q]==NULL)
{
t->arr[q] = Node();
}
t = t->arr[q];
}
t->eoc = true;
}
bool search(string p, Trie* r)
{
Trie* temp = r;
for(int i=0;i<p.length();i++)
{
int q = p[i]-'a';
if(temp->arr[q]==NULL)
{
return false;
}
else
{
temp = temp->arr[q];
}
}
return temp->eoc;
}
bool searcher(string p, int i, Trie* r, int n)
{
if(i==n)
{
return r->eoc;
}
else
{
if(p[i]=='.')
{
for(int l=0;l<26;l++)
{
if(r->arr[l]!=NULL && searcher(p,i+1,r->arr[l],n))
{
return true;
}
}
return false;
}
else
{
int q = p[i]-'a';
if(r->arr[q]==NULL)
{
return false;
}
else
{
return searcher(p,i+1,r->arr[q],n);
}
}
}
}
int main()
{
boost;
int n;
cin >> n;
Trie* root = Node();
for(int i=0;i<n;i++)
{
string s;
cin >> s;
insert(s, root);
}
if(search("abha", root))
{
cout << "Y\n";
}
else
{
cout << "N\n";
}
return 0;
}