ACM程序设计 书中题目 B

Description

We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.

Input Specification

Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output Specification

Output is the message translated to English, one word per line. FatMouse 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

Output for Sample Input

cat
eh
loops
 
 
这道题大概就是自建一个词典,每一个英文单词对应着胖老鼠语言的一个单词,要求输入胖老鼠语言的一个单词,
输出与之对应的英文单词,如果词典中没有收录,就输出eh。
 
 
思路:
这种对应关系用到map很容易解决,问题是结束循环的方式,如果要空一行结束循环,while()括号内输入的字符串
必须能接受空格,当输入为空时结束循环。但能接受空格的话,每一行的输入会是一行长字符串,无法存到map的下标与其对应的变量。然后用到了同学的一个分割字符串的方法sscanf()。问题就很容易解决了。

源程序:
#include<bits/stdc++.h>
using namespace std;
int main()
{

    map<string,string>l;
    string a;
    char b[1000],a1[1000],a2[1000];
    int i;
    while(gets(b))
    {
        a=b;
        if(a=="")
            break;
        sscanf(b,"%s%s",a1,a2);
        l[a2]=a1;
    }
    while(cin>>a)
    {
        if(l[a]=="")
            cout<<"eh"<<endl;
        else
            cout<<l[a]<<endl;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值