CF1451C String Equality
Posted 诸葛阵御
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1451C String Equality相关的知识,希望对你有一定的参考价值。
题目描述
Ashish has two strings aa and bb , each of length nn , and an integer kk . The strings only contain lowercase English letters.
He wants to convert string aa into string bb by performing some (possibly zero) operations on aa .
In one move, he can either
- choose an index ii ( 1 \\leq i\\leq n-11≤i≤n−1 ) and swap a_iai and a_{i+1}ai+1 , or
- choose an index ii ( 1 \\leq i \\leq n-k+11≤i≤n−k+1 ) and if a_i, a_{i+1}, \\ldots, a_{i+k-1}ai,ai+1,…,ai+k−1 are all equal to some character cc ( c \\neqc= 'z'), replace each one with the next character (c+1)(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 aa .
Help Ashish determine if it is possible to convert string aa into bb after performing some (possibly zero) operations on it.
输入格式
The first line contains a single integer tt ( 1 \\leq t \\leq 10^51≤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 nn ( 2 \\leq n \\leq 10^62≤n≤106 ) and kk ( 1 \\leq k \\leq n1≤k≤n ).
The second line of each test case contains the string aa of length nn consisting of lowercase English letters.
The third line of each test case contains the string bb of length nn consisting of lowercase English letters.
It is guaranteed that the sum of values nn among all test cases does not exceed 10^6106 .
输出格式
For each test case, print "Yes" if Ashish can convert aa into bb after some moves, else print "No".
You may print the letters of the answer in any case (upper or lower).
题意翻译
本题有多组数据。
第一行输入一个整数 t,表示数据组数。
对于每组数据:
第一行输入两个正整数 n,k。
第二行输入一个长度为 n 字符串 a。
第三行输入一个长度为 n 字符串 b。
Ashish 会对字符串 a 进行以下两种操作:
- 选择一个位置 i(1 ≤ i ≤ n−1)并交换 a_i 和 a_{i+1}。
- 选择一个位置 i(1≤i≤n−k+1),满足 ai,ai+1,…,ai+k−1 都等于某个字符 c(c!='z'),把 ai,ai+1,…,ai+k−1 都变成 (c+1)。
你需要回答 Ashish 能否通过若干个(可能是零个)操作将字符串 a 变成字符串 b。如果可以,请输出 Yes
,否则,输出 No
。
输入输出样例
输入 #1复制
4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc
输出 #1复制
No Yes No Yes
说明/提示
In the first test case it can be shown that it is impossible to convert aa into bb .
In the second test case,
"abba" \\xrightarrow{\\text{inc}}inc "acca" \\xrightarrow{\\text{inc}}inc \\ldots… \\xrightarrow{\\text{inc}}inc "azza".
Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.
In the fourth test case,
"aaabba" \\xrightarrow{\\text{swap}}swap "aaabab" \\xrightarrow{\\text{swap}}swap "aaaabb" \\xrightarrow{\\text{inc}}inc \\ldots… \\xrightarrow{\\text{inc}}inc "ddaabb" \\xrightarrow{\\text{inc}}inc \\ldots… \\xrightarrow{\\text{inc}}inc "ddddbb" \\xrightarrow{\\text{inc}}inc \\ldots… \\xrightarrow{\\text{inc}}inc "ddddcc".
思路:
题中未对操作次数要求,故可无限次执行操作1,则此题中无需注意顺序,只需统计每种字母出现的次数,题目就变为了每次可将k个字母变成它们的下一个字母,能否将字符串a变为字符串b
代码
#include <stdio.h>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while (t--) {
int n,k;
cin>>n>>k;
int a[30]={0};
int b[30]={0};
char x;
//统计字母出现次数
for (int i=0;i<n;i++) {
cin>>x;
a[x-'a']++;
}
for (int i=0;i<n;i++) {
cin>>x;
b[x-'a']++;
}
int f=0;
for (int i=0;i<26-1;i++) {
if (a[i]%k==b[i]%k&&a[i]>=b[i]) { //能通过每次将k个字母变为下一个字母使字母出现次数相同 并且a串中该字母出现次数大于b串中
a[i+1]=a[i+1]+(a[i]-b[i]); //将多余的放到下一个
} else {
f=1;
break;
}
}
if (f==1||a[25]!=b[25]) { //25为字母'z',若最后两串中字母'z'的数目对不上也否
cout<<"No"<<endl;
} else {
cout<<"Yes"<<endl;
}
}
return 0;
}
因为循环(a~y)中每次将a串中该字母出现次数大于b串中该字母出现的次数(因为第一个if条件,a串中该字母次数减去b串中该字母出现次数必为k的倍数,a串中该字母出现次数也必定大于等于b串中该字母出现次数,不符合条件的字符串无论操作多少次都无法成功)条件加到下一个字母的出现次数(即执行了操作2),最后需再判断下字母'z'的情况
以上是关于CF1451C String Equality的主要内容,如果未能解决你的问题,请参考以下文章
Satisfiability of Equality Equations - LeetCode