-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_nochar.cpp
89 lines (77 loc) · 1.28 KB
/
dict_nochar.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
#include <iostream>
#include <cstdio>
using namespace std;
int flag=0;
class node{
public:
node *next[26];
//char ch;
bool b;
};
node *mk(){
node *n = new node;
n -> b = false;
for(int i = 0; i < 26; i++)
n->next[i] = NULL;
return n;
}
void recur(node *head, string s, string &root){
if(head!=NULL){
if(head->b && s!=root){
flag=1;
printf("%s\n", s.c_str());
}
for(int i=0; i<26; i++){
if ( head->next[i] != NULL ) {
recur(head->next[i], s+(char)('a'+i), root);
}
}
}
}
int main()
{
int n;
cin>>n;
node *head = mk(), *h = NULL;
//cout<<"hi"<<(int)head->ch;
string s;
// char *s;
int indx, ct;
while(n-- > 0){
h = head;
cin >> s;
// scanf("%s", s);
for(int i=0; i<s.size(); i++){
indx = (int)(s[i] - 'a');
if(h->next[indx] == NULL){
node *p = mk();
h->next[indx] = p;
}
h = h->next[indx];
if(i== s.size() - 1)
h->b = true;
//cout<<h->ch<<indx<<" ";
}
}
scanf("%d", &n);
int t = 1;
while(n-- > 0){
cin >> s;
// scanf("%s", s);
h = head;
ct=0;
while(h!=NULL && ct < s.size()){
indx = (int)(s[ct++] - 'a');
h = h->next[indx];
//cout<<h->ch<<" ";
}
printf("Case #%d:\n", t);
if(h!=NULL)
recur(h, s, s);
if(flag == 0)
printf("No match.\n");
flag=0;
t++;
}
return 0;
}