Problem p03488 (C++)

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

int main() {
  string s;
  int x, y;
  cin >> s >> x >> y;
  s += 'T';
  bool flg_x = false, fst = true;
  int d = 0;
  vector<int> dx, dy;
  for (char c : s) {
    if (c == 'F') {
      d++;
    } else if (fst) {
      x -= d;
      fst = false;
      d = 0;
    } else {
      if (flg_x) {
        dx.push_back(d);
      } else {
        dy.push_back(d);
      }
      flg_x = !flg_x;
      d = 0;
    }
  }
  x = abs(x);
  y = abs(y);
  vector<vector<bool>> dp_x(dx.size() + 1, vector<bool>(2 * s.size() + 1));
  dp_x.at(0).at(s.size()) = true;
  for (int i = 0; i < dx.size(); i++) {
    for (int j = 0; j <= 2 * s.size(); j++) {
      if (j >= dx.at(i))
        dp_x.at(i + 1).at(j) =
            dp_x.at(i + 1).at(j) || dp_x.at(i).at(j - dx.at(i));
      if (j <= 2 * s.size() - dx.at(i))
        dp_x.at(i + 1).at(j) =
            dp_x.at(i + 1).at(j) || dp_x.at(i).at(j + dx.at(i));
    }
  }
  if (!dp_x.at(dx.size()).at(s.size() + x)) {
    cout << "No" << endl;
    return 0;
  }
  vector<vector<bool>> dp_y(dy.size() + 1, vector<bool>(2 * s.size() + 1));
  dp_y.at(0).at(s.size()) = true;
  for (int i = 0; i < dy.size(); i++) {
    for (int j = 0; j <= 2 * s.size(); j++) {
      if (j >= dy.at(i))
        dp_y.at(i + 1).at(j) =
            dp_y.at(i + 1).at(j) || dp_y.at(i).at(j - dy.at(i));
      if (j <= 2 * s.size() - dy.at(i))
        dp_y.at(i + 1).at(j) =
            dp_y.at(i + 1).at(j) || dp_y.at(i).at(j + dy.at(i));
    }
  }
  if (!dp_y.at(dy.size()).at(s.size() + y)) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
}


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

int main() {
  string s;
  int x, y;
  cin >> s >> x >> y;
  s += 'T';
  bool flg_x = false, fst = true;
  int d = 0;
  vector<int> dx, dy;
  for (char c : s) {
    if (c == 'F') {
      d++;
    } else if (fst) {
      x -= d;
      fst = false;
      d = 0;
    } else {
      if (flg_x) {
        dx.push_back(d);
      } else {
        dy.push_back(d);
      }
      flg_x = !flg_x;
      d = 0;
    }
  }
  x = abs(x);
  y = abs(y);
  if (x > s.size()) {
    cout << "No" << endl;
    return 0;
  }
  vector<vector<bool>> dp_x(dx.size() + 1, vector<bool>(2 * s.size() + 1));
  dp_x.at(0).at(s.size()) = true;
  for (int i = 0; i < dx.size(); i++) {
    for (int j = 0; j <= 2 * s.size(); j++) {
      if (j >= dx.at(i))
        dp_x.at(i + 1).at(j) =
            dp_x.at(i + 1).at(j) || dp_x.at(i).at(j - dx.at(i));
      if (j <= 2 * s.size() - dx.at(i))
        dp_x.at(i + 1).at(j) =
            dp_x.at(i + 1).at(j) || dp_x.at(i).at(j + dx.at(i));
    }
  }
  if (!dp_x.at(dx.size()).at(s.size() + x)) {
    cout << "No" << endl;
    return 0;
  }
  vector<vector<bool>> dp_y(dy.size() + 1, vector<bool>(2 * s.size() + 1));
  dp_y.at(0).at(s.size()) = true;
  for (int i = 0; i < dy.size(); i++) {
    for (int j = 0; j <= 2 * s.size(); j++) {
      if (j >= dy.at(i))
        dp_y.at(i + 1).at(j) =
            dp_y.at(i + 1).at(j) || dp_y.at(i).at(j - dy.at(i));
      if (j <= 2 * s.size() - dy.at(i))
        dp_y.at(i + 1).at(j) =
            dp_y.at(i + 1).at(j) || dp_y.at(i).at(j + dy.at(i));
    }
  }
  if (!dp_y.at(dy.size()).at(s.size() + y)) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
}