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 is the message translated to English, one word per line. Foreign words 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
使用map。
注意中间空行读取时s[0]=0(这是因为gets在读取完一行后跳过'\n'但并未保存'\n',所以s[0]='\0'=0)
完整代码:
/*UVa: 0.399s*/ /*ZOJ: 180ms,15824KB*/ #include<cstdio> #include<string> #include<map> using namespace std; char s[25], a[15], b[15]; map<string, string> m; int main() { while (gets(s), s[0]) sscanf(s, "%s%s", a, b), m[string(b)] = string(a); while (gets(s)) puts(m.find(s) != m.end() ? m[s].c_str() : "eh"); return 0; }