• POJ2503-Babelfish


    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1304498724

     

    大致题意:

    输入一个字典,字典格式为“英语à外语”的一一映射关系

    然后输入若干个外语单词,输出他们的 英语翻译单词,如果字典中不存在这个单词,则输出“eh”

     

    解题思路:

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

     

    用STL毫无难度(要真说难,也就是空行的处理有一点技巧),也可以用hash做,不过比较麻烦

     

     

    [cpp] view plaincopy
     
    1. //Memory  Time  
    2. //17344K 1563MS   
    3.   
    4. #include<iostream>  
    5. #include<string>  
    6. #include<map>  
    7. using namespace std;  
    8.   
    9. int main(void)  
    10. {  
    11.     char english[11],foreign[11];  
    12.   
    13.     map<string,bool>appear;  //记录foreign与engliash的配对映射是否出现  
    14.     map<string,string>translate; //记录foreign到engliash的映射  
    15.   
    16.     /*Input the dictionary*/  
    17.   
    18.     while(true)  
    19.     {  
    20.         char t;  //temporary  
    21.   
    22.         if((t=getchar())==' ')  //判定是否输入了空行  
    23.             break;  
    24.         else     //输入english  
    25.         {  
    26.             english[0]=t;  
    27.             int i=1;  
    28.             while(true)  
    29.             {  
    30.                 t=getchar();  
    31.                 if(t==' ')  
    32.                 {  
    33.                     english[i]='';  
    34.                     break;  
    35.                 }  
    36.                 else  
    37.                     english[i++]=t;  
    38.             }  
    39.         }  
    40.           
    41.         cin>>foreign;  
    42.         getchar();  //吃掉 输入foreign后的 回车符  
    43.   
    44.         appear[foreign]=true;  
    45.         translate[foreign]=english;  
    46.     }  
    47.   
    48.     /*Translate*/  
    49.   
    50.     char word[11];  
    51.     while(cin>>word)  
    52.     {  
    53.         if(appear[word])  
    54.             cout<<translate[word]<<endl;  
    55.         else  
    56.             cout<<"eh"<<endl;  
    57.     }  
    58.       
    59.     return 0;  
    60. }  
  • 相关阅读:
    DS博客作业02--栈和队列
    DS博客作业01--线性表
    c博客06-结构体&文件
    C博客作业05--指针
    C语言博客作业04--数组
    C语言博客作业03--函数
    JAVA面向对象设计大作业——QQ联系人系统
    DS博客作业05--查找
    DS博客作业04--图
    DS博客作业03--树
  • 原文地址:https://www.cnblogs.com/gongpixin/p/4477470.html
Copyright © 2020-2023  润新知