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 题目大意: 输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一些 ...
随机推荐
- 谷歌被墙后,能够搜索的ip地址
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/langresser/article/details/32339707 http://209.116. ...
- 微信开发 api 需要 https 服务器
微信开发 api 需要 https 服务器 先建一个环境,本地的 https 服务器. 以下这篇不错,很完整. https://zhuanlan.zhihu.com/p/23640321
- bzoj 1043 [HAOI2008]下落的圆盘——圆的周长
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1043 算每个圆被它后面的圆盖住了多少圆弧即可.注意判断这个圆完全被后面盖住的情况. #inc ...
- 在 myeclipse中进行连接sql server的测试
在 myeclipse中,连接 sql server 用的 url connection 与 java 代码 连接的 url值完全相同. (一下为 java的jdbc连接 sql server 成功的 ...
- Mysql auto_increment总结
一.为什么InnoDB表要建议用自增列做主键 我们先了解下InnoDB引擎表的一些关键特征: InnoDB引擎表是基于B+树的索引组织表(IOT): 每个表都需要有一个聚集索引(clustered i ...
- lnmp环境自动化部署
lnmp.sh #!/bin/bash#This project to install lnmp#Author:菜逼cd命令玩家#Time:2016.10.13#objective:简化人工手动操作, ...
- HBase快速上手
一.创建单节点HBase实例 https://hbase.apache.org/book.html#quickstart (一)jdk版本要求Java: HBase Version JDK 7 JDK ...
- 压测工具Locuse的使用
我是听朋友提起的"蝗虫"(Locust),然而她不想用python,我就拿来试一试~ http的 各种压测工具也已经太多了,所以主要是试试locust在当前比较流行的rpc协议上的 ...
- 深入浅出 Java Concurrency (8): 加锁的原理 (Lock.lock)
接上篇,这篇从Lock.lock/unlock开始.特别说明在没有特殊情况下所有程序.API.文档都是基于JDK 6.0的. public void java.util.concurrent.lock ...
- 关于where和having的直观理解
一,查询区别 where是对前面select的字段没有要求,直接查询库表的 having是对前面的select的字段有要求,字段已经select出来的 可以用having进行处理 select id, ...