Description

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 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 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 Input

Input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
Output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

思路:
逻辑层面没什么好想的,但是数据操作层面会出现很多的问题
(1)字符串数组的覆盖,如果被覆盖的字符串长度更长,则要在新字符串的后面加上'\0',不然容易出现奇怪的字母
(2)然后想思路的时候就不要局限在所给的数组,而要从题目的规则出发,自己构想出一般情况然后再从头开始推
(3)对于每个request的原始姓名而言,他每被修改一次,就给他压进栈一次,利用栈的原理和标记数组实现

#include <iostream>
#include <cstring>
#include <stack>
using namespace std; struct R{
char start[];
char end[];
int val;
}requests[];
stack<int> s;
int vis[]; void cover(char* str1,char* str2)
{
int len = strlen(str2);
for(int i = ;i < len;i++)
str1[i] = str2[i];
str1[len] = '\0';
} bool scmp(char* str1,char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 != len2) return false;
int flag = ;
for(int i = ;i < len1;i++)
if(str1[i] == str2[i]) flag++;
if(flag == len1) return true;
else return false;
} int main()
{
int n;
while(cin>>n)
{
int ans = ;
while(!s.empty())
s.pop();
memset(vis,,sizeof(vis));
for(int i = ;i <= n;i++)
{
cin>>requests[i].start>>requests[i].end;
requests[i].val = ;//i为起点
int flag = ;
for(int j = ;j < i;j++)
if(requests[j].val && scmp(requests[i].start,requests[j].end)) {
cover(requests[j].end,requests[i].end);
requests[i].val = ;
s.push(j);
flag = j;
break;
}
if(requests[i].val) {
ans++;
s.push(i);
}
}
if(requests[n].val) s.push(n);
cout<<ans<<endl;
while(!s.empty()) {
int tmp = s.top();
s.pop();
if(vis[tmp]) continue;
cout<<requests[tmp].start<<' '<<requests[tmp].end<<endl;
vis[tmp] = ;
} }
return ;
}

Misha and Changing Handles的更多相关文章

  1. ACM Misha and Changing Handles

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  2. 【CodeForces - 501B 】Misha and Changing Handles(map)

    Misha and Changing Handles CodeForces原题是英文,这里就直接上中文好了,翻译不是太给力,但是不影响做题 ^▽^ Description  神秘的三角洲里还有一个传说 ...

  3. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  4. B. Misha and Changing Handles

    B. Misha and Changing Handles time limit per test 1 second memory limit per test 256 megabytes input ...

  5. CodeForces 501B Misha and Changing Handles(STL map)

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  6. CodeForces 501B - Misha and Changing Handles

    有N个改名的动作,输出改完名的最终结果. 拿map做映射 #include <iostream> #include <map> #include <string> ...

  7. codeforces 501 B Misha and Changing Handles 【map】

    题意:给出n个名字变化,问一个名字最后变成了什么名字 先用map顺着做的,后来不对, 发现别人是将变化后的那个名字当成键值来做的,最后输出的时候先输出second,再输出first 写一下样例就好理解 ...

  8. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. ListView的优化

    1. ListView须要设置adapter,它的item是通过adapter的方法getView(int position, View convertView, ViewGroup parent)获 ...

  2. Cloudra公司CCP:DS——认证数据专家

    原文:http://vision.cloudera.com/24195/. 译文: 每天我都能看到大数据怎样改变我们生活的文章.数据科学家们正在生物医药领域找寻新的方法治愈癌症.帮助银行与欺诈做斗争, ...

  3. mysql 变量is null 和 not exists区别

    问题: 使用游标遍历时,发现使用 select var into tmp where var=? 然后判断if tmp is null时,不能走完所有的遍历.经debug发现, 当var为空时,则跳出 ...

  4. 拓扑排序 HDU1285

    这个题是个模板题,可以直接用拓扑排序的模板来做, AC代码 #include <stdio.h> #include<iostream> #include <string. ...

  5. spring03autowire属性

    1.创建需要的实体类 public class Student { //学生实体类 private String name; //姓名 private Integer age; //年龄 privat ...

  6. javascript MD5

    var MD5 = function (string) { function RotateLeft(lValue, iShiftBits) { return (lValue<<iShift ...

  7. sql 判断表、列、视图等是否存在

    1 判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名')     drop database [数据库名] 2 判 ...

  8. Lesson 3: The Amazing New Mobile Web

    Lesson 3: The Amazing New Mobile Web Article 1: This is Responsive by Brad Frost 各种响应式网站设计的资源. Artic ...

  9. java JNI 的实现(2)-java和C/C++的相互调用.

    目录 概述 一,java代码 二,稍微注意通过javah生成的'C/C++'.h头文件和源java代码的关系 三,在C/C++中实现java的native方法(完整C/C++) 1,修改age,即Ja ...

  10. HTML5 microdata

    schema.org 测试地址 http://www.google.com/webmasters/tools/richsnippets