以下部分内容转载自琴影老师博客:这是一个传送门 感谢帮助!
今天做了一道算法题,题目本身不是特别难,内容如下:
What Are You Talking About
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?
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.
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.大意就是:Ignatius很幸运,遇到了一个火星人,火星人丢给他一本火星的历史书和一本英文-火星文对照字典,现在来帮他翻译这本历史书。
这个问题只有一个测试用例,由字典和书两部分组成。字典部分由单独一行“START”开始,接下来的每行包含两个字符串,一个是英文,一个是对应的火星文。字典部分以单独一行“END”结束。书的部分由单独一行“START”开始,这个字符串应该被忽略,然后是一篇火星文写的文章。如果是字典里出现了的火星单词,就把翻译后的新单词写进译文中,如果不是,就不必翻译它,只需把旧单词抄到译文中。空格(' ')、tab(′ ')、换行('
')和所有标点都不翻译。单独一行“END”标志着书部分的结尾,这也是输入的结尾。所有单词都是小写字母,每个单词最多包含10个字符,每行最多包含3000个字符。
输出翻译后的文本。
这道题很明显是用STL的map做,但是我写的代码运行后却发现输出怎么样都不对,于是在网上看了一些别人写的代码,发现他们都用到了getchar(); 函数,而我没有。这里贴上我看到的两位dalao的代码,方法不太一样但想法差不多:
(传送门1 )
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <cctype> 6 #include <map> 7 using namespace std; 8 9 int main() 10 { 11 char buf[12], sign, s1[12], s2[12], ch; 12 map<string, string> mp; 13 int id = 0; 14 gets(buf); //strip START 15 while(scanf("%s%s", s1, s2), strcmp(s1, "END")){ 16 mp[s2] = s1; 17 } 18 getchar(); //注意此处! 19 while(scanf("%c", &ch)){ 20 if(isalpha(ch)) buf[id++] = ch; 21 else{ 22 buf[id] = '