Codeforces Round #285 (Div. 2) ---B. Misha and Changing Handles

来源:互联网 发布:java线程使用 编辑:程序博客网 时间:2024/06/06 03:00
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.

Sample test(s)
input
5Misha ILoveCodeforcesVasya PetrovPetrov VasyaPetrov123ILoveCodeforces MikeMirzayanovPetya Ivanov
output
3Petya IvanovMisha MikeMirzayanov

Vasya VasyaPetrov123

这一题有点类似赋值的过程,FZUOJ上有一题很类似的题目,解题思路方法几乎一模一样,我的思路使用一个vis数组,将赋值的中间过程标记为访问过,然后再输出。

#include<cstdio>#include<iostream>#include<cstring>using namespace std;char s[1001][30];char s1[1001][30];int main(){    int n;    scanf("%d", &n);    int ans = 0;    char temp[50];    char temp2;    int vis[1001];    memset(vis, 0, sizeof(vis));    for (int i = 0; i < n; i++)    {        scanf("%s%s", s[i],s1[i]);    }    for (int i = 0; i < n; i++)    {        strcpy(temp, s1[i]);        //vis[i] = 1;        for (int j = 0; j < n; j++)//遍历一边        {            if (j != i)            {                if (!vis[j])                {                    if (strcmp(temp, s[j]) == 0)                    {                        strcpy(temp, s1[j]);                        vis[j] = 1;//标记                        ans++;                    }                }            }        }        strcpy(s1[i], temp);    }    printf("%d\n", n - ans);    for (int i = 0; i < n; i++)    {        if (!vis[i])        {            printf("%s %s\n", s[i], s1[i]);        }    }    return 0;}

0 0
原创粉丝点击