POJ2503Babelfish

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1304498724

 

大致题意:

输入一个字典,字典格式为“英语à外语”的一一映射关系

然后输入若干个外语单词,输出他们的 英语翻译单词,如果字典中不存在这个单词,则输出“eh

 

解题思路:

水题,输入时顺便用STLmap标记外语是否出现过,然后再用map建立“外语à英语”的映射,那么输出时先查找“出现”的标记,若有出现过,再输出映射,否则输出“eh”。

 

STL毫无难度(要真说难,也就是空行的处理有一点技巧),也可以用hash做,不过比较麻烦

 

 1 //Memory  Time
2 //17344K 1563MS
3
4 #include<iostream>
5 #include<string>
6 #include<map>
7 using namespace std;
8
9 int main(void)
10 {
11 char english[11],foreign[11];
12
13 map<string,bool>appear; //记录foreign与engliash的配对映射是否出现
14 map<string,string>translate; //记录foreign到engliash的映射
15
16 /*Input the dictionary*/
17
18 while(true)
19 {
20 char t; //temporary
21
22 if((t=getchar())=='\n') //判定是否输入了空行
23 break;
24 else //输入english
25 {
26 english[0]=t;
27 int i=1;
28 while(true)
29 {
30 t=getchar();
31 if(t==' ')
32 {
33 english[i]='\0';
34 break;
35 }
36 else
37 english[i++]=t;
38 }
39 }
40
41 cin>>foreign;
42 getchar(); //吃掉 输入foreign后的 回车符
43
44 appear[foreign]=true;
45 translate[foreign]=english;
46 }
47
48 /*Translate*/
49
50 char word[11];
51 while(cin>>word)
52 {
53 if(appear[word])
54 cout<<translate[word]<<endl;
55 else
56 cout<<"eh"<<endl;
57 }
58
59 return 0;
60 }
原文地址:https://www.cnblogs.com/lyy289065406/p/2122250.html