题目链接:http://poj.org/problem?id=2503
代码:
#include<cstdio> #include<string> #include<map> #include<iostream> using namespace std; int main(void){ char english[11],foreign[11]; map<string,bool>appear; //记录foreign与english的配对映射是否出现 map<string,string>translate; //记录foreign到english的映射 char ch; while((ch=getchar())!=‘\n‘){ english[0]=ch; scanf("%s%s",english+1,foreign); appear[foreign]=true; translate[foreign]=english; getchar(); } char word[11]; while(~scanf("%s",word)){ if(appear[word]) cout<<translate[word]<<endl; else puts("eh"); } return 0; }
另附一个大神的做法:https://www.cnblogs.com/shenben/p/5619304.html
虽然运行很慢(我的做法的2倍),不过思路很直接。
#include<cstdio> #include<cstring> #include<string> #include<iostream> #include<map> using namespace std; map<string,string>m; char s[30],x[11],y[11]; int main(){ while(1){ gets(s); if(s[0]==‘\0‘) break; sscanf(s,"%s %s",x,y);//分割 m[y]=x; } while(gets(y)){ if(m[y]=="") cout<<"eh"<<endl; else cout<<m[y]<<endl; } return 0; }
原文地址:https://www.cnblogs.com/fushang/p/8469819.html
时间: 2024-11-08 21:48:03