-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletion_and_search.cpp
77 lines (75 loc) · 1.38 KB
/
deletion_and_search.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
#include<bits/stdc++.h>
using namespace std;
struct Trie{
int endNode;
Trie *next[26];
};
Trie* getNode()
{
Trie *root = new Trie();
root->endNode=0;
for(int i=0;i<26;i++)
{
root->next[i]=NULL;
}
return root;
}
void insert(Trie *root,string s)
{ Trie *node = root;
for(int i=0;i<s.size();i++)
{
int u=s[i]-'a';
if(node->next[u]==NULL)
{
node->next[u]=getNode();
}
node=node->next[u];
}
node->endNode+=1;
}
bool search(Trie *root,string s)
{
Trie *node = root;
for(int i=0;i<s.size();i++)
{int u=s[i]-'a';
if(node->next[u]==NULL)
return false;
node=node->next[u];
}
if((node!=NULL)&&((node->endNode)>=1))
return true;
else
return false;
}
void deletion(Trie *root,string s)
{
Trie *node = root;
for(int i=0;i<s.size();i++)
{ int u=s[i]-'a';
if(node->next[u]==NULL)
return;
node=node->next[u];
}
if((node->endNode>=1)&&(node!=NULL))
node->endNode-=1;
}
int main()
{
Trie *root = getNode();
for(int i=0;i<5;i++)
{string s;
cin>>s;
insert(root,s);}
for(int i=0;i<3;i++)
{string s;
cin>>s;
deletion(root,s);}
for(int i=0;i<5;i++)
{string x;
cin>>x;
if(search(root,x))
cout<<"YES\n";
else
cout<<"NO\n";
}
}