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;
}

 

转载于:https://www.cnblogs.com/caijiaming/p/9400990.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值