Codeforce B. Misha and changing Handles

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.