hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 12617    Accepted Submission(s): 4031


Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 
Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab(' '), enter(' ') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 
Output
In this problem, you have to output the translation of the history book.
 
Sample Input
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
 
Sample Output
hello, i'm from mars.
i like earth!
Hint
Huge input, scanf is recommended.
 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1800 1671 1247 1298 1072 
 
  字典树,经典题,字典翻译
  WA了1次,后来发现是我把要翻译的单词搞错了,如果一个可以翻译的较长的单词包含着另一个可以翻译的较短的单词,这样遍历句子的时候就会先碰到较短的单词,WA的程序会先把这个短单词翻译出来,但实际上这是不对的,应该翻译整个较长的单词,而不是遇到什么就翻译什么。
  我的程序运行时间是281MS,用链表做的,用数组应该会快些。
  题意
  只有一组测试数据,分成两部分。第一部分是字典,给你若干个单词以及对应的翻译,以START开始,END结束;第二部分是翻译部分,给你若干句子,要求你根据上面给出的字典,将火星文翻译成英文,以START开始,END结束。这里翻译的时候,要注意只有字典中有对应翻译的单词才翻译,字典中没有对应翻译的单词以及标点符号空格原样输出。
  思路
  将字典中的火星文单词,也就是右边的单词构造成一棵字典树。在单词结束的节点中存储其对应的英文翻译。输入的时候读取整行,然后遍历每一个字符,将所有字母连续的记录到一个字符数组中,直到遇到一个非英文字母的字符为止,这个时候从字典树中查找这个单词有没有对应的英文翻译,如果有,输出这个翻译,如果没有,输出原来的单词。
  注意
  1.输入方法,我使用的scanf+gets。
  2.不要搞错要翻译的单词,遇到一个非字母的字符算一个单词。
  3.尽量不要使用cin,cout,容易超时
  代码
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <malloc.h>
 5 using namespace std;
 6 
 7 struct Tire{
 8     Tire *next[26];
 9     char *trans;  //定义一个指向一维数组的指针
10     Tire()  //构造函数
11     {
12         int i;
13         for(i=0;i<26;i++){
14             next[i]=NULL;
15         }
16         trans = NULL;
17     }
18 };
19 
20 Tire root;
21 
22 void Insert(char trans[],char word[])   //将单词word插入到字典树中,并在最后加上翻译trans
23 {
24     Tire *p = &root;
25     int i;
26     for(i=0;trans[i];i++){
27         int n = trans[i]-'a';
28         if(p->next[n]==NULL)
29             p->next[n] = new Tire;
30         p = p->next[n];
31     }
32     p->trans = (char*)malloc(sizeof(char)*11);
33     strcpy(p->trans,word);
34 }
35 void Find(char str[])   //找到对应的翻译并输出,没找到则输出原来的字符串
36 {
37     Tire *p = &root;
38     int i;
39     for(i=0;str[i];i++){
40         int n = str[i]-'a';
41         if(p->next[n]==NULL){
42             printf("%s",str);
43             return ;
44         }
45         p = p->next[n];
46     }
47     if(p->trans==NULL)
48         printf("%s",str);
49     else
50         printf("%s",p->trans);
51 }
52 
53 int main()
54 {
55     char word[11],trans[11];
56     scanf("%s",word);
57     while(scanf("%s",word)!=EOF){   //输入字典
58         if(strcmp(word,"END")==0)   //遇到结束标记
59             break;
60         scanf("%s",trans);
61         Insert(trans,word); //将单词word插入到字典树中,并在最后加入其翻译
62     }
63     scanf("%s",word);
64     getchar();
65     char str[3001];
66     while(gets(str)){
67         if(strcmp(str,"END")==0)
68             break;
69         int i,j=0;
70         char t[3001]={0};
71         for(i=0;str[i];i++){
72             if('a'<=str[i] && str[i]<='z'){ //检测到的是小写字母
73                 t[j++] = str[i];
74             }
75             else{
76                 t[j] = '';
77                 if(t[0]!=''){ //不是空的
78                     Find(t);    //找到对应的翻译并输出,没找到则输出原来的字符串
79                     t[0]='',j=0;  //初始化t
80                 }
81                 printf("%c",str[i]);
82             }
83         }
84         printf("
");
85     }
86 
87     return 0;
88 }

Freecode : www.cnblogs.com/yym2013

原文地址:https://www.cnblogs.com/yym2013/p/3781741.html