B. Misha and Changing Handles
1 second
256 megabytes
standard input
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.
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.
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.
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
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的更多相关文章
- ACM Misha and Changing Handles
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...
- 【CodeForces - 501B 】Misha and Changing Handles(map)
Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description 神秘的三角洲里还有一个传说 ...
- 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...
- Misha and Changing Handles
Description Misha hacked the Codeforces site. Then he decided to let all the users change their hand ...
- 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 ...
- CodeForces 501B - Misha and Changing Handles
有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...
- codeforces 501 B Misha and Changing Handles 【map】
题意:给出n个名字变化,问一个名字最后变成了什么名字 先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first 写一下样例就好理解 ...
- 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 ...
- 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 ...
随机推荐
- 将一行很长的js代码格式化输出方便查看
之前的一行js代码,有2万多字符,打开这个网址,粘贴到左边空白框,点下面格式化: 参考下面文章: 数千行的js代码变成了一行,如何复原,该换行的换行,该对齐的对齐_开发工具_小邯韩的博客-CSDN博客 ...
- 《SQL 进阶教程》 查找局部不一致的数据
-- 从下面这张商品表里找出价格相等的商品的组合 select * from products p1LEFT JOIN products p2on p1.price = p2.price and p1 ...
- Linux命令:sed命令
sed是一种流编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏 ...
- 使用mysql服务实现负载均衡
mysql 长连接(tcp协议)模拟负载均衡 1.存在member,member的port与mysql服务port一致为3306 2.vip绑定浮动ip 3.开启mysql服务: #service m ...
- Windows服务器权限分析
一.Windows常见用户 二.Windows常见用组 2.1Windows常见组 2.2Windows2003常见组 三.Windows目录权限 四.Windows2003默认权限 五.不同环境下的 ...
- 5.使用Redis+Flask维护动态Cookies池
1.为什么要用Cookies池? 网站需要登录才可爬取,例如新浪微博 爬取过程中如果频率过高会导致封号 需要维护多个账号的Cookies池实现大规模爬取 2.Cookies池的要求 自动登录更新 定时 ...
- 用C语言写一个Helloworld_实现第一步编译运行
编写第一个hello world 创建helloworld.c // 程序头文件 #include <stdio.h> // 主入口函数 int main(int arc, char* a ...
- 解决在高分屏下开发winform界面变形
Form.AutoScaleMode = AutoScaleMode.None; 需要在超大屏下显示的时候,再考虑 AutoScaleMode.Font; AutoScaleMode.Dpi;
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示成功的操作
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:在元素获取焦点时显示(如:键盘操作的用户)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...