POJ 2503 Babelfish 字典树

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24994   Accepted: 10703

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops
 1 /* 功能Function Description:     POJ-2503  字典树
 2    开发环境Environment:          DEV C++ 4.9.9.1
 3    技术特点Technique:
 4    版本Version:
 5    作者Author:                   可笑痴狂
 6    日期Date:                      20120810
 7    备注Notes:
 8         注意输入格式的控制
 9 */
10 #include<stdio.h>
11 #include<string.h>
12 #include<malloc.h>
13 
14 typedef struct node
15 {
16     char s[20];
17     struct node *next[26];
18 }node;
19 
20 void insert(char *a,char *b,node *T)
21 {
22     node *p,*q;
23     int i,j,id;
24     p=T;
25     i=0;
26     while(b[i])
27     {
28         id=b[i]-'a';
29         if(p->next[id]==NULL)
30         {
31             q=(node *)malloc(sizeof(node));
32             memset(q->s,'\0',sizeof(q->s));
33             for(j=0;j<26;++j)
34                 q->next[j]=NULL;
35             p->next[id]=q;
36         }
37         p=p->next[id];
38         ++i;
39     }
40     strcpy(p->s,a);
41 }
42 
43 void search(char *a,node *T)
44 {
45     int id,i=0;
46     node *p=T;
47     while(a[i])
48     {
49         id=a[i]-'a';
50         if(p->next[id]==NULL)
51         {
52             printf("eh\n");
53             return;
54         }
55         p=p->next[id];
56         ++i;
57     }
58     if((p->s)[0]!='\0')
59         printf("%s\n",p->s);
60     else
61         printf("eh\n");
62 }
63 
64 int main()
65 {
66     char a[20],b[20];
67     char c[40];
68     node *T;
69     int i,len,k;
70     T=(node *)malloc(sizeof(node));
71     memset(T->s,'\0',sizeof(T->s));
72     for(i=0;i<26;++i)
73         T->next[i]=NULL;
74     while(gets(c)&&c[0]!='\0')  //注意输入格式,输入空行时结束while循环
75     {
76         len=strlen(c);
77         for(i=0;c[i]!=' ';++i)
78             a[i]=c[i];
79         a[i]='\0';
80         for(++i,k=0;i<=len;++i)
81             b[k++]=c[i];
82         insert(a,b,T);
83     }
84     while(scanf("%s",a)!=EOF)
85     {
86         search(a,T);
87     }
88     return 0;
89 }

 


                            

你可能感兴趣的:(poj)