C. String Equality

194 篇文章 1 订阅
71 篇文章 0 订阅

https://codeforces.com/contest/1451/problem/C

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≤i≤n−11≤i≤n−1) and swap aiai and ai+1ai+1, or
  • choose an index ii (1≤i≤n−k+11≤i≤n−k+1) and if ai,ai+1,…,ai+k−1ai,ai+1,…,ai+k−1 are all equal to some character cc (c≠c≠ '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.

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

Copy

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

output

Copy

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

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+5;
const ll mod=1e9+7;
ll n,t,k,s,q;
char x[maxn],y[maxn];
ll a[30],b[30];
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        cin>>x;
        cin>>y;
        for(int i=0;i<26;i++)
        {
            a[i]=0;
            b[i]=0;
        }
        for(int i=0;i<n;i++)
        {
            a[x[i]-'a']++;
            b[y[i]-'a']++;
        }
        int flag=1;
        for(int i=25;i>=0;i--)
        {
            //cout<<i<<" "<<a[i]<<" "<<b[i]<<endl;
            if(b[i]==0)
            {
                if(a[i]!=0)
                {
                    flag=0;
                    break;
                }
            }
            else
            {
                if(a[i]==b[i])
                {
                    continue;
                }
                else if(a[i]>b[i])
                {
                    flag=0;
                    break;
                }
                else
                {
                    ll u=b[i]-a[i];
                    if(u%k!=0)
                    {
                        flag=0;
                        break;
                    }
                    for(int j=i-1;j>=0;j--)
                    {
                        if(a[j])
                        {
                            if(a[j]>=k)
                            {
                                ll v=min(u/k,a[j]/k);
                                u-=v*k;
                                a[j]-=v*k;
                            }
                        }
                        if(u==0)
                            break;
                    }

                    if(u!=0)
                    {
                        flag=0;
                        break;
                    }
                }
            }
        }
        if(flag)
        {
            cout<<"Yes"<<endl;
        }
        else
            cout<<"No"<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值