Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 16010;
const int OFFSET = 8000;
const int N = 2000;

vi dx, dy;
bool dpx[N][MAX], dpy[N][MAX];

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

  bool x_dir = true;
  int last_t = -1;
  rep(i, s.size()) {
    if (s[i] == 'F')
      continue;
    if (x_dir)
      dx.push_back(i - last_t - 1);
    else
      dy.push_back(i - last_t - 1);
    last_t = i;
    x_dir = !x_dir;
  }
  if (x_dir)
    dx.push_back(s.size() - last_t - 1);
  else
    dy.push_back(s.size() - last_t - 1);

  int x_max = dx[0] + OFFSET, x_min = dx[0] + OFFSET;
  rep1(i, dx.size() - 1) {
    x_max += dx[i];
    x_min -= dx[i];
  }
  rep(i, dx.size() + 1) for (int j = x_min; j <= x_max; j++) dpx[i][j] = false;
  dpx[0][dx[0] + OFFSET] = true;
  rep1(i, dx.size() - 1) {
    for (int j = x_min; j <= x_max; j++) {
      if (x_min <= j - dx[i] && dpx[i - 1][j - dx[i]])
        dpx[i][j] = true;
      if (j + dx[i] <= x_max && dpx[i - 1][j + dx[i]])
        dpx[i][j] = true;
    }
  }

  int y_max = OFFSET, y_min = OFFSET;
  rep(i, dy.size()) {
    y_max += dy[i];
    y_min -= dy[i];
  }
  rep(i, dy.size() + 1) for (int j = y_min; j <= y_max; j++) dpy[i][j] = false;
  dpy[0][OFFSET] = true;
  rep(i, dy.size()) {
    for (int j = y_min; j <= y_max; j++) {
      if (y_min <= j - dy[i] && dpy[i][j - dy[i]])
        dpy[i + 1][j] = true;
      if (j + dy[i] <= y_max && dpy[i][j + dy[i]])
        dpy[i + 1][j] = true;
    }
  }

  if (dpx[dx.size() - 1][x + OFFSET] && dpy[dy.size()][y + OFFSET])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 18000;
const int OFFSET = 9000;
const int N = 5000;

vi dx, dy;
bool dpx[N][MAX], dpy[N][MAX];

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

  bool x_dir = true;
  int last_t = -1;
  rep(i, s.size()) {
    if (s[i] == 'F')
      continue;
    if (x_dir)
      dx.push_back(i - last_t - 1);
    else
      dy.push_back(i - last_t - 1);
    last_t = i;
    x_dir = !x_dir;
  }
  if (x_dir)
    dx.push_back(s.size() - last_t - 1);
  else
    dy.push_back(s.size() - last_t - 1);

  int x_max = dx[0] + OFFSET, x_min = dx[0] + OFFSET;
  rep1(i, dx.size() - 1) {
    x_max += dx[i];
    x_min -= dx[i];
  }
  rep(i, dx.size() + 1) for (int j = x_min; j <= x_max; j++) dpx[i][j] = false;
  dpx[0][dx[0] + OFFSET] = true;
  rep1(i, dx.size() - 1) {
    for (int j = x_min; j <= x_max; j++) {
      if (x_min <= j - dx[i] && dpx[i - 1][j - dx[i]])
        dpx[i][j] = true;
      if (j + dx[i] <= x_max && dpx[i - 1][j + dx[i]])
        dpx[i][j] = true;
    }
  }

  int y_max = OFFSET, y_min = OFFSET;
  rep(i, dy.size()) {
    y_max += dy[i];
    y_min -= dy[i];
  }
  rep(i, dy.size() + 1) for (int j = y_min; j <= y_max; j++) dpy[i][j] = false;
  dpy[0][OFFSET] = true;
  rep(i, dy.size()) {
    for (int j = y_min; j <= y_max; j++) {
      if (y_min <= j - dy[i] && dpy[i][j - dy[i]])
        dpy[i + 1][j] = true;
      if (j + dy[i] <= y_max && dpy[i][j + dy[i]])
        dpy[i + 1][j] = true;
    }
  }

  if (dpx[dx.size() - 1][x + OFFSET] && dpy[dy.size()][y + OFFSET])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}