You have just moved from Waterloo to a big city. The people here speakan incomprehensible dialect of a foreign language. Fortunately, you havea dictionary to help you understand them.
Input consists of up to 100,000 dictionary entries, followed by a blankline, followed by a message of up to 100,000 words. Each dictionaryentry is a line containing an English word, followed by a space and aforeign language word. No foreign word appears more than once in thedictionary. 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 10lowercase letters.Output is the message translated to English, one word per line. Foreignwords not in the dictionary should be translated as "eh".
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
cat eh loops
题意: 这么短的题目还用我解释?
解法: 按出题者意思是要用哈希算法, 但是可用STL中的map
AC代码:
#include<stdio.h> #include<iostream> #include<string> #include<map> using namespace std; char str1[30]; char str2[30]; char str[50]; map<string, string> Map; int main() { Map.clear(); while(gets(str) != NULL) { if(str[0] == '\0') break; sscanf(str, "%s %s", str1, str2); Map[str2] = str1; } while(scanf("%s", str) != EOF) { if(Map.find(str) != Map.end()) cout<< Map[str] <<endl; else printf("eh\n"); } return 0; }