Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

string s;
int x, y;

int main() {
  cin >> s;
  scanf("%d %d", &x, &y);
  vector<int> movex;
  vector<int> movey;
  int n = s.length();

  bool direc = true;
  while (true) {
    if (direc) {
      int l = s.find('T');
      if (l == -1) {
        movex.push_back(s.length());
        break;
      }
      movex.push_back(l);
      s = s.substr(l + 1, s.length() - l + 1);
      direc = false;
    } else {
      int l = s.find('T');
      if (l == -1) {
        movey.push_back(s.length());
        break;
      }
      movey.push_back(l);
      s = s.substr(l + 1, s.length() - l + 1);
      direc = true;
    }
  }

  x -= movex[0];
  movex.erase(movex.begin());

  vector<vector<bool>> dpx(movex.size() + 1, vector<bool>(2 * n + 1, false));
  vector<vector<bool>> dpy(movey.size() + 1, vector<bool>(2 * n + 1, false));
  dpx[0][n] = true;
  dpy[0][n] = true;
  REP(i, movex.size()) {
    REP(j, 2 * n + 1) {
      if (dpx[i][j]) {
        if (j + movex[i] <= 2 * n)
          dpx[i + 1][j + movex[i]] = true;
        if (j - movex[i] >= 0)
          dpx[i + 1][j - movex[i]] = true;
      }
    }
  }
  REP(i, movey.size()) {
    REP(j, 2 * n + 1) {
      if (dpy[i][j]) {
        if (j + movey[i] <= 2 * n)
          dpy[i + 1][j + movey[i]] = true;
        if (j - movey[i] >= 0)
          dpy[i + 1][j - movey[i]] = true;
      }
    }
  }
  printf(dpx[movex.size()][x + n] && dpy[movey.size()][y + n] ? "Yes" : "No");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

string s;
int x, y;

int main() {
  cin >> s;
  scanf("%d %d", &x, &y);
  vector<int> movex;
  vector<int> movey;
  int n = s.length();

  bool direc = true;
  while (true) {
    if (direc) {
      int l = s.find('T');
      if (l == -1) {
        movex.push_back(s.length());
        break;
      }
      movex.push_back(l);
      s = s.substr(l + 1, s.length() - l + 1);
      direc = false;
    } else {
      int l = s.find('T');
      if (l == -1) {
        movey.push_back(s.length());
        break;
      }
      movey.push_back(l);
      s = s.substr(l + 1, s.length() - l + 1);
      direc = true;
    }
  }

  x -= movex[0];
  movex.erase(movex.begin());

  vector<vector<bool>> dpx(movex.size() + 1, vector<bool>(2 * n + 1, false));
  vector<vector<bool>> dpy(movey.size() + 1, vector<bool>(2 * n + 1, false));
  dpx[0][n] = true;
  dpy[0][n] = true;
  REP(i, movex.size()) {
    REP(j, 2 * n + 1) {
      if (dpx[i][j]) {
        if (j + movex[i] <= 2 * n)
          dpx[i + 1][j + movex[i]] = true;
        if (j - movex[i] >= 0)
          dpx[i + 1][j - movex[i]] = true;
      }
    }
  }
  REP(i, movey.size()) {
    REP(j, 2 * n + 1) {
      if (dpy[i][j]) {
        if (j + movey[i] <= 2 * n)
          dpy[i + 1][j + movey[i]] = true;
        if (j - movey[i] >= 0)
          dpy[i + 1][j - movey[i]] = true;
      }
    }
  }
  if (0 <= x + n && x + n <= 2 * n && 0 <= y + n && y + n <= 2 * n)
    printf(dpx[movex.size()][x + n] && dpy[movey.size()][y + n] ? "Yes\n"
                                                                : "No\n");
  else
    printf("No\n");
  return 0;
}