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. 【Sublime】设置显示编码格式

    Mac 上的 Sublime 显示编码格式,设置方法: 右下角显示的 UTF-8 就是当前的编码格式. 添加如下代码: { "font_size": 18, // Display ...

  2. 【iOS】iOS 调试快速定位程序在哪崩溃

    iOS 开发过程中经常遇到程序崩溃.快速定位程序在哪崩溃的步骤如下: 1. 2. 3. 这样设置后,程序崩溃时会定位到崩溃的语句,如下: 原文链接:iOS开发何如在调试的时候轻松找到程序在哪里崩溃

  3. Apple放大绝进行反取证

    取证说穿了其实就是攻防,这本是正义与邪恶的对决,亦即执法单位与嫌疑犯两者之间的事,但现实生活中要比这复杂多了. 怎么说呢?举个例子大家便理解了.取证人员费尽心思,用尽各种手法,努力地想要自手机上提取重 ...

  4. l命令练习题1

    1.创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令) [root@localhost ~]# mkdir -p /guanli/zonghe | mk ...

  5. java的jar打包工具的使用

    java的jar打包工具的使用 java的jar是一个打包工具,用于将我们编译后的class文件打包起来,这里面主要是举一个例子用来说明这个工具的使用. 在C盘下的temp文件夹下面:         ...

  6. [译]Python中的异步IO:一个完整的演练

    原文:Async IO in Python: A Complete Walkthrough 原文作者: Brad Solomon 原文发布时间:2019年1月16日 翻译:Tacey Wong 翻译时 ...

  7. java中线程安全,线程死锁,线程通信快速入门

    一:多线程安全问题 ###1 引入 /* * 多线程并发访问同一个数据资源 * 3个线程,对一个票资源,出售 */ public class ThreadDemo { public static vo ...

  8. 【错误】【vscode】"'#' not expected here"

    今天使用vscode发现完整的代码报错了,但依然可以运行

  9. Spark 系列(五)—— Spark 运行模式与作业提交

    一.作业提交 1.1 spark-submit Spark 所有模式均使用 spark-submit 命令提交作业,其格式如下: ./bin/spark-submit \ --class <ma ...

  10. python3学习--文件读写

    这一篇我们来看文件读写操作. 打开和创建文件主要是open()函数: f = open('filename','r') # 读模式 f = open('filename','w') # 写模式 f = ...