Description
We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse 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
Output for Sample Input
cat eh loops
这道题大概就是自建一个词典,每一个英文单词对应着胖老鼠语言的一个单词,要求输入胖老鼠语言的一个单词,
输出与之对应的英文单词,如果词典中没有收录,就输出eh。
思路:
这种对应关系用到map很容易解决,问题是结束循环的方式,如果要空一行结束循环,while()括号内输入的字符串
必须能接受空格,当输入为空时结束循环。但能接受空格的话,每一行的输入会是一行长字符串,无法存到map的下标与其对应的变量。然后用到了同学的一个分割字符串的方法sscanf()。问题就很容易解决了。
源程序:
#include<bits/stdc++.h> using namespace std; int main() { map<string,string>l; string a; char b[1000],a1[1000],a2[1000]; int i; while(gets(b)) { a=b; if(a=="") break; sscanf(b,"%s%s",a1,a2); l[a2]=a1; } while(cin>>a) { if(l[a]=="") cout<<"eh"<<endl; else cout<<l[a]<<endl; } }