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实例:使用PHPExcel导入Excel2003文档和Excel2007文档到MySQL数据库中
如果要使用phpExcelReader将Excel 数据导入到mysql 数据库,请读者点击这个文章查看. 使用phpExcelReader将Excel 数据导入到mysql 数据库. 下面我们介绍另 ...
- java与模式读后总结
一 老规则边看边写书上的代码,磨磨蹭蹭三个多星期终于把一本1000+的java与模式看完了. 于是,在这里贴上自己对每个模式的思考和总结,其实这个东西在我边看边写的时候已经写了一大半,博文再写一次算是 ...
- asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
1.带有Render的方法返回值是void,在方法内部进行输出:不带的返回值类型为MvcHtmlString,所以只能这样使用: @Html.Partial 对应 @{Html.RenderParti ...
- Unity C# 脚本的单例
今天学习了一个比较不错的单例模式 public class UnitySigleton <T>: MonoBehaviour where T:class { public static T ...
- spring数组注入
数组注入 public class MyCollection { private String[]array; private List<String>list; ...
- wcf问题集锦
1.处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供 ...
- 转 --简单解决Linq多条件组合问题
本文笔者用清晰的实例,解决了Linq多条件问题,思路十分的清晰,笔者也很细心的做了描述,希望能给你带来帮助. 最近有个项目准备功能改版,师兄吩咐:尽可能地做到万般皆Linq,所以很多东西都要从存储过程 ...
- js之BOM和DOM
今天我们来学习js中的一些基础的操作. 一.BOM对象 BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其他与页面内容不直接相 ...
- mysq表的三种关系,数据的增删改以及单表多表查询
一丶三种关系 分析步骤: #.先站在左表的角度去找 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id) #.再站在右表的角度去找 ...
- 部署webservice到远程服务器
在本地编写好webservice后并在本机验证正确后,在本地发布后,直接将发布时设置的文件夹复制到远程服务器上,在远程服务器的IIS上默认网站->新建虚拟目录->设置别名->物理路径 ...