NOI 1806:词典(stl map)

 

1806:词典

总时间限制:

3000ms

内存限制:

65536kB

描述

你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。

输入

首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。

输出

在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。

样例输入

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

样例输出

cat
eh
loops

 

 
 

当输入空行时停止建树:getline()

while(1)
    {
        getline(cin,z,'\n');
        if(z=="")break;
///
    }

按空格分割E文和X文,string的substr(start,length)和substring(start,end)

        int pos=z.find(" ");
        string x=z.substr(pos+1);
        string y=z.substr(0,pos);

录入map,注意让X文成为游标,xx.insert(pair<xxx,xxx>(xx,xx))

m.insert(pair<string,string>(x,y));

迭代器:【迭代器指向的->first和->second】

 

map<string,string>::iterator it;<string,string>::iterator it;

map中,find传入游标,传出指向节点的指针。如果没找到,返回xxx.end():

it=m.find(s);=m.find(s);

AC:

#include <iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
using namespace std;

int main()
{
    map<string,string>m;
    string z;
    while(1)
    {
        getline(cin,z,'\n');
        if(z=="")break;
        int pos=z.find(" ");
        string x=z.substr(pos+1);
        string y=z.substr(0,pos);
        m.insert(pair<string,string>(x,y));
        //cout<<"efefe"<<endl;
    }
    string s;
    while(cin>>s)
    {
        map<string,string>::iterator it;
        it=m.find(s);
        if(it!=m.end())
            cout<<it->second<<endl;
        else
            cout<<"eh"<<endl;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值