poj 2503 Babelfish
Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 26498   Accepted: 11378

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

Hint

Huge input and output,scanf and printf are recommended.
 
简单的字典树就可以做……
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 const int sonnum = 26, base = 'a';
 9 struct Trie 
10 {
11   int num; bool terminal; char dic[20];
12   Trie *son[sonnum];
13 };
14 Trie *NewTrie()
15 {
16   Trie *temp = new Trie;
17   temp->num = 1; temp->terminal = false; 
18   memset(temp->dic, 0, sizeof(temp->dic));
19   for (int i = 0; i < sonnum; ++i) temp->son[i] = NULL;
20   return temp;
21 }
22 
23 void Insert(Trie *pnt, char *s, int len, char *t)
24 {
25   Trie *temp = pnt;
26   for (int i = 0; i < len; ++i)
27   {
28     if (temp->son[s[i]-base] == NULL)
29       temp->son[s[i]-base] = NewTrie();
30     else temp->son[s[i]-base]->num++;
31     temp = temp->son[s[i]-base];
32   }
33   temp->terminal = true;
34   strcpy(temp->dic, t);
35 //  temp->dic = t;
36 }
37 
38 Trie *Find(Trie *pnt, char *s, int len)
39 {
40   Trie *temp = pnt;
41   for (int i = 0; i < len; ++i)
42   {
43     if (temp->son[s[i]-base] == NULL)
44     {
45       printf("eh\n");
46       return temp;
47     }
48     else temp = temp->son[s[i]-base];
49   }
50   if (temp->terminal == true)
51     printf("%s\n", temp->dic);
52   return temp;
53 }
54 
55 int main(void)
56 {
57 #ifndef ONLINE_JUDGE
58   freopen("poj2503.in", "r", stdin);
59 #endif
60   Trie *pnt = NewTrie();
61   char a[20], b[20], t;
62   while (1)
63   {
64     scanf("%s", a);
65     t = getchar();
66     if (t == '\n')
67       break;
68     scanf("%s", b);
69     Insert(pnt, b, strlen(b), a);
70   }
71   Find(pnt, a, strlen(a));
72   while (~scanf("%s", a))
73   {
74     Find(pnt, a, strlen(a));
75   }
76 
77   return 0;
78 }

但是我有一个困惑,关于指针的。

如果把结构体中的dic换成指针形式,char *dic; 在Insert函数中写成 temp->dic = t; 但是程序结果为什么输出的是原来的单词,而不是翻译后的单词?

这个问题现在还没想明白,以后还得多花时间学学C语言啊……

posted on 2013-02-27 00:28  aries__liu  阅读(186)  评论(0编辑  收藏  举报