Problem - 501B - Codeforces(string+map)
2015-01-12 21:14
357 查看
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.
Sample test(s)
input
output
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.
Sample test(s)
input
5 Misha ILoveCodeforces Vasya Petrov Petrov VasyaPetrov123 ILoveCodeforces MikeMirzayanov Petya Ivanov
output
3 Petya Ivanov Misha MikeMirzayanov Vasya VasyaPetrov123 <pre name="code" class="cpp">/* * author : qiu * coding : utf-8 * time : 2015-1-12 * */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <map> #define debug puts("-----") #define pi (acos(-1.0)) #define eps (1e-8) #define inf (1<<28) using namespace std; map<string, string> mp, tmp; vector<string> ve; int main(){ string s1, s2; int p; while(cin >> p){ for(int i=0; i<p; i++){ cin >> s1 >> s2; if(i == 0) tmp[s1]=s2; map<string, string>::iterator it; int flag=0; for(it = mp.begin(); it != mp.end(); ++it){ string str=it->second; if(str == s1){ tmp[it->first]=s2; flag = 1; } } if(!flag) tmp[s1]=s2; mp = tmp; } tmp.clear(); map<string, string>::iterator it; cout << mp.size() << endl; for(it = mp.begin(); it != mp.end(); ++it){ tmp[it->second]=it->first; } // 后面题目修改,按new name排 map<string, string>::iterator ie; for(ie = tmp.begin(); ie != tmp.end(); ++ie){ cout << ie->second << " " << ie->first << endl; } } return 0; }
相关文章推荐
- Problem - 499B - Codeforces(string+map)
- 2018.2.2【 CodeForces - 501B 】解题报告(STL,map,字符串处理)
- 2016"百度之星" - 资格赛(Astar Round1)Problem D(map+string)
- codeforces 789/problem/B [map][坑]
- codeforces 877 problem B Nikita and string 【思维 + 前后缀思想】
- codeforces 499B Lecture(map与string简单用法)
- codeforces 501 B Misha and Changing Handles 【map】
- UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
- BestCoder Round #48 ($)HDU5284 wyh2000 and a string problem
- codeforces 651C(map、去重)
- codeforces 803G Periodic RMQ Problem
- Codeforces 889B (Codeforces Round #445 Div. 1) Restoration of string 图论
- 【CodeForces123D】String
- codeforces 56D Changing a String (dp编辑距离)
- Volume 1. Elementary Problem Solving :: String Uva 401,10010,10361,537,409
- jsp遍历List<Map<String,Object>>,输出<Map<String,Objectr>>
- hihoCoder 1260 String Problem I 【字典树 + DFS】
- codeforces710E Generate a String(dp)
- 【codeforces 749A】Bachgold Problem
- Codeforces 890 D. Restoration of string (技巧)