POJ 2503.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 oopslaySample Output
cat eh loopsHint
Huge input and output,scanf and printf are recommended.
这道题的意思就是进行单词翻译
关键点在于:
- 读入数据
- 映射
由于数据存在一行两个单词和一行一个单词两种形式,因此,我们再读入时需要区分开。
另一方面,由于需要对单词进行映射,因此我们可以采用map,进行string对string的映射
最后输出时使用cout来输出(string格式)
如果需要进一步优化时间可以采用trie树
AC代码:GitHub
1 /* 2 By:OhYee 3 Github:OhYee 4 HomePage:http://www.oyohyee.com 5 Email:oyohyee@oyohyee.com 6 Blog:http://www.cnblogs.com/ohyee/ 7 8 かしこいかわいい? 9 エリーチカ! 10 要写出来Хорошо的代码哦~ 11 */ 12 13 #include <cstdio> 14 #include <algorithm> 15 #include <cstring> 16 #include <cmath> 17 #include <string> 18 #include <iostream> 19 #include <vector> 20 #include <list> 21 #include <queue> 22 #include <stack> 23 #include <map> 24 using namespace std; 25 26 //DEBUG MODE 27 #define debug 0 28 29 //循环 30 #define REP(n) for(int o=0;o<n;o++) 31 32 const int maxn = 100005; 33 const int maxm = 100; 34 35 inline int read_string(char s[]) { 36 char c; 37 int i = 0; 38 //while(!(((c = getchar()) ==' ') || (c >= 'a'&&c <= 'z'))) 39 // if(c == EOF) 40 // return 0; 41 if((c = getchar())== EOF) 42 return 0; 43 while((c == ' ') || (c >= 'a'&&c <= 'z')) { 44 s[i++] = c; 45 c = getchar(); 46 } 47 s[i] = '\0'; 48 return i; 49 } 50 51 bool Do() { 52 char temp[maxm*2]; 53 map<string,string> dict; 54 map<string,string>::iterator it; 55 char a[maxm],b[maxm]; 56 57 while(read_string(temp)) { 58 if(strcmp(temp,"") == 0) 59 break; 60 sscanf(temp,"%s %s",a,b); 61 dict[b] = a; 62 } 63 64 while(scanf("\n%s",a) != EOF) { 65 cout << (dict.count(a) ? dict[a] : "eh" )<< "\n"; 66 } 67 68 69 return false; 70 } 71 72 int main() { 73 while(Do()); 74 return 0; 75 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com