CF 1675E - Replace With the Previous, Minimize

Replace With the Previous, Minimize

You are given a string s s s of lowercase Latin letters.
The following operation can be used:

  • select one character (from ‘a’ to ‘z’) that occurs at least once in the string. And replace all such characters in the string with the previous one in alphabetical order on the loop. For example, replace all ‘c’ with ‘b’ or replace all ‘a’ with ‘z’.

And you are given the integer k k k — the maximum number of operations that can be performed. Find the minimum lexicographically possible string that can be obtained by performing no more than k k k operations.
The string a = a 1 a 2 … a n a=a_1a_2…a_n a=a1a2an is lexicographically smaller than the string b = b 1 b 2 … b n b=b_1b_2…b_n b=b1b2bn if there exists an index k ( 1 ≤ k ≤ n ) k (1≤k≤n) k(1kn) such that a 1 = b 1 , a 2 = b 2 , . . . , a k − 1 = b k − 1 a_1=b_1, a_2=b_2, ..., a_k−1=b_k−1 a1=b1,a2=b2,...,ak1=bk1, but a k < b k a_k<b_k ak<bk.
 
Input
The first line contains a single integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) —the number of test cases in the test.
This is followed by descriptions of the test cases.
The first line of each test case contains two integers n n n and k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 1 ≤ k ≤ 1 0 9 ) k (1≤n≤2⋅10^5, 1≤k≤10^9) k(1n2105,1k109) — the size of the string s s s and the maximum number of operations that can be performed on the string s s s.
The second line of each test case contains a string s s s of length n n n consisting of lowercase Latin letters.
It is guaranteed that the sum n n n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2105.
 
Output
For each test case, output the lexicographically minimal string that can be obtained from the string s s s by performing no more than k k k operations.
 
Example
input

4
3 2
cba
4 5
fgde
7 5
gndcafb
4 19
ekyv

output

aaa
agaa
bnbbabb
aapp

题目大意
给你一个只有小写字符 a a a ~ z z z 构成的字符串 s s s ,和将字符串 s s s 内相同字符前移一次的最大次数 k k k ,且使得该字符串字典序最小,最后输出该字符串。
解题思路
因为要字典序最小,所以我们要尽量使前面的字符转化为字符 a a a 或者靠近字符 a a a 。转化分为两种情况:

  • 若该字符能在 k k k 步内转化为字符 a a a ,那么其实所有比它小的字符都能转化为字符 a a a
  • 若该字符不能在 k k k 步内转化为字符 a a a ,那么就看上一种情况后剩余的步数可以将该字符转化为什么字符,它就是剩余步数可达的字符,把所有在可达字符到该字符之间的字符都可以转化为可达字符。

最后把所有小于等于第一种情况的字符都转化为字符 a a a 就可以了。

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t; cin>>t;
    while(t--)
    {
        int n, k; cin>>n>>k;
        string s; cin>>s;
        char mxCh = 'a'; //至少可以把字符 a 都换成 a (废话
        for(auto ch:s)
        {
            if(ch-'a' > k) // k 步数不能将 小于或等于 该类字符直接转化为 a (步数不够
            {
                char mnCh = char(ch - (k-mxCh) - 'a'); //剩余步数可达的最小字符
                for(auto& cc:s) //将 可达字符 到 第一个无法直接转化为 a 的字符都转化为可达字符
                    if(cc>=mnCh && cc<=ch) cc = mnCh;
                break;
            }
            mxCh = max(mxCh, ch); //可以将 小于或等于 该类字符直接转化为 a 的字符 (同时 -'a' 后也是用了的步数
        }
        for(auto& cc:s) //把能直接转化为 a 的转化掉
            if(cc <= mxCh) cc = 'a';
        cout<<s<<'\n';
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花生ono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值