Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;

  int fcnt = 0;
  int tcnt = 0;
  vector<int> f, t;
  f.emplace_back(0);
  t.emplace_back(0);

  bool state = true;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'T') {
      tcnt++;
    } else {
      if (tcnt != 0) {
        if (state) {
          f.emplace_back(fcnt);
          if (tcnt % 2 != 0) {
            state = !state;
          }
          tcnt = 0;
          fcnt = 0;
        } else if (!state) {
          t.emplace_back(fcnt);
          if (tcnt % 2 != 0) {
            state = !state;
          }
          tcnt = 0;
          fcnt = 0;
        }
      }
      fcnt++;
    }
  }

  if (fcnt != 0) {
    if (state) {
      f.emplace_back(fcnt);
    } else {
      t.emplace_back(fcnt);
    }
  }
  rep(i, f.size()) { dump(f.at(i)); }

  rep(i, t.size()) { dump(t.at(i)); }

  vector<vector<int>> dpx(f.size() + 1, vector<int>(16020, 0));
  vector<vector<int>> dpy(t.size() + 1, vector<int>(16020, 0));
  dpx.at(0).at(8000) = 1;
  dpy.at(0).at(8000) = 1;
  for (int i = 1; i < f.size(); i++) {
    for (int j = 0; j < 16020; j++) {
      if (s.at(0) == 'F' && i == 1) {
        dpx.at(i).at(j + f.at(i)) = 1;
        continue;
      }
      if (dpx.at(i - 1).at(j) == 1) {
        if (j + f.at(i) < 16020)
          dpx.at(i).at(j + f.at(i)) = 1;
        if (j - f.at(i) >= 0)
          dpx.at(i).at(j - f.at(i)) = 1;
      }
    }
  }

  for (int i = 1; i < t.size(); i++) {
    for (int j = 0; j < 16020; j++) {
      if (dpy.at(i - 1).at(j) == 1) {
        if (j + t.at(i) < 16020)
          dpy.at(i).at(j + t.at(i)) = 1;
        if (j - t.at(i) >= 0)
          dpy.at(i).at(j - t.at(i)) = 1;
      }
    }
  }

  if (dpx.at(f.size() - 1).at(x + 8000) == 1 &&
      dpy.at(t.size() - 1).at(y + 8000) == 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
typedef long long ll;
#define dump(x) cerr << #x << "=" << x << endl
using P = pair<int, int>;

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;

  int fcnt = 0;
  int tcnt = 0;
  vector<int> f, t;
  f.emplace_back(0);
  t.emplace_back(0);

  bool state = true;
  for (int i = 0; i < s.size(); i++) {
    if (s.at(i) == 'T') {
      tcnt++;
    } else {
      if (tcnt != 0) {
        if (state) {
          f.emplace_back(fcnt);
          if (tcnt % 2 != 0) {
            state = !state;
          }
          tcnt = 0;
          fcnt = 0;
        } else if (!state) {
          t.emplace_back(fcnt);
          if (tcnt % 2 != 0) {
            state = !state;
          }
          tcnt = 0;
          fcnt = 0;
        }
      }
      fcnt++;
    }
  }

  if (fcnt != 0) {
    if (state) {
      f.emplace_back(fcnt);
    } else {
      t.emplace_back(fcnt);
    }
  }
  rep(i, f.size()) { dump(f.at(i)); }

  rep(i, t.size()) { dump(t.at(i)); }

  vector<vector<int>> dpx(f.size() + 1, vector<int>(16020, 0));
  vector<vector<int>> dpy(t.size() + 1, vector<int>(16020, 0));
  dpx.at(0).at(8000) = 1;
  dpy.at(0).at(8000) = 1;
  for (int i = 1; i < f.size(); i++) {
    for (int j = 0; j < 16020; j++) {
      if (s.at(0) == 'F' && i == 1) {
        dpx.at(i).at(8000 + f.at(i)) = 1;
        continue;
      }
      if (dpx.at(i - 1).at(j) == 1) {
        if (j + f.at(i) < 16020)
          dpx.at(i).at(j + f.at(i)) = 1;
        if (j - f.at(i) >= 0)
          dpx.at(i).at(j - f.at(i)) = 1;
      }
    }
  }

  for (int i = 1; i < t.size(); i++) {
    for (int j = 0; j < 16020; j++) {
      if (dpy.at(i - 1).at(j) == 1) {
        if (j + t.at(i) < 16020)
          dpy.at(i).at(j + t.at(i)) = 1;
        if (j - t.at(i) >= 0)
          dpy.at(i).at(j - t.at(i)) = 1;
      }
    }
  }

  if (dpx.at(f.size() - 1).at(x + 8000) == 1 &&
      dpy.at(t.size() - 1).at(y + 8000) == 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}