Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & (-x))
#define pi acos(-1.0)
#define eps 1e-8
#define MOD 100000007
#define INF 100000000
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(i, a, n) for (register int i = a; i <= n; ++i)
#define FDR(i, a, n) for (register int i = a; i >= n; --i)
#define bug puts("H");
#define lch p << 1, l, mid
#define rch p << 1 | 1, mid + 1, r
#define mp make_pair
#define pb push_back
typedef pair<int, int> PII;
typedef vector<int> VI;
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
inline char nc() {
  static char buf[1000000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int Scan() {
  char ch = nc();
  int sum = 0, f = 1;
  if (ch == '-')
    f = -1, ch = nc();
  while (!(ch >= '0' && ch <= '9'))
    ch = nc();
  while (ch >= '0' && ch <= '9')
    sum = sum * 10 + ch - 48, ch = nc();
  return sum * f;
}
const int N = 8005;

char s[N];
VI v[2];
int dp[2][N * 10];

int sol(int x) { return x + N; }
int main() {
  int x, y;
  scanf("%s%d%d", s, &x, &y);
  int cnt = 0, num = 0;
  for (int i = 0; s[i]; ++i) {
    if (s[i] == 'T')
      v[cnt].pb(num), cnt ^= 1, num = 0;
    else
      ++num;
  }
  v[cnt].pb(num);
  // for (int i=0; i<v[0].size(); ++i) cout<<v[0][i]<<endl;
  int flag = 0;
  dp[flag][sol(0)] = dp[flag][sol(v[0][0])] = 1;
  for (int i = 1; i < v[0].size(); ++i) {
    flag ^= 1;
    mem(dp[flag], 0);
    for (int j = N; j >= -N; --j) {
      dp[flag][sol(j)] |= dp[flag ^ 1][sol(j - v[0][i])];
      dp[flag][sol(j)] |= dp[flag ^ 1][sol(j + v[0][i])];
    }
  }
  if (dp[flag][sol(x)] != 1)
    puts("No");
  else {
    mem(dp, 0);
    flag = 0;
    dp[flag][sol(0)] = 1;
    for (int i = 0; i < v[1].size(); ++i) {
      flag ^= 1;
      mem(dp[flag], 0);
      for (int j = N; j >= -N; --j) {
        dp[flag][sol(j)] |= dp[flag ^ 1][sol(j - v[1][i])];
        dp[flag][sol(j)] |= dp[flag ^ 1][sol(j + v[1][i])];
      }
    }
    puts(dp[flag][sol(y)] ? "Yes" : "No");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & (-x))
#define pi acos(-1.0)
#define eps 1e-8
#define MOD 100000007
#define INF 100000000
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(i, a, n) for (register int i = a; i <= n; ++i)
#define FDR(i, a, n) for (register int i = a; i >= n; --i)
#define bug puts("H");
#define lch p << 1, l, mid
#define rch p << 1 | 1, mid + 1, r
#define mp make_pair
#define pb push_back
typedef pair<int, int> PII;
typedef vector<int> VI;
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
inline char nc() {
  static char buf[1000000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int Scan() {
  char ch = nc();
  int sum = 0, f = 1;
  if (ch == '-')
    f = -1, ch = nc();
  while (!(ch >= '0' && ch <= '9'))
    ch = nc();
  while (ch >= '0' && ch <= '9')
    sum = sum * 10 + ch - 48, ch = nc();
  return sum * f;
}
const int N = 8005;

char s[N];
VI v[2];
int dp[2][N * 10];

int sol(int x) { return x + N * 2; }
int main() {
  int x, y;
  scanf("%s%d%d", s, &x, &y);
  int cnt = 0, num = 0;
  for (int i = 0; s[i]; ++i) {
    if (s[i] == 'T')
      v[cnt].pb(num), cnt ^= 1, num = 0;
    else
      ++num;
  }
  v[cnt].pb(num);
  // for (int i=0; i<v[0].size(); ++i) cout<<v[0][i]<<endl;
  int flag = 0;
  dp[flag][sol(0)] = dp[flag][sol(v[0][0])] = 1;
  for (int i = 1; i < v[0].size(); ++i) {
    flag ^= 1;
    mem(dp[flag], 0);
    for (int j = N; j >= -N; --j) {
      dp[flag][sol(j)] |= dp[flag ^ 1][sol(j - v[0][i])];
      dp[flag][sol(j)] |= dp[flag ^ 1][sol(j + v[0][i])];
    }
  }
  if (dp[flag][sol(x)] != 1)
    puts("No");
  else {
    mem(dp, 0);
    flag = 0;
    dp[flag][sol(0)] = 1;
    for (int i = 0; i < v[1].size(); ++i) {
      flag ^= 1;
      mem(dp[flag], 0);
      for (int j = N; j >= -N; --j) {
        dp[flag][sol(j)] |= dp[flag ^ 1][sol(j - v[1][i])];
        dp[flag][sol(j)] |= dp[flag ^ 1][sol(j + v[1][i])];
      }
    }
    puts(dp[flag][sol(y)] ? "Yes" : "No");
  }
  return 0;
}