题目链接:
http://poj.org/problem?id=2503
题目大意:
给你一本字典。字典上每一行为一个英语单词和一个其他国家单词。这样我们就可以通过字典把英语单词
翻译成其他国家单词,也可以将其他国家单词翻译为英语单词了。现在再给你几个外国单词,问:字典中
是否有这个单词的翻译。如果有,就输出翻译,否则,输出"eh"。
思路:
这道题其实可以用STL中的map或是字典树来做。map的做法是,建立两个map,一个对应存放翻译,一
个用来判断翻译是否存在。注意输入可以先将一行输入进来,判断是否为"\n"。再用sscanf将英语单词和
其他国家单词拆分为两个字符串s,t。之后用map存起来。然后根据输入的单词进行判断输出。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
using namespace std;
char s[11],t[11],word[11],str[22];
int main()
{
map<string,string> H;
map<string,bool> HH;
while(gets(str))
{
if(strlen(str) == 0)
break;
sscanf(str,"%s %s",s,t);
H[t] = s;
HH[t] = true;
}
while(cin >> word)
{
if(HH[word])
cout << H[word] << endl;
else
cout << "eh" << endl;
}
return 0;
}