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 ...
随机推荐
- Nuxt的SEO实践
第9章:Nuxt的SEO实践 1. 引言 Nuxt框架在SEO方面的优势主要体现在以下几个方面: 服务器端渲染(SSR): Nuxt默认支持SSR,这意味着搜索引擎爬虫可以直接看到完整的页面内容,而不 ...
- Java进阶知识点:接口幂等性
幂等概念 在计算机中,表示对同一个过程应用相同的参数多次和应用一次产生的效果是一样,这样的过程即被称为满足幂等性. 也可以进行如下表述:一个HTTP请求方法,如果被请求多次和被请求一次效果 ...
- 浅谈Spring、Spring MVC、Spring Boot和Spring Cloud的关系和区别
Spring 框架就像一个家族,有众多衍生产品,例如 boot.security.jpa等等.但它们的基础都是Spring的IOC和AOP等.IOC提供了依赖注入的容器,AOP解决了面向横切面编程 ...
- 代码随想录第五天 | 哈希表part01
哈希表理论基础 建议:大家要了解哈希表的内部实现原理,哈希函数,哈希碰撞,以及常见哈希表的区别,数组,set 和map. 什么时候想到用哈希法,当我们遇到了要快速判断一个元素是否出现集合里的时候,就要 ...
- C++项目提示“error MSB4019: 找不到导入的项目”
报错:C:\Users\DELL\Desktop\PD Manager-20240912(1)\PD Manager\PD Manager\PD Manager.vcxproj(66,5): erro ...
- 基于语义检索的知识问答(RAG范式)
知识驱动型AI应用场景 知识驱动型AI应用场景式企业级AI智能体的常见抓手.该类型的场景能充分利用大语言模型的自然语言处理能力,相对独立的提供全新的用户体验.落地该场景,可以在有限的预算内大幅提升企业 ...
- 有关Spring事务的传播机制
这是一个非常常见的关于 Spring 事务传播机制 的问题,核心问题是: 在同一个类中,方法 A 调用方法 B,而方法 B 上有 @Transactional 注解.当调用方法 A 时,如果发生异常, ...
- 从数组和List中随机抽取若干不重复的元素
一.从数组中随机抽取若干不重复元素 /** * @function:从数组中随机抽取若干不重复元素 * * @param paramArray:被抽取数组 * @param count:抽取元素的个数 ...
- 使用Spread控件构建Checkbook工程的技术指南
引言 在现代Web应用开发中,电子表格控件已成为处理财务数据.报表展示等场景的重要工具.葡萄城的Spread控件作为一款功能强大的ASP.NET表格组件,为开发者提供了丰富的API和灵活的定制能力.本 ...
- Selenui + Cucumber framwork structure