time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 newis 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
input

Copy
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
output

Copy
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

题意:输入n行,每行两个单词代表旧昵称和新昵称,问一共有几个用户更改昵称,输出这些用户的初始昵称和最终昵称

题解:map匹配:把新昵称最为键值,如果旧昵称匹配过,更新匹配

#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<map>
#include<set>
#define ll long long
using namespace std;
map<string,string>m;
string s,ss;
int n;
int main()
{
cin>>n;
while(n--)
{
cin>>s>>ss;//s旧名字,ss新名字
if(m.count(s)==)
m[ss]=s;
else
{
m[ss]=m[s];
m.erase(s);
}
}
cout<<m.size()<<endl;
map<string,string>::iterator it;
for(it=m.begin();it!=m.end();it++)
cout<<it->second<<' '<<it->first<<endl; return ; }

B. Misha and Changing Handles的更多相关文章

  1. ACM Misha and Changing Handles

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  2. 【CodeForces - 501B 】Misha and Changing Handles(map)

    Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description  神秘的三角洲里还有一个传说 ...

  3. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  4. Misha and Changing Handles

    Description Misha hacked the Codeforces site. Then he decided to let all the users change their hand ...

  5. 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 ...

  6. CodeForces 501B - Misha and Changing Handles

    有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...

  7. codeforces 501 B Misha and Changing Handles 【map】

    题意:给出n个名字变化,问一个名字最后变成了什么名字 先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first 写一下样例就好理解 ...

  8. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. 【剑指Offer面试编程题】题目1355:扑克牌顺子--九度OJ

    题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...

  2. 关于c++的头文件和变量声明

    写再最前面:摘录于柳神的笔记:   C++的头⽂件⼀般是没有像C语⾔的 .h 这样的扩展后缀的,⼀般情况下C语⾔⾥⾯的头⽂件去掉 .h 然 后在前⾯加个 c 就可以继续在C++⽂件中使⽤C语⾔头⽂件中 ...

  3. SpringBoot + redis + @Cacheable注解实现缓存清除缓存

    一.Application启动类添加注解 @EnableCaching 二.注入配置 @Bean public CacheManager cacheManager(RedisTemplate redi ...

  4. 阿里分布式开放消息服务(ONS)原理与实践——笔记整理

    1.MQ场景    1)订单异步解耦    2)解决分布式事务问题    3)应用于聊天平台    4)大规模机器的Cache同步    5)MySQL BinLog订阅数据分发2.ONS应用场景  ...

  5. 个人相关API - 行驶方向判断函数

    calculateDrivingDirection(newCoordinates,oldCoordinates){ let o = { direction: '', deviation:null, i ...

  6. get your sqlserver database back by using EMC NW NMM

    Dear all Yes ~ We can backup our sqlserver by EMC NW NMM. That is true and NW is a very very powerfu ...

  7. Windows一键启动多个软件

    @echo off title 启动工作环境 @echo 正在启动FileZilla%start+空格+/d+空格+目录路径+空格+程序名 % start /d"F:\安装包\FileZil ...

  8. red hat 7、centos7的root密码破译

    一.在开机画面时按"E". 二.找到linux16开头的这段,在段尾添加空格"rd.break"然后按Ctrl+x进入系统紧急救援模式. 三.新的界面出现命令行 ...

  9. 记一次关于NVROM中遇到的“Could not prepare Boot variable:No space left on device”问题的解决历程

    注:关于我电脑遇到的问题,不是一两句话能够说清楚的.为了能够比较完整的呈现问题的某些细节,在这篇博客中我会添加许多问题发生的背景,如果当中有观点与您的三观不合,请立即停止阅读,及时止损. 注:此篇文章 ...

  10. P1002 A+B for Polynomials (25分)

    1002 A+B for Polynomials (25分)   This time, you are supposed to find A+B where A and B are two polyn ...