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. 批量升级BMC固件asu64、ipmitool

    需求:通过服务器远程管理IP批量升级IMM.UEFI固件 工具:asu64.ipmitool.iflash64.cdc_interface.sh 下载:http://pan.baidu.com/s/1 ...

  2. linux系统中中断已连接的用户

    1.用w命令查看当前系统登录的用户 [root@rhel7 ~]# w :: up :, users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOG ...

  3. jsp页面中定时的方法

    $(function(){ totaladd(); //定时时触发的函数 setInterval(totaladd,3000);//设置定时1000=1秒 }); function totaladd( ...

  4. hibernate注解原理

    持续更新中.. hibernate注解用的是java注解,用到的是java反射机制.

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

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

  6. oracle 存储过程,函数和包

    创建存储过程: 语法:create [or replace] PROCEDURE 过程名(参数列表)  AS PLSQL子程序体: 调用 存储过程的方式 两种1.execute(exec)     - ...

  7. 武汉科技大学ACM :1003: A+B for Input-Output Practice (III)

    Problem Description Your task is to Calculate a + b. Input Input contains multiple test cases. Each ...

  8. hadoop实现共同出现的单词(Word co-occurrence)

    共同出现的单词(Word co-occurrence)是指在一个句子中相邻的两个单词.每一个相邻的单词就是一个Co-Occurrence对. Sample Input: a b cc, c d d c ...

  9. 1.3.1 switch 语句中的 String

    switch语句是一种高效的多路语句,可以省掉很多繁杂的嵌套if判断: 在Java 6及之前,case语句中的常量只能是byte.char.short和int(也可以是对应的封装类)或枚举常量,在Ja ...

  10. C#中HashTable和快速排序的用法

    题目主要是写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来.   自从周四拿到题目以后,发现又要用到万恶的数据结构了,不得不说这是我的短板,所有上周20号到 ...