题解 | 小红的大小写字母

小红的大小写字母

https://www.nowcoder.com/practice/e82a7b09d26c4b20b5b4abc468ee6619

存一下大小写字符个数,分两种情况讨论,一种是k次全可以反转(c1 >= k),另一种就是尽量反转所有小写,剩余次数只针对一个反转,如果剩余次数为奇数没办法只能少得到一个大写字母,因为必须刚好用完k次

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

void solve() {
    int n,k;
    cin >> n >> k;
    string s; cin >> s;
    int c1 = 0,c2 = 0;
    for(char x : s) {
        if(x >= 'a' && x <= 'z') c1++;
        else c2++;
    }
    if(c1 >= k) cout << k + c2 << endl;
    else {
        if((k - c1) % 2) c1--;
        // cerr << c1 << " " << c2 << endl;
        cout << c1 + c2 << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // cin >> T;
    while (T--) solve();
    return 0;
}

全部评论

相关推荐

纯真的河老师在喝茶:第一个是这个时间点岗位少,第二个是这个简历重复度太高了,10个有9个简历差不多的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务