-
Notifications
You must be signed in to change notification settings - Fork 0
/
Help me pradyuman.txt
132 lines (123 loc) · 3.27 KB
/
Help me pradyuman.txt
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
132
Pradyumn is tired of using auto - correct feature in his smartphone. He needs to correct his auto - correct more times than the auto - correct corrects him. Pradyumn is thinking to make his own app for mobile which will restrict auto - correct feature, instead it will provide auto - completion. Whenever Pradyumn types factorial, auto - correct changes it to fact. Pradyumn's App will show options such as fact, factorial, factory. Now, he can chose from any of these options. As Pradyumn is busy with his front - end work of his App. He asks you to help him. He said "You will be given set of words(words may repeat too but counted as one only). Now, as user starts the app, he will make queries(in lowercase letters only). So, you have to print all the words of dictionary which have the prefix same as given by user as input. And if no such words are available, add this word to your dictionary." As you know, Pradyumn want this app to be as smart as him :P So, implement a program for him such that he can release it on Fun Store.
INPUT CONSTRAINTS
1≤N≤30000
sum(len(string[i]))≤2∗10^5
1≤Q≤10^3
INPUT FORMAT
Single integer N which denotes the size of words which are input as dictionary
N lines of input, where each line is a string of consisting lowercase letter
Single integer Q which denotes the number of queries.
Q number of lines describing the query string on each line given by user
OUTPUT FORMAT
If auto - completions exists, output all of them in lexicographical order else output "No suggestions" without quotes
NOTE: All strings are lowercase
SAMPLE INPUT
3
fact
factorial
factory
2
fact
pradyumn
SAMPLE OUTPUT
fact
factorial
factory
No suggestions
#include <bits/stdc++.h>
using namespace std;
struct node
{
node *a[26];
int ct;
node()
{
for(int i=0;i<26;i++)
{
a[i]=NULL;
ct=0;
}
}
};
void insert(node *root,string s)
{
node *temp=root;
for(int i=0;i<s.length();i++)
{
if(!temp->a[s[i]-'a'])
{
temp->a[s[i]-'a']=new node();
}
if(i==s.length()-1)
temp->a[s[i]-'a']->ct=1;
temp=temp->a[s[i]-'a'];
}
}
void dfs(node *root,string s)
{
if(!root){
cout<<s<<endl;
return;
}
// cout<<s<<endl;
if(root and root->ct!=0)
{
cout<<s<<endl;
}
int i;
int ct=0;
for(i=0;i<26;i++)
{
if(!root->a[i]){
ct++;
continue;
}
s.push_back(i+'a');
dfs(root->a[i],s);
s.pop_back();
}
}
void query(node *root,string s)
{
node *temp=root;
bool flag=false;
for(int i=0;i<s.length();i++)
{
if(!temp->a[s[i]-'a'])
{
cout<<"No suggestions\n";
insert(root,s);
flag=true;
break;
}
else
{
temp=temp->a[s[i]-'a'];
}
}
if(flag==false)
{
// cout<<s;
dfs(temp,s);
}
}
int main()
{
int n,q;
cin>>n;
node *root=new node();
for(int i=0;i<n;i++)
{
string s;
cin>>s;
insert(root,s);
}
cin>>q;
for(int i=0;i<q;i++)
{
string s;
cin>>s;
query(root,s);
}
return 0;
}