《ACM程序设计》书中题目B(“胖老鼠的语言”说好的建国后动物不许成精呢?)

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


这道题总体说来就是老鼠成精了要加入世贸组织,但是他们有自己的语言,需要输入一个词典,来翻译老鼠的语言。

输入分为两部分:

1.“人类语言 空格 胖老鼠语言”为一组,一组占一行,以一行空行作为结束;

2.输入一个老鼠词,输出一个人类词,循环输入。

大体思路是这样的:

1.检索方式不能用一般的数组思想,最多100005个单词暴力检索一定会超时的,所以可以用map容器;

2.因为词典输入必须以空行结束,所以可以用整体输入,判断是否为空后再输入到容器里;

3.检索就没什么难度了,不为空输出,为空输出“eh”。

程序如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
        map<string,string> m;    //定义map数组
        string aa;
        char a1a2[105],a1[105],a2[105];       //a1a2存储每一行字符串
        int i;
        while(gets(a1a2))
        {
                aa=a1a2;
                if(aa=="")    //输入字符串为空结束循环
                        break;
                else
                {
                        sscanf(a1a2,"%s%s",a1,a2);
                        m[a2]=a1;    //将字符串分开转入两个新字符串中
                }
        }
        while(cin>>aa)  //检索输出
        {
                if(m[aa]=="")
                        cout<<"eh"<<endl;
                else
                        cout<<m[aa]<<endl;
        }
}

整体思路捋清后不算特别难,利用map可以比较容易的解决问题,一个难点就是空行结束的要求比较难@~@,要利用整体判断;



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值