您的位置:首页 > 其它

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
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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: