YetnotherrokenKeoard
题面翻译
Polycarp的笔记本键盘坏了。
现在,当他按下 ‘b’ 键时,它的行为类似于退格键:删除已输入字符串中最后一个小写字母。如果已输入字符串中没有小写字母,则完全忽略该按键。
类似地,当他按下 ‘B’ 键时,它删除已输入字符串中最后一个大写字母。如果已输入字符串中没有大写字母,则完全忽略该按键。
给定一个按键序列,输出处理所有按键后的结果。
输出格式
样例输入 #1
12
ARaBbbitBaby
YetAnotherBrokenKeyboard
Bubble
Improbable
abbreviable
BbBB
BusyasaBeeinaBedofBloomingBlossoms
CoDEBARbIES
codeforces
bobebobbes
b
TheBBlackbboard
样例输出 #1
ity
YetnotherrokenKeoard
le
Imprle
revile
usyasaeeinaedofloominglossoms
CDARIES
codeforces
es
helaoard
这个题并不难只要倒序遍历一遍,中途记录b 和 B的数量,如果s[i]是b或者B肯定不用加入答案字符串,然后遇到小写字母时看看b的个数是不是0,如果是则加入答案字符串,如果不是b的个数减去1,大写字母一样,但是这里有个我经常用的不太好的东西就是,ans = s[i] + ans;这样似乎是不用最后再把答案倒过来了,但是他的时间复杂度是O(n),每一次加都是,所以就会超时,还不如最后把ans 倒过来,差别自己体会
下面展示一些 ac代码
。
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
typedef long long ll;
string s;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int q;
cin >> q;
while(q --)
{
int cntb = 0, cntB = 0;
cin >> s;
string ans = "";
for(int i = (int)s.size() - 1;i >= 0;i --)
{
if(s[i] == 'b')cntb ++;
else if(s[i] == 'B')cntB ++;
else if(s[i] >= 'a' && s[i] <= 'z'){
if(cntb)cntb --;
else ans += s[i];
}else if(s[i] >= 'A' && s[i] <='Z'){
if(cntB)cntB --;
else ans += s[i];
}
}
reverse(ans.begin(),ans.end());
cout << ans << endl;
}
return 0;
}