又一个贪心。
将所有字母从前往后加入队列,若果当前加入的是 且队列不为空就弹掉队首,表示删去这个字符。
为什么这样没有后效性?
因为我们能删的只有 和 ,所以只要这一位是 ,前面无论是什么都无关,所以删掉一定是最优的。
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
int n,m,t,a[1002],b,c,d,p[1002];
char s[300002];
queue<char>q;
int main(){
t=read();
while(t--){
scanf("%s",s+1);
n=strlen(s+1);
re int ans=0;
while(!q.empty())q.pop();
for(re int i=1;i<=n;++i){
if(q.empty())q.push(s[i]);
else if(s[i]=='A')q.push(s[i]);
else q.pop(),ans+=2;
}
printf("%d\n",n-ans);
}
}