D. Love Rescue

time limit per test2 seconds

memory limit per test256 megabytes

Problem Description

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. 题意就是通过将一种字母替换成另一种字母,让给你的A,B两个字符串变得相同,要求改变次数最小。
  2. 刚开始瞎搞了半天,然后冷静下来想了一下其实就是一个并查集,只有需要改变的才改变,这样得出来的结果一定是最小的。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100; int father[220],n;
char s[2][maxn]; int find(int x){
if(father[x] == x)
return x;
return father[x] = find(father[x]);
} void merge(int a,int b){
int fa = find(a);
int fb = find(b);
father[fa] = fb;
} int main(){
scanf("%d",&n);
scanf("%s%s",s[0],s[1]); for(int i=0;i<210;i++)
father[i] = i; int ans = 0;
for(int i=0;i<n;i++){
if(find(s[0][i]) != find(s[1][i])){
ans++;
merge(s[0][i],s[1][i]);
}
} printf("%d\n",ans);
for(int i=0;i<210;i++){
if(father[i] != i)
printf("%c %c\n",i,father[i]);
}
return 0;
}

Codeforces Round #464 (Div. 2) D. Love Rescue的更多相关文章

  1. Codeforces Round #464 (Div. 2) E. Maximize!

    题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...

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

    Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...

  3. Codeforces Round #464 (Div. 2) C. Convenient For Everybody

    C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...

  4. Codeforces Round #464 (Div. 2) B. Hamster Farm

    B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...

  5. Codeforces Round #464 (Div. 2) A Determined Cleanup

    A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...

  6. Codeforces Round #464 (Div. 2) B. Hamster Farm[盒子装仓鼠/余数]

    B. Hamster Farm time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]

    A. Love Triangle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. Codeforces Round #464 (Div. 2)

    A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. 面向对象设计与构造:oo课程总结

    面向对象设计与构造:OO课程总结 第一部分:UML单元架构设计 第一次作业 UML图 MyUmlInteraction类实现接口方法,ClassUnit和InterfaceUnit管理UML图中的类和 ...

  2. 【踩坑】vue 无法让后台保存 session

    今天在调试 iblog 客户端时,发现登录后进行增加.删除.更新操作时都提示还没有登录. 此问题曾经在用 ajax 调试时出现过,解决办法是,在请求时带上 creditials: true ,即让发出 ...

  3. linux cached过高导致性能变低

    场景: 拿到了客户50个文件,平均每个文件大概40M左右的txt,文件在S3上,需要导入到数据库,40M解析出来大概是80W条左右的数据. 描述: 在刚开始执行导入时,因为数据验证复杂程度不同,每个文 ...

  4. Java的HashMap和HashTable(转)

    来源:http://www.cnblogs.com/devinzhang/archive/2012/01/13/2321481.html 1. HashMap 1)  hashmap的数据结构 Has ...

  5. python全栈测试题(一)

    1.执行Python脚本的两种方式 如果想要永久保存代码,就要用文件的方式 如果想要调试代码,就要用交互式的方式即终端命令下和python环境中 2.Pyhton单行注释和多行注释分别用什么? pyt ...

  6. path的join和resolve

    连接路径:path.join([path1][, path2][, ...]) path.join()方法可以连接任意多个路径字符串.要连接的多个路径可做为参数传入. path.join()方法在接边 ...

  7. echarts的title和legend重合解决(各种小细节)

    一:关于title与legend重叠 1.重合样子 2.解决办法: legend:{ show: true, top:"6%",//与上方的距离 可百分比% 可像素px }, 3. ...

  8. 用xaml画的带阴影3D感的圆球

    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <E ...

  9. ImportError: No module named PIL

    解决方法: 在C:\Python27\Scripts目录下打开命令行,输入: pip install Pillow

  10. webpack打包过程如何调试?

    本文适用于已经会使用webpack的前端开发人员,但是想进一步了解webpack细节和进阶. 首先请读者按照我前一篇文章 Webpack 10分钟入门介绍的步骤,在本地搭建一个webpack的hell ...