Codeforces #685 div2

CF div2 #685

C. String Equality

1
6 2
aabbde
deeeee

我的想法是直接排序然后模拟加

但是像这一组就炸了

问题在于它加的模拟是从当前最小的起步,这里没有办法保证是不是最小的,本来应该让d加到e,中间两个b一起的,但是这里就成了b单飞了。

WA的代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define max(a,b) (a>b ? a:b)
#define min(a,b) (a<b ? a:b)
#define swap(a,b) (a^=b^=a^=b)
#define maxn 2050000
#define minn -105
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'|ch>'9')last=ch,ch=getchar();
    while(ch>='0'&&ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    return ans;
}
string s;

int n,k;
int a[maxn],b[maxn];
int c[maxn];
void solve()
{
    cin>>n>>k;
    cin>>s;
    for(int i=0;i<n;i++)a[i]=s[i]-'a';
    cin>>s;
    for(int i=0;i<n;i++)b[i]=s[i]-'a';
    sort(a,a+n);
    sort(b,b+n);
    bool ok=1;
    int cur=0;
    for(;cur<n-k+1;cur++)
    {
        //cout<<cur<<endl;
        if(a[cur]-b[cur]>0)
        {
            cout<<"No\n";
            return;
        }
        if(a[cur]==b[cur])continue;
        int tmp=b[cur]-a[cur];
        for(int i=1;i<k;i++)
        {
            if(a[cur+i]!=a[cur])
            {
                cout<<"No\n";
                //cout<<cur<<" "<<cur+i<<endl;
                return;
            }
            a[cur+i]+=tmp;
        }
        a[cur]+=tmp;
    }
    for(;cur<n;cur++)
        if(a[cur]!=b[cur])ok=0;
    if(ok)cout<<"Yes\n";
    else cout<<"No\n";

}
int main()
{

    int _t;
    _t=read();
    while(_t--)solve();
    return 0;
}
/*
1
10 2
debaaaaaba
beadeeeabe

1
6 2
aabbde
deeeee

*/

AC的代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define max(a,b) (a>b ? a:b)
#define min(a,b) (a<b ? a:b)
#define swap(a,b) (a^=b^=a^=b)
#define maxn 27
#define minn -105
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'|ch>'9')last=ch,ch=getchar();
    while(ch>='0'&&ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    return ans;
}
string s;

int n,k;
int a[maxn],b[maxn];
int c[maxn];
void solve()
{
    cin>>n>>k;
    cin>>s;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(int i=0;i<n;i++)a[s[i]-'a']++;
    cin>>s;
    for(int i=0;i<n;i++)b[s[i]-'a']++;
    for(int cur=0;cur<26;cur++)
    {
        if(a[cur]-b[cur]<0)
        {
            cout<<"No\n";
            return;
        }
        if(a[cur]==b[cur])continue;
        a[cur]-=b[cur];
        if(a[cur]%k)
        {
            cout<<"No\n";
            return;
        }
        a[cur+1]+=a[cur];
    }
    cout<<"Yes\n";

}
int main()
{

    int _t;
    _t=read();
    while(_t--)solve();
    return 0;
}
/*
1
10 2
debaaaaaba
beadeeeabe

1
6 2
aabbde
deeeee

*/

posted @ 2020-11-22 08:58  et3_tsy  阅读(113)  评论(0编辑  收藏  举报