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. springboot 模板

    参考:https://blog.csdn.net/wangb_java/article/details/71775637

  2. Python的常用库

    读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...

  3. Systemverilog for design 笔记(一)

    转载请标明出处 一.     System Verilog 声明的位置 1.       包(packages) Verilog要求局部声明: variables, nets, tasks and f ...

  4. Linux centosVMware Tomcat介绍、安装jdk、安装Tomcat

    一.Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开 ...

  5. redhat 7.6 VI编辑操作

    模式一: 浏览模式 0 : 光标到行首 $ : 光标到行尾 gg:光标到首行 G:光标到尾行 yy:复制光标所在行 dd:剪切光标所在行,删除行 y11y:复制光标所在行,往下数,一共10行 p:粘贴 ...

  6. java中内部类的实例化

  7. Node.js之EventEmiter

    参考: 1. Node.js之EventEmiter讲解 2. Node.js之events事件 3. 在线运行node.js代码 Node.js主要API使用异步事件驱动模型,异步I/O操作完成时, ...

  8. 页面自动执行js的3种方法

    1.最简单的调用方式,直接写到html的body标签里面:   <html> <body onload="load();"> </body> & ...

  9. 吴裕雄--天生自然JAVAIO操作学习笔记:单人信息管理程序

    import java.io.* ; public class ExecDemo03{ public static void main(String args[]) throws Exception{ ...

  10. java并发AtomicIntegerFieldUpdater

    java并发AtomicIntegerFieldUpdater 支持对象的成员变量原子操作类由AtomicIntegerFieldUpdater,AtomicLongFieldUpdater, Ato ...