http://codeforces.com/problemset/problem/499/B

B. Lecture
 
 

You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.

You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.

You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.

You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.

Input

The first line contains two integers, n and m (1 ≤ n ≤ 3000, 1 ≤ m ≤ 3000) — the number of words in the professor's lecture and the number of words in each of these languages.

The following m lines contain the words. The i-th line contains two strings aibi meaning that the word ai belongs to the first language, the word bi belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once.

The next line contains n space-separated strings c1, c2, ..., cn — the text of the lecture. It is guaranteed that each of the strings cibelongs to the set of strings {a1, a2, ... am}.

All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters.

Output

Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.

Sample test(s)
input
4 3
codeforces codesecrof
contest round
letter message
codeforces contest letter contest
output
codeforces round letter round
input
5 3
joll wuqrd
euzf un
hbnyiyc rsoqqveh
hbnyiyc joll joll euzf joll
output
hbnyiyc joll joll un joll

题解:题目很简单,就是给两组字符串映射,再给你一串字符串,对于串的每个单词,都在映射对儿里选择长度最短的那个。之所以这里还要写解题报告,是因为我wa了一发,wa在map的find的概念上。对于字符串,map里存的是一个地址值。我代码里字符串是new出来的,同一个字符串,前后两次地址值不同,直接find是找不到的,需要遍历,且用strcmp比较。

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <map> using namespace std; const int N=;
map<char*,char*> ma;
map<char*,char*>::iterator it;
int n,m; int main(){
char* s,*t;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
s=new char[N];
scanf("%s",s);
t=new char[N];
scanf("%s",t);
ma[s]=t;
}
for(int i=;i<=n;i++){
s=new char[N];
scanf("%s",s);
int l1=strlen(s);
for(it=ma.begin();it!=ma.end();it++){//需要注意一点就是,这里不能直接用it=ma.find(s)。因为ma里存的都是字符串的地址值,而s是new出来的,即便字符串内容一样,但是地址值是不同的,find的结果一定是end()。
if(!strcmp(s,it->first))//这里只能用strcmp,而不能用==。
break;
}
t=it->second;
int l2=strlen(t);
if(l1>l2) printf("%s",t);
else printf("%s",s);
if(i==n) puts("");
else putchar(' ');
}
return ;
}

cf499B-Lecture 【map】的更多相关文章

  1. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  2. 01 语言基础+高级:1-6 集合_day04【Map】

    day04 [Map] 主要内容 Map集合 教学目标 能够说出Map集合特点 使用Map集合添加方法保存数据 使用”键找值”的方式遍历Map集合 使用”键值对”的方式遍历Map集合 能够使用Hash ...

  3. 【Map】MapTest

    package cn.itcast.p1.map.test; import java.util.HashMap; import java.util.Map; public class MapTest2 ...

  4. 【Map】获取字符串中,每一个字母出现的次数

    package cn.itcast.p1.map.test; import java.util.Iterator; import java.util.Map; import java.util.Tre ...

  5. HDU 4941 Magical Forest 【离散化】【map】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点.每一个点有一个数值.点的xy坐标是0~10^9.点存在于矩阵中.然后 ...

  6. [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】

    题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...

  7. 【Map】Double Queue

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13258   Accepted: 5974 Des ...

  8. 【map】p1184 高手之在一起

    题目背景 高手是可以复活的,这点我们大家都知道. 题目描述 高手列出了一个详尽的日程表,这次他要追求的则是一个心灵纯洁的小萝莉.他和她都是要上课的,但是也会有时间空闲,于是高手决定无时无刻都要跟着她. ...

  9. UVa-156 Ananagrams 反片语【map】【vector】

    题目链接:https://vjudge.net/contest/211547#problem/D 题目大意: 输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一些 ...

随机推荐

  1. Let’s Encrypt 将于 2018 年免费提供通配符证书

    旨在让每个网站都启用 HTTPS 加密的 Let's Encrypt CA 宣布将于 2018 年 1 月免费提供通配符证书(Wildcard certificate).通配符证书是一种可被多个子域使 ...

  2. input type=number 禁止输入字符“e”的办法

    输入框input,的type设置为number,本想只输入数字,但是字符“e”却能通过, 首先科普一下, <body> <input onkeypress="getCode ...

  3. 面向对象(PHP学习)

    在对超大型项目的开发过程中,如果使用面向过程地开发,代码量是非常的庞大,这将大量的用到判断和循环嵌套, 和很多很相似的代码,不仅使项目代码量更加的庞大,还不利于开发,重用及维护. 面向对象就能很好的解 ...

  4. golang里面检测对象是否实现了接口的方法

    写法有点怪异,记一下吧 _, implemented := this.delegate.(IGenTcpServerDelegate) if implemented { this.delegate.G ...

  5. nginx限制请求之二:(ngx_http_limit_req_module)模块

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  6. 基于python的测试框架behave----context

    使用behave时,每一步对应的方法都需要参数context,那么context是什么,为什么都需要传这个对象? 在所有用例中的context都是behave.runner.Context类的一个实例 ...

  7. MongoDB day01

    MongoDB芒果数据库 数据存储阶段 文件管理阶段(.txt .doc .xlc) 优点:数据可以长期保存:数据有一定格式化规范:可以大量存储:使用简单方便 缺点:数据一致性差:用户查找修改不方便: ...

  8. CentOS 7 安装Nginx 并配置自动启动

    1.官网下载安装包 http://nginx.org/en/download.html,选择适合Linux的版本,这里选择最新的版本,下载到本地后上传到服务器或者centos下直接wget命令下载. ...

  9. java之IO整理(下)

    一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...

  10. android:修改PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色

    1.修改PagerTabStrip中的背景颜色 我们在布局中直接设置background属性即可: <android.support.v4.view.ViewPager android:id=& ...