您的位置:首页 > 其它

Codeforces 501B:Misha and Changing Handles(略水+小小技巧)

2016-09-28 22:37 399 查看
B. Misha and Changing Handles

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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 handlenew 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 andnew,
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.

Examples

input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov


output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123


题目大意:给你n对字符串,代表某个id1修改成了id2,让你输出总共的用户人数,输出他们最开始的id和最新的id。
解题思路:边输入,边更新,详细请看代码注释。
代码如下:
#include <cstdio>
#include <cstring>
struct node
{
char old[25];//旧的
char niu[25];//新的
}a[1010];
int main()
{
int n;
scanf("%d",&n);
int num=0;//记录用户个数
for(int i=0;i<n;i++)
{
char s1[25],s2[25];
scanf("%s",s1);
scanf("%s",s2);
int flag=0;
for(int j=0;j<num;j++)//在已知的用户里面查找是否修改为最新id
{
if(strcmp(a[j].niu,s1)==0)
{
flag=1;
strcpy(a[j].niu,s2);
}
}
if(flag==0)//找不到,那么新建一个用户
{
strcpy(a[num].old,s1);
strcpy(a[num].niu,s2);
num++;
}
}
printf("%d\n",num);
for(int i=0;i<num;i++)
{
printf("%s %s\n",a[i].old,a[i].niu);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  技巧 水题