Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
// #define PRINT(x) { std::cout << #x << ": " << x << std::endl; }
#define PRINT(x)

char S[8001] = {};
int X, Y;
int x0 = 0;

char get(char *tbl, int v, int rng) {
  PRINT(v);
  PRINT(rng);
  if (v < -rng || rng < v) {
    return 0;
  }
  PRINT((int)tbl[v + rng]);
  return tbl[v + rng];
}

bool dp(int v0, const vector<int> &v, int vf) {
  if (v.empty()) {
    return v0 == vf;
  }
  int sum = 0;
  vector<int> rng;
  rng.push_back(sum);
  for (int i = 0; i < v.size(); ++i) {
    sum += v[i];
    rng.push_back(sum);
  }
  int vvf = vf - v0;
  PRINT(vvf);
  PRINT(sum);
  if (vvf < -sum || sum < vvf) {
    return false;
  }
  char tbl1[8000];
  char tbl2[8000];
  char *fr_tbl = tbl1;
  char *to_tbl = tbl2;
  memset(fr_tbl, 0, 2 * rng[0] + 1);
  fr_tbl[0] = 1;
  for (int i = 0; i < v.size(); ++i) {
    memset(to_tbl, 0, 2 * rng[i + 1] + 1);
    for (int j = -rng[i + 1]; j <= rng[i + 1]; ++j) {
      to_tbl[j + rng[i + 1]] =
          get(fr_tbl, j + v[i], rng[i]) || get(fr_tbl, j - v[i], rng[i]);
    }
    std::swap(fr_tbl, to_tbl);
  }
  int ans = get(fr_tbl, vvf, rng[v.size()]);
  return ans;
}

int main(int argc, char *argv[]) {
  scanf("%s", S);
  scanf("%d %d", &X, &Y);
  const int ns = strlen(S);
  int is;
  for (is = 0; is < ns; ++is) {
    if (S[is] == 'F')
      x0 += 1;
    else
      break;
  }
  ++is;
  int state = 1; // 1(y) -> 0(x) -> 1(y)...
  vector<int> vx;
  vector<int> vy;
  int v = 0;
  for (; is < ns; ++is) {
    if (S[is] == 'F') {
      ++v;
    } else {
      if (state)
        vy.push_back(v);
      else
        vx.push_back(v);
      state = !state;
      v = 0;
    }
  }
  if (state)
    vy.push_back(v);
  else
    vx.push_back(v);

  if (dp(x0, vx, X) && dp(0, vy, Y)) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;
// #define PRINT(x) { std::cout << #x << ": " << x << std::endl; }
#define PRINT(x)

char S[8001] = {};
int X, Y;
int x0 = 0;

char get(char *tbl, int v, int rng) {
  PRINT(v);
  PRINT(rng);
  if (v < -rng || rng < v) {
    return 0;
  }
  PRINT((int)tbl[v + rng]);
  return tbl[v + rng];
}

bool dp(int v0, const vector<int> &v, int vf) {
  if (v.empty()) {
    return v0 == vf;
  }
  int sum = 0;
  vector<int> rng;
  rng.push_back(sum);
  for (int i = 0; i < v.size(); ++i) {
    sum += v[i];
    rng.push_back(sum);
  }
  int vvf = vf - v0;
  PRINT(vvf);
  PRINT(sum);
  if (vvf < -sum || sum < vvf) {
    return false;
  }
  char tbl1[16000];
  char tbl2[16000];
  char *fr_tbl = tbl1;
  char *to_tbl = tbl2;
  memset(fr_tbl, 0, 2 * rng[0] + 1);
  fr_tbl[0] = 1;
  for (int i = 0; i < v.size(); ++i) {
    memset(to_tbl, 0, 2 * rng[i + 1] + 1);
    for (int j = -rng[i + 1]; j <= rng[i + 1]; ++j) {
      to_tbl[j + rng[i + 1]] =
          get(fr_tbl, j + v[i], rng[i]) || get(fr_tbl, j - v[i], rng[i]);
    }
    std::swap(fr_tbl, to_tbl);
  }
  int ans = get(fr_tbl, vvf, rng[v.size()]);
  return ans;
}

int main(int argc, char *argv[]) {
  scanf("%s", S);
  scanf("%d %d", &X, &Y);
  const int ns = strlen(S);
  int is;
  for (is = 0; is < ns; ++is) {
    if (S[is] == 'F')
      x0 += 1;
    else
      break;
  }
  ++is;
  int state = 1; // 1(y) -> 0(x) -> 1(y)...
  vector<int> vx;
  vector<int> vy;
  int v = 0;
  for (; is < ns; ++is) {
    if (S[is] == 'F') {
      ++v;
    } else {
      if (state)
        vy.push_back(v);
      else
        vx.push_back(v);
      state = !state;
      v = 0;
    }
  }
  if (state)
    vy.push_back(v);
  else
    vx.push_back(v);

  if (dp(x0, vx, X) && dp(0, vy, Y)) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }
  return 0;
}