CF1383A String Transformation 1 (并查集)
codeforces链接:https://codeforces.com/problemset/problem/1383/A
CF1383A String Transformation 1
题目描述
Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter $ y $ Koa selects must be strictly greater alphabetically than $ x $ (read statement for better understanding). You can make hacks in these problems independently.
Koa the Koala has two strings $ A $ and $ B $ of the same length $ n $ ( $ |A|=|B|=n $ ) consisting of the first $ 20 $ lowercase English alphabet letters (ie. from a to t).
In one move Koa:
- selects some subset of positions $ p_1, p_2, \ldots, p_k $ ( $ k \ge 1; 1 \le p_i \le n; p_i \neq p_j $ if $ i \neq j $ ) of $ A $ such that $ A_{p_1} = A_{p_2} = \ldots = A_{p_k} = x $ (ie. all letters on this positions are equal to some letter $ x $ ).
- selects a letter $ y $ (from the first $ 20 $ lowercase letters in English alphabet) such that $ y>x $ (ie. letter $ y $ is strictly greater alphabetically than $ x $ ).
- sets each letter in positions $ p_1, p_2, \ldots, p_k $ to letter $ y $ . More formally: for each $ i $ ( $ 1 \le i \le k $ ) Koa sets $ A_{p_i} = y $ . Note that you can only modify letters in string $ A $ .
Koa wants to know the smallest number of moves she has to do to make strings equal to each other ( $ A = B $ ) or to determine that there is no way to make them equal. Help her!
输入格式
Each test contains multiple test cases. The first line contains $ t $ ( $ 1 \le t \le 10 $ ) — the number of test cases. Description of the test cases follows.
The first line of each test case contains one integer $ n $ ( $ 1 \le n \le 10^5 $ ) — the length of strings $ A $ and $ B $ .
The second line of each test case contains string $ A $ ( $ |A|=n $ ).
The third line of each test case contains string $ B $ ( $ |B|=n $ ).
Both strings consists of the first $ 20 $ lowercase English alphabet letters (ie. from a to t).
It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .
输出格式
For each test case:
Print on a single line the smallest number of moves she has to do to make strings equal to each other ( $ A = B $ ) or $ -1 $ if there is no way to make them equal.
输入输出样例 #1
输入 #1
5
3
aab
bcc
4
cabc
abcb
3
abc
tsr
4
aabd
cccd
5
abcbd
bcdda
输出 #1
2
-1
3
2
-1
说明/提示
- In the $ 1 $ -st test case Koa:
- selects positions $ 1 $ and $ 2 $ and sets $ A_1 = A_2 = $ b ( $ \color{red}{aa}b \rightarrow \color{blue}{bb}b $ ).
- selects positions $ 2 $ and $ 3 $ and sets $ A_2 = A_3 = $ c ( $ b\color{red}{bb} \rightarrow b\color{blue}{cc} $ ).
- In the $ 2 $ -nd test case Koa has no way to make string $ A $ equal $ B $ .
- In the $ 3 $ -rd test case Koa:
- selects position $ 1 $ and sets $ A_1 = $ t ( $ \color{red}{a}bc \rightarrow \color{blue}{t}bc $ ).
- selects position $ 2 $ and sets $ A_2 = $ s ( $ t\color{red}{b}c \rightarrow t\color{blue}{s}c $ ).
- selects position $ 3 $ and sets $ A_3 = $ r ( $ ts\color{red}{c} \rightarrow ts\color{blue}{r} $ ).
思路:
首先这道题目是A转换成B,同时改变时候只能由字母表前面位置的字母转换成位置靠后的字母,不难发现,无解的情况是,A中相同位置的字母出现大于B中相同位置的字母,那么正常情况下,我们可以使用并查集进行维护,只有Ai的父节点不等于Bi的父节点时候,进行建边,同时操作数++
题解
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
string s1,s2;
int t;
int ans,len;
vector<int> father = vector<int> (26, 0);
void init() {
for (int i = 0; i < 26; ++i) {
father[i] = i;
}
}
int find(int u) {
return u == father[u] ? u : father[u]=find(father[u]);
}
void join(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return ;
ans++;
father[v] = u;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
int cmd=0;
ans=0;
cin>>len;
cin>>s1>>s2;
init();
for(int i=0;i<len;i++)
{
if(s1[i]>s2[i])
{
cmd=1;
break;
}
join(s1[i]-'a',s2[i]-'a');
}
if(cmd)
{
cout<<-1<<endl;
}
else
{
cout<<ans<<endl;
}
}
return 0;
}
CF1383A String Transformation 1 (并查集)的更多相关文章
- String Reconstruction (并查集)
并查集维护和我这个位置的字母连续的已经被填充的字母能到达的最右边的第一个还没有填充的位置,然后把这个位置填上应该填的东西,然后把这个位置和下一个位置连接起来,如果下一个位置还没有填,我就会把下一个位置 ...
- CodeForces 828C String Reconstruction(并查集思想)
题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...
- CF954I Yet Another String Matching Problem 并查集、FFT
传送门 题意:给出两个由小写$a$到$f$组成的字符串$S$和$T$($|S| \geq |T|$),给出变换$c1\,c2$表示将两个字符串中所有$c1$字符变为$c2$,求$S$的每一个长度为$T ...
- Codeforces 828C String Reconstruction【并查集巧妙运用】
LINK 题目大意 给你n个串和在原串中的出现位置,问原串 思路 直接跑肯定是GG 考虑怎么优化 因为保证有解,所以考虑过的点我们就不再考虑 用并查集维护当前每个点之后最早的没有被更新过的点 然后就做 ...
- hiho_1066_并查集
题目大意 给出N个操作,每个操作可能为两种类型之一: 1. 认定两个人属于同一个组织 2. 查询两个人是否是同一个组织 要求对于每个操作类型2,给出结果,即查询的两个人是否属于同一个组织.其中,任何人 ...
- 1034. Head of a Gang (30) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...
- 【8.22校内测试】【数学】【并查集】【string】
今天的t2t3能打出来80分的暴力都好满足啊QwQ.(%%%$idy$ 今天的签到题,做的时候一眼就看出性质叻qwq.大于11的所有数分解合数都可以用4.6.9表示,乱搞搞就可以了. #include ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
随机推荐
- maven导入org.apache.pdfbox
PDF和图片相互转换用到的maven依赖如下: <dependency> <groupId>org.apache.pdfbox</groupId> <arti ...
- [python] python抽象基类使用总结
在Python中,抽象基类是一类特殊的类,它不能被实例化,主要用于作为基类被其他子类继承.抽象基类的核心作用是为一组相关的子类提供统一的蓝图或接口规范,明确规定子类必须实现的方法,从而增强代码的规范性 ...
- 【非对称加密】详解及Java实现
非对称加密详解及Java实现 一.非对称加密概述 非对称加密(Asymmetric Cryptography),也称为公钥加密,是一种使用一对密钥(公钥和私钥)进行加密和解密的加密方法.它与对称加密的 ...
- Scratch之Android的Animation动画的四种动画效果——透明度渐变动画
废话不多说,先上图为敬 效果演示 编写的程序展示 讲话开始: 怎么想到的 在平时上课的时候,有一个学生拿着他好基友写的游戏程序给我看,最开始写的原型是叫虚像的积木块程序.通过一个局部变量开关控制虚像是 ...
- 智能指标 AIMetrics 赋能:构建一体化数据智能决策中枢
数据,究竟是静态的历史记录还是流动的企业资产?随着企业依赖数据决策的加深,这个问题变得愈加关键.过去,我们常常把数据看作"存储"的对象,但在今天,数据正逐步成为推动智能决策.创新和 ...
- Docker安装与基础使用
一.Docker介绍 Docker介绍 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.D ...
- Luogu P7503 「HMOI R1」文化课
题传 先想一个巨 shaber 的暴力 DP:设 \(f_{i}\) 为对前 \(i\) 个人分段的最优解,则: \[f_{i}=\max_{0\le j<i}\{f_{j}+\operator ...
- SQL Server中使用临时表进行数据备份与恢复
在日常的数据库管理中,我们经常需要对数据进行备份和恢复操作.SQL Server提供了多种工具和命令来帮助我们完成这些任务.本文将介绍一种简单的方法,即使用临时表来备份特定记录,清空表,然后将数据恢复 ...
- Xamarin.Android 特性-ActivityAttribute -中文说明
using System; // 指定该特性用于类,并且不可重复或被继承 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, ...
- C# Splitcontainer控件固定Panel的大小
https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/112222711 IsSpliterFixed属性设为False FixedPa ...