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加上 ...
随机推荐
- Rabbitmq~对Vhost的配置
rabbitmq里有一些概念我们要清楚,如vhost,channel,exchange,queue等,而前段时间在部署rabbitmq环境时启用了虚拟主机vhost,感觉他主要是起到了消息隔离的作用, ...
- 【ExtJS】关于自定义组件
一.命名规范 在你编码过程中对类,名字空间以及文件名使用统一的命名规则对你代码的组织,结构化以及可读性有很大的好处. 1.类命名规范: 类名最好只包含字母,在多数情况下,数字是不鼓励使用的,除非非要用 ...
- 关于Json字符串"反序列化Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path..."
描述的很清楚就是说给它的不是一个对象,而是一个数组,所以他在建议你用JArray去解析,但是你明明就是给它的一个对象,并不是一个数组 这是我下意识的去把我的json字符串中的"[ ]&quo ...
- fabric 自动创建仓库并下载或更新代码
#!/usr/bin/python # -*- coding: utf-8 -* from fabric.api import * from fabric.contrib.files import * ...
- 类变量方法,局部变量和成员变量的区别(this关键字的使用)
变量名首写字母使用小写,如果由多个单词组成,从第2个单词开始的其他单词的首写字母使用大写. 如果局部变量的名字和成员变量的名字相同, 要想在该方法中使用成员变量,必须使用关键字this class P ...
- javaSE练习13——(知识点:类的继承 方法的覆盖)
设计2个类,要求如下: (知识点:类的继承 方法的覆盖)1.定义一个汽车类Vehicle, 1.1 属性包括:汽车品牌brand(String类型).颜色color(String类型 )和速度spee ...
- CMDB认识和需求分析
一.认识ITIL ITIL即IT基础架构库(Information Technology Infrastructure Library,信息技术基础架构库)由英国政府部门CCTA(Central ...
- 【代码笔记】Java Web初入:XML的进一步深入了解
2015-12-25 文件名 guojia.xml <?xml version="1.0" encoding="GB2312"?> <! ...
- Visual Studio Code 设置中文语言版本
设置方法有两种: 方法一1.选择扩展 搜索“Language”,在下列选项选择 Chinese (Simplified) Language Pack for Visual Studio Code安装, ...
- 关于ButterKnife 设置全局配置
先在项目的根目录的build.grade添加:classpath'com.neenbedankt.gradle.plugins:android-apt:1.8' 然后在app build.grade ...