题目链接:
https://ac.nowcoder.com/acm/contest/15167/D
题意:
给一个长度为n的字符串,可以修改其中k个非a字母,问修改之后为aa的字串最多是多少,并输出修改后的字符串
解题思路:
观察可得,只有当连续非a字符串填满的时候,我们才可以额外获得一个合法答案,因此我们要尽量先填小的。统计序列中连续非a的字母长度和位置,根据长度进行升序排序,进行填充。要注意的是,对于开头或者结尾是非a的放到排序的末尾,因为这类字符串是不会获得额外字符串的。
代码实现:
#include <bits/stdc++.h>
#define Mid ((l + r) / 2)
#define lson (rt << 1)
#define rson (rt << 1 | 1)
using namespace std;
int read() {
char c; int num, f = 1;
while(c = getchar(),!isdigit(c)) if(c == '-') f = -1; num = c - '0';
while(c = getchar(), isdigit(c)) num = num * 10 + c - '0';
return f * num;
}
struct node {
int len, st;
} a[500009];
int n, k, tot = 1;
char c[500009];
bool cmp(node a, node b) {
if(a.st == 1 || a.st + a.len - 1 == n) return 0;
if(b.st == 1 || b.st + a.len - 1 == n) return 1;
return a.len < b.len;
}
signed main()
{
n = read

本文介绍了一种算法,用于解决给定字符串中通过有限次字符替换以最大化“aa”子串数量的问题。该算法首先统计字符串中连续非'a'字符的长度与位置,然后按长度升序排列并依次替换,确保尽可能多地形成“aa”子串。
最低0.47元/天 解锁文章
885

被折叠的 条评论
为什么被折叠?



