您的位置:首页 > 其它

POJ 2503 单词映射(map)

2015-05-12 17:13 218 查看

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output

cat
eh
loops

大致题意:
输入一个字典,字典格式为“英语à外语”的一一映射关系
然后输入若干个外语单词,输出他们的 英语翻译单词,如果字典中不存在这个单词,则输出“eh”

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

 

题解1:

# include<iostream>
# include <cstdio>
# include<string>
# include<map>
using namespace std;

int main(void)
{
char english[11],foreign[11];

map<string,bool>appear;  //记录foreign与engliash的配对映射是否出现
map<string,string>translate; //记录foreign到engliash的映射

/*Input the dictionary*/

while(true)
{
char t;  //temporary

if((t=getchar())=='\n')  //判定是否输入了空行
break;
else     //输入english
{
english[0]=t;
int i=1;
while(true)
{
t=getchar();
if(t==' ')
{
english[i]='\0';
break;
}
else
english[i++]=t;
}
}

cin>>foreign;
getchar();  //吃掉 输入foreign后的 回车符

appear[foreign]=true;
translate[foreign]=english;
}

/*Translate*/

char word[11];
while(cin>>word)
{
if(appear[word])
cout<<translate[word]<<endl;
else
cout<<"eh"<<endl;
}

return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: