Problem - 501B - Codeforces(string+map)
来源:互联网 发布:配电网优化运行 编辑:程序博客网 时间:2024/05/01 19:03
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 handlenew is 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 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.
5Misha ILoveCodeforcesVasya PetrovPetrov VasyaPetrov123ILoveCodeforces MikeMirzayanovPetya Ivanov
3Petya IvanovMisha MikeMirzayanovVasya 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 - 501B - Codeforces(string+map)
- Problem - 499B - Codeforces(string+map)
- codeforces 789/problem/B [map][坑]
- Codeforces Problem 708B Recover the String(implementation&math)
- codeforces 499B Lecture(map与string简单用法)
- Problem - 216B - Codeforces
- Problem - 218B - Codeforces
- codeforces problem/408/B
- Codeforces Problem - 50B
- Problem - 376B - Codeforces
- problem - 616B -codeforces
- Codeforces Problem 333B
- Codeforces Problem 332B
- 容器map的应用:CodeForces - 501B
- codeforces 128B. String
- codeforces 128B - String
- Codeforces 742B Arpa’s obvious problem and Mehrdad’s terrible solution(map水题)
- codeforces-Problem 193B - Xor
- C语言编译错误 fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory
- DeviceUuidFactory
- Matrix Factorization, Algorithms, Applications, and Avaliable packages
- 利用OpenCV + PyQt4操作摄像头的demo
- 微信NATIVE动态支付(V3版本)
- Problem - 501B - Codeforces(string+map)
- gnuplot 拟合 fit函数的用法
- 6、文件的路径问题、错误的处理
- 仿函数
- 黑马程序员——Mac OS X终端的常用操作命令(UNIX指令)
- day2:关于目标文件
- UVA1395 Slim Span(kruskal算法)
- mysql数据库设计-多对多
- linux shell 编程