cf499B-Lecture 【map】
http://codeforces.com/problemset/problem/499/B
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.
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 ai, bi 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 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.
4 3
codeforces codesecrof
contest round
letter message
codeforces contest letter contest
codeforces round letter round
5 3
joll wuqrd
euzf un
hbnyiyc rsoqqveh
hbnyiyc joll joll euzf joll
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】的更多相关文章
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- 01 语言基础+高级:1-6 集合_day04【Map】
day04 [Map] 主要内容 Map集合 教学目标 能够说出Map集合特点 使用Map集合添加方法保存数据 使用”键找值”的方式遍历Map集合 使用”键值对”的方式遍历Map集合 能够使用Hash ...
- 【Map】MapTest
package cn.itcast.p1.map.test; import java.util.HashMap; import java.util.Map; public class MapTest2 ...
- 【Map】获取字符串中,每一个字母出现的次数
package cn.itcast.p1.map.test; import java.util.Iterator; import java.util.Map; import java.util.Tre ...
- HDU 4941 Magical Forest 【离散化】【map】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 题目大意:给你10^5个点.每一个点有一个数值.点的xy坐标是0~10^9.点存在于矩阵中.然后 ...
- [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...
- 【Map】Double Queue
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13258 Accepted: 5974 Des ...
- 【map】p1184 高手之在一起
题目背景 高手是可以复活的,这点我们大家都知道. 题目描述 高手列出了一个详尽的日程表,这次他要追求的则是一个心灵纯洁的小萝莉.他和她都是要上课的,但是也会有时间空闲,于是高手决定无时无刻都要跟着她. ...
- UVa-156 Ananagrams 反片语【map】【vector】
题目链接:https://vjudge.net/contest/211547#problem/D 题目大意: 输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一些 ...
随机推荐
- python字符串常用
参考这一篇: http://www.runoob.com/python/python-strings.html
- 好用的 convert freestyle jenkins jobs to pipeline 插件使用
1. 安装 安装,重启即可,非常简单 2. 使用 原有配置,主要列出执行 生成的jenkins pipeline 3. 构建效果 4. 参考资料 https:// ...
- js 正则表达式符号含义
\ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个单词的边界. -或- 对正则表达式功 ...
- vue数据已渲染成 但还是报错 变量 undefined
问题:页面上的数据已渲染出来,但是控制台还是报错变量未undefined,主要是当页面加载完成后,数据并未加载完,所以会报次错误. 解决办法:在数据渲染的主节点(最外层的div)添加 v-if=“da ...
- Linux之 find之 ctime,atime,mtime
在Linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime atime 最后一次访问时间, 如 ls, more 等, 但 chmod, chow ...
- bzoj 2039 [2009国家集训队]employ人员雇佣——二元关系
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 用最小割看.对于一组关系 i , j ,如果都选,收益 2*Ei,j,可以看作0,作为 ...
- mysql大数据量之limit优化
背景:当数据库里面的数据达到几百万条上千万条的时候,如果要分页的时候(不过一般分页不会有这么多),如果业务要求这么做那我们需要如何解决呢?我用的本地一个自己生产的一张表有五百多万的表,来进行测试,表名 ...
- Renesas CAT CONFIG
CAT CELL "sf_cellular_api.h" typedef enum e_sf_cellular_at_cmd_index { SF_CELLULAR_AT_CMD_ ...
- .NET System.Web.HttpContext.Current.Request报索引超出数组界限。
移动端使用Dio发送 FormData, 请求类型 multipart/form-data, FormData内可以一个或多个包含文件时. 请求接口时获取上传的fomdata数据使用 System.W ...
- zookeeper基本讲解(Java版,真心不错)
1. 概述 Zookeeper是Hadoop的一个子项目,它是分布式系统中的协调系统,可提供的服务主要有:配置服务.名字服务.分布式同步.组服务等. 它有如下的一些特点: 简单 Zookeeper的核 ...