您的位置:首页 > 其它

POJ2503 Babelfish(Map键值对的应用)

2015-06-02 11:47 281 查看
Babelfish

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 35424Accepted: 15146
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.

解题思路:

裸Map的运用,直接上代码:

AC代码:

#include<iostream>
#include<string>
#include<map>
#include<cstdio>

using namespace std;

int main()
{
map<string,string> M;
string a;
//freopen("1.txt","r",stdin);
while(getline(cin,a))
{
string first;
string second;
if(0 == a.length())
{
break;
}
int i = a.find_first_of(' ');
first = a.substr(0,i); //0 - i
int j = a.find_first_not_of(' ',i);
second = a.substr(j,a.length() - j); //j开始共a.length - j位
M[second] = first;
}
string b;
while(getline(cin,b))
{
if(M.count(b))
{
string c = M[b];
printf("%s\n",c.c_str());
//cout<<M[b]<<endl;
}
else
{
printf("eh\n");
//cout<<"eh"<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: