1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cstdlib>
5
using
namespace std;
6
const
int N=
26;
7
const
int maxn=
3007;
8
struct node
9 {
10
int flag;
11
char t[
11];
12
struct node *next[N];
13 };
14
struct node *creat()
15 {
16
int i;
17
struct node *p=
new (
struct node);
18 p->flag=
0;
19
for(i=
0;i<N;i++)
20 {
21 p->next[i]=NULL;
22 }
23
return p;
24 }
25
void insert(
struct node **root,
char *s,
char *s1)
26 {
27
int i,k;
28
struct node *p;
29
if(!(p=*root))
30 {
31 p=*root=creat();
32 }
33 i=
0;
34
while(s[i])
35 {
36 k=s[i++]-
'
a
';
37
if(p->next[k]==NULL)
38 p->next[k]=creat();
39 p=p->next[k];
40 }
41 p->flag=
1;
42 strcpy(p->t, s1);
43 }
44
int search(
struct node **root,
char *s,
char *s1)
45 {
46
int i=
0,k;
47
struct node *p;
48
if(!(p=*root))
49 {
50
return
0;
51 }
52
while(s[i])
53 {
54 k=s[i++]-
'
a
';
55
if(!(p->next[k]))
56
return
0;
57 p=p->next[k];
58 }
59
if(p->flag)
60 {
61 strcpy(s1, p->t);
62
return
1;
63 }
64
return
0;
65 }
66
int main()
67 {
68
int i,j,l,k,len;
69
struct node *root=NULL;
70
char s[
11],s1[
11],s2[
11];
71
while(~scanf(
"
%s
",s))
72 {
73
if(!strcmp(s,
"
END
"))
74
break;
75
if(strcmp(s,
"
START
"))
76 {
77 scanf(
"
%s
",s2);
78 insert(&root,s2,s);
79 }
80 }
81 getchar();
82
char line[maxn];
83
while(gets(line))
84 {
85
if(!strcmp(line,
"
END
"))
86
break;
87
if(strcmp(line,
"
START
"))
88 {
89 len=strlen(line);
90 k=
0;
91 memset(s,
0,
sizeof(s));
92
for(i=
0;i<len;i++)
93 {
94
95
if(islower(line[i]))
96 {
97 s[k++]=line[i];
98
if(i==len-
1)
99 {
100 memset(s1,
0,
sizeof(tm));
101
if(search(&root,s,s1))
102 {
103 l=strlen(s1);
104
for(j=
0;j<l;j++)
105 printf(
"
%c
",s1[j]);
106 }
107
else
108 {
109
for(j=
0;j<k;j++)
110 printf(
"
%c
",s[j]);
111 }
112
break;
113 }
114 }
115
else
116 {
117 memset(s1,
0,
sizeof(s1));
118
if(search(&root,s,s1))
119 {
120 l=strlen(s1);
121
for(j=
0;j<l;j++)
122 printf(
"
%c
",s1[j]);
123 }
124
else
125
for(j=
0;j<k;j++)
126 printf(
"
%c
",s[j]);
127 printf(
"
%c
",line[i]);
128 k=
0;
129 memset(s,
0,
sizeof(s));
130 }
131 }
132 puts(
"");
133 }
134 memset(line,
0,
sizeof(line));
135 }
136
return
0;
137 }
138