Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  vector<int> xa, ya;
  int n = (int)s.size();
  int flag = 0;
  int flag2 = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'T') {
      if (flag2 == 0)
        flag2 = 2;
      if (flag == 0 && cnt > 0) {
        xa.push_back(cnt);
      } else if (flag == 1 && cnt > 0)
        ya.push_back(cnt);
      cnt = 0;
      flag = (flag + 1) % 2;
    } else {
      if (flag2 == 0)
        flag2 = 1;
      cnt++;
    }
  }
  if (cnt > 0 && flag == 0)
    xa.push_back(cnt);
  else if (cnt > 0 && flag == 1)
    ya.push_back(cnt);
  vector<vector<bool>> xt(17000, vector<bool>((int)xa.size() + 1, false)),
      yt(17000, vector<bool>((int)xa.size() + 1, false));
  xt[8010][0] = true, yt[8010][0] = true;
  for (int i = 0; i < (int)xa.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (xt[j][i] == false)
        continue;
      xt[j + xa[i]][i + 1] = xt[j + xa[i]][i + 1] || xt[j][i];
      if (flag2 == 1 && i == 0)
        continue;
      xt[j - xa[i]][i + 1] = xt[j - xa[i]][i + 1] || xt[j][i];
    }
  }
  for (int i = 0; i < (int)ya.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (yt[j][i] == false)
        continue;
      yt[j + ya[i]][i + 1] = yt[j + ya[i]][i + 1] || yt[j][i];
      yt[j - ya[i]][i + 1] = yt[j - ya[i]][i + 1] || yt[j][i];
    }
  }
  bool fx = false, fy = false;
  for (int i = 0; i < 17000; i++) {
    if (xt[i][(int)xa.size()] && i - 8010 == x)
      fx = true;
    if (yt[i][(int)ya.size()] && i - 8010 == y)
      fy = true;
  }
  if (fx && fy)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  vector<int> xa, ya;
  int n = (int)s.size();
  int flag = 0;
  int flag2 = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'T') {
      if (flag2 == 0)
        flag2 = 2;
      if (flag == 0 && cnt > 0) {
        xa.push_back(cnt);
      } else if (flag == 1 && cnt > 0)
        ya.push_back(cnt);
      cnt = 0;
      flag = (flag + 1) % 2;
    } else {
      if (flag2 == 0)
        flag2 = 1;
      cnt++;
    }
  }
  if (cnt > 0 && flag == 0)
    xa.push_back(cnt);
  else if (cnt > 0 && flag == 1)
    ya.push_back(cnt);
  vector<vector<bool>> xt(17000, vector<bool>((int)xa.size() + 1, false)),
      yt(17000, vector<bool>((int)ya.size() + 1, false));
  xt[8010][0] = true, yt[8010][0] = true;
  for (int i = 0; i < (int)xa.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (xt[j][i] == false)
        continue;
      xt[j + xa[i]][i + 1] = xt[j + xa[i]][i + 1] || xt[j][i];
      if (flag2 == 1 && i == 0)
        continue;
      xt[j - xa[i]][i + 1] = xt[j - xa[i]][i + 1] || xt[j][i];
    }
  }
  for (int i = 0; i < (int)ya.size(); i++) {
    for (int j = 0; j < 17000; j++) {
      if (yt[j][i] == false)
        continue;
      yt[j + ya[i]][i + 1] = yt[j + ya[i]][i + 1] || yt[j][i];
      yt[j - ya[i]][i + 1] = yt[j - ya[i]][i + 1] || yt[j][i];
    }
  }
  bool fx = false, fy = false;
  for (int i = 0; i < 17000; i++) {
    if (xt[i][(int)xa.size()] && i - 8010 == x)
      fx = true;
    if (yt[i][(int)ya.size()] && i - 8010 == y)
      fy = true;
  }
  if (fx && fy)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}