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'.
题意:用最少的字母对,使得两个字符串相等。字母对之间的字母可以相互转换。
思路:每对字符之间建条边,然后跑并查集即可。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 #define N 305500 5 int arr[N]; 6 int f[N]; 7 int n; 8 string s1,s2; 9 struct str{ 10 int x,y; 11 }st[N]; 12 vector<pair<int,int> > ans; 13 int getf(int v){ 14 if(v==f[v]){ 15 return f[v]; 16 }else{ 17 f[v]=getf(f[v]); 18 return f[v]; 19 } 20 } 21 int merge(int u,int v){ 22 int t1=getf(u); 23 int t2=getf(v); 24 if(t1!=t2){ 25 f[t2]=t1; 26 ans.push_back(make_pair(u,v)); 27 return 1; 28 } 29 return 0; 30 } 31 void init(){ 32 for(int i=0;i<=26+1;i++) 33 f[i]=i; 34 } 35 signed main(){ 36 init(); 37 cin>>n>>s1>>s2; 38 int cnt=0; 39 for(int i=0;i<n;i++){ 40 if(s1[i]==s2[i]) 41 continue; 42 else{ 43 st[cnt].x=s1[i]-'a'; 44 st[cnt].y=s2[i]-'a'; 45 cnt++; 46 st[cnt].x=s2[i]-'a'; 47 st[cnt].y=s1[i]-'a'; 48 cnt++; 49 } 50 } 51 int add=0; 52 for(int i=0;i<cnt;i++){ 53 if(merge(st[i].x,st[i].y)){ 54 add++; 55 } 56 } 57 cout<<add<<'\n'; 58 for(int i=0;i<ans.size();i++){ 59 cout<<(char)(ans[i].first+'a')<<" "<<(char)(ans[i].second+'a')<<'\n'; 60 } 61 return 0; 62 }
原文地址:https://www.cnblogs.com/pengge666/p/11845370.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 我又踩坑了!如何为HttpClient请求设置Content-Type标头?
- 在CentOS7上安装开源Verilog仿真工具EpicSim
- 【CS学习笔记】27、applet渗透测试手法
- Python-WSGI接口
- CentOS7-Nginx配置Let's-Encrypt-SSL证书
- CentOS7-Nginx编译安装
- 总被审稿人提起的多重假设检验校正是什么?
- Python网络编程
- 读一本书 | systemverilog之内建数据类型
- 实战解读ASP.NET Core身份认证
- 读一本书 | systemverilog之内建数据类型(下)
- 面向对象有限元编程|节点类
- 面向对象有限元编程|抽象
- Python多线程
- Python描述器