Codeforces Round #464 (Div. 2) D题【最小生成树】
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
3
abb
dad
2
a d
b a
8
drpepper
cocacola
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'.
题意:用最少的字母对,使得两个字符串相等。字母对之间的字母可以相互转换。
思路:每对字符之间建条边,然后跑并查集即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 305500
int arr[N];
int f[N];
int n;
string s1,s2;
struct str{
int x,y;
}st[N];
vector<pair<int,int> > ans;
int getf(int v){
if(v==f[v]){
return f[v];
}else{
f[v]=getf(f[v]);
return f[v];
}
}
int merge(int u,int v){
int t1=getf(u);
int t2=getf(v);
if(t1!=t2){
f[t2]=t1;
ans.push_back(make_pair(u,v));
return ;
}
return ;
}
void init(){
for(int i=;i<=+;i++)
f[i]=i;
}
signed main(){
init();
cin>>n>>s1>>s2;
int cnt=;
for(int i=;i<n;i++){
if(s1[i]==s2[i])
continue;
else{
st[cnt].x=s1[i]-'a';
st[cnt].y=s2[i]-'a';
cnt++;
st[cnt].x=s2[i]-'a';
st[cnt].y=s1[i]-'a';
cnt++;
}
}
int add=;
for(int i=;i<cnt;i++){
if(merge(st[i].x,st[i].y)){
add++;
}
}
cout<<add<<'\n';
for(int i=;i<ans.size();i++){
cout<<(char)(ans[i].first+'a')<<" "<<(char)(ans[i].second+'a')<<'\n';
}
return ;
}
Codeforces Round #464 (Div. 2) D题【最小生成树】的更多相关文章
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- Codeforces Round #786 (Div. 3) 补题记录
小结: A,B,F 切,C 没写 1ll 对照样例才发现,E,G 对照样例过,D 对照样例+看了其他人代码(主要急于看后面的题,能调出来的但偷懒了. CF1674A Number Transforma ...
随机推荐
- UVALive-8201-BBP Formula
8201-BBP Formula Time limit: 3.000 seconds In 1995, Simon Plouffe discovered a special summation sty ...
- triples I(按位或运算及3的特性)(2019牛客暑期多校训练营(第四场)D)
示例1: 输入: 2 3 7 输出: 1 32 3 6 说明:3=3, (3|6)=7 题意:输出尽可能少的数字,他们的按位或结果为输入的数字a. 题解:(表示看不懂题解,看山东大佬的代码看懂的)首先 ...
- 5-24 c++语言之【基础知识】
最近一段时间继续开始了c++的学习,作为c plus plus 难免会与c语言做一个对比,很明显的感受到c++语言注重代码的复用性和拓展性,而c语言更加注重其算法的高效性,这也是今后需要注意的地方,避 ...
- hadoop mapper reducer
Local模式运行MR流程------------------------- 1.创建外部Job(mapreduce.Job),设置配置信息 2.通过jobsubmitter将job.xml + sp ...
- fatfs系统的移植
integer.h FATFS的数据类型定义(一般不需要更改,其他的文件都需要引用这个文件的内容) ffcon.h FATFS的配置文件,配置项的各个参数都需要在这里修改 一个细致的讲解fatfs ...
- Visual Studio中找不到.Net Core SDK
在win 7 64位上安装了.Net Core 2.1 x86 SDK后,又卸载重新安装了.Net Core 3/2 x64 SDK.结果在VS中新建项目时没有.Net Core 3.1 SDK. 在 ...
- 关于MQ的几件小事(三)如何保证消息不重复消费
1.幂等性 幂等(idempotent.idempotence)是一个数学与计算机学概念,常见于抽象代数中. 在编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.幂等函数,或 ...
- VS2017 CMD多出 “进程 6420)已退出,返回代码为: 0”的内容
执行cmd, 命令行多出如下内容 xxxx\project.exe (进程 6420)已退出,返回代码为: 0. VS 取消设置方式: 工具->选项->调试-->常规 拉到最 ...
- 《你不知道的Javascript》感悟篇—对象属性遍历的那些事
划重点 本篇笔者将重点介绍JavaScript中 getOwnPropertyNames .Object.keys.for ... in 的使用及他们之间的异同点. getOwnPropertyNam ...
- Java 之 字符输入流[Reader]
一.字符输入流 java.io.Reader 抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中. 它定义了字符输入流的基本共性功能方法. public void close() :关 ...