题解 | 小红的大小写字母
小红的大小写字母
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;
}
查看8道真题和解析
