10282 - Babelfish
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。
注意中间空行读取时s[0]=0(这是因为gets在读取完一行后跳过'\n'但并未保存'\n',所以s[0]='\0'=0)
完整代码:
/*UVa: 0.399s*/ /*ZOJ: 180ms,15824KB*/ #include<cstdio> #include<string> #include<map> using namespace std; char s[25], a[15], b[15]; map<string, string> m; int main() { while (gets(s), s[0]) sscanf(s, "%s%s", a, b), m[string(b)] = string(a); while (gets(s)) puts(m.find(s) != m.end() ? m[s].c_str() : "eh"); return 0; }

本文介绍了一个使用map实现的Babelfish翻译程序,该程序可以将外语文本翻译成英文,对于新迁居到大城市的人来说,这有助于理解当地人的方言。程序通过读取字典文件中的英语单词和外语文本对应关系,然后翻译输入的外语文本。
859

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



