题解报告:hdu 1075 What Are You Talking About

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075

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('\t'), enter('\n') 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!

问题描述

   伊格内修斯非常幸运,昨天他遇到了火星人。但他不知道火星人使用的语言。火星人离开时给他一本火星历史书和一本字典。现在伊格内修斯想把历史书翻译成英文。你能帮助他吗?  

输入

  这个问题只有一个测试用例,测试用例由两部分组成,即字典部分书本部分。字典部分以单行开头,包含一个字符串“START”,这个字符串应该被忽略,接下来是一些行,每行包含两个字符串,第一个是英文单词,第二个是火星人的相应单词语言。具有单个字符串“END”的行表示目录部分的结束,并且该字符串应该被忽略。本书部分以单行开头,包含一个字符串“START”,这个字符串应该被忽略,然后是一篇用火星文写成的文章。你应该用字典将文章翻译成英文。如果你在字典中找到这个单词,你应该翻译它,并将新单词入你的翻译中,如果你无法在字典中找到这个单词,你就需要翻译它,只需将旧单词复制到你的翻译中。空格(''),选项卡('\ t'),输入('\ n')和所有标点不应该被翻译。(不是英文的字符原样输出带有单个字符串“END”的行表示书籍部分的结尾,这也是输入的结尾。所有单词都是小写字母,每个单词最多包含10个字符,每行最多包含3000个字符。  

输出

 在这个问题中,你必须输出历史书的翻译。

解题思路: 此题有两种解法:map解法+Trie解法。这里提供map解法。题目说得很清楚,给出书本部分,去找字典中的翻译并输出,这种键值很容易想到map关联式容器,用火星单词做key,英文单词做value,用find来查找字典,这样处理就简单多了。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 map<string,string>mp;
 4 int main()
 5 {
 6     mp.clear();//清空map容器
 7     string s1,s2;
 8     cin>>s1;//输入START,不需要吸收换行符,因为上下均为cin输入字符串
 9     while(cin>>s1){
10         if(s1=="END")break;
11         cin>>s2;
12         mp[s2]=s1;//映射建立字典键值
13     }
14     cin>>s1;//读入START
15     char ch=getchar();//吃掉回车符的影响,因为下一个读取的是单个字符
16     while(1){
17         s1="";//重新将s1赋值为空字符串,用来记录单词的字符串
18         while(1){
19             scanf("%c",&ch);//每次读取当前字符
20             if(!((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')))break;//只要不是英文字母就直接退出,即为一个单词
21             s1+=ch;//加进来作为一个单词
22         }
23         if(s1=="END")break;//如果是END的话就直接退出循环
24         if(mp.find(s1)==mp.end())cout<<s1;//当迭代器指向尾后迭代器表明找不到,原样输出火星字符串
25         else cout<<mp[s1];//否则输出字典单词
26         printf("%c",ch);//输出不是字母的字符
27     }
28     return 0;
29 }

 

posted @ 2018-03-26 21:58  霜雪千年  阅读(362)  评论(0编辑  收藏  举报