首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】501B - Misha and Changing Handles(STL - string & vector & pair)

【CodeForces】501B - Misha and Changing Handles(STL - string & vector & pair)

作者头像
FishWang
发布2025-08-26 14:39:32
发布2025-08-26 14:39:32
15600
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

B. Misha and Changing Handles

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 handlenew 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 andnew, 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

代码语言:javascript
代码运行次数:0
运行
复制
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

output

代码语言:javascript
代码运行次数:0
运行
复制
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

用pair<string , string>记录改变前和改变后的值,存在vector中,每输入一个数就一次遍历、修改。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
int main()
{
	int n;
	string a,b;
	vector<pair<string,string> > d;
	scanf ("%d",&n);
	while (n--)
	{
		cin >> a >> b;
		bool flag = false;
		for (int i = 0 ; i < d.size() ; i++)
		{
			if (d[i].se == a)
			{
				d[i].se = b;
				flag = true;
			}
		}
		if (!flag)		//没有可以改名的 
			d.push_back(make_pair(a,b));
	}
	printf ("%d\n",d.size());
	for (int i = 0 ; i < d.size() ; i++)
		cout << d[i].fi + ' ' + d[i].se << endl;
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档