zoukankan      html  css  js  c++  java
  • 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
    ";
                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
    ";
                    //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
    ";
        else cout<<"No
    ";
    
    }
    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
    ";
                return;
            }
            if(a[cur]==b[cur])continue;
            a[cur]-=b[cur];
            if(a[cur]%k)
            {
                cout<<"No
    ";
                return;
            }
            a[cur+1]+=a[cur];
        }
        cout<<"Yes
    ";
    
    }
    int main()
    {
    
        int _t;
        _t=read();
        while(_t--)solve();
        return 0;
    }
    /*
    1
    10 2
    debaaaaaba
    beadeeeabe
    
    1
    6 2
    aabbde
    deeeee
    
    */
    
    
  • 相关阅读:
    AT4144[ARC098D]Donation【Kruskal重构树,dp】
    YbtOJ#643机器决斗【贪心,李超树】
    P3273[SCOI2011]棘手的操作【线段树,并查集】
    AT3950[AGC022E]Median Replace【贪心,dp】
    P3760[TJOI2017]异或和【树状数组】
    AT4505[AGC029F]Construction of a tree【构造题,hall定理,网络流】
    Ybt#452序列合并【期望dp】
    AT3949[AGC022D]Shopping【贪心】
    AT4995[AGC034E] Complete Compress【树形dp】
    P4338[ZJOI2018]历史【LCT】
  • 原文地址:https://www.cnblogs.com/et3-tsy/p/14018237.html
Copyright © 2011-2022 走看看