题解 AT2059 【[AGC005A] STring】
本题与括号匹配相似
我用的两个栈
好吧,这种题目用vector的erase都能过,况且这个vector常数小
而大家都是用栈,我也用。
代码:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<char> st, st2;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    int len = s.length() - 1;
    for(register int i = 0; i <= len; i++)
    {
        if(s[i] == 'S') st.push('S');
        else if(s[i] == 'T' && !st.empty()) st.pop();
        else st2.push('T');
    }
    cout << st.size() + st2.size() << endl;
    return 0;
}
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号