P9539 题解

· · 题解

题目传送门

思路

这题我们有几种操作:

  1. 从前往后,如果 s_i\ne \text a(其中 \text a 是小写字母 \text a,下同),则将其改为 a,这个操作的次数要尽可能多;
  2. 如果相似度还是太高了,那从后往前,将原先就是 \text a 的字母改成 \text b,这个操作的次数要尽可能少。

代码

# include <bits/stdc++.h> //万能头
using namespace std;
int n, l, r, now;
string a, b;
int main () {
    cin >> n >> l >> r >> a;
    l = n - l, r = n - r, swap (l, r);
    b = a; //备份
    for (int i = 0; i < n && now < r; ++ i)
        if (a[i] > 'a')
            b[i] = 'a', ++ now;
    if (now >= l) {
        cout << b;
        return 0;
    }
    for (int i = n - 1; ~ i && now < l; -- i)
        if (a[i] < 'b')
            b[i] = 'b', ++ now;
    cout << b;
    return 0;
}