Problem p03488 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iterator>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MOD 1000000007
using namespace std;

long dp[8005][8005];

bool judge(vector<long> &mv, long val) {
  long i, j;
  long size = (long)mv.size();
  long sum = 0;
  for (i = 0; i < size; i++) {
    sum += mv[i];
  }
  if (sum < val || (sum - val) % 2 == 1) {
    return false;
  }
  long val2 = (sum - val) / 2;

  vector<vector<bool>> dp(size + 1, vector<bool>(val2 + 1, false));
  dp[0][0] = true;
  for (i = 1; i <= size; i++) {
    for (j = 0; j <= val2; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= mv[i - 1]) {
        dp[i][j] = dp[i][j] || dp[i - 1][j - mv[i]];
      }
    }
  }
  return dp[size][val2];
}

int main(int argc, char *argv[]) {
  char str[8005] = {0};
  scanf("%s", str);
  long len = strlen(str);

  long x, y;
  scanf("%ld %ld", &x, &y);

  long mvx0 = 0;
  vector<long> mvx;
  vector<long> mvy;

  long tcount = 0;
  long i;
  for (i = 0; i < len; i++) {
    long count = 0;
    long k = i;
    while (k < len && str[k] == 'F') {
      count++;
      k++;
    }
    if (count > 0) {
      if (tcount % 2 == 0) {
        if (tcount == 0)
          mvx0 = count;
        else
          mvx.push_back(count);
      } else {
        mvy.push_back(count);
      }
      i += (count - 1);
    } else {
      tcount++;
    }
  }

  if (judge(mvx, abs(x - mvx0)) && judge(mvy, abs(y))) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iterator>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MOD 1000000007
using namespace std;

long dp[8005][8005];

bool judge(vector<long> &mv, long val) {
  long i, j;
  long size = (long)mv.size();
  long sum = 0;
  for (i = 0; i < size; i++) {
    sum += mv[i];
  }
  if (sum < val || (sum - val) % 2 == 1) {
    return false;
  }
  long val2 = (sum - val) / 2;

  vector<vector<bool>> dp(size + 1, vector<bool>(val2 + 1, false));
  dp[0][0] = true;
  for (i = 1; i <= size; i++) {
    for (j = 0; j <= val2; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= mv[i - 1]) {
        dp[i][j] = dp[i][j] || dp[i - 1][j - mv[i - 1]];
      }
    }
  }
  return dp[size][val2];
}

int main(int argc, char *argv[]) {
  char str[8005] = {0};
  scanf("%s", str);
  long len = strlen(str);

  long x, y;
  scanf("%ld %ld", &x, &y);

  long mvx0 = 0;
  vector<long> mvx;
  vector<long> mvy;

  long tcount = 0;
  long i;
  for (i = 0; i < len; i++) {
    long count = 0;
    long k = i;
    while (k < len && str[k] == 'F') {
      count++;
      k++;
    }
    if (count > 0) {
      if (tcount % 2 == 0) {
        if (tcount == 0)
          mvx0 = count;
        else
          mvx.push_back(count);
      } else {
        mvy.push_back(count);
      }
      i += (count - 1);
    } else {
      tcount++;
    }
  }

  if (judge(mvx, abs(x - mvx0)) && judge(mvy, abs(y))) {
    printf("Yes\n");
  } else {
    printf("No\n");
  }

  return 0;
}