POJ 2503 Babelfish map
Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 42962 | Accepted: 18190 |
Description
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
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
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
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
Source
这个题目的大概意思是先自己定义一个字典序,即每个英文单词有个对应的英文单词,直接用map存下这个对应关系
然后没输入一个单词,输出他的对应单词,如果没有这个单词的对应就输出eh
方法是用两个map,一个存字典对应关系,一个存他是否存在字典对应关系中,存在用true,否则false.
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cstdlib> #include <map> #include <string> using namespace std; int main() { map<string,bool>appear;//判断这个单词是否在你定义的字典对应关系中 map<string,string>tra;//定义一个map存下每个单词的对应关系 char a[20],f[20]; while(1) { char t; if((t=getchar())=='\n') break;//这是个处理的技巧,当输入回车符时才结束循环,否则继续输入 else //这也是个输入的技巧,当前面一个单词和后面一个单词用空格隔开时,可以这样将两个单词分开存到两个字符串中 { a[0] = t; int i = 1; while(1) { t = getchar(); if(t==' ') { a[i]='\0';//输入空格时结束前面的输入,再开始第二个单词 break; } else a[i++] = t; } } cin >> f; getchar(); appear[f] = true; tra[f] = a; } char word[11]; while(cin >> word) { if(appear[word]) cout << tra[word] << endl; else cout << "eh" << endl; } return 0; }
彼时当年少,莫负好时光。