其他分享
首页 > 其他分享> > Codeforce 1451-C. String Equality

Codeforce 1451-C. String Equality

作者:互联网

题目:

C. String Equality time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

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

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.

Input

The first line contains a single integer tt (1≤t≤1051≤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≤n≤1062≤n≤106) and kk (1≤k≤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 106106.

Output

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

Example   input
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc
output
No
Yes
No
Yes
Note

In the first test case it can be shown that it is impossible to convert aa into bb.

In the second test case,

"abba" −→inc→inc "acca" −→inc→inc …… −→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" −→−−swap→swap "aaabab" −→−−swap→swap "aaaabb" −→inc→inc …… −→inc→inc "ddaabb" −→inc→inc …… −→inc→inc "ddddbb" −→inc→inc …… −→inc→inc "ddddcc".

 

人生第一篇博客,纯小白,学了一年的算法,代码简陋,欢迎点评,有错误还请指正,会继续努力写好代码!

题意简述:给你两个长度相同的字符串a和字符串b,均由小写字母组成,可以用以下变换方式:1、相邻的两个字符交换位置。2、k个连续的字母变换为下个字母,但是字符z,不能变化!!!,问能不能把字符串a,变换为字符串b,如果能输出“Yes”,不能输出“No”。

 

大致思路:因为相邻的两个字符交换位置,所以不用考虑顺序(太好了!!),然后只要从‘a’开始遍历,分别统计字符串a和字符串b每个字母出现的次数,在进行比较中和,余下的判断能不能被k整除就可以了。(是不是有点思路了),但是用数组来统计会超时(最后只能用map来统计的QAQ)。

 

详细代码:

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>

using namespace std;
typedef long long ll;
int i, j, k;
int main()
{
    int t, n, k;
    cin >> t;
    string a, b;
    map<int, int>c, d;
    while (t--)
    {
        c.clear();
        d.clear();
        cin >> n >> k;
        cin >> a >> b;
        for (i = 0; i < n; i++)
        {
            c[a[i]-'a']++;
            d[b[i]-'a']++;
        }
        int num = 0, flag = 1;
        for (i = 0; i < 26; i++)
        {
            num += c[i];
            if (num < d[i])
            {
                flag = 0;
                break;
            }
            num -= d[i];
            if (num % k != 0)
            {
                flag = 0;
                break;
            }
        }
        if (flag)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}

最后博主祝各位大佬们新的一年“Happy New Year!”。

标签:case,aa,Equality,String,Codeforce,each,test,string,inc
来源: https://www.cnblogs.com/520chen/p/14253436.html