Codeforces Round #464 (Div. 2) D题最小生成树

Posted pengge666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #464 (Div. 2) D题最小生成树相关的知识,希望对你有一定的参考价值。

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
3
abb
dad
Output
2
a d
b a
Input
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‘.

题意:用最少的字母对,使得两个字符串相等。字母对之间的字母可以相互转换。

思路:每对字符之间建条边,然后跑并查集即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 #define N 305500
 5 int arr[N];
 6 int f[N];
 7 int n;
 8 string s1,s2;
 9 struct str{
10     int x,y;
11 }st[N];
12 vector<pair<int,int> > ans;
13 int getf(int v){
14     if(v==f[v]){
15         return f[v];
16     }else{
17         f[v]=getf(f[v]);
18         return f[v];
19     }
20 }
21 int merge(int  u,int v){
22     int t1=getf(u);
23     int t2=getf(v);
24     if(t1!=t2){
25         f[t2]=t1;
26         ans.push_back(make_pair(u,v));
27         return 1;
28     }
29     return 0;
30 }
31 void init(){
32     for(int i=0;i<=26+1;i++)
33         f[i]=i;
34 }
35 signed main(){
36     init();
37     cin>>n>>s1>>s2;
38     int cnt=0;
39     for(int i=0;i<n;i++){
40         if(s1[i]==s2[i])
41             continue;
42         else{
43             st[cnt].x=s1[i]-a;
44                st[cnt].y=s2[i]-a;
45                cnt++;
46             st[cnt].x=s2[i]-a;
47             st[cnt].y=s1[i]-a;
48             cnt++;    
49         }
50     }
51     int add=0;
52     for(int i=0;i<cnt;i++){
53         if(merge(st[i].x,st[i].y)){
54             add++;
55         }
56     }
57     cout<<add<<
;
58     for(int i=0;i<ans.size();i++){
59         cout<<(char)(ans[i].first+a)<<" "<<(char)(ans[i].second+a)<<
;
60     }
61     return 0;
62 }

 

 

以上是关于Codeforces Round #464 (Div. 2) D题最小生成树的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #464 F. Cutlet

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)

Codeforces Round #726 (Div. 2) B. Bad Boy(贪心)

[Codeforces 464D]World of Darkraft(期望DP)

Codeforces 464E #265 (Div. 1) E. The Classic Problem 主席树+Hash