Codeforces Round #685 (Div. 2) C

Codeforces Round #685 (Div. 2) C

大意

给你长度都为 (N) 且只有小写字母的字符串 (a,b) ,和一个数字 (k)

你可以对 (a) 进行如下操作:

  1. 交换相邻两个字符的位置
  2. 将连续 (k) 个相同的字符变成下一个字符,无法从 (z) 变为 (a)

问:

是否可以让 (a) 在任意次数的变换后和 (b) 相等。

思路

首先,由操作一可以推出任意两个字符的位置都可以被调换。

那么无需考虑 (a,b) 中字符的相对位置,仅需考虑字符的数量关系。

从z到a考虑,(a,b) 中每个字母数量的差值都应该是 (k) 的倍数。

其次,因为 (a) 中字母只能从小往大升,所以说从大往小考虑过程中 (b) 的字母总数量一定时刻都要大于等于 (a) 的字母总数量。

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

#define ll long long
#define ull unsigned long long
#define cint const int&
#define Pi acos(-1)

const int mod = 998244353;
const int inf_int = 0x7fffffff;
const ll inf_ll = 0x7fffffffffffffff;
const double ept = 1e-9;

int t, n, k;
string a, b;
int sum[27][2];

int main() {
    cin >> t;
    while(t--) {
        memset(sum, 0, sizeof sum);
        cin >> n >> k >> a >> b;
        for(int i=0; i<n; i++)
            ++sum[a[i]-'a'+1][0], ++sum[b[i]-'a'+1][1];
        int s=0;
        bool flag=0;
        for(int i=26; i && !flag; i--) {
            if(abs(sum[i][1]-sum[i][0])%k) flag=1;
            else s += sum[i][1]-sum[i][0];
            if(s<0) flag=1;
        }
        if(!flag) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ullio/p/14021010.html