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. 定时延时设计FPGA

    以50MHZ时钟为例,进行1秒钟延时,并输出延时使能信号. 首先计算需要多少次计时,MHZ=10的六次方HZ.T=20ns 一秒钟需要计时次数为5的七次方即5000_0000. 然后计算需要几位的寄存 ...

  2. 自定义SWT控件四之其它下拉框

    4.其它下拉框 4.1 添加联动二级多选择框(有添加按钮和删除按钮) package com.view.control.select; import java.util.ArrayList; impo ...

  3. S2:类的构造函数

    类的构造函数构造函数名与类名形同,不返回任何值,主要完成对象的初始化工作. (1)在构造函数中,可以给属性设置默认值(2)this只带当前对象 (3)如果不给属性赋初始值,则会以默认值来填充.(4)如 ...

  4. C#下载文件,Stream 和 byte[] 之间的转换

    stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...

  5. Java - 自动配置log4j的日志文件路径

    目录 1 日志路径带来的痛点 2 log4j.properties文件的配置 3 彻底解决痛点 3.1 单独的Java程序包 (非Java Web项目) 3.2 Web项目 4 附录 - 获取当前项目 ...

  6. CentOS 7服务器安装brook和bbr加速

    一.安装Brook 执行一键部署脚本 $ wget -N --no-check-certificate wget -N --no-check-certificate https://raw.githu ...

  7. .netcore持续集成测试篇之开篇简介及Xunit基本使用

    系列目录 为了支持跨平台,微软为.net平台提供了.net core test sdk,这样第三方测试框架诸如Nunit,Xunit等只需要按照sdk提供的api规范进行开发便可以被dotnet cl ...

  8. Java Web基础面试题整理

    Tomcat的缺省端口是多少,怎么修改 tomcat默认缺省端口是8080 修改方法: 找到Tomcat目录下的conf文件夹 进入conf文件夹里面找到server.xml文件 打开server.x ...

  9. 前端小知识-js

    一.对象冒充 function student(name,age){ this.name = name; this.age = age; this.show = function(){ console ...

  10. Linux 中如何查询端口被占用的情况

    Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000,如下图 可以看到8000端口已经被轻量级文件系统转发服务lwfs ...