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 题目大意: 输入一些单词,找出所有满足以下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一些 ...
随机推荐
- iptables规则绑定在port而不是拦截在协议栈
版权声明:本文为博主原创,无版权.未经博主同意能够任意转载,无需注明出处,任意改动或保持可作为原创! https://blog.csdn.net/dog250/article/details/2417 ...
- 安装xamp之后,appach、mysql等问题的总结
问题一:无法启动的问题 如果他们无法启动,大多数情况是端口号被占用. 首先就是查看端口号:点击“netstart“按钮查看端口号的使用详情 如果被占用就点击"config”按钮,进行端口号的 ...
- Qt学习笔记(1) hello world
Qt的简介: Qt是一个跨平台的C++ GUI库实现,原本只是以为它只提供一些图形接口,看来我还是低估了它,采用文档Qt学习之路2开始学习,不知道这个文档是不是有点老了,管他呢,先了解下. 搭建环境: ...
- Asp.net中的web.config配置文件(转)
最近开始学习.NET的开发,首先碰到的就是web.config的配置问题,把网上大虾的资料转发记录一下,以备不时之需. 原贴路径如下:http://blog.csdn.net/hbqhdlc/arti ...
- laravel的auth用户认证的例子
参考http://www.cnblogs.com/yjf512/p/4042356.html 需要注意的是,生成的测试数据,password部分必须用laravel自带的hash一下 Hash::ma ...
- JS Data 对象 成员方法
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- 利用反射及ActionFilterAttribute实现MVC权限管理
1.利用反射获取当前程序集下的所有控制器和方法,拼接后写入到数据库. public void GetRightInfo() { ; var controllerTypes = Assembly.Get ...
- codechef January Lunchtime 2017简要题解
题目地址https://www.codechef.com/LTIME44 Nothing in Common 签到题,随便写个求暴力交集就行了 Sealing up 完全背包算出得到长度≥x的最小花费 ...
- Django跳转
跳转问题 如果我现在停留在文章的详情页,用户未登陆,如果你要评论,或者点赞就应该回到登陆页面登陆 如果登陆成功了,就要返回到当初跳转过来的页面 第一种通过前后端传送数据 $('.hit').click ...
- jdbc调用sparksql on yarn
spark sql访问hive表 1.将hive-site.xml拷贝到spark目录下conf文件夹 2.(非必需)将mysql的jar包引入到spark的classpath,方式有如下两种: 方式 ...