Babelfish (关于map<string,string>的用法

题目链接:https://vjudge.net/contest/237395#problem/A

学习博客:https://blog.csdn.net/lyy289065406/article/details/6647413

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Huge input and output,scanf and printf are recommended.
题目大意:每行输入两个字符串,前面的是Englih,后面的是字典里的前面那个的翻译,当遇到空行接着输入的是要你查找的单词,如果找到了输出对应的English,没找到则输出eh
个人思路:第一眼看到就觉得是用map来做,因为map可以存任意类型的变量,而且是一对一的关系,这就非常符合了,但是这题的输入有点麻烦,具体看代码
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<map>
using namespace std;
int main()
{
    char s1[15],s2[15];
    map<string,bool>appear;//刚开始默认为false
    map<string,string>translate;
    while(1)
    {
        char t;
        if((t=getchar())=='\n')
            break;
        else
        {
            s1[0]=t;
            int i=1;
            while(1)
            {
                t=getchar();
                if(t==' ')
                {
                    s1[i]='\0';
                    break;
                }
                else
                    s1[i++]=t;
            }
        }
        cin>>s2;
        getchar();
        appear[s2]=true;
        translate[s2]=s1;
    }
    char word[15];
    while(cin>>word)
    {
        if(appear[word])
            cout<<translate[word]<<endl;
        else
            cout<<"eh"<<endl;
    }
    return 0;
}

 

posted @ 2018-08-01 14:16  执||念  阅读(305)  评论(0编辑  收藏  举报