跳到正文

题解

P9539 题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年8月15日
收录
2025年12月12日
更新
2026年10月28日
篇幅
约 1 分钟
获赞
3
题目
P9539

版本

  1. v1当前

题目传送门

思路

这题我们有几种操作:

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

代码

C++
# 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;
}
原文