Codeforces Round #685 (Div. 2) C. String Equality(简单思维)

2023-10-30 04:08

本文主要是介绍Codeforces Round #685 (Div. 2) C. String Equality(简单思维),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:https://codeforc.es/contest/1451/problem/C

Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters.

He wants to convert string a into string b by performing some (possibly zero) operations on a.

In one move, he can either

choose an index i (1≤i≤n−1) and swap ai and ai+1, or
choose an index i (1≤i≤n−k+1) and if ai,ai+1,…,ai+k−1 are all equal to some character c (c≠ ‘z’), replace each one with the next character (c+1), that is, ‘a’ is replaced by ‘b’, ‘b’ is replaced by ‘c’ and so on.
Note that he can perform any number of operations, and the operations can only be performed on string a.

Help Ashish determine if it is possible to convert string a into b after performing some (possibly zero) operations on it.

Input
The first line contains a single integer t (1≤t≤105) — the number of test cases. The description of each test case is as follows.

The first line of each test case contains two integers n (2≤n≤106) and k (1≤k≤n).

The second line of each test case contains the string a of length n consisting of lowercase English letters.

The third line of each test case contains the string b of length n consisting of lowercase English letters.

It is guaranteed that the sum of values n among all test cases does not exceed 106.

Output
For each test case, print “Yes” if Ashish can convert a into b after some moves, else print “No”.

You may print the letters of the answer in any case (upper or lower).

Example

input

4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc

output

No
Yes
No
Yes

题意

给出两个同样长度的字符串 a , b 。可以对 a 做如下操作:1、将两个相邻字符交换;2、将连续 k 个相同字符都变为他们的下一个( a 变为 b , b 变为 c…… z 不能变)。问能否将 a 变为 b 。

分析

因为操作 1 ,我们可以发现最终字符的顺序我们是不用管的,只需要将 a 变成各字符数和 b 都对应相等就可以了。
我们从 ‘a’ 字符开始遍历 26 个字母,如果 b 中有,就抵消掉,如果抵消过后还有剩余字数,那么肯定要都转化成下一个字母,如果转化不完,说明 a 必定不能转化为 b ,如果某种字母不够,那么说明也是不能的。
注意判断是否转化了 ‘z’ 。

代码
#include<bits/stdc++.h>
using namespace std;int t;
int n,k;
int a[2][27];
char s[1000007];int main()
{scanf("%d",&t);while(t--){memset(a, 0, sizeof(a));scanf("%d%d",&n,&k);scanf("%s",s);for(int i=0;i<n;i++)a[0][s[i] - 'a']++;scanf("%s",s);for(int i=0;i<n;i++)a[1][s[i] - 'a']++;int flag = 1;for(int i=0;i<26;i++){if(a[0][i] < a[1][i]){flag = 0;break;}int minn = min(a[0][i], a[1][i]);a[0][i] -= minn;a[1][i] -= minn;if(a[0][i] % k != 0){flag = 0;break;}a[0][i + 1] += a[0][i];a[0][i] = 0;}if(a[0][26] > 0) flag = 0;//如果有则说明转化了z,但z是不能转化的 if(flag == 1) printf("Yes\n");else printf("No\n");}return 0;
}

这篇关于Codeforces Round #685 (Div. 2) C. String Equality(简单思维)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/305483

相关文章

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

SpringBoot整合Apache Spark实现一个简单的数据分析功能

《SpringBoot整合ApacheSpark实现一个简单的数据分析功能》ApacheSpark是一个开源的大数据处理框架,它提供了丰富的功能和API,用于分布式数据处理、数据分析和机器学习等任务... 目录第一步、添加android依赖第二步、编写配置类第三步、编写控制类启动项目并测试总结ApacheS

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf

C++中处理文本数据char与string的终极对比指南

《C++中处理文本数据char与string的终极对比指南》在C++编程中char和string是两种用于处理字符数据的类型,但它们在使用方式和功能上有显著的不同,:本文主要介绍C++中处理文本数... 目录1. 基本定义与本质2. 内存管理3. 操作与功能4. 性能特点5. 使用场景6. 相互转换核心区别

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

redis数据结构之String详解

《redis数据结构之String详解》Redis以String为基础类型,因C字符串效率低、非二进制安全等问题,采用SDS动态字符串实现高效存储,通过RedisObject封装,支持多种编码方式(如... 目录一、为什么Redis选String作为基础类型?二、SDS底层数据结构三、RedisObject