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. kubernetes对接第三方认证

    kubernetes对接第三方认证 kubernetes离线安装包地址 概述 本文介绍如何使用github账户去关联自己kubernetes账户.达到如下效果: 使用github用户email作为ku ...

  2. ue4使用SceneCapture2D创建小地图示例 蓝图

    做C++项目的时候遇到了一个小地图的问题,从网上找了个蓝图的思路,转载一下. 原文:https://www.engineworld.cn/thread-3835-1-1.html 本文使用ue4提供的 ...

  3. Superset 官方入门教程中文翻译

    本文翻译自 Superset 的官方文档:Toturial - Creating your first dashboard 最新版本的 Superset 界面与功能上与文档中提到的会有些许出入,以实际 ...

  4. Computing Jobs

    docker&k8shadoopsparkhbasemesosrediskafkazookeeper SCSI.NVMe.PCIe devops

  5. 1、JAVA的小白之路

    大学的时光过得很快,转眼我已经大二了,在大一时,学习了C\C++,对于语言有一定基础,在未来的道路上,我需要攒足干劲,积累足够的知识和技能,去走上社会. 我的第一任大学班主任告诉我:“作为程序员,你至 ...

  6. 基于RBAC的权限框架

    RBAC权限框架(Role-Based Access Control)基于角色的权限访问控制的框架,通过用户-角色-权限的关联,非常方便的进行权限管理,在这里不再说明什么是RBAC,请自行百度. 谢谢 ...

  7. Spring入门(六):条件化的bean

    1. 概念 默认情况下,Spring中定义的bean在应用程序启动时会全部装配,不管当前运行的是哪个环境(Dev,QA或者Prod),也不管当前运行的是什么系统(Windows或者Linux),但有些 ...

  8. ecshop 管理后台菜单及权限管理机制

    ecshop 所有的一级菜单选项存放于languages\zh_cn\admin\common.php 文件里面,使用 $_LANG['02_cat_and_goods'] = '商品管理';  这样 ...

  9. pythonday05数据类型(三)

    ---恢复内容开始--- 今日内容 1.字典 2.强制转换 3.习题讲解 1.字典 帮助用户去表示一个事物的信息(事物是有多个属性). info = {"name":'刘伟达',' ...

  10. SIMBOSS:物联网业务如何应用领域驱动设计?

    前言 在这个万物互联的时代,物联网业务蓬勃发展,但也瞬息万变,对于开发人员来说,这是一种挑战,但也是一种“折磨”. 在业务发展初期,因为时间有限,我们一般会遵循“小步快跑,迭代试错”的原则进行业务开发 ...