Babelfish(map) POJ2503

Babelfish

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.

分析:

输入量巨大,题目建议用scanf输入,但是光用scanf不能判断空行,我也没想到好的解决办法,一开始是用getline()+sstream的方式将读入的单词存进map中,但是超时了(sstream很慢,时间卡得紧得肯定要超时)
之后我了解到map也能直接存字符数组,那么既然string方法超时那么就改用字符数组,利用gets()读入一整行,存进字符数组中,再用sscanf进行处理,将两个单词分开存入两个字符数组
代码:

#include 
#include 
#include 
#include 
using namespace std;
map<string,string> m;
int main()
{
	char str[25],str1[25],str2[25];
	int n,i,j,k;
	while(gets(str) && str[0]!='\0'){
		sscanf(str,"%s%s",str1,str2);
		m[str2]=str1;
	}
	while(gets(str)){
		if(m.find(str)!= m.end() ) 
			cout<<m[str]<<endl;
		else cout<<"eh\n";
	}
	return 0;
}

你可能感兴趣的:(STL)