目录

CF1451C-String Equality

CF1451C-String Equality

题目:

题目描述:

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 \leq i\leq n-1 $ ) and swap $ a_i $ and $ a_{i+1} $ , or
  • choose an index $ i $ ( $ 1 \leq i \leq n-k+1 $ ) and if $ a_i, a_{i+1}, \ldots, a_{i+k-1} $ are all equal to some character $ c $ ( $ c \neq $ ‘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.

输入格式:

The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^5 $ ) — 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 \leq n \leq 10^6 $ ) and $ k $ ( $ 1 \leq k \leq 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 $ 10^6 $ .

输出格式:

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).

样例:

样例输入1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc

样例输出1:

1
2
3
4
No
Yes
No
Yes

思路:

实现:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "ybwhead/ios.h"
string a,b;
int c[30],d[30];
int main()
{
	int TTT;
	yin>>TTT;
	while(TTT--)
	{
		int n,k;
		memset(c,0,sizeof(c));
		memset(d,0,sizeof(d));
		yin>>n>>k;
		yin>>a>>b;
		for(int i=0;i<a.size();i++)c[a[i]-'a']++;
		for(int i=0;i<b.size();i++)d[b[i]-'a']++;
		bool flg=1;
		for(int i=0;i<=26;i++)
		{
			if(c[i]<d[i])
			{
				flg=0;break;
			}
			else
			{
				if((c[i]-d[i])%k!=0)
				{
					flg=0;break;
				}
				else
				{
					c[i+1]+=c[i]-d[i];
				}
			}
		}
		if(flg)
		{
			puts("YES");
		}else puts("NO");
	}
	return 0;
}