Codeforces Round #464 (Div. 2) D. Love Rescue

Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.

This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.

More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.

The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.

The third line contains the lettering on Tolya's t-shirt in the same format.

Output

In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.

In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.

If there are many optimal answers, output any.

Examples
input
Copy
3
abb
dad
output
2
a d
b a
input
Copy
8
drpepper
cocacola
output
7
l e
e d
d c
c p
p o
o r
r a
Note

In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.

题意:现有一种操作,能使字母c1变成c2,还有两个字符串,问你最少要多少个(c1,c2)组合能把两个字符串变成一样。

思路:样例给我们的启示就是把所有的字母都变成一样的,这样两个字符串就一样了,我们需要的操作数是字母数量减一,但是如果只想到这一点并不全对,因为还有这样的样例:

3

ab

cd

按照上面的方法我们得到的结果是3,但是最优解一眼就可以看出来是2。

所以正确的思路是把对应位置不相同的字母看成之间有一条边(如果对应位置相同的字母不用管),这样两个字符串中所有的字母就构成了一个图,其中有很多联通块,把每个联通块按照上面说的方法,求出不同字母数量减一,总共的和就是答案。

我用的是并查集保存联通关系,用dfs也是可以的。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

char a[100010];
char b[100010];
int f[30];

int find(int x)
{
    if(f[x] == x)
        return f[x];
    else
        return f[x] = find(f[x]);
}
int main(void)
{
    int n,i,j;
    while(scanf("%d",&n)==1)
    {
        for(i=0;i<26;i++)
            f[i] = i;
        scanf("%s%s",a,b);
        for(i=0;i<n;i++)
        {
            if(a[i] == b[i])
                continue;
            int x = find(a[i]-'a');
            int y = find(b[i]-'a');
            if(x != y)
                f[x] = y;
        }
        int ans = 0;
        for(i=0;i<26;i++)
        {
            int cnt = 0;
            for(j=0;j<26;j++)
            {
                if(find(j) == i)
                    cnt++;
            }
            if(cnt != 0)
                ans += cnt-1;
        }
        printf("%d\n",ans);
        for(i=0;i<26;i++)
        {
            if(i == find(i))
                continue;
            printf("%c %c\n",'a'+i,'a'+find(i));
        }
    }


    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值