码迷,mamicode.com
首页 > 其他好文 > 详细

codeforce B. Misha and Changing Handles

时间:2015-01-13 15:59:23      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:     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 of two non-empty strings old andnew, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Stringsold and new are distinct. The lengths of the strings do not exceed20.

The requests are given chronologically. In other words, by the moment of a query there is a single person with handleold, and handle new 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 and new, separated by a space, meaning that before the user had handleold, and after all the requests are completed, his handle isnew. You may output lines in any order.

Each user who changes the handle must occur exactly once in this description.

Sample test(s)
Input
<pre>5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov

Output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya 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

标签:

原文地址:http://blog.csdn.net/u013514722/article/details/42677195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!