poj 2503 Babelfish(字典树哈希)

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 29059 Accepted: 12565

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

题意:给一个字典,输入若干个字符串,问再字典中与它对应的字符串,若没有输出“eh”;

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 char s1[100010][20],s2[100010][20];
 5 
 6 struct node
 7 {
 8     int flag;
 9     struct node *next[26];
10 };
11 struct node* creat()
12 {
13     struct node *p = (struct node *)malloc(sizeof(struct node));
14     p->flag = 0;
15     for(int i = 0; i < 26; i++)
16         p->next[i] = NULL;
17     return p;
18 }
19 
20 void insert(struct node *p, char s[],int cnt)
21 {
22     for(int i = 0; s[i]; i++)
23     {
24         if(p->next[s[i]-'a'] == NULL)
25             p->next[s[i]-'a'] = creat();
26         p = p->next[s[i]-'a'];
27     }
28     p->flag = cnt;
29 }
30 int search(struct node *p, char s[])
31 {
32     for(int i = 0; s[i]; i++)
33     {
34         if(p->next[s[i]-'a'] == NULL)
35            return -1;
36         p = p->next[s[i]-'a'];
37     }
38     return p->flag;
39 }
40 int main()
41 {
42     char s[20],str[20];
43     int cnt = 0;
44     struct node *root;
45     root = creat();
46     while(gets(s))
47     {
48         if(strcmp(s,"") == 0)
49             break;
50         sscanf(s,"%s %s",s1[cnt],s2[cnt]);
51         insert(root,s2[cnt],cnt);
52         cnt++;
53     }
54     while(scanf("%s",str)!= EOF)
55     {
56         int f = search(root,str);
57         if(f == -1)
58             printf("eh
");
59         else printf("%s
",s1[f]);
60     }
61     return 0;
62 }
View Code



原文地址:https://www.cnblogs.com/LK1994/p/3378161.html