C. String Equality

本文介绍了一个字符串转换问题,目标是通过特定操作将一个字符串变为另一个字符串。详细解释了两种操作方式:字符交换与字符递增替换,并提供了一段C++代码实现解决方案。

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;
}

 

我想将代码private void BindLoginResult(LoginResult result) { if (result.LoginStatus == LoginStatus.UnLogin) { this.lblInfo.Text = "欢迎使用极简单行阅读器,请点击右键菜单注册或登录"; for (int i = 0; i < this.cm.Items.Count; i++) { (this.cm.Items[i] as Control).Visibility = (i < 2) ? Visibility.Visible : Visibility.Collapsed; } this.nmiRegister.Visible = false; this.CheckUpgrade(result); } else { this.lblInfo.Text = "欢迎使用极简单行阅读器,请点击右键菜单打开文本或者网页"; for (int j = 0; j < this.cm.Items.Count; j++) { Control control = this.cm.Items[j] as Control; control.Visibility = (j < 2) ? Visibility.Collapsed : Visibility.Visible; if ((control.Name == "miWebAction") || (control.Name == "miAppend")) { control.Visibility = Visibility.Collapsed; } } if (result.LoginStatus == LoginStatus.Login) { if (!this.CheckUpgrade(result)) { return; } this.BindAppMenuItem(result.AppList); this.BindAppendMenu(result.Append); this.BindChannelMenu(result.Channel); this.ShowMsgWhenLoad(result.Msg, result.MsgCaption, result.MsgUrl, result.MsgCloseFlag); this.nmiRegister.Visible = true; } if ((result.LoginStatus == LoginStatus.Offline) || (result.LoginStatus == LoginStatus.Error)) { this.lblInfo.Text = "欢迎使用极简单行阅读器[离线],请点击右键菜单打开文本"; this.miOpenUParent.Visibility = Visibility.Collapsed; this.spAppCenter.Visibility = Visibility.Collapsed; this.miAppCenter.Visibility = Visibility.Collapsed; this.miAppGame.Visibility = Visibility.Collapsed; this.miAppOther.Visibility = Visibility.Collapsed; this.miRegister.Visibility = Visibility.Collapsed; this.nmiRegister.Visible = false; if (result.LoginStatus == LoginStatus.Error) { this.lblInfo.Text = result.ErrorMsg; } } this.BindHistoryMenuItem(); this.BindHistoryUrlMenuItem(); this.tbMut0.Text = this.lblInfo.Text; this.StartActiveCheck(); this.BindFavMenuItem(); if (this.mod == 1) { this.spMut.Visibility = Visibility.Visible; this.btnLastUrl.Visibility = Visibility.Collapsed; this.lblInfo.Visibility = Visibility.Hidden; this.lblFocus.Text = ""; this.lblLast.Text = ""; this.miMod.Header = "单行模式"; this.miChannel.IsEnabled = false; this.miAuto.IsEnabled = false; this.InitModStyle(); this.ReadLine(false); } } } 2 0x2private void BindLoginResult(LoginResult result) { if (result.LoginStatus == LoginStatus.UnLogin) { this.lblInfo.Text = "欢迎使用极简单行阅读器,请点击右键菜单注册或登录"; for (int i = 0; i < this.cm.Items.Count; i++) { (this.cm.Items[i] as Control).Visibility = (i < 2) ? Visibility.Visible : Visibility.Collapsed; } this.nmiRegister.Visible = false; this.CheckUpgrade(result); } else { this.lblInfo.Text = "欢迎使用极简单行阅读器,请点击右键菜单打开文本或者网页"; for (int j = 0; j < this.cm.Items.Count; j++) { Control control = this.cm.Items[j] as Control; control.Visibility = (j < 2) ? Visibility.Collapsed : Visibility.Visible; if ((control.Name == "miWebAction") || (control.Name == "miAppend")) { control.Visibility = Visibility.Collapsed; } } if (result.LoginStatus == LoginStatus.Login) { if (!this.CheckUpgrade(result)) { return; } this.BindAppMenuItem(result.AppList); this.BindAppendMenu(result.Append); this.BindChannelMenu(result.Channel); this.ShowMsgWhenLoad(result.Msg, result.MsgCaption, result.MsgUrl, result.MsgCloseFlag); this.nmiRegister.Visible = true; } if ((result.LoginStatus == LoginStatus.Offline) || (result.LoginStatus == LoginStatus.Error)) { this.lblInfo.Text = "欢迎使用极简单行阅读器[离线],请点击右键菜单打开文本"; this.miOpenUParent.Visibility = Visibility.Collapsed; this.spAppCenter.Visibility = Visibility.Collapsed; this.miAppCenter.Visibility = Visibility.Collapsed; this.miAppGame.Visibility = Visibility.Collapsed; this.miAppOther.Visibility = Visibility.Collapsed; this.miRegister.Visibility = Visibility.Collapsed; this.nmiRegister.Visible = false; if (result.LoginStatus == LoginStatus.Error) { this.lblInfo.Text = result.ErrorMsg; } } this.BindHistoryMenuItem(); this.BindHistoryUrlMenuItem(); this.tbMut0.Text = this.lblInfo.Text; this.StartActiveCheck(); this.BindFavMenuItem(); if (this.mod == 1) { this.spMut.Visibility = Visibility.Visible; this.btnLastUrl.Visibility = Visibility.Collapsed; this.lblInfo.Visibility = Visibility.Hidden; this.lblFocus.Text = ""; this.lblLast.Text = ""; this.miMod.Header = "单行模式"; this.miChannel.IsEnabled = false; this.miAuto.IsEnabled = false; this.InitModStyle(); this.ReadLine(false); } } } 在反汇编结果中改为private void BindLoginResult(LoginResult result) { this.lblInfo.Text = "欢迎使用极简单行阅读器,请点击右键菜单打开文本或者网页"; for (int j = 0; j < this.cm.Items.Count; j++) { Control control = this.cm.Items[j] as Control; control.Visibility = (j < 2) ? Visibility.Collapsed : Visibility.Visible; if ((control.Name == "miWebAction") || (control.Name == "miAppend")) { control.Visibility = Visibility.Collapsed; } } if (result.LoginStatus == LoginStatus.Login) { if (!this.CheckUpgrade(result)) { return; } this.BindAppMenuItem(result.AppList); this.BindAppendMenu(result.Append); this.BindChannelMenu(result.Channel); this.ShowMsgWhenLoad(result.Msg, result.MsgCaption, result.MsgUrl, result.MsgCloseFlag); this.nmiRegister.Visible = true; } this.BindHistoryMenuItem(); this.BindHistoryUrlMenuItem(); this.tbMut0.Text = this.lblInfo.Text; this.StartActiveCheck(); this.BindFavMenuItem(); if (this.mod == 1) { this.spMut.Visibility = Visibility.Visible; this.btnLastUrl.Visibility = Visibility.Collapsed; this.lblInfo.Visibility = Visibility.Hidden; this.lblFocus.Text = ""; this.lblLast.Text = ""; this.miMod.Header = "单行模式"; this.miChannel.IsEnabled = false; this.miAuto.IsEnabled = false; this.InitModStyle(); this.ReadLine(false); } },需要如何修改 Offset OpCode Operand 0 ldarg.1 1 callvirt Read133.Utils.LoginStatus Read133.Model.LoginResult::get_LoginStatus() 6 ldc.i4.1 7 bne.un.s -> (43) ldarg.0 9 ldarg.0 a ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblInfo f ldstr 欢迎使用极简单行阅读器,请点击右键菜单注册或登录 14 callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 19 ldc.i4.0 1a stloc.0 1b br.s -> (28) ldloc.0 1d ldarg.0 1e ldfld System.Windows.Controls.ContextMenu Read133.MainWindow::cm 23 callvirt System.Windows.Controls.ItemCollection System.Windows.Controls.ItemsControl::get_Items() 28 ldloc.0 29 callvirt System.Object System.Windows.Controls.ItemCollection::get_Item(System.Int32) 2e isinst System.Windows.Controls.Control 33 ldloc.0 34 ldc.i4.2 35 blt.s -> (22) ldc.i4.0 37 ldc.i4.2 38 br.s -> (23) callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 3a ldc.i4.0 3b callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 40 ldloc.0 41 ldc.i4.1 42 add 43 stloc.0 44 ldloc.0 45 ldarg.0 46 ldfld System.Windows.Controls.ContextMenu Read133.MainWindow::cm 4b callvirt System.Windows.Controls.ItemCollection System.Windows.Controls.ItemsControl::get_Items() 50 callvirt System.Int32 System.Windows.Data.CollectionView::get_Count() 55 blt.s -> (11) ldarg.0 57 ldarg.0 58 ldfld System.Windows.Forms.MenuItem Read133.MainWindow::nmiRegister 5d ldc.i4.0 5e callvirt System.Void System.Windows.Forms.MenuItem::set_Visible(System.Boolean) 63 ldarg.0 64 ldarg.1 65 call System.Boolean Read133.MainWindow::CheckUpgrade(Read133.Model.LoginResult) 6a pop 6b ret 6c ldarg.0 6d ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblInfo 72 ldstr 欢迎使用极简单行阅读器,请点击右键菜单打开文本或者网页 77 callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 7c ldc.i4.0 7d stloc.1 7e br.s -> (82) ldloc.1 80 ldarg.0 81 ldfld System.Windows.Controls.ContextMenu Read133.MainWindow::cm 86 callvirt System.Windows.Controls.ItemCollection System.Windows.Controls.ItemsControl::get_Items() 8b ldloc.1 8c callvirt System.Object System.Windows.Controls.ItemCollection::get_Item(System.Int32) 91 isinst System.Windows.Controls.Control 96 stloc.2 97 ldloc.2 98 ldloc.1 99 ldc.i4.2 9a blt.s -> (63) ldc.i4.2 9c ldc.i4.0 9d br.s -> (64) callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 9f ldc.i4.2 a0 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) a5 ldloc.2 a6 callvirt System.String System.Windows.FrameworkElement::get_Name() ab ldstr miWebAction b0 call System.Boolean System.String::op_Equality(System.String,System.String) b5 brtrue.s -> (75) ldloc.2 b7 ldloc.2 b8 callvirt System.String System.Windows.FrameworkElement::get_Name() bd ldstr miAppend c2 call System.Boolean System.String::op_Equality(System.String,System.String) c7 brfalse.s -> (78) ldloc.1 c9 ldloc.2 ca ldc.i4.2 cb callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) d0 ldloc.1 d1 ldc.i4.1 d2 add d3 stloc.1 d4 ldloc.1 d5 ldarg.0 d6 ldfld System.Windows.Controls.ContextMenu Read133.MainWindow::cm db callvirt System.Windows.Controls.ItemCollection System.Windows.Controls.ItemsControl::get_Items() e0 callvirt System.Int32 System.Windows.Data.CollectionView::get_Count() e5 blt.s -> (50) ldarg.0 e7 ldarg.1 e8 callvirt Read133.Utils.LoginStatus Read133.Model.LoginResult::get_LoginStatus() ed brtrue.s -> (122) ldarg.1 ef ldarg.0 f0 ldarg.1 f1 call System.Boolean Read133.MainWindow::CheckUpgrade(Read133.Model.LoginResult) f6 brtrue.s -> (96) ldarg.0 f8 ret f9 ldarg.0 fa ldarg.1 fb callvirt System.Collections.Generic.List`1<Read133.Model.ReApp> Read133.Model.LoginResult::get_AppList() 100 call System.Void Read133.MainWindow::BindAppMenuItem(System.Collections.Generic.List`1<Read133.Model.ReApp>) 105 ldarg.0 106 ldarg.1 107 callvirt System.Collections.Generic.List`1<Read133.Model.LinkInfo> Read133.Model.LoginResult::get_Append() 10c call System.Void Read133.MainWindow::BindAppendMenu(System.Collections.Generic.List`1<Read133.Model.LinkInfo>) 111 ldarg.0 112 ldarg.1 113 callvirt System.String Read133.Model.LoginResult::get_Channel() 118 call System.Void Read133.MainWindow::BindChannelMenu(System.String) 11d ldarg.0 11e ldarg.1 11f callvirt System.String Read133.Model.LoginResult::get_Msg() 124 ldarg.1 125 callvirt System.String Read133.Model.LoginResult::get_MsgCaption() 12a ldarg.1 12b callvirt System.String Read133.Model.LoginResult::get_MsgUrl() 130 ldarg.1 131 callvirt System.Boolean Read133.Model.LoginResult::get_MsgCloseFlag() 136 call System.Void Read133.MainWindow::ShowMsgWhenLoad(System.String,System.String,System.String,System.Boolean) 13b ldarg.0 13c ldfld System.Windows.Forms.MenuItem Read133.MainWindow::nmiRegister 141 ldc.i4.1 142 callvirt System.Void System.Windows.Forms.MenuItem::set_Visible(System.Boolean) 147 ldarg.1 148 callvirt Read133.Utils.LoginStatus Read133.Model.LoginResult::get_LoginStatus() 14d ldc.i4.2 14e beq.s -> (130) ldarg.0 150 ldarg.1 151 callvirt Read133.Utils.LoginStatus Read133.Model.LoginResult::get_LoginStatus() 156 ldc.i4.3 157 bne.un.s -> (171) ldarg.0 159 ldarg.0 15a ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblInfo 15f ldstr 欢迎使用极简单行阅读器[离线],请点击右键菜单打开文本 164 callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 169 ldarg.0 16a ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miOpenUParent 16f ldc.i4.2 170 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 175 ldarg.0 176 ldfld System.Windows.Controls.Separator Read133.MainWindow::spAppCenter 17b ldc.i4.2 17c callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 181 ldarg.0 182 ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miAppCenter 187 ldc.i4.2 188 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 18d ldarg.0 18e ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miAppGame 193 ldc.i4.2 194 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 199 ldarg.0 19a ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miAppOther 19f ldc.i4.2 1a0 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 1a5 ldarg.0 1a6 ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miRegister 1ab ldc.i4.2 1ac callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 1b1 ldarg.0 1b2 ldfld System.Windows.Forms.MenuItem Read133.MainWindow::nmiRegister 1b7 ldc.i4.0 1b8 callvirt System.Void System.Windows.Forms.MenuItem::set_Visible(System.Boolean) 1bd ldarg.1 1be callvirt Read133.Utils.LoginStatus Read133.Model.LoginResult::get_LoginStatus() 1c3 ldc.i4.3 1c4 bne.un.s -> (171) ldarg.0 1c6 ldarg.0 1c7 ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblInfo 1cc ldarg.1 1cd callvirt System.String Read133.Model.LoginResult::get_ErrorMsg() 1d2 callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 1d7 ldarg.0 1d8 call System.Void Read133.MainWindow::BindHistoryMenuItem() 1dd ldarg.0 1de call System.Void Read133.MainWindow::BindHistoryUrlMenuItem() 1e3 ldarg.0 1e4 ldfld System.Windows.Controls.TextBlock Read133.MainWindow::tbMut0 1e9 ldarg.0 1ea ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblInfo 1ef callvirt System.String System.Windows.Controls.TextBlock::get_Text() 1f4 callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 1f9 ldarg.0 1fa call System.Void Read133.MainWindow::StartActiveCheck() 1ff ldarg.0 200 call System.Void Read133.MainWindow::BindFavMenuItem() 205 ldarg.0 206 ldfld System.Int32 Read133.MainWindow::mod 20b ldc.i4.1 20c bne.un.s -> (226) ret 20e ldarg.0 20f ldfld System.Windows.Controls.StackPanel Read133.MainWindow::spMut 214 ldc.i4.0 215 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 21a ldarg.0 21b ldfld System.Windows.Controls.Button Read133.MainWindow::btnLastUrl 220 ldc.i4.2 221 callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 226 ldarg.0 227 ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblInfo 22c ldc.i4.1 22d callvirt System.Void System.Windows.UIElement::set_Visibility(System.Windows.Visibility) 232 ldarg.0 233 ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblFocus 238 ldstr 23d callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 242 ldarg.0 243 ldfld System.Windows.Controls.TextBlock Read133.MainWindow::lblLast 248 ldstr 24d callvirt System.Void System.Windows.Controls.TextBlock::set_Text(System.String) 252 ldarg.0 253 ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miMod 258 ldstr 单行模式 25d callvirt System.Void System.Windows.Controls.HeaderedItemsControl::set_Header(System.Object) 262 ldarg.0 263 ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miChannel 268 ldc.i4.0 269 callvirt System.Void System.Windows.UIElement::set_IsEnabled(System.Boolean) 26e ldarg.0 26f ldfld System.Windows.Controls.MenuItem Read133.MainWindow::miAuto 274 ldc.i4.0 275 callvirt System.Void System.Windows.UIElement::set_IsEnabled(System.Boolean) 27a ldarg.0 27b call System.Void Read133.MainWindow::InitModStyle() 280 ldarg.0 281 ldc.i4.0 282 call System.Void Read133.MainWindow::ReadLine(System.Boolean) 287 ret 内容
10-18
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值