题解:P12341 [蓝桥杯 2025 省 A/Python B 第二场] 消消乐
思路
这道题要我们消除到不能再消除时,字符串长度最大。很明显,我们一定要用尽量前的 A 来消除尽量后的 B,从而使字符串大,剩下来的模拟即可。
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
cin.tie(0)->sync_with_stdio(0);
string x;
cin>>x;
int ans=x.size();
int l=0,r=x.size()-1;
while(l<r){
while(x[l]!='A'&&l<x.size()) l++;
while(x[r]!='B'&&r>=0) r--;
if(l>=r) break;
ans-=2;
l++;
r--;
}
cout<<ans;
return 0;
}