1

POJ 2503 - Babelfish

 2 years ago
source link: https://exp-blog.com/algorithm/poj/poj2503-babelfish/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

POJ 2503 - Babelfish | 眈眈探求


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

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

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

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

//Memory  Time
//17344K 1563MS 

#include<iostream>
#include<string>
#include<map>
using namespace std;

int main(void)
{
    char english[11],foreign[11];

    map<string,bool>appear;  //记录foreign与engliash的配对映射是否出现
    map<string,string>translate; //记录foreign到engliash的映射

    /*Input the dictionary*/

    while(true)
    {
        char t;  //temporary

        if((t=getchar())=='\n')  //判定是否输入了空行
            break;
        else     //输入english
        {
            english[0]=t;
            int i=1;
            while(true)
            {
                t=getchar();
                if(t==' ')
                {
                    english[i]='\0';
                    break;
                }
                else
                    english[i++]=t;
            }
        }

        cin>>foreign;
        getchar();  //吃掉 输入foreign后的 回车符

        appear[foreign]=true;
        translate[foreign]=english;
    }

    /*Translate*/

    char word[11];
    while(cin>>word)
    {
        if(appear[word])
            cout<<translate[word]<<endl;
        else
            cout<<"eh"<<endl;
    }

    return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK