zoukankan      html  css  js  c++  java
  • POJ 2503 单词映射(map)

    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:

     1 # include<iostream>  
     2 # include <cstdio>
     3 # include<string>  
     4 # include<map>  
     5 using namespace std;  
     6   
     7 int main(void)  
     8 {  
     9     char english[11],foreign[11];  
    10   
    11     
    12     map<string,string>translate; //记录foreign到engliash的映射  
    13   
    14     /*Input the dictionary*/  
    15   
    16     while(true)  
    17     {  
    18         char t;  //temporary  
    19   
    20         if((t=getchar())=='
    ')  //判定是否输入了空行  
    21             break;  
    22         else     //输入english  
    23         {  
    24             english[0]=t;  
    25             int i=1;  
    26             while(true)  
    27             {  
    28                 t=getchar();  
    29                 if(t==' ')  
    30                 {  
    31                     english[i]='';  
    32                     break;  
    33                 }  
    34                 else  
    35                     english[i++]=t;  
    36             }  
    37         }  
    38           
    39         cin>>foreign;  
    40         getchar();  //吃掉 输入foreign后的 回车符  
    41   
    42         
    43         translate[foreign]=english;  
    44     }  
    45   
    46     /*Translate*/  
    47   
    48     char word[11];  
    49     while(cin>>word)  
    50     {  
    51         if(translate.find(word) == translate.end()) //没找到
    52            cout<<"eh"<<endl; 
    53         else 
    54            cout<<translate[word]<<endl; 
    55     }  
    56       
    57     return 0;  
    58 }  
    View Code

    题解2:

     1 # include<iostream>  
     2 # include <cstdio>
     3 # include<string>  
     4 # include<map>  
     5 using namespace std;  
     6   
     7 int main(void)  
     8 {  
     9     char english[11],foreign[11];  
    10   
    11     map<string,bool>appear;  //记录foreign与engliash的配对映射是否出现  
    12     map<string,string>translate; //记录foreign到engliash的映射  
    13   
    14     /*Input the dictionary*/  
    15   
    16     while(true)  
    17     {  
    18         char t;  //temporary  
    19   
    20         if((t=getchar())=='
    ')  //判定是否输入了空行  
    21             break;  
    22         else     //输入english  
    23         {  
    24             english[0]=t;  
    25             int i=1;  
    26             while(true)  
    27             {  
    28                 t=getchar();  
    29                 if(t==' ')  
    30                 {  
    31                     english[i]='';  
    32                     break;  
    33                 }  
    34                 else  
    35                     english[i++]=t;  
    36             }  
    37         }  
    38           
    39         cin>>foreign;  
    40         getchar();  //吃掉 输入foreign后的 回车符  
    41   
    42         appear[foreign]=true;  
    43         translate[foreign]=english;  
    44     }  
    45   
    46     /*Translate*/  
    47   
    48     char word[11];  
    49     while(cin>>word)  
    50     {  
    51         if(appear[word])  
    52             cout<<translate[word]<<endl;  
    53         else  
    54             cout<<"eh"<<endl;  
    55     }  
    56       
    57     return 0;  
    58 }  
    View Code
  • 相关阅读:
    intelliJ IDEA最常用的快捷键
    Git使用说明
    mac快速安装程序
    java面试-String、StringBuffer和StringBuilder的区别
    linux静态与动态库创建及使用实例
    linux下动态库编译的依赖问题
    动态库与静态库的区别
    gcc-4.8.3安装,gdb-7.6安装
    设计模式之单件模式
    设计模式之抽象工厂模式
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/4497962.html
Copyright © 2011-2022 走看看