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;
}
Babelfish翻译挑战
本文介绍了一种基于字典的简单翻译程序实现方案。该程序利用标准输入接收外语词汇与对应英语词汇的映射,并根据这些映射将一段外语消息翻译成英语。通过使用C++中的map容器来存储映射关系,程序能够高效地完成翻译任务。
1228

被折叠的 条评论
为什么被折叠?



