Problem p03986 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz size()
#define print(X) cout << (X) << "\n"
static const int INF = 1e+9 + 7;
static const ll INFL = 1e+18 + 7;
int n, m, l;
int p;
string s, t;
ll d[200010], dp[300][300];
char field[200][200];

int main() {
  vc vec;
  cin >> s;
  rep(i, 0, s.sz) vec.pb(s[i]);
  rep(i, 0, vec.sz - 1) {
    if (vec[i] == 'S' && vec[i + 1] == 'T') {
      vec.erase(vec.begin() + i);
      vec.erase(vec.begin() + i);
      i -= 2;
    }
  }
  rep(i, 0, vec.sz - 1) {
    if (vec[i] == 'S' && vec[i + 1] == 'T') {
      vec.erase(vec.begin() + i);
      vec.erase(vec.begin() + i);
      i = -1;
    }
  }
  // rep(i,0,vec.sz)cout << vec[i];
  // puts("");
  print(vec.sz);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (a); i > (n); i--)
#define lep(i, a, n) for (ll i = (a); i <= (n); i++)
#define pel(i, a, n) for (ll i = (a); i >= (n); i--)
#define clr(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz size()
#define print(X) cout << (X) << "\n"
static const int INF = 1e+9 + 7;
static const ll INFL = 1e+18 + 7;
int n, m, l;
int p;
string s, t;
ll d[200010], dp[300][300];
char field[200][200];

int main() {
  vc vec;
  cin >> s;
  rep(i, 0, s.sz) vec.pb(s[i]);
  rep(i, 0, vec.sz - 1) {
    if (vec[i] == 'S' && vec[i + 1] == 'T') {
      vec.erase(vec.begin() + i);
      vec.erase(vec.begin() + i);
      i -= 2;
    }
  }
  rep(i, 1, vec.sz) {
    if (vec[i - 1] == 'S' && vec[i] == 'T') {
      vec.erase(vec.begin() + (i - 1));
      vec.erase(vec.begin() + (i - 1));
      i = 0;
    }
  }
  // rep(i,0,vec.sz)cout << vec[i];
  // puts("");
  print(vec.sz);
  return 0;
}