Codeforces 939D - Love Rescue
传送门:http://codeforces.com/contest/939/problem/D
本题是一个数据结构问题——并查集(Disjoint Set)。
给出两个长度相同,且仅由小写字母组成的字符串S=s[1..n]、T=t[1..n]。已知一个无序对(u,v)可以完成任意次的以下转换操作:u→v,v→u。求将字符串S转换为T所需要的最少的无序对的数目,并打印出相应可行的方案下的所有无序对。
首先构造一张无向图G=<V,E>,表示S→T的状态转换图:结点集V={‘a’,’b’,’c’,...,’z’};边集E={(si,ti)|si≠ti,i=1,2,...,n}。对于这张图的连通分量,构造一个边集最小的连通图——即无向图的生成树。可以采用简化的Kruskal算法求这棵生成树。
实际上,这是一个并查集的问题。通过并查集,对于每一个i,判断si、ti是否在同一个集合中:若二者不在同一个集合中,则合并二者所在的集合。参考程序如下:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_V 26
#define MAX_N 100005 char s[MAX_N], t[MAX_N];
char uset[MAX_V], vset[MAX_V]; //Disjoint Set
int pa[MAX_V];
int rank[MAX_V]; void init(int n)
{
memset(pa, , sizeof(pa));
memset(rank, , sizeof(rank));
for (int i = ; i < n; i++) {
pa[i] = i;
rank[i] = ;
}
} int find(int x)
{
if (pa[x] == x) return x;
else return pa[x] = find(pa[x]);
} void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return;
if (rank[x] < rank[y]) pa[x] = y;
else {
pa[y] = x;
if (rank[x] == rank[y]) rank[x]++;
}
} bool same(int x, int y)
{
return find(x) == find(y);
} int main(void)
{
int n;
scanf("%d", &n);
scanf("%s", s);
scanf("%s", t);
int cnt = ;
init(MAX_V);
for (int i = ; i < n; i++) {
if (s[i] != t[i]) {
int u = s[i] - 'a';
int v = t[i] - 'a';
if (!same(u, v)) {
unite(u, v);
uset[cnt] = u + 'a';
vset[cnt] = v + 'a';
cnt++;
}
}
}
printf("%d\n", cnt);
for (int i = ; i < cnt; i++)
printf("%c %c\n", uset[i], vset[i]);
return ;
}
Codeforces 939D - Love Rescue的更多相关文章
- Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理
D. Chip 'n Dale Rescue Rangers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...
- codeforces 590B B. Chip 'n Dale Rescue Rangers(二分+计算几何)
题目链接: B. Chip 'n Dale Rescue Rangers time limit per test 1 second memory limit per test 256 megabyte ...
- Codeforces Round #327 (Div. 1) B. Chip 'n Dale Rescue Rangers 二分
题目链接: 题目 B. Chip 'n Dale Rescue Rangers time limit per test:1 second memory limit per test:256 megab ...
- Codeforces Round #464 (Div. 2) D. Love Rescue
D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...
- Codeforces 939 D Love Rescue
Love Rescue 题意:Valya 和 Tolya 是一对情侣, 他们的T恤和头巾上都有小写字母,但是女朋友嫌弃男朋友上T恤上的字不和她的头巾上的字一样,就很生气, 然后来了一个魔法师, 它可以 ...
- Codeforces Round #672 (Div. 2) D. Rescue Nibel!(排序)
题目链接:https://codeforces.com/contest/1420/problem/D 前言 之前写过这场比赛的题解,不过感觉这一题还可以再单独拿出来好好捋一下思路. 题意 给出 $n$ ...
- CodeForces 590B Chip 'n Dale Rescue Rangers
这题可以o(1)推出公式,也可以二分答案+验证. #include<iostream> #include<cstring> #include<cmath> #inc ...
- codeforces590b//Chip 'n Dale Rescue Rangers//Codeforces Round #327 (Div. 1)
题意:从一点到另一点,前t秒的风向与t秒后风向不同,问到另一点的最短时间 挺难的,做不出来,又参考了别人的代码.先得到终点指向起点的向量,设T秒钟能到.如果T>t则受风1作用t秒,风2作用T-t ...
- Codeforces Round #327 590B Chip 'n Dale Rescue Rangers(等效转换,二分)
t和可到达具有单调性,二分就不多说了.下面说下O(1)的做法,实际上是等效转换,因为答案一定存在,如果在t0之前,那么分解一下 直接按照只有v计算就可以了.反过来如果计算的结果大于t0,那么表示答案在 ...
随机推荐
- The bytes/str dichotomy in Python 3
The bytes/str dichotomy in Python 3 - Eli Bendersky's website https://eli.thegreenplace.net/2012/01/ ...
- CI框架下CSS和JS的路径问题
注意:CI框架下的CSS和JS的引用必须放在框架外面,比如,可建立resource文件夹与application同级,用来封装CSS和JS. 在view层用resource里面CSS和JS可采用以下几 ...
- java多线程——饥饿和公平
一.概念 饥饿:如果一个线程因为 CPU 时间全部被其他线程抢走而得不到 CPU 运行时间,这种状态被称之为“饥饿”: 二.饥饿原因 高优先级线程吞噬所有的低优先级线程的 CPU 时间. 线程被永久堵 ...
- FormatString格式大众人全
FormatString格式大众人全 Posted on 2010-08-12 16:14 moss_tan_jun 阅读(457) 评论(0) 编辑 收藏 格式化日期和数字的字符串经常要用到这个, ...
- 【POI 2010】 Antisymmetry
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2084 [算法] manacher [代码] #include<bits/std ...
- 洛谷 P4149 [ IOI 2011 ] Race —— 点分治
题目:https://www.luogu.org/problemnew/show/P4149 仍然是点分治: 不过因为是取 min ,所以不能用容斥,那么子树之间就必须分开算,记录桶时注意这个: 每次 ...
- Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space
题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...
- java静态代理实例
package test; class ProxyTest { public static void main(String[] args) { ProxyClass proxy = new Prox ...
- C#窗体间传值的简便方法/工具
一.问题:窗体间传值必须需要窗体之间有联系,具体有如下方式 窗体间传值涉及到窗体A必须拥有窗体B,这样才可以实现A-B之间传值 窗体A与窗体B在窗体/实例C中,A-B可互相通讯 其他方式,不细讨论,复 ...
- 【原】cocos2d-x开发笔记:获取Sprite上某一个点的透明度,制作不规则按钮
本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要执行不同的事件