Love Rescue

题意:Valya 和 Tolya 是一对情侣, 他们的T恤和头巾上都有小写字母,但是女朋友嫌弃男朋友上T恤上的字不和她的头巾上的字一样,就很生气, 然后来了一个魔法师, 它可以购买符文, 每个符文可以让一个字母变成另外一个字母,魔法师想购买足够的符文使得他们的字母串相同, 因为买符文要钱, 现在需要求最小的购买符文数目,和符文形式, 多种答案可以输出任意一种。

题解:用并查集将有联系的字母分到一组, 然后将一组内的符文进行串联,这样每一组内的任意2个符文, 都可以由上面的字符变成下面的字符。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
const int INF = 0x3f3f3f3f;
typedef pair<int,int> pll;
int pre[];
int vis[];
int Find(int x)
{
if(x == pre[x]) return x;
return x = Find(pre[x]);
}
vector<int> ans[];
map<int,int> ccc;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
for(int i = ; i < ; i++)
pre[i] = i, vis[i] = ;
int len;
string str1, str2;
cin >> len;
cin >> str1 >> str2;
for(int i = ; i < len; i++)
{
if(str1[i] == str2[i]);
vis[str1[i]-'a'] = vis[str2[i]-'a'] = ;
int u = Find(str1[i]-'a'), v = Find(str2[i]-'a');
if(u == v) continue;
pre[u] = v;
}
int tot = ;
for(int i = ; i < ; i++)
{
if(!vis[i]) continue;
int x = Find(i);
if(!ccc.count(x))
{
tot++;
int sss = ccc.size();
ccc[x] = sss + ;
}
ans[ccc[x]].push_back(i);
}
int cnt = ;
char ans1[], ans2[];
for(int i = ; i <= tot; i++)
{
int u = -;
for(int j = ; j < ans[i].size(); j++)
{
if(u == -) u = j;
else
{
ans1[++cnt] = ans[i][u] + 'a';
ans2[cnt] = ans[i][j] + 'a';
u = j;
}
}
}
cout << cnt << endl;
for(int i = ; i <= cnt; i++)
cout << ans1[i] << ' ' << ans2[i] << endl;
return ;
}

Codeforces 939 D Love Rescue的更多相关文章

  1. Codeforces 939.E Maximize!

    E. Maximize! time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  2. Codeforces 939 时区模拟 三分

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  3. 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 ...

  4. Codeforces 939D - Love Rescue

    传送门:http://codeforces.com/contest/939/problem/D 本题是一个数据结构问题——并查集(Disjoint Set). 给出两个长度相同,且仅由小写字母组成的字 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #464 (Div. 2) D. Love Rescue

    D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...

  8. Codeforces Round #672 (Div. 2) D. Rescue Nibel!(排序)

    题目链接:https://codeforces.com/contest/1420/problem/D 前言 之前写过这场比赛的题解,不过感觉这一题还可以再单独拿出来好好捋一下思路. 题意 给出 $n$ ...

  9. CodeForces 590B Chip 'n Dale Rescue Rangers

    这题可以o(1)推出公式,也可以二分答案+验证. #include<iostream> #include<cstring> #include<cmath> #inc ...

随机推荐

  1. 深入理解HashMap(jdk7)

    HashMap的结构图示 ​ jdk1.7的HashMap采用数组+单链表实现,尽管定义了hash函数来避免冲突,但因为数组长度有限,还是会出现两个不同的Key经过计算后在数组中的位置一样,1.7版本 ...

  2. 【iOS】edgesForExtendedLayout

    在 iOS 7.0 中,苹果引入了一个新的属性,叫做 edgesForExtendedLayou,它的默认值为 UIRectEdgeAll. 当你的容器是 navigationController 时 ...

  3. 放出一批学生管理系统jsp源码,部分有框架

    基于jsp+struts 2的学生管理系统eclipse - 源码码头   https://www.icodedock.com/article/25.html 基于jsp+mysql的JSP学生成绩管 ...

  4. Educational Codeforces Round 70 (Rated for Div. 2)

    这次真的好难...... 我这个绿名蒟蒻真的要崩溃了555... 我第二题就不会写...... 暴力搜索MLE得飞起. 好像用到最短路?然而我并没有学过,看来这个知识点又要学. 后面的题目赛中都没看, ...

  5. vue面试题整理vuejs基础知识整理

    初级参考 1.v-show 与 v-if 区别 v-show 是css隐藏,v-if是直接销毁和创建,所以频繁切换的适合用v-show 2.计算属性和 watch 的区别 计算属性是自动监听依赖值的变 ...

  6. 深入理解struts的运行机制

    扫码关注公众号,不定期更新干活 在此申明本博文并非原创,原文:http://blog.csdn.net/lenotang/article/details/3336623,本文章是在此文章基础上进行优化 ...

  7. 『开发技术』GPU训练加速原理(附KerasGPU训练技巧)

    0.深入理解GPU训练加速原理 我们都知道用GPU可以加速神经神经网络训练(相较于CPU),具体的速度对比可以参看我之前写的速度对比博文: [深度应用]·主流深度学习硬件速度对比(CPU,GPU,TP ...

  8. Dubbo的基本介绍及使用

    一,前言 ​ 在面对新技术新事物的时候,我们首先应该了解这是一个什么东东,了解为什么使用这门技术,如果我们不使用又会有什么影响.比如,本篇博客介绍Dubbo的基本使用,此时我们应该先要明白我为什么要使 ...

  9. 100天搞定机器学习|Day 30-32 微积分的本质

    3blue1brown系列课程,精美的动画,配上生动的讲解,非常适合帮助建立数学的形象思维,非常值得反复观看: http://www.3blue1brown.com/ 哔哩哔哩: https://sp ...

  10. 如何使用WorkManager执行后台任务(上)

    0x00 简述 WorkManager 是 Android Jetpack中的一部分,它主要是封装了 Android 后台任务的调度逻辑.在前文<Android后台任务处理指南>一文中知道 ...