Codeforces Round #464 (Div. 2) D. Love Rescue
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’.
解题心得:
- 题意就是通过将一种字母替换成另一种字母,让给你的A,B两个字符串变得相同,要求改变次数最小。
- 刚开始瞎搞了半天,然后冷静下来想了一下其实就是一个并查集,只有需要改变的才改变,这样得出来的结果一定是最小的。
#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的更多相关文章
- Codeforces Round #464 (Div. 2) E. Maximize!
题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...
- Codeforces Round #464 (Div. 2) D题【最小生成树】
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her b ...
- Codeforces Round #464 (Div. 2) C. Convenient For Everybody
C. Convenient For Everybody time limit per test2 seconds memory limit per test256 megabytes Problem ...
- Codeforces Round #464 (Div. 2) B. Hamster Farm
B. Hamster Farm time limit per test2 seconds memory limit per test256 megabytes Problem Description ...
- Codeforces Round #464 (Div. 2) A Determined Cleanup
A. Love Triangle time limit per test1 second memory limit per test256 megabytes Problem Description ...
- 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 ...
- 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 ...
- Codeforces Round #464 (Div. 2)
A. Love Triangle time limit per test: 1 second memory limit per test: 256 megabytes input: standard ...
- 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 ...
随机推荐
- PHP面试题及答案(五)
1. 禁用COOKIE 后 SEESION 还能用吗? 答案: 不能. 2. 抓取远程图片到本地,你会用什么函数? 答案: fsockopen(). 3.求两个日期的差数,例如2007-2-5 ~ 2 ...
- HDU 5501——The Highest Mark——————【贪心+dp】
The Highest Mark Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- Spring Boot相关组件的添加
在勾选相关组件后, pom.xml文件上发生了根本的变化 1.这是最简单的项目的pom文件 <?xml version="1.0" encoding="UTF-8& ...
- uvm_reg_adapter——寄存器模型(十八)
uvm_reg_adapter 功能就是在uvm_reg_bus_op和总线操作之间的转换.主要包含两个函数reg2bus 和bus2reg. //-------------------------- ...
- 打造颠覆你想象中的高性能,轻量级的webform框架---js直接调后台的封装(第三天)
如果你没有看我第二天写的内容的,我想你是看不懂的!!!! 好了,废话不多说,怎么才能让我们的代码变得牛逼起来呢?怎么封装我们的代码呢?我们不可能 每个页面都需要那样写吧,那我们来一步一步来封装 我们的 ...
- POJ-2195 Going Home---KM算法求最小权值匹配(存负边)
题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...
- Android(java)学习笔记87:Android音视频MediaRecorder用法
1. Android语音录制可以通过 MediaRecorder 和 AudioRecorder: MediaRecorder本来是多媒体录制控件,可以同时录制视频和语音,当不指定视频源时就只录制语 ...
- Using Autorelease Pool Blocks
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAut ...
- python_26_dictionary
#key-value 字典无下标 所以乱序,key值尽量不要取中文 info={ 'stu1101':'Liu Guannan', 'stu1102':'Wang Ruipu', 'stu1103': ...
- Python实现购物小程序
一.需求 1.登录 { ‘xxx1’:{'passwd':'123','role':1,'moeny':10000,"carts":['mac']}, 'xxx1':{'passw ...