练习cf1800B. Count the Number of Pairs

题目如下
B. Count the Number of Pairs
time limit per test2 seconds
memory limit per test256 megabytes
Kristina has a string 𝑠 of length 𝑛, consisting only of lowercase and uppercase Latin letters. For each pair of lowercase letter and its matching uppercase letter, Kristina can get 1 burl. However, pairs of characters cannot overlap, so each character can only be in one pair.

For example, if she has the string 𝑠 = "aAaaBACacbE", she can get a burl for the following character pairs:

𝑠1 = "a" and 𝑠2 = "A"
𝑠4 = "a" and 𝑠6 = "A"
𝑠5 = "B" and 𝑠10 = "b"
𝑠7= "C" and 𝑠9 = "c"
Kristina wants to get more burles for her string, so she is going to perform no more than 𝑘 operations on it. In one operation, she can:

either select the lowercase character 𝑠𝑖 (1≤𝑖≤𝑛) and make it uppercase.
or select uppercase character 𝑠𝑖 (1≤𝑖≤𝑛) and make it lowercase.
For example, when 𝑘 = 2 and 𝑠 = "aAaaBACacbE" it can perform one operation: choose 𝑠3 = "a" and make it uppercase. Then she will get another pair of 𝑠3 = "A" and 𝑠8 = "a"

Find maximum number of burles Kristina can get for her string.

Input
The first line of input data contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases.

The description of the test cases follows.

The first line of each test case contains two integers 𝑛 (1≤𝑛≤2⋅105) and 𝑘 (0≤𝑘≤𝑛) — the number of characters in the string and the maximum number of operations that can be performed on it.

The second line of each test case contains a string 𝑠 of length 𝑛, consisting only of lowercase and uppercase Latin letters.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅105.

Output
For each test case, print exactly one integer on a separate line: the maximum number of burles that Kristina can get for her string 𝑠.

题目大意
现有n个字母组成的串,每个字母的大小写构成一对,现在有m次操作机会,每次可以将一个字母变成大写或小写,最多可以有几对大小写。

题目分析
先统计每串中每个字母(大小写分开)的个数,

点击查看代码
map<char, int> up;
        map<char, int> low;
        for(char ch : s){
            if(ch >= 97 && ch <= 122){
                low[ch]++;
            }else if(ch >= 65 && ch <= 90){
                up[ch]++;
            }
        }
遍历每个字母的大小写分别的个数,先找出直接能配对的个数,计算剩下的可能配对的最大对数,再与m进行比较,注意m每次的减少
点击查看代码
for(char ch = 'a'; ch <= 'z'; ++ch){
            int l = low[ch];
            int u = up[toupper(ch)];
            int direct = min(l, u);
            sum += direct;
            int rem = abs(l - u) / 2; //剩下可能的最大对数
            int extra = min(rem, m);
            sum += extra;
            m -= extra;
        }

完整

点击查看代码
#include <iostream>
#include <map>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        string s;
        cin >> s;
        int sum = 0;
        map<char, int> up;
        map<char, int> low;
        for(char ch : s){
            if(ch >= 97 && ch <= 122){
                low[ch]++;
            }else if(ch >= 65 && ch <= 90){
                up[ch]++;
            }
        }
        for(char ch = 'a'; ch <= 'z'; ++ch){
            int l = low[ch];
            int u = up[toupper(ch)];
            int direct = min(l, u);
            sum += direct;
            int rem = abs(l - u) / 2; //剩下可能的最大对数
            int extra = min(rem, m);
            sum += extra;
            m -= extra;
        }
        cout << sum << endl;
    }
    return 0;
}
posted @ 2025-07-21 21:37  sirro1uta  阅读(8)  评论(0)    收藏  举报