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. iOS 注释

    1) 参数的注释: UIButton *btnSend;/**< 发送按钮 */ 效果: 2) 方法的注释: type1(无参数): /** table 相关设置 */ -(void)confi ...

  2. Redis Sentinel基本实现原理

    一.出现的背景: Redis 主从复制模式下一旦主节点由于故障不能提供服务,需要人工将从节点晋升为主节点,同时还要通知应用方更新主节点地址,对于很多应用这种场景的这种故障处理方式是非常浪费人力的.为了 ...

  3. 用scrapy爬取搜狗Lofter图片

    用scrapy爬取搜狗Lofter图片 # -*- coding: utf-8 -*- import json import scrapy from scrapy.http import Reques ...

  4. 深入理解Apache Kafka

    一.介绍 Kafka在世界享有盛名,大部分互联网公司都在使用它,那么它到底是什么呢? Kafka由LinkedIn公司于2011年推出,自那时起功能逐步迭代,目前演变成一个完整的平台级产品,它允许您冗 ...

  5. A human being,who loves football and music

    ---title: aboutdate: 2019-08-09 20:52:27---[A human being,who loves football and music.](https://eel ...

  6. SpringBoot-Admin的使用

    [**前情提要**]Spring Boot Actuator 提供了对单个 Spring Boot 应用的监控,信息包含应用状态.内存.线程.堆栈等,比较全面的监控了 Spring Boot 应用的整 ...

  7. win7-BIOS中开启AHCI模式电脑蓝屏怎么办?

    win7-BIOS中开启AHCI模式电脑蓝屏怎么办? 来源:U大师 u盘装系统 不少网友都表示给电脑安装win7系统后,如果在BIOS中开启IDE模式就一切正常而为AHCI模式时就会出现蓝屏.其实那是 ...

  8. 常用linux的命令

    常用但是容易忘记的命令 查看java项目的进程 ps -ef | grep java jps 根据进程查询端口 lsof -i | grep pid netstat -nap | grep pid p ...

  9. 在linux中用同一个版本的R 同时安装 Seurat2 和 Seurat3

    在linux中用同一个版本的R 同时安装 Seurat 2 和 Seurat 3 Seurat  作为单细胞分析中的重量级R包,有多好用用,用过的人都知道.Seurat 分析流程基本涵盖了单细胞分析中 ...

  10. Mybatis中使用PageHelper插件进行分页

    分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...