codeforces round #685 第六场(div2)

第三题
就是给你两个字符串,a,b,问你能不能把a变成b,有两种操作
1.交换a的相邻两个位置
2.k,给你k,然后要求k个相等的可以都加1,就是假设k=2,aa变成bb,然后也可以变成cc
无限次操作。。

思路

其实挺简单的,就是把a串和b串hash到一个字母数组中,就是存各自有多少个。虽然要求b不能动,但是你可以在下面操作中,实现b动,或者说实现b不动。具体看代码吧。以后这种题还是少用比较复杂的解题思路,虽然不知道上一个思路咋错的,但显然比较复杂,而且还有bug,虽然我觉得哪个bug是对的,

  • 正确代码
#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6 + 10;
#define ll long long
int hsh[30];
int hsh1[30];
int main()
{
    
    
    int t;
    cin >> t;
    while (t--)
    {
    
    
        int n, k;
        cin >> n >> k;
        string a, b;
        cin >> a >> b;

        memset(hsh, 0, sizeof(hsh));
        memset(hsh1, 0, sizeof(hsh1));
        for (int i = 0; i < a.length(); i++)
        {
    
    
            int x = a[i] - 'a';
                hsh[x]++;
        }
        for (int i = 0; i < b.length(); i++)
        {
    
    
            int x = b[i] - 'a';
            hsh1[x]++;
        }
        int i;
        int chabu = 0;
        bool judge = true;
        for (int i = 0; i < 26; i++)
        {
    
    
            if (hsh1[i] < hsh[i])
            {
    
    
                if ((hsh[i] - hsh1[i]) % k != 0)
                {
    
    
                    judge = false;
                    break;
                }
                else
                    chabu += (hsh[i] - hsh1[i]);
            }
            else
            {
    
    
                if ((hsh1[i] - hsh[i]) % k != 0)
                {
    
    
                    judge = false;
                    break;
                }
                else
                {
    
    
                    chabu-= (hsh1[i] - hsh[i]);
                    if (chabu < 0)
                    {
    
    
                        judge = false;
                        break;
                    }
                }
            }
        }
        if (judge == false)
            cout << "No" << endl;
        else
            cout << "YES" << endl;
    }
}

  • 错误代码
#include <bits/stdc++.h>
 
using namespace std;
const int maxn = 1e6 + 10;
#define ll long long
int hsh[30];
 
int main()
{
    
    
    int t;
    cin >> t;
    while (t--)
    {
    
    
        int n, k;
        cin >> n >> k;
        string a, b;
        cin >> a >> b;
 
        memset(hsh, 0, sizeof(hsh));
        for (int i = 0; i < a.length(); i++)
        {
    
    
            int x = a[i] - 'a';
                hsh[x]++;
        }
        int i;
        bool judge = true;
        for (i = 0; i < b.length(); i++)
        {
    
    
            int flag = 0;
            for (int j = i + 1; j < i + k; j++)
            {
    
    
                if (b[j] != b[i])
                {
    
    
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
    
    
                int c = b[i] - 'a';
                bool flag1 = 1;
                for (int d = c; d >= 0; d--)
                {
    
    
                    if (hsh[d] >= k)
                    {
    
    
                        hsh[d] -= k;
                        flag1 = 0;
                        break;
                    }
                }
                if (flag1 == 1)
                {
    
    
                    judge = false;
                }
                if (flag1 == 0)
                    i += (k-1);
            }
        }
        int l;
        for (l = 0; l < b.length(); l++)
        {
    
    
            int flag = 0;
            for (int j = l + 1; j < l + k; j++)
            {
    
    
                if (b[j] != b[l])
                {
    
    
                    flag = 1;
                    break;
                }
            }
            if (flag == 1)
            {
    
    
                int m = b[l] - 'a';
                if (hsh[m] <= 0)
                {
    
    
                    judge = false;
                    break;
                }
                else
                    hsh[m]--;
            }
            else
                l +=( k-1);
        }
        if (judge == false)
            cout << "No" << endl;
        else
            cout << "YES" << endl;
    }
}

在这里插入图片描述
主要的bug我觉得是这里,应该要用哪一个补。

  • 贴一下官方代码
#include <bits/stdc++.h>
using namespace std;

int32_t main() {
    
    
    ios::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;
    while(t--) {
    
    
        int i, n, k; string a, b;
        cin >> n >> k >> a >> b;
        array<int, 27> have{
    
    }, need{
    
    };
        for(auto& c: a)
            have[c-'a']++;
        for(auto& c: b)
            need[c-'a']++;
     
        bool bad = false;
        for(i = 0; i < 26; i++) {
    
    
            if(have[i] < need[i] || (have[i] -= need[i]) % k)
                bad = true;
            have[i+1] += have[i];
        }
        cout << (bad? "No" : "Yes") << '\n';
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46264636/article/details/109919160