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

Input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
Output
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)的更多相关文章

  1. CodeForces 501B - Misha and Changing Handles

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

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

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

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

  4. HDU 4585 Shaolin(STL map)

    Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit cid= ...

  5. HDU 4585 Shaolin (STL map)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  6. HDU 2094 产生冠军(STL map)

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 【UVA】10391 Compound Words(STL map)

    题目 题目     分析 自认已经很简洁了,虽说牺牲了一些效率     代码 #include <bits/stdc++.h> using namespace std; set <s ...

  8. HDU 1263 水果 (STL map)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

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

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

随机推荐

  1. opensuse13.2国内源和设置命令

    ustc-osshttp://mirrors.ustc.edu.cn/opensuse/distribution/13.2/repo/oss/ustc-non-osshttp://mirrors.us ...

  2. elasticsearch 2.4.0执行update的时候发现的一个问题

    请关注inline参数的变化 正确: POST /test/type1/1/_update{ "script" : { "inline": "ctx. ...

  3. HAProxy advanced Redis health check---ref

    http://blog.exceliance.fr/2014/01/02/haproxy-advanced-redis-health-check/ HAProxy advanced Redis hea ...

  4. 微信小程序开发踩坑记录

    1.由于小程序wx.request()方法是异步的,在app.js执行ajax后,各分页加载app.js的全局数据时,无法按顺序加载.例: //app.js App({ ajax:function() ...

  5. axios请求报Uncaught (in promise) Error: Request failed with status code 404

    使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...

  6. WPF-MVVM学习心德(WinForm转WPF心德)

    接触MVVM接近一段时间了,有一点理解,写下来. 之前是做winform的,工作需要,学习wpf.优缺点就不用说类,网上一大堆.我自己理解的话,有下面几点: 1.首先是界面的xmal和界面分离:wpf ...

  7. 分布式环境Tomcat多节点集群下共享目录配置,tomcat虚拟目录+nfs

    我们可能有这种场景: 集群环境下,多个web容器需要请求一个共享目录下的文件,比如保存图片或者录音文件,任意一个节点保存后其他节点需要及时获取,此时就需要目录进行同步了,否则Nginx负载到任意一个节 ...

  8. SQL Server 2008 R2如何开启数据库的远程连接(转)

    SQL Server 2008默认是不允许远程连接的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,远程连接数据库.需要做两个部分的配置: SQL Server Manag ...

  9. scope 作用域(bean 的生存范围)

    默认是 singleton ,单例模式,如下代码: @Test public void testAddUser() throws Exception { ApplicationContext ctx ...

  10. 国家与大洲对应关系json数据

    [ { "continent_cname": "欧洲", "continent_name": "EU", "c ...