词典
你刚从滑铁卢搬到了一个大城市,这里的人们讲一种难以理解的外语方言。幸运的是,你有一本字典来帮助你理解它们。
输入格式:
输入第一行是正整数N和M,后面是N行字典条目(最多10000条),然后是M行要翻译的外语单词(最多10000个)。每一个字典条目都包含一个英语单词,后面跟着一个空格和一个外语单词。 输入中的每个单词都由最多10个小写字母组成。
输出格式:
输出翻译后的英文单词,每行一个单词。非词典中的外来词汇输出“eh”。
输入样例:
5 3
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
输出样例:
cat
eh
loops
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int N=10010;
int n,m;
map<string,string> dic;
map<string,string> ::iterator it;
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
{
string a,b;
cin>>a>>b;
dic[b]=a;
}
for(int i=0;i<m;i++)
{
string x;
cin>>x;
if(dic[x]!="\0")
cout<<dic[x]<<endl;
else
cout<<"eh"<<endl;
}
return 0;
}
map 使用一般方法:
map初始化默认值为< int , string >{0,"\0"}
C++ maps是一种关联式容器,包含“关键字/值”对和python中字典差不多
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数, (帮助评论区理解: 因为key值不会重复,所以只能是1 or 0)
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数