CodeForces 501B Misha and Changing Handles(STL map)
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.
Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.
Input
The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.
Next q lines contain the descriptions of the requests, one per line.
Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.
The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.
Output
In the first line output the integer n — the number of users that changed their handles at least once.
In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.
Each user who changes the handle must occur exactly once in this description.
Examples
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123 题目意思:
有N个改名的动作,输出改完名的最终结果。 分析:
利用map将key作为新名字,value作为旧名字,使其一一对应
注意好好体会map的用法
自己也看了一会才看明白
code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long LL;
int main()
{
//利用map将key作为新名字,value作为旧名字,使其一一对应。
string str1,str2;
int n;
cin>>n;
map<string,string>mm;
map<string,string>::iterator it;
while(n--)
{
cin>>str1>>str2;
if(mm.find(str1)==mm.end())//如果旧名字不存在,那么直接将这一对存储在map中
{
mm[str2]=str1;
}
else
{
//如果就名字存在,那么将当前的新名字和旧名字所对应的更旧的名字作为一对存储在map中
mm[str2]=mm[str1];
mm.erase(str1);//通过key删除
}
}
int cont = mm.size();
cout << cont << endl;
for(it=mm.begin(); it!=mm.end(); it++)
{
cout << it->second << " " << it->first << endl; }
return ;
}
CodeForces 501B Misha and Changing Handles(STL map)的更多相关文章
- CodeForces 501B - Misha and Changing Handles
有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...
- 【CodeForces - 501B 】Misha and Changing Handles(map)
Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description 神秘的三角洲里还有一个传说 ...
- CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)
ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from ...
- HDU 4585 Shaolin(STL map)
Shaolin Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit cid= ...
- HDU 4585 Shaolin (STL map)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 2094 产生冠军(STL map)
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【UVA】10391 Compound Words(STL map)
题目 题目 分析 自认已经很简洁了,虽说牺牲了一些效率 代码 #include <bits/stdc++.h> using namespace std; set <s ...
- HDU 1263 水果 (STL map)
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...
随机推荐
- TOJ 3184 Mine sweeping
描述 I think most of you are using system named of xp or vista or win7.And these system is consist of ...
- 3d旋转卡牌
做成向中心缩放就行了,和旋转效果一样的
- Spring Security怎样不让默认的ProviderManager清除密码等信息
<authentication-manager erase-credentials="false"> ... </authentication-manager&g ...
- windows环境下MySQL-5.7.12-winx64下载安装与配置
系统:64位Win-7 官网压缩包:mysql-5.7.12-winx64.zip 前后花了一些时间,以前都是下载软件直接安装在本地,现在这个不一样,下载压缩包后要解压缩到安装目录,然后在控制台下配置 ...
- js中的break,continue,return
js中的break,continue, return (转) 面向对象编程语法中我们会碰到break ,continue, return这三个常用的关键字,那么关于这三个关键字的使用具体的操作是什么呢 ...
- WPF MVVM 如何在ViewModel中操作View中的控件事件
(在学习Wpf的时候,做一个小例子,想在TextBox改变后,检验合法性,并弹出提示.在找了很多贴后,发现这个小例子,抄袭过来,仅供参考. 最后也找到了适合自己例子的办法:在出发TextChanged ...
- 2、Dubbo源码解析--服务发布原理(Netty服务暴露)
一.服务发布 - 原理: 首先看Dubbo日志,截取重要部分: 1)暴露本地服务 Export dubbo service com.alibaba.dubbo.demo.DemoService to ...
- 移动Web布局
移动Web开发之移动页面布局 前言 本文针对手机设备设计的页面,并非兼容全设备的响应式布局,常见的MobileWeb页面如H5页面.手机页面.WAP页.webview页面等等.在不同尺寸的手机设备上, ...
- Oracle数据库错误消息
Oracle数据库错误消息 导出错误消息 l EXP-00000导出终止失败 原因:导出时产生Oracle错误. 操作:检查相应的Oracle错误消息. l EXP-00001数据域被截断 - 列长度 ...
- cordova 开发 android app 简要流程
1. 安装cordova:npm install -g cordova --registry=https://registry.npm.taobao.org 2. 创建cordova工程:进入工作目录 ...