1. 程式人生 > >題解報告:poj 2503 Babelfish(map)

題解報告:poj 2503 Babelfish(map)

cti 格式化 .... 數據 rtu water sequence words end

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
解題思路:查字典--->map鍵值對,由於時間限制,要用C語言的輸入輸出,因此輸入時考慮用sscanf函數來格式化讀取的字符串,其他常規處理即可。
int sscanf (const char *str,const char * format,........);
sscanf函數會將參數str字符串根據參數format字符串來轉換並格式化數據,轉換後的結果存於對應的參數內。返回值:如果成功,該函數返回成功匹配和賦值的個數。如果到達文件末尾或發生讀錯誤,則返回EOF。

AC代碼(2157ms):
 1 #include<iostream>
 2 #include<map>
 3 #include<string.h>
 4 #include<cstdio>
 5 using namespace std;
 6 const int maxn=30;
 7 char str[maxn],obj[maxn],ans[maxn];
 8 map<string,string> mp;
 9 int main(){
10     while(gets(str)&&strlen(str)){
11         sscanf(str,"
%s%s",ans,obj);//格式字符串 12 mp[obj]=ans;//映射(字典) 13 } 14 while(gets(str)){ 15 if(mp.find(str)!=mp.end())printf("%s\n",mp[str].c_str()); 16 else printf("eh\n"); 17 } 18 return 0; 19 }

題解報告:poj 2503 Babelfish(map)