prithivMLmods/Coder-Stat huggingface dataset:
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

string s;
int i;

int main() {

  cin >> s;
  for (int i = 1; i <= 5000; ++i)
    for (int i = 0; i < s.size(); ++i) {
      if (s[i] == 'S' && s[i + 1] == 'T') {
        s.erase(s.begin() + i);
        s.erase(s.begin() + i);
      }
    }

  cout << s.size();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

string s;
int i;

int main() {

  cin >> s;

  while (i < s.size()) {
    if (s[i] == 'T' && s[i - 1] == 'S') {
      s.erase(s.begin() + i - 1);
      s.erase(s.begin() + i - 1);
      --i;
    } else if (s[i] == 'S' && s[i + 1] == 'T') {
      s.erase(s.begin() + i);
      s.erase(s.begin() + i);
    } else
      i++;
  }

  cout << s.size();

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(a) (a.begin(), a.end())
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  string s;
  cin >> s;
  deque<char> st;
  st.push_back(s[0]);
  REP(i, 1, s.size()) {
    char now = s[i], mae = st.back();
    if (mae == 'S') {
      if (now == 'T')
        st.pop_back();
      else
        st.push_back('S');
    } else {
      st.push_back(now);
    }
  }
  string ans;
  cout << st.size() << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = s; i < n; i++)
#define all(a) (a.begin(), a.end())
typedef long long ll;
using Graph = vector<vector<int>>;
const int INF = 1000000007;
int main() {
  string s;
  cin >> s;
  deque<char> st;
  st.push_back(s[0]);
  REP(i, 1, s.size()) {
    char now = s[i];
    char mae = 'T';
    if (st.size() != 0)
      mae = st.back();
    if (mae == 'S') {
      if (now == 'T')
        st.pop_back();
      else
        st.push_back('S');
    } else {
      st.push_back(now);
    }
  }
  string ans;
  cout << st.size() << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
stack<int> st;
char s[100005];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'S')
      st.push(0);
    else {
      if (!st.empty() && st.top() == 0)
        st.pop();
      else
        st.push(1);
    }
  }
  printf("%d\n", st.size());
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
stack<int> st;
char s[200005];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'S')
      st.push(0);
    else {
      if (!st.empty() && st.top() == 0)
        st.pop();
      else
        st.push(1);
    }
  }
  printf("%d\n", st.size());
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define P pair<ll, ll>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>

////////////////////////////////////////////////////////

int main() {
  string s;
  cin >> s;
  bool f = true;
  while (f) {
    bool f = true;
    rep(i, 0, sz(s) - 1) {
      if (s[i] == 'S' && s[i + 1] == 'T') {
        s.erase(i, 2);
        f = false;
        i--;
      }
    }
    if (f)
      break;
  }
  co(sz(s));

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define P pair<ll, ll>
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>

////////////////////////////////////////////////////////

int main() {
  string s;
  cin >> s;
  bool f = true;
  while (f) {
    bool f = true;
    rep(i, 0, sz(s) - 5) {
      if (s[i] == 'S' && s[i + 1] == 'S' && s[i + 2] == 'S' &&
          s[i + 3] == 'T' && s[i + 4] == 'T' && s[i + 5] == 'T') {
        s.erase(i, 6);
        f = false;
        i -= 5;
      }
    }
    rep(i, 0, sz(s) - 1) {
      if (s[i] == 'S' && s[i + 1] == 'T') {
        s.erase(i, 2);
        f = false;
        i--;
      }
    }
    if (f)
      break;
  }
  co(sz(s));

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int C;
char p[101000];
int main() {
  int i, c = 0, r = 0;
  scanf("%s", p);
  for (i = 0; p[i]; i++) {
    r++;
    if (p[i] == 'S')
      c++;
    else {
      if (c) {
        r -= 2;
        c--;
      }
    }
  }
  printf("%d\n", r);
}

Fixed submission:
#include <stdio.h>
int C;
char p[201000];
int main() {
  int i, c = 0, r = 0;
  scanf("%s", p);
  for (i = 0; p[i]; i++) {
    r++;
    if (p[i] == 'S')
      c++;
    else {
      if (c) {
        r -= 2;
        c--;
      }
    }
  }
  printf("%d\n", r);
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
const int maxn = 100005;
char a[maxn], b[maxn];
int len;
int main() {
  scanf("%s", a);
  for (int i = 0; a[i]; i++)
    if (a[i] == 'S')
      b[++len] = 'S';
    else if (a[i] == 'T')
      if (b[len] == 'S' && len)
        len--;
      else
        b[++len] = 'T';
  printf("%d\n", len);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
const int maxn = 200005;
char a[maxn], b[maxn];
int len;
int main() {
  scanf("%s", a);
  for (int i = 0; a[i]; i++)
    if (a[i] == 'S')
      b[++len] = 'S';
    else if (a[i] == 'T')
      if (b[len] == 'S' && len)
        len--;
      else
        b[++len] = 'T';
  printf("%d\n", len);
  return 0;
}
Problem p03986 (Python)

Buggy submission (status: Runtime Error):
# coding: utf-8
# Your code here!

X = list(input())
num = len(X)
stack = []

for i in X:
    if i == "S":
        stack.append(i)
    else:
        if stack[-1] == "T" or stack[0] == "":
            stack.append(i)
        else:
            stack.pop()
print(len(stack))


Fixed submission:
# coding: utf-8
# Your code here!

X = list(input())
num = len(X)
stack = []

for i in X:
    if i == "S":
        stack.append(i)
    else:
        if stack == [] or stack[-1] == "T":
            stack.append(i)
        else:
            stack.pop()
print(len(stack))
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  string x;
  cin >> x;
  stack<char> s;
  s.push(x[0]);
  for (int i = 1; i < x.length(); i++) {
    if (x[i] == 'S') {
      s.push('S');
    } else if (s.top() == 'S') {
      s.pop();
    } else {
      s.push('T');
    }
  }
  cout << s.size() << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  string x;
  cin >> x;
  stack<char> s;
  for (int i = 0; i < x.length(); i++) {
    if (s.empty()) {
      s.push(x[i]);
    } else if (x[i] == 'S') {
      s.push('S');
    } else if (s.top() == 'S') {
      s.pop();
    } else {
      s.push('T');
    }
  }
  cout << s.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, j, n) for (int i = (j); i < (n); i++)
#define erep(i, j, n) for (int i = (j); i <= (n); i++)
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef long long i64;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  stack<char> sta;
  sta.push(s[0]);
  rep(i, 1, s.size()) {
    if (s[i] == 'T' && sta.top() == 'S')
      sta.pop();
    else
      sta.push(s[i]);
  }
  cout << sta.size() << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, j, n) for (int i = (j); i < (n); i++)
#define erep(i, j, n) for (int i = (j); i <= (n); i++)
#define all(i) i.begin(), i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef long long i64;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;
  stack<char> sta;
  rep(i, 0, s.size()) {
    if (s[i] == 'T' && !sta.empty() && sta.top() == 'S')
      sta.pop();
    else
      sta.push(s[i]);
  }
  cout << sta.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  string ans = "";
  ans += s[0];
  for (int i = 1; i < s.size(); i++) {
    if (ans.back() == 'S' && s[i] == 'T')
      ans.pop_back();
    else
      ans += s[i];
  }
  cout << ans.size() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  string ans = "";
  ans += s[0];
  for (int i = 1; i < s.size(); i++) {
    if (ans.size() > 0 && ans.back() == 'S' && s[i] == 'T')
      ans.pop_back();
    else
      ans += s[i];
  }
  cout << ans.size() << endl;
  return 0;
}
Problem p03986 (Python)

Buggy submission (status: Time Limit Exceeded):
x = input()
while "ST" in x:
    x = x.replace("ST", "")
print(len(x))


Fixed submission:
x = input()
while "ST" in x:
    x = x.replace("ST", "")
    n = len(x) // 2
    if ("T" not in x[:n]) and ("S" not in x[n:]):
        x = ""
        break
print(len(x))
Problem p03986 (Python)

Buggy submission (status: Time Limit Exceeded):
X = input()
X_length = len(X)
change = True
while change:
    X = X.replace("ST", "", 1)
    change = len(X) != X_length
    X_length = len(X)
print(X_length)


Fixed submission:
X = input()
stack = []
for s in X:
    if s == "S":
        stack.append(s)
    elif s == "T":
        if len(stack) == 0 or stack[-1] == "T":
            stack.append(s)
        else:
            stack.pop(-1)
print(len(stack))
Problem p03986 (Python)

Buggy submission (status: Runtime Error):
X = input()
# X = 'SSTTST'
# X = 'TSSTTTSS'

# naive case
# len_prev = -1
# while len_prev != len(X):
#     len_prev = len(X)
#     X = X.replace('ST', '')
# print(len(X))


i = 0
cnt = []
X += " "
cur = 1
for i in range(len(X) - 1):
    if X[i] == X[i + 1]:
        cur += 1
    else:
        cnt.append((X[i], cur))
        cur = 1

total = 0
for i in range(len(cnt) - 1):
    if cnt[i][0] == "S":
        if cnt[i][1] > cnt[i + 1][1]:
            if i + 2 < len(cnt) - 1:
                cnt[i + 2][1] += cnt[i][1] - cnt[i + 1][1]
        total += min(cnt[i][1], cnt[i + 1][1])
print(len(X) - 1 - 2 * total)


Fixed submission:
X = input()
# X = 'SSTTST'
# X = 'TSSTTTSS'

# naive case
# len_prev = -1
# while len_prev != len(X):
#     len_prev = len(X)
#     X = X.replace('ST', '')
# print(len(X))


i = 0
cnt = []
X += " "
cur = 1
for i in range(len(X) - 1):
    if X[i] == X[i + 1]:
        cur += 1
    else:
        cnt.append((X[i], cur))
        cur = 1

total = 0
for i in range(len(cnt) - 1):
    if cnt[i][0] == "S":
        if cnt[i][1] > cnt[i + 1][1]:
            if i + 2 < len(cnt):
                cnt[i + 2] = (cnt[i + 2][0], cnt[i + 2][1] + cnt[i][1] - cnt[i + 1][1])
        total += min(cnt[i][1], cnt[i + 1][1])
print(len(X) - 1 - 2 * total)
Problem p03986 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

X = input()

ret = deque([X[0]])
for i in range(1, len(X)):
    x = X[i]
    if x == "S":
        ret.append(x)
    else:
        if ret[-1] == "S":
            ret.pop()
        else:
            ret.append(x)
print(len(ret))


Fixed submission:
from collections import deque

X = input()

ret = deque([X[0]])
for i in range(1, len(X)):
    x = X[i]
    if x == "S":
        ret.append(x)
    else:
        if len(ret) > 0 and ret[-1] == "S":
            ret.pop()
        else:
            ret.append(x)
print(len(ret))
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

char s[N];
int st[N], tp = 0;

int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'T' && tp && s[st[tp]] == 'S') {
      tp--;
    } else {
      st[++tp] = i;
    }
  }
  printf("%d\n", tp);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e7 + 10;

char s[N];
int st[N], tp = 0;

int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'T' && tp && s[st[tp]] == 'S') {
      tp--;
    } else {
      st[++tp] = i;
    }
  }
  printf("%d\n", tp);
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;
int main() {
  string str;
  cin >> str;
  for (int i = 0; i < str.size() - 1; i++) {
    if (str[i] == 'S' && str[i + 1] == 'T') {
      str = str.substr(0, i) + str.substr(i + 2, str.size() - i - 1);
      if (i > 0)
        i -= 2;
      else
        i--;
    }
    // cout<<str<<endl;
    if (str.size() == 0 || str.size() == 1)
      break;
  }
  cout << str.size() << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  string str;
  cin >> str;
  for (int i = 0; i < str.size() - 1; i++) {
    if (str[i] == 'S' && str[i + 1] == 'T') {
      str.erase(str.begin() + i);
      str.erase(str.begin() + i);
      if (i > 0)
        i -= 2;
      else
        i--;
    }
    // cout<<str<<endl;
    if (str.size() == 0 || str.size() == 1)
      break;
  }
  cout << str.size() << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll inf_l = 1e18;
const int MAX = 1e6;

int main() {
  string x;
  cin >> x;
  stack<char> s;
  rep(i, x.size()) {
    if (x[i] == 'S')
      s.push(x[i]);
    else {
      if (s.top() == 'S')
        s.pop();
      else
        s.push(x[i]);
    }
  }
  cout << s.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9 + 7;
const ll inf_l = 1e18;
const int MAX = 1e6;

int main() {
  string x;
  cin >> x;
  stack<char> s;
  rep(i, x.size()) {
    if (x[i] == 'S')
      s.push(x[i]);
    else {
      if (s.size() != 0 && s.top() == 'S')
        s.pop();
      else
        s.push(x[i]);
    }
  }
  cout << s.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ld long double
#define ll long long int
#define ull unsigned long long int
#define rep(i, n) for (i = 0; i < n; i++)
#define fin(ans) cout << (ans) << endl
#define INF INT_MAX;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long LLINF = 1LL << 60;
// set<int>::iterator it;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //////////////////////////////////////////////////////

  int i;
  string s;
  cin >> s;
  for (i = s.length() - 2; i >= 0; i--) {
    if (s[i] == 'S' && s[i + 1] == 'T') {
      s = s.substr(0, i) + s.substr(i + 2);
    }
  }
  cout << s.length() << endl;

  //////////////////////////////////////////////////////
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ld long double
#define ll long long int
#define ull unsigned long long int
#define rep(i, n) for (i = 0; i < n; i++)
#define fin(ans) cout << (ans) << endl
#define INF INT_MAX;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long LLINF = 1LL << 60;
// set<int>::iterator it;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //////////////////////////////////////////////////////

  int i;
  string s;
  cin >> s;
  for (i = s.length() - 2; i >= 0; i--) {
    if (s[i] == 'S' && s[i + 1] == 'T') {
      s = s.erase(i, 2);
    }
  }
  cout << s.length() << endl;

  //////////////////////////////////////////////////////
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string x;
  cin >> x;
  stack<char> st;
  st.push(x[0]);
  for (int i = 1; i < x.size(); i++) {
    char tmp = st.top();
    if (tmp == 'S' && x[i] == 'T') {
      st.pop();
    } else {
      st.push(x[i]);
    }
  }
  if (st.empty()) {
    cout << 0 << endl;
  } else {
    cout << st.size() << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string x;
  cin >> x;
  stack<char> st;
  st.push(x[0]);
  for (int i = 1; i < x.size(); i++) {
    if (st.empty()) {
      st.push(x[i]);
      continue;
    }
    char tmp = st.top();
    if (tmp == 'S' && x[i] == 'T') {
      st.pop();
    } else {
      st.push(x[i]);
    }
  }
  if (st.empty()) {
    cout << 0 << endl;
  } else {
    cout << st.size() << endl;
  }
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define endl '\n'
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

void solve() {
  string s;
  cin >> s;
  int n = sz(s);
  stack<char> g;
  g.push(s[0]);
  rep(i, 1, n) {
    if (s[i] == 'S')
      g.push(s[i]);
    else if (g.top() == 'S')
      g.pop();
    else
      g.push(s[i]);
  }
  cout << sz(g);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define endl '\n'
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

void solve() {
  string s;
  cin >> s;
  int n = sz(s);
  stack<char> g;
  g.push(s[0]);
  rep(i, 1, n) {
    if (g.empty() || s[i] == 'S')
      g.push(s[i]);
    else if (g.top() == 'S')
      g.pop();
    else
      g.push(s[i]);
  }
  cout << sz(g);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1e5 + 123;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 9;

inline void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
char a[12345];
int prv[12345], nxt[12345], cnt;

int main() {
  boost();
  cin >> a + 1;
  int n = strlen(a + 1);
  for (int i = 1; i <= n; i++)
    prv[i] = i - 1, nxt[i] = i + 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != 'S' && a[i] != 'T')
      continue;
    if (a[prv[i]] == 'S' && a[i] == 'T') {
      a[prv[i]] = a[i] = '#';
      prv[i] = prv[prv[i]];
      prv[i + 1] = prv[i];
    }
  }
  for (int i = 1; i <= n; i++)
    cnt += (a[i] == '#');
  cout << n - cnt;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1e5 + 123;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 9;

inline void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
char a[223455];
int prv[223455], nxt[223455], cnt;

int main() {
  boost();
  cin >> a + 1;
  int n = strlen(a + 1);
  for (int i = 1; i <= n; i++)
    prv[i] = i - 1, nxt[i] = i + 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != 'S' && a[i] != 'T')
      continue;
    if (a[prv[i]] == 'S' && a[i] == 'T') {
      a[prv[i]] = a[i] = '#';
      prv[i] = prv[prv[i]];
      prv[i + 1] = prv[i];
    }
  }
  for (int i = 1; i <= n; i++)
    cnt += (a[i] == '#');
  cout << n - cnt;

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string X;
  cin >> X;
  stack<char> s;
  s.push(X.front());
  auto n = X.size();
  for (int i = 1; i < n; i++) {
    if (X[i] == 'T' && s.top() == 'S')
      s.pop();
    else
      s.push(X[i]);
  }
  cout << s.size() << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string X;
  cin >> X;
  stack<char> s;
  s.push(X.front());
  auto n = X.size();
  for (int i = 1; i < n; i++) {
    if (s.size() > 0 && X[i] == 'T' && s.top() == 'S')
      s.pop();
    else
      s.push(X[i]);
  }
  cout << s.size() << endl;

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string s;
  cin >> s;

  stack<char> st;
  st.push(s[0]);
  for (int i = 1; i < s.length(); i++) {
    if (s[i] == 'S')
      st.push(s[i]);
    else if (st.top() == s[i])
      st.push(s[i]);
    else
      st.pop();
  }
  cout << st.size() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  string s;
  cin >> s;

  stack<char> st;
  for (auto x : s) {
    if (st.empty())
      st.push(x);
    else if (x == 'S')
      st.push(x);
    else if (st.top() == x)
      st.push(x);
    else
      st.pop();
  }
  cout << st.size() << endl;

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char a[200 + 5];
int alen, cnt, res;
int main() {
  scanf("%s", a);
  alen = res = strlen(a);
  for (int i = 0; i < alen; i++) {
    if (a[i] == 'S')
      cnt++;
    if (a[i] == 'T' && cnt >= 1) {
      cnt--;
      res -= 2;
    }
  }
  printf("%d\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char a[200000 + 5];
int alen, cnt, res;
int main() {
  scanf("%s", a);
  alen = res = strlen(a);
  for (int i = 0; i < alen; i++) {
    if (a[i] == 'S')
      cnt++;
    if (a[i] == 'T' && cnt >= 1) {
      cnt--;
      res -= 2;
    }
  }
  printf("%d\n", res);
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  string X;
  cin >> X;

  while (1) {
    n = X.length();
    for (int i = 0; i < X.length(); i++) {
      if (X[i] == 'S' && X[i + 1] == 'T') {
        X.erase(X.begin() + i, X.begin() + i + 2);
        // cout<<i<<endl;
      }
    }
    if (n == X.length())
      break;
  }

  cout << X.length() << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  string X;
  cin >> X;

  while (1) {
    n = X.length();
    for (int i = 0; i < X.length(); i++) {
      if (X[i] == 'S' && X[i + 1] == 'T') {
        for (int j = 0; j < X.length(); j++) {
          if (X[i - j] == 'S' && X[i + 1 - j] == 'T') {
            X.erase(X.begin() + i - j, X.begin() + i + 2 - j);
          } else {
            break;
          }
        }
      }
    }
    if (n == X.length())
      break;
  }

  cout << X.length() << endl;

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
using ll = long long;
#define vl vector<long long>
#define vll vector<vector<long long>>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// 以上テンプレ

int main() {
  string X;
  cin >> X;
  stack<char> ans;
  ll a = X.size();
  rep(i, a) {
    if (X.at(i) == 'S') {
      ans.push('S');
    } else if (X.at(i) == 'T' && ans.top() == 'S') {
      ans.pop();
    } else {
      ans.push('T');
    }
  }
  cout << ans.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
using ll = long long;
#define vl vector<long long>
#define vll vector<vector<long long>>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// 以上テンプレ

int main() {
  string X;
  cin >> X;
  stack<char> ans;
  ll a = X.size();
  rep(i, a) {
    if (X.at(i) == 'S') {
      ans.push('S');
    } else if (X.at(i) == 'T' && ans.empty()) {
      ans.push('T');
    } else if (X.at(i) == 'T' && ans.top() == 'S') {
      ans.pop();
    } else {
      ans.push('T');
    }
  }
  cout << ans.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;
string s;
char st[100005];
int u, i;
int main() {
  cin >> s;
  for (i = 0; i < s.size(); i++) {
    if (u && st[u] == 'S' && s[i] == 'T')
      u--;
    else
      st[++u] = s[i];
  }
  cout << u;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;
string s;
char st[200005];
int u, i;
int main() {
  cin >> s;
  for (i = 0; i < s.size(); i++) {
    if (u && st[u] == 'S' && s[i] == 'T')
      u--;
    else
      st[++u] = s[i];
  }
  cout << u;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<LL, LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x) * (x))
#define pdd pair<double, double>
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
int main() {
  char c[100005];
  scanf("%s", c);
  int ans = 0;
  int sum = 0;
  for (int i = 0; c[i] != 0; i++) {
    if (c[i] == 'S')
      sum++;
    else {
      if (sum)
        sum--;
      else
        ans++;
    }
  }
  printf("%d\n", ans + sum);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<LL, LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x) * (x))
#define pdd pair<double, double>
#define MEMS(x) memset(x, -1, sizeof(x))
#define MEM(x) memset(x, 0, sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
int main() {
  char c[200005];
  scanf("%s", c);
  int ans = 0;
  int sum = 0;
  for (int i = 0; c[i] != 0; i++) {
    if (c[i] == 'S')
      sum++;
    else {
      if (sum)
        sum--;
      else
        ans++;
    }
  }
  printf("%d\n", ans + sum);
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int ans, i, n;
char st[210];
deque<char> dq;
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  scanf("%s", &st);
  n = strlen(st);
  for (i = 0; i < n; ++i) {
    if (st[i] == 'S')
      dq.push_back(st[i]);
    else {
      if (!dq.empty() && dq.back() == 'S') {
        dq.pop_back();
        while (!dq.empty() && dq.back() == 'T') {
          dq.pop_back();
          if (!dq.empty() && dq.back() == 'S')
            dq.pop_back();
          else {
            dq.push_front('T');
            break;
          }
        }
      } else
        dq.push_back(st[i]);
    }
    while (!dq.empty() && dq.front() == 'T')
      dq.pop_front(), ++ans;
  }
  while (!dq.empty())
    dq.pop_back(), ++ans;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int ans, i, n;
char st[200010];
deque<char> dq;
int main() {
  // freopen("a.in","r",stdin);
  // freopen("a.out","w",stdout);
  scanf("%s", &st);
  n = strlen(st);
  for (i = 0; i < n; ++i) {
    if (st[i] == 'S')
      dq.push_back(st[i]);
    else {
      if (!dq.empty() && dq.back() == 'S') {
        dq.pop_back();
        while (!dq.empty() && dq.back() == 'T') {
          dq.pop_back();
          if (!dq.empty() && dq.back() == 'S')
            dq.pop_back();
          else {
            dq.push_front('T');
            break;
          }
        }
      } else
        dq.push_back(st[i]);
    }
    while (!dq.empty() && dq.front() == 'T')
      dq.pop_front(), ++ans;
  }
  while (!dq.empty())
    dq.pop_back(), ++ans;
  printf("%d\n", ans);
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<bool> vecbool;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<vecint> vec2int;
typedef vector<vecbool> vec2bool;
int dx[4]{-1, +1, 0, 0};
int dy[4]{0, 0, -1, +1};
int main() {
  string s;
  cin >> s;
  stack<char> v;
  int n = s.size();
  int ans = n;
  REP(i, n) {
    if (s[i] == 'S') {
      v.push(s[i]);
    } else {
      if (v.top() == 'S') {
        v.pop();
        ans -= 2;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
typedef long long ll;
typedef vector<bool> vecbool;
typedef vector<int> vecint;
typedef vector<ll> vecll;
typedef vector<string> vecstr;
typedef vector<vecint> vec2int;
typedef vector<vecbool> vec2bool;
int dx[4]{-1, +1, 0, 0};
int dy[4]{0, 0, -1, +1};
int main() {
  string s;
  cin >> s;
  stack<char> v;
  int n = s.size();
  int ans = n;
  REP(i, n) {
    if (s[i] == 'S') {
      v.push(s[i]);
    } else {
      if (!v.empty() && v.top() == 'S') {
        v.pop();
        ans -= 2;
      }
    }
  }
  cout << ans << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  stack<char> st;
  int n = s.size(), cnt = 0;
  for (int i = 0; i < n; i++) {
    if (i > 0 and s[i] == 'T') {
      char tmp = st.top();
      if (tmp == 'S') {
        st.pop();
        cnt++;
        continue;
      }
    }
    st.push(s[i]);
  }

  cout << n - cnt * 2 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  stack<char> st;
  int n = s.size(), cnt = 0;
  for (int i = 0; i < n; i++) {
    if (i > 0 and s[i] == 'T') {
      if (st.size() < 1)
        continue;
      char tmp = st.top();
      if (tmp == 'S') {
        st.pop();
        cnt++;
        continue;
      }
    }
    st.push(s[i]);
  }

  cout << n - cnt * 2 << endl;

  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < (int)(n); i++)
#define INF 999999999

/*Solve*/
int main() {
  string str;
  stack<int> stk;
  cin >> str;
  rep(i, str.size()) {
    if (i == 0) {
      stk.push(str[i]);
    } else if (str[i] == 'S') {
      stk.push(str[i]);
    } else if (str[i] == 'T') {
      if (stk.top() == 'T' || stk.empty()) {
        stk.push(str[i]);
      } else if (stk.top() == 'S') {
        stk.pop();
      }
    }
  }
  cout << stk.size();
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < (int)(n); i++)
#define INF 999999999

/*Solve*/
int main() {
  string str;
  stack<int> stk;
  cin >> str;
  rep(i, str.size()) {
    if (i == 0) {
      stk.push(str[i]);
    } else if (str[i] == 'S') {
      stk.push(str[i]);
    } else if (str[i] == 'T') {
      if (stk.empty() || stk.top() == 'T') {
        stk.push(str[i]);
      } else if (stk.top() == 'S') {
        stk.pop();
      }
    }
  }
  cout << stk.size();
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
const int N = 205;

char str[N];
int s, t, l;

void init() {
  scanf("%s", str);
  l = strlen(str);
  for (int i = 0; i < l; i++) {
    if (str[i] == 'S') {
      s++;
    } else {
      if (s) {
        s--;
        t++;
      }
    }
  }
  printf("%d\n", l - 2 * t);
}

int main() {

  init();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
const int N = 200005;

char str[N];
int s, t, l;

void init() {
  scanf("%s", str);
  l = strlen(str);
  for (int i = 0; i < l; i++) {
    if (str[i] == 'S') {
      s++;
    } else {
      if (s) {
        s--;
        t++;
      }
    }
  }
  printf("%d\n", l - 2 * t);
}

int main() {

  init();
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define INF 2e9
typedef long long ll;
ll n;
char q[111111];
string s;
int main() {
  cin >> s;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == 'T' && n > 0 && q[n] == 'S')
      n--;
    else
      q[++n] = s[i];
  }
  cout << n;
}

Fixed submission:
// #include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define INF 2e9
typedef long long ll;
ll n;
char q[211111];
string s;
int main() {
  cin >> s;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == 'T' && n > 0 && q[n] == 'S')
      n--;
    else
      q[++n] = s[i];
  }
  cout << n;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a;
  cin >> a;
  int tmp = a.find("ST");
  while (a.length() > 0 && tmp >= 0) {
    a.erase(tmp, 2);
    // int x=max(tmp-1,0);
    tmp = a.find("ST");
    if (tmp == string::npos && (a[tmp] != 'S' || a[tmp + 1] != 'T'))
      tmp = -1;
  }
  cout << a.length() << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a;
  cin >> a;
  int tmp = a.find("ST");
  while (a.length() > 0 && tmp >= 0) {
    a.erase(tmp, 2);
    int x = max(tmp - 1, 0);
    tmp = a.find("ST", x);
    if (tmp == string::npos)
      tmp = -1;
  }
  cout << a.length() << endl;
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int N = 1e5 + 5;

int n;
char s[N];
vector<char> v;

int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; i++) {
    v.push_back(s[i]);
    size_t sz = v.size();
    if (sz >= 2) {
      if (v[sz - 2] == 'S' && v[sz - 1] == 'T') {
        v.pop_back();
        v.pop_back();
      }
    }
  }
  printf("%u\n", v.size());
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int N = 2 * 1e5 + 5;

int n;
char s[N];
vector<char> v;

int main() {
  scanf("%s", s);
  n = strlen(s);
  for (int i = 0; i < n; i++) {
    v.push_back(s[i]);
    size_t sz = v.size();
    if (sz >= 2) {
      if (v[sz - 2] == 'S' && v[sz - 1] == 'T') {
        v.pop_back();
        v.pop_back();
      }
    }
  }
  printf("%u\n", v.size());
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
using namespace std;

const int MAXN = 200 + 10;
char S[MAXN];
int N, ans, cnt;

int main() {
  register int i;
  scanf("%s", S + 1);
  ans = N = strlen(S + 1);
  for (i = 1; i <= N; ++i)
    if (S[i] == 'S')
      ++cnt;
    else if (cnt)
      --cnt, ans -= 2;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
using namespace std;

const int MAXN = 2000000 + 10;
char S[MAXN];
int N, ans, cnt;

int main() {
  register int i;
  scanf("%s", S + 1);
  ans = N = strlen(S + 1);
  for (i = 1; i <= N; ++i)
    if (S[i] == 'S')
      ++cnt;
    else if (cnt)
      --cnt, ans -= 2;
  printf("%d\n", ans);
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include "stdio.h"

char s[100005];
char st[100005];
int pt;

int main() {
  scanf(" %s", s);
  for (int i = 0; s[i]; i++) {
    if (pt != 0 && st[pt - 1] == 'S' && s[i] == 'T')
      pt--;
    else
      st[pt++] = s[i];
  }
  printf("%d\n", pt);
}

Fixed submission:
#include "stdio.h"

char s[200005];
char st[200005];
int pt;

int main() {
  scanf(" %s", s);
  for (int i = 0; s[i]; i++) {
    if (pt != 0 && st[pt - 1] == 'S' && s[i] == 'T')
      pt--;
    else
      st[pt++] = s[i];
  }
  printf("%d\n", pt);
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (i = m; i <= n; i++)
#define rsp(it, s) for (set<int>::iterator it = s.begin(); it != s.end(); it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int, int>
#define Lson L, mid, rt << 1
#define Rson mid + 1, R, rt << 1 | 1
const int maxn = 1e5 + 10;
using namespace std;
ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p;
    p = p * p;
    q >>= 1;
  }
  return f;
}
int n, m, k, t;
inline ll read() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
char a[maxn];
stack<char> p;
int main() {
  int i, j;
  scanf("%s", &a);
  for (i = 0; a[i]; i++) {
    if (!p.empty() && p.top() == 'S' && a[i] == 'T')
      p.pop();
    else
      p.push(a[i]);
  }
  printf("%d\n", p.size());
  // system("Pause");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (i = m; i <= n; i++)
#define rsp(it, s) for (set<int>::iterator it = s.begin(); it != s.end(); it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int, int>
#define Lson L, mid, rt << 1
#define Rson mid + 1, R, rt << 1 | 1
const int maxn = 2e5 + 10;
using namespace std;
ll gcd(ll p, ll q) { return q == 0 ? p : gcd(q, p % q); }
ll qpow(ll p, ll q) {
  ll f = 1;
  while (q) {
    if (q & 1)
      f = f * p;
    p = p * p;
    q >>= 1;
  }
  return f;
}
int n, m, k, t;
inline ll read() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
char a[maxn];
stack<char> p;
int main() {
  int i, j;
  scanf("%s", &a);
  for (i = 0; a[i]; i++) {
    if (!p.empty() && p.top() == 'S' && a[i] == 'T')
      p.pop();
    else
      p.push(a[i]);
  }
  printf("%d\n", p.size());
  // system("Pause");
  return 0;
}
Problem p03986 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define FOR(a, b, c) for (int a = b; a < c; a++)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int main() {
  char x[200005];
  cin >> x;
  int lx = strlen(x);
  vector<char> a;
  FOR(i, 0, lx) { a.push_back(x[i]); }
  int la = lx;
  FOR(i, 1, la) {
    if (a[i - 1] == 'S' && a[i] == 'T') {
      remove(a, i - 1);
      remove(a, i - 1);
      i = 0;
      la -= 2;
    }
  }

  cout << a.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FOR(a, b, c) for (int a = b; a < c; a++)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int main() {
  char x[200005];
  cin >> x;
  int lx = strlen(x);
  vector<char> a;
  FOR(i, 0, lx) { a.push_back(x[i]); }
  int la = lx;
  FOR(i, 1, la) {
    if (a[i - 1] == 'S' && a[i] == 'T') {
      remove(a, i - 1);
      remove(a, i - 1);
      i -= 2;
      la -= 2;
    }
  }

  cout << a.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999

using namespace std;

int32_t main() {
  string s;
  cin >> s;

  stack<char> q;
  q.push(s[0]);
  int i = 1;
  FOR(i, 1, s.length()) {
    if (s[i] == 'T' && q.top() == 'S') {
      q.pop();
    } else {
      q.push(s[i]);
    }
  }

  cout << q.size() << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999

using namespace std;

int32_t main() {
  string s;
  cin >> s;

  stack<char> q;
  q.push(s[0]);
  int i = 1;
  FOR(i, 1, s.length()) {
    if (!q.empty() && s[i] == 'T' && q.top() == 'S') {
      q.pop();
    } else {
      q.push(s[i]);
    }
  }

  cout << q.size() << endl;
}
Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

int N;
vector<int> a;
char s[100010];

int main() {
  scanf("%s", s);
  N = strlen(s);
  for (int i = 0; i < N; i++) {
    if (s[i] == 'S')
      a.pb(0);
    else {
      if (a.size() != 0 && a.back() == 0) {
        a.pop_back();
      } else
        a.pb(1);
    }
  }
  printf("%d\n", a.size());

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

int N;
vector<int> a;
char s[200010];

int main() {
  scanf("%s", s);
  N = strlen(s);
  for (int i = 0; i < N; i++) {
    if (s[i] == 'S')
      a.pb(0);
    else {
      if (a.size() != 0 && a.back() == 0) {
        a.pop_back();
      } else
        a.pb(1);
    }
  }
  printf("%d\n", a.size());

  return 0;
}