cf1907BB. YetnotherrokenKeoard

题目如下
B. YetnotherrokenKeoard
time limit per test1 second
memory limit per test256 megabytes
Polycarp has a problem — his laptop keyboard is broken.

Now, when he presses the 'b' key, it acts like an unusual backspace: it deletes the last (rightmost) lowercase letter in the typed string. If there are no lowercase letters in the typed string, then the press is completely ignored.

Similarly, when he presses the 'B' key, it deletes the last (rightmost) uppercase letter in the typed string. If there are no uppercase letters in the typed string, then the press is completely ignored.

In both cases, the letters 'b' and 'B' are not added to the typed string when these keys are pressed.

Consider an example where the sequence of key presses was "ARaBbbitBaby". In this case, the typed string will change as follows: "" −→𝙰 "A" −→𝚁 "AR" −→𝚊 "ARa" −→𝙱 "Aa" −→𝚋 "A" −→𝚋 "A" −→𝚒 "Ai" −→𝚝 "Ait" −→𝙱 "it" −→𝚊 "ita" −→𝚋 "it" −→𝚢 "ity".

Given a sequence of pressed keys, output the typed string after processing all key presses.

Input
The first line of the input data contains an integer 𝑡 (1≤𝑡≤1000), the number of test cases in the test.

The following contains 𝑡 non-empty lines, which consist of lowercase and uppercase letters of the Latin alphabet.

It is guaranteed that each line contains at least one letter and the sum of the lengths of the lines does not exceed 106.

Output
For each test case, output the result of processing the key presses on a separate line. If the typed string is empty, then output an empty line.
题目大意
根据规则,对于现有的字符串,每次输入“b“,就要删除最靠后的一个小写字母;每次输入”B“,就要删除最靠后的一个大写字母;
最后输出这个字符串;

题目分析
初始化两个计数器,skip_lower和skip_upper 分别记录待删除的小写字母和大写字母的个数。
对每个字符串从后往前遍历,
碰到'b',skip_lower++,表示之后碰到的小写字母要删除;
碰到‘B’,skip_upper++,表示之后碰到的大写字母要删除。
只遍历一遍,有skip_lower和skip_upper的累积,在后续的遍历中处理掉。
由于是从后往前的遍历,在遍历完后,还需要将结果字符串反转得到正确顺序

在此题目中为了便于字母的直接添加,故使用string类型来表示结果。
完整代码

点击查看代码
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

bool is_lower(char ch) {
    return ch >= 'a' && ch <= 'z';
}

bool is_upper(char ch) {
    return ch >= 'A' && ch <= 'Z';
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--){
        char s[2000006];
        scanf("%s", s);
        int n = strlen(s);
        string result;
        int skip_lower = 0, skip_upper = 0;
        for(int i = n - 1; i >= 0; i--){
            char ch = s[i];
            if(ch == 'b')
                skip_lower++;
            else if(ch == 'B')
                skip_upper++;
            else if(is_lower(ch)){
                if(skip_lower > 0)
                    skip_lower--;
                else
                    result += ch;
            }else if(is_upper(ch)){
                if (skip_upper > 0)
                    skip_upper--;
                else
                    result += ch;
            }
        }
        reverse(result.begin(), result.end());
        printf("%s\n", result.c_str());
    }
    return 0;
}
posted @ 2025-07-09 22:32  sirro1uta  阅读(10)  评论(0)    收藏  举报