• 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
  • 相关阅读:
    计算小于12的阶乘
    ubuntu下gvim启动出现gtk warning Invalid input string
    UBUNTU基础知识
    Ubuntu下创建软链接
    linux命令行介绍及使用(二)
    Ubuntu问题sudo: /etc/sudoers is mode 0640should be 0440的解决方法
    安装mp3插件
    Ubuntu下GTK的安装
    linux命令行介绍及使用(三)
    VB.NET中用GDI+画饼图
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/4497962.html
Copyright © 2020-2023  润新知