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

D. Love Rescue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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就可以多次相互转化,所以一个字符可以间接的转化为另外一个字符,我们就可以把能相互转化的字符看成一个连通分支。而施展魔法的次数也就是让所有字符全部连通所要加的边,刚开始所有点都是孤立的。所以用并查集来处理,比较简便。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=1e5+10;
int pre[maxn];
char a[maxn],b[maxn];
int ans,n;
int Find(int x)
{
    int r=x;
    //寻找根节点
    while(pre[r]!=r)
        r=pre[r];
    int i=x,j;
    //路径压缩
    while(i!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}

int join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    //判断是否连通
    if(fx!=fy)
    {
        //如果不连通的话加一条边让他们连通
        pre[fx]=fy;
        ans++;
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<26;i++)
            pre[i]=i;
        ans=0;
        scanf("%s %s",a,b);
        for(int i=0;i<n;i++)
        {
            int na=a[i]-'a';
            int nb=b[i]-'a';
            join(na,nb);
        }
        printf("%d\n",ans);
        for(int i=0;i<26;i++)
        {
            if(i!=pre[i])
                printf("%c %c\n",i+'a',pre[i]+'a');
        }
    }
    return 0;
}
顺便附上java代码:
import java.util.Scanner;  


public class Main { 
	static int[] pre=new int[100005];
	static int ans;
    public static void main(String[] args) { 
    	Scanner in= new Scanner(System.in);
    	while(in.hasNext()) {
    		int n=in.nextInt();
    		String aa=in.next();
    		String bb=in.next();
    		for(int i=0;i<26;i++)
    			pre[i]=i;
    		ans=0;
    		char[] a=aa.toCharArray();
    		char[] b=bb.toCharArray();
    		for(int i=0;i<n;i++) {
    			int na=a[i]-'a';
    			int nb=b[i]-'a';
    			join(na,nb);
    		}
    		System.out.println(ans);
    		for(int i=0;i<26;i++) {
    			if(i!=pre[i])
    				System.out.printf("%c %c\n", i+'a',pre[i]+'a');
    				
    		}
    	}
        
    }  
    public static int Find(int x) {
    	int r=x;
    	while(r!=pre[r]) {
    		r=pre[r];
    	}
    	int i=x,j;
    	while(i!=r) {
    		j=pre[i];
    		pre[i]=r;
    		i=j;
    	}
    	return r;
    }
    
    public static void join(int x,int y) {
    	int fx=Find(x);
    	int fy=Find(y);
    	if(fx!=fy) {
    		pre[fx]=fy;
    		ans++;
    	}
    }
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值