Codeforces Round #464 (Div. 2) 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'.

 

 代码:

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<vector>
 6 struct DSU{
 7     long long *father;
 8     long long *min_able;
 9     DSU(long long n){//初始化一个节点数为n+1(编号0-n)的集合 
10         father = new long long[n+1];
11         /*把每一个节点设置为根节点,
12         也就是每个节点都是一个独立的集合*/
13         for(long long i=0;i<=n;i++)
14             father[i]=i;
15     }
16     long long Get_root(long long x){//得到x节点的根节点 
17         if(father[x]==x)//如果某节点的父亲依然是这个节点
18             return x;//那么这个点就是根节点
19         //如果不是根节点 
20         long long root = Get_root(father[x]);//那么递归地找它父亲的根节点 
21         father[x] = root;//路径压缩 
22         return root;//返回根节点的值 
23     }
24     void Union(long long y,long long x){//合并两个集合 
25         long long root_x = Get_root(x);
26         long long root_y = Get_root(y);
27         father[root_x] = root_y;//x的根节点接在y的树上
28         Get_root(x);    
29     }
30     bool Judge(long long x,long long y){//判断两个节点是否是一个集合的
31          //等价于根节点是否相同 
32          return Get_root(x)==Get_root(y);    
33     }
34 };
35 DSU a(50);
36 char s1[110000];
37 char s2[110000];
38 int maps[50][50];
39 struct Data{
40     int l,r;
41 };
42 vector<Data> ans;
43 int main(){
44     int n;
45     memset(maps,0,sizeof(maps));
46     scanf("%d",&n);
47     scanf("%s%s",s1,s2);
48     for(int i=0;i<n;i++){
49         maps[s1[i]-'a'][s2[i]-'a']=1;
50         maps[s2[i]-'a'][s1[i]-'a']=1;
51     }
52     for(int i=0;i<26;i++){
53         for(int j=0;j<26;j++){
54             if(maps[i][j])
55                 a.Union(i,j);
56         }
57     }
58     for(int i=0;i<26;i++){
59         if(a.Get_root(i)!=i){
60             Data pt;
61             pt.l = i;
62             pt.r = a.Get_root(i);
63             ans.push_back(pt);
64         }
65     }
66     printf("%d\n",ans.size());
67     for(int i=0;i<ans.size();i++){
68         printf("%c %c\n",ans[i].r+'a',ans[i].l+'a');
69     }
70     return 0;
71 }

 

posted @ 2018-03-09 10:21  晓风微微  阅读(223)  评论(0编辑  收藏  举报