stl-map的一道很好的题目
Damn Single (25)
“Damn Single (单身狗)” is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=50000), the total number of couples. Then N lines of the couples follow, each gives a couple of ID’s which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer M (<=10000) followed by M ID’s of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.
Output Specification:
First print in a line the total number of lonely guests. Then in the next line, print their ID’s in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.
Sample Input:
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
Sample Output:
5
10000 23333 44444 55555 88888
思路:pat甲级的一道题目,个人的第一次看到用map容器的用法,map的用法主要用于标记,这道题个人感觉很不错,运用map,很好说明map的一些常用的用法。用法在代码注释。
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){ int n,count=0; string a,b; map<string,string> c; map<string,int> d; vector<string> re; cin>>n; for(int i=1;i<=n;i++){ cin>>a>>b; c.insert(pair<string,string>(a,b));//对a和b进行插入,说明a是key,b是value;c.insert(pair<string,string>(b,a)); //想反的在进行一个插入,a与b此时连在一起的感觉。} cin>>n; for(int i=1;i<=n;i++){ cin>>a; d[a]=1; //对a进行标记;} int flag=0; map<string,int> ::iterator it; for(it=d.begin();it!=d.end();it++){ if(d.count(c[it->first])==0){ //count函数说明value有没有出现,c【}it-》first 】的值是夫妻对方;主要要明白这里的it->first。count++; re.push_back(it->first); //map中的it->first是自动排序的。} } cout<<count<<endl; for(int i=0;i<re.size();i++){ cout<<re.at(i)<<(i==re.size()-1?'\n':' '); } return 0;
}
stl-map的一道很好的题目相关推荐
- hdu1426一道很有意思的题目:数独
Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行 ...
- zjzj只爱数学--一道很厉害的题目(原创)
题目背景 zjzjzjzjzjzj 大佬最喜欢的,就是找到一些红的数学题,把他们改变成蓝的,紫的,甚至黑的. 题目描述 有一天, zjzjzjzjzjzj 大(jù)佬(ruò) 看到了 fxtfxt ...
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)已跪~~~~(_)~~~~
某公司招聘的面试环节, 有这样一个题目:结构体作为STL map的key时需要注意什么? 对于懂STL map的同学来说, 这个题目还是比较easy的, 先看程序: #include <iost ...
- STL map 简介
STL map 简介 转载于:http://www.cnblogs.com/TianFang/archive/2006/12/30/607859.html 1.目录 map简介 map的功能 使用ma ...
- C++ STL map的使用
C++ STL map的使用 2009-12-11 作者:tanker1024 来源:tanker1024的blog 1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的 ...
- 学习STL map, STL set之数据结构基础
STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. C++ STL 之所以得到广泛的赞誉,也被很多人使用 ...
- STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树
STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树 最近在做项目时发现一个crash的问题,当时得到的dmp文件显示crash在一个以map为循环变量的循环中,crash位置在如下 ...
- 漫画:位运算技巧整理汇总+一道被嫌弃的题目
(这首歌挺喜欢的...分享出来) 今天是小浩算法"365刷题计划"第65天.这两天总有人来问我,做公众号赚了多少钱,或者就是怎么能和你一样,2个月就做到7000粉丝.说实话,至少到 ...
- C++ STL map的使用和性能分析
1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操 作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 ...
最新文章
- javascript 两个数组组成一个对象
- 语义分割--Attention to Scale: Scale-aware Semantic Image Segmentation
- 【Android 应用开发】Android游戏音效实现
- java并查集计算机网络连通,poj2236 Wireless Network(并查集)
- 140.String Compression
- 书评– Kubernetes Up&Running,作者:Kelsey Hightower
- 焦虑的移动互联网开发者如何破局?专题解析
- 设计灵感|元素拼接的海报到底好看在哪里?
- word双引号间距大_微软判定:Word 编辑英文句号后“两个空格”为拼写异常
- Linux下SVN搭建与配置
- JDK环境变量配置(win10)
- Ubuntu16.04 与本地Win7共享文件夹
- [Maven] 无法获取私服最新Jar包的问题
- linux iio 设备驱动,Linux设备驱动之IIO子系统——IIO框架数据读取
- 小学期学Java有感
- 网站下载工具有哪些?
- Myeclipse 是如何启动tomcat服务
- 计算机主机配置单2020,2020电脑配置清单
- asp.net mvc Html.BeginForm()用法
- 【python】爬取正在上映的豆瓣电影信息(etree、xpath)