码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2503 Babelfish(map入门)

时间:2018-02-25 19:25:57      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:一个   tchar   str   pre   map   while   span   etc   代码   

题目链接: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;
}

 

POJ 2503 Babelfish(map入门)

标签:一个   tchar   str   pre   map   while   span   etc   代码   

原文地址:https://www.cnblogs.com/fushang/p/8469819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!