forked from sanyathisside/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhoneDirectoryMapAlgo.cpp
131 lines (116 loc) · 2.74 KB
/
PhoneDirectoryMapAlgo.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Given a list of contacts which exist in a phone directory and a query string str.
The task is to implement search query for the phone directory.
Run a search query for each prefix p of the query string str(i.e from index 1 to str length) that prints
all the distinct recommended contacts which have the same prefix as our query (p) in lexicographical order.
Please refer the explanation part for better understanding.
NOTE: If there is no match between query and contacts , print "0".
*/
/* Explanation :
By running the query on contact list, we get,
Suggestions based on "m" are:
mongroovetest mondaytesting monkeytest
Suggestions based on "mo" are:
mongroovetest mondaytesting monkeytest
Suggestions based on "mon" are:
mongroovetest mondaytesting monkeytest
Suggestions based on "monk" are:
monkeytest
No Results Found for "monka", So print "0".
No Results Found for "monkat", So print "0".
*/
#define ll long long
using namespace std;
struct TrieNode{
TrieNode *child[26];
bool isEnd;
TrieNode(){
for(int i=0;i<26;i++)
child[i]=NULL;
isEnd=false;
}
};
void insert(TrieNode* root,string &key)
{ TrieNode* curr=root;
for(int i=0;i<key.size();i++)
{ int index=key[i]-'a';
if(curr->child[index]==NULL)
curr->child[index]=new TrieNode();
curr=curr->child[index];
}
curr->isEnd=true;
}
int islastNode(TrieNode* root)
{
for(int i=0;i<26;i++)
{
if(root->child[i])
return 0;
}
return 1;
}
void partialsearch(TrieNode* curr,string key)
{
if(curr->isEnd)
{
cout<<key<<' ';
}
if(islastNode(curr))
return;
for(int i=0;i<26;i++)
{
if(curr->child[i]!=NULL)
{
key.push_back(i+97);
partialsearch(curr->child[i],key);
key.pop_back();
}
}
return;
}
int search(TrieNode* root,string &key)
{
TrieNode *curr=root;
for(int i=0;i<key.size();i++)
{
int index=key[i]-'a';
if(curr->child[index]==NULL)
return 0;
curr=curr->child[index];
}
bool end=(curr->isEnd==true);
bool last=islastNode(curr);
if(end&& last)
{cout<<key; return -1;}
if(!last)
{partialsearch(curr,key);
return 1;}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
TrieNode* root=new TrieNode();
for(int i=0;i<n;i++)
{
cin>>s;
insert(root,s);
}
string s1;
cin>>s1;
for(int i=1;i<=s1.length();i++)
{
string str=s1.substr(0,i);
int temp=search(root,str);
if(!temp)
cout<<temp;
cout<<endl;
}
}
//code
return 0;
}