题意:
解法:
将S看作左括号,T看作右括号,
由于每次都优先消去左边的ST,
那么用栈模拟括号匹配的过程即可,
剩下的字符数量就是最后栈中的字符数量.
code:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=2e5+5;
char s[maxm];
int n;
void solve(){
cin>>(s+1);
n=strlen(s+1);
stack<char>stk;
for(int i=1;i<=n;i++){
if(s[i]=='S'){
stk.push(s[i]);
}else{
if(stk.size()&&stk.top()=='S'){
stk.pop();
}else{
stk.push(s[i]);
}
}
}
cout<<stk.size()<<endl;
}
signed main(){
ios::sync_with_stdio(0);
solve();
return 0;
}