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 基于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

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接