Problem C: Babelfish
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 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 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
Output for Sample Input
cat eh loops
题目大意:给你一个方言,和其对应的英语单词。要求你按照方言查找出英语单词。
解析:直接用map就可以轻松解决。
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string,string> hash;
int main() {
string str;
hash.clear();
while(getline(cin,str)) {
if(str[0] == '\0'){
break;
}
int pos = str.find(' ');
string key = str.substr(pos+1);
string value = str.substr(0,pos);
hash.insert(make_pair(key,value));
}
string s;
map<string,string>::iterator it;
while(cin >> s) {
it = hash.find(s);
if(it == hash.end()) {
cout << "eh" << endl;
}else {
cout << hash[s] << endl;
}
}
return 0;
}