这道题的解法是真的妙
注意int会向下取整
#include <iostream> #include <algorithm> #include <string> #include <map> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; map<int, int> a, A; for (auto c : s) { if (c >= 'a' && c <= 'z') a[c - 'a']++; else A[c - 'A']++; } int res = 0, cnt = 0; for (int i = 0; i < 26; i++) { res += min(a[i], A[i]); cnt += abs(a[i] - A[i]) / 2; } res += min(cnt, k); cout << res << endl; } return 0; }
Code over!