Title Link: Http://codeforces.com/contest/501/problem/B
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 the Non-empty strings old andnew, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new is distinct. The lengths of the strings does not exceed.
The requests is given chronologically. In other words, by the moment of a query there are a single person with handleold, and handlenew are not u Sed and have not been used by anyone.
Output
The first line output the integer n -the number of users, changed their handles at least once.
The next n lines print the mapping between the old and the new handles of the users. Each of them must contain-strings, old andnew, separated by a space, meaning that before the user H Ad handleold, and after all the requests is completed, he handle isnew. You may output lines on any order.
Each user is changes the handle must occur exactly once in this description.
Sample Test (s) Input
<pre>5misha Ilovecodeforcesvasya Petrovpetrov vasyapetrov123ilovecodeforces MikeMirzayanovPetya Ivanov
Output
3Petya Ivanovmisha Mikemirzayanovvasya VasyaPetrov123
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; const int maxn=1005; int main () { string name1[maxn],name2[maxn]; string s1,s2; int m; cin>>m; int cnt=0; while (m--) { cin>>s1>>s2; int gg=0; for (int i=1;i<=cnt;i++) if (S1==name2[i]) { gg=i; break; } if (!gg) { name1[++cnt]=s1; NAME2[CNT]=S2; Continue; } name2[gg]=s2; } printf ("%d\n", CNT); for (int i=1;i<=cnt;i++) cout<<name1[i]<< "" <<Name2[i]<<endl; return 0; }
Codeforce B. Misha and changing Handles