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. usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备

    在本次尝试中,我的安卓手机(HTC One X) 通过OTG线作为usb主机模式列出当前插入的usb设备,版本要求minSDKVersion="12". 没有外设的情况下,结果如下 ...

  2. hibernate课程 初探单表映射1-6 hibernate项目建立以及导入jar包

    hibernate 项目建立 1 new ==>java project hibernate 导入jar包 1 windows==>prerence==>java ==>bui ...

  3. 从零开始的全栈工程师——js篇2.1(js开篇)

    JS开篇 一.js介绍 全称 javascript 但不是java 他是一门前台语言 而java是后台语言js作者 布兰登·艾奇 前台语言:运行在客户端的后台语言:跟数据库有关的 能干什么?    页 ...

  4. ngnix入门配置

    文件1.首先到ngnix下载页面下载你操作系统对应的ngnix压缩包    http://nginx.org/en/download.html 博主我是window10操作系统  上面是我解压之后放的 ...

  5. NetBeans切换其他界面风格

    NetBeans是一款优秀的IDE,但是界面过亮,让我从使用以来就又爱又恨,经过一番摸索,测试出一款扩展软件 个人挺喜欢的分享出来 废话不多说下面教程: 1.安装NetBeans: 2.下载外观包ja ...

  6. HTML 笔记之 HTML 元素的概念

    HTML 不是编程语言,它是一种标记语言 HTML 中常见的几个名词是 元素. 标签(开始标签和结束标签). 属性. 元素内容 这篇文章将要介绍的是 HTML 元素的概念 元素的概念 例子: < ...

  7. django ORM 简单示例简绍

    简单 models 操作 class Host(models.Model): nid = models.AutoField(primary_key=True) #Nid为主键 hostname = m ...

  8. Paoding-Rose学习

    * HttpServletRequest.getContextPath 获取web程序root.如果是默认位置,返回””空串,否则返回 /根路径名 * rose是如何扫描到资源的 利用spring提供 ...

  9. HTML和CSS中一些有趣的

    CSS Rese http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css <link rel="styleshe ...

  10. mybatis-注解实现crud

    1.在接口上注解sql package com.java1234.mappers; import java.util.List; import org.apache.ibatis.annotation ...