P9539 题解

题目传送门

思路

这题我们有几种操作:

  1. 从前往后,如果 sias_i\ne \text a(其中 a\text a 是小写字母 a\text a,下同),则将其改为 aa,这个操作的次数要尽可能多;
  2. 如果相似度还是太高了,那从后往前,将原先就是 a\text a 的字母改成 b\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;
}
posted @ 2023-08-15 21:39  Vitamin_B  阅读(10)  评论(0)    收藏  举报  来源