CodeForces - 501B Misha and Changing Handles(map用法)

本文解析了一个关于 Codeforces 用户更改用户名逻辑的问题,通过分析请求处理过程,介绍了如何使用 map 数据结构跟踪用户的新旧用户名映射,并给出了 Python 和 C++ 的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/501/B点击打开链接


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 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
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

emm这道题一开始先用python写的 后面用c++写了下 发现一些问题 

1.不知是不是我理解题意有问题 偶然发现数据只有如同样例那样往后匹配而没有往前匹配 

如例子:

5
Misha ILoveCodeforces
Petrov VasyaPetrov123
Vasya Petrov
ILoveCodeforces MikeMirzayanov
Petya Ivanov

之所以发现这个问题 因为我用c++写的代码没有处理第一个字符串的值得存在 上面那个例子处理错误 但是却A了。。

2.用map的问题:键的类型为string 值也为string时 当键不存在时 查询该键对应值为空串 即\0 

3.同样出现在查询时的问题 当查询不存在的键值之后 map会为你自动创建这个键为你所查询的键 值为空的一个元素 

这个可以再下面的例子中得到验证:

#include <bits/stdc++.h>
using namespace std;
map<string,string> mmap;
int main()
{
	if(mmap["1"]=="")
		cout << mmap.size() <<endl;
}
这个的结果是1

这是用c++写的时候发现的问题

相比之下 python似乎更加优雅。。

为了处理c++这个问题 我会的方法只好重头跑一边。。 (不知道怎么处理这种情况) 不过只是增加常数复杂度

下面附两份代码

python:

x=int(input())
dic={}
s=[]
for i in range(x):
    mid=input()
    s.clear()
    s=[a for a in mid.split()]
    if s[1] in dic:
        ss=dic[s[1]]
        dic.pop(s[1])
        dic[s[0]]=ss
    else:
        flag=0
        for key,value in dic.items():
            if value==s[0]:
                dic[key]=s[1]
                flag=1
        if flag==0:
            dic[s[0]]=s[1]
print (len(dic))
for key,value in dic.items():
    print (key,end=" ")
    print(value)
c++:

#include <bits/stdc++.h>
using namespace std;
map <string,string> mmap;
map <string,string>::iterator it;
int main()  
{
	int t;
	cin >> t;
	while(t--)
	{
		string a;
		string b;
		cin >> a >> b;
		if(mmap[b]!="")
			{	
				for(it=mmap.begin();it!=mmap.end();it++)
				{
					if(it->first==b)
					{
						mmap.erase(it);
						break;
					}
				}
			}
		int flag=0;
		for(it=mmap.begin();it!=mmap.end();it++)
		{
			if(it->second==a)
			{
				it->second=b;
				flag=1;
				break;
			}
		}
		if(!flag)
			mmap[a]=b;
	}
	int flag=0;
	for(it=mmap.begin();it!=mmap.end();it++)
		if(it->second=="")
			flag++;
	cout << mmap.size()-flag << endl;
	for(it=mmap.begin();it!=mmap.end();it++)
		if(it->second!="")
			cout << it->first << " " << it->second << endl;
}  
因为自动增加的关系 只能用flag出去额外增加的个数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值