题目来源:zoj 1109
作者:npufz
题目:
Description
We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse 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
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
map < string, string > ma;
int main()
{
string s1,s2,s;
while(getline(cin,s))
{
s1.clear();
s2.clear();
if(s[0]=='\0') break;
int i;
for(i=0;s[i]!=' ';i++)
s1+=s[i];
s1[i]='\0';
for(i++;i<s.length();i++)
s2+=s[i];
s2[i]='\0';
ma[s2]=s1;
}
while(cin>>s)
{
if(!ma[s].empty())
cout<<ma[s]<<endl;
else
cout<<"eh"<<endl;
}
return 0;
}
总结:直接用MAP容器很方便,也可用哈希函数加二分查找,不过上述程序是有BUG的,因为读入数据时老是崩掉,但是交了就A了,但一直没有发现哪儿有问题