Problem p03490 (C++)

Buggy submission (status: Runtime Error):
/*****************
 *   I<3   C++   *
 *  IWANT ANYAC  *
 * ILOVEPROGRAM! *
 *IT'SINTERESTING*
 * ILOVEPROGRAM! *
 *  INCONTESTS   *
 *   GETSCORE    *
 *    ACCODE     *
 *     LETS      *
 *      GO       *
 *Written by:    *
 *    charlieyan *
 *****************/
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex.h>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <errno.h>
#include <exception>
#include <fenv.h>
#include <float.h>
#include <fstream>
#include <functional>
#include <inttypes.h>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdbool.h>
#include <stdexcept>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <tgmath.h>
#include <time.h>
#include <utility>
#include <vector>
#include <wchar.h>
#include <wctype.h>
#define LL long long
#define pb push_back
#define pob pop_back
#define mp make_pair
#define FILL(a, v) memset((void *)a, v, sizeof(a))
#define FILL0(a) FILL(a, 0)
#define FILL1(a) FILL(a, -1)
#define FILLBIG(a) FILL(a, 0x7f)
#define ALL(a) (a).begin(), (a).end()
#define Inf ((int)0x7f7f7f7f)
#define INF ((long long)0x7f7f7f7f7f7f7f7f)
#define Mod 1000000007
#define foreach(it, x)                                                         \
  for (__typeof(x.begin()) it = x.begin(); it != x.end(); it++)
#define FOR(i, x) for (int i = 0; i < x; i++)
#define RFOR(i, x) for (int i = x - 1; i >= 0; i--)
#define FFOR(i, a, b) for (int i = a; i < b; i++)
#define FRFOR(i, a, b) for (int i = a - 1; i >= b; i--)
#define SZ(a) ((int)(a).size())
#define FI first
#define SE second
#define OVER(a) return puts(a), 0
#define fastio ios::sync_with_stdio(0)
#define filei(f) freopen(f, "r", stdin)
#define fileo(f) freopen(f, "w", stdout)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define MASK(a) (1 << (a))
#define MASKX(x, a) ((x) << (a))
#define BIT(a, b) ((a >> b) & 1)
#define cddx(a) priority_queue<a>
#define cxdd(a) priority_queue<a, vector<a>, greater<a>>
#define deval(a)                                                               \
  { cerr << #a << " = " << a << endl; }
#define dearr(be, en)                                                          \
  {                                                                            \
    cerr << #be;                                                               \
    cerr << " = {";                                                            \
    for (auto i = be; i != en; i++)                                            \
      cerr << ' ' << *i;                                                       \
    cerr << " }\n";                                                            \
  }
#define reg register
#define prtime eprintf("\n\nTime used: %dms", clock());
using namespace std;
typedef pair<int, int> pii;
typedef unsigned long long UINT;
typedef unsigned int uint;
typedef long long INT;
typedef long double ldouble;
typedef pair<INT, INT> PII;
typedef stringstream sst;
typedef istringstream iss;
typedef vector<int> vint;
typedef vector<INT> VINT;
typedef vector<pii> vpii;
typedef vector<PII> VPII;
typedef priority_queue<int> pqi;
typedef priority_queue<INT> PQI;
typedef priority_queue<pii> pqp;
typedef priority_queue<PII> PQP;
typedef priority_queue<int, vint, greater<int>> xqi;
typedef priority_queue<INT, VINT, greater<INT>> XQI;
typedef priority_queue<pii, vpii, greater<pii>> xqp;
typedef priority_queue<PII, VPII, greater<PII>> XQP;

int dpz[4007][8007], dpf[4007][8007];
inline int &dp(int a, int b) {
  if (b >= 0)
    return dpz[a][b];
  else
    return dpf[a][-b];
}
string s;
int x, y;
vector<int> jilu[2];

int main() {
  cin >> s >> x >> y;
  int ii = 0, mode = 0;
  while (s[ii] != 'T')
    x--, ii++;
  for (; ii < SZ(s); ii++) {
    if (s[ii] == 'T') {
      mode = 1 - mode;
      jilu[mode].push_back(0);
    } else
      jilu[mode][SZ(jilu[mode]) - 1]++;
  }
  dp(0, 0) = 1;
  for (int i = 1; i <= SZ(jilu[0]); i++)
    for (int j = -8006; j <= 8006; j++) {
      if (j - jilu[0][i - 1] >= -8006)
        dp(i, j) |= dp(i - 1, j - jilu[0][i - 1]);
      if (j + jilu[0][i - 1] <= 8006)
        dp(i, j) |= dp(i - 1, j + jilu[0][i - 1]);
    }
  if (!dp(SZ(jilu[0]), x))
    OVER("No");
  for (int i = 1; i <= SZ(jilu[0]); i++)
    for (int j = -8006; j <= 8006; j++)
      dp(i, j) = 0;
  dp(0, 0) = 1;
  for (int i = 1; i <= SZ(jilu[1]); i++)
    for (int j = -8006; j <= 8006; j++) {
      if (j - jilu[1][i - 1] >= -8006)
        dp(i, j) |= dp(i - 1, j - jilu[1][i - 1]);
      if (j + jilu[1][i - 1] <= 8006)
        dp(i, j) |= dp(i - 1, j + jilu[1][i - 1]);
    }
  if (!dp(SZ(jilu[1]), y))
    OVER("No");
  OVER("Yes");
}

Fixed submission:
/*****************
 *   I<3   C++   *
 *  IWANT ANYAC  *
 * ILOVEPROGRAM! *
 *IT'SINTERESTING*
 * ILOVEPROGRAM! *
 *  INCONTESTS   *
 *   GETSCORE    *
 *    ACCODE     *
 *     LETS      *
 *      GO       *
 *Written by:    *
 *    charlieyan *
 *****************/
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex.h>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <errno.h>
#include <exception>
#include <fenv.h>
#include <float.h>
#include <fstream>
#include <functional>
#include <inttypes.h>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdbool.h>
#include <stdexcept>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <tgmath.h>
#include <time.h>
#include <utility>
#include <vector>
#include <wchar.h>
#include <wctype.h>
#define LL long long
#define pb push_back
#define pob pop_back
#define mp make_pair
#define FILL(a, v) memset((void *)a, v, sizeof(a))
#define FILL0(a) FILL(a, 0)
#define FILL1(a) FILL(a, -1)
#define FILLBIG(a) FILL(a, 0x7f)
#define ALL(a) (a).begin(), (a).end()
#define Inf ((int)0x7f7f7f7f)
#define INF ((long long)0x7f7f7f7f7f7f7f7f)
#define Mod 1000000007
#define foreach(it, x)                                                         \
  for (__typeof(x.begin()) it = x.begin(); it != x.end(); it++)
#define FOR(i, x) for (int i = 0; i < x; i++)
#define RFOR(i, x) for (int i = x - 1; i >= 0; i--)
#define FFOR(i, a, b) for (int i = a; i < b; i++)
#define FRFOR(i, a, b) for (int i = a - 1; i >= b; i--)
#define SZ(a) ((int)(a).size())
#define FI first
#define SE second
#define OVER(a) return puts(a), 0
#define fastio ios::sync_with_stdio(0)
#define filei(f) freopen(f, "r", stdin)
#define fileo(f) freopen(f, "w", stdout)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define MASK(a) (1 << (a))
#define MASKX(x, a) ((x) << (a))
#define BIT(a, b) ((a >> b) & 1)
#define cddx(a) priority_queue<a>
#define cxdd(a) priority_queue<a, vector<a>, greater<a>>
#define deval(a)                                                               \
  { cerr << #a << " = " << a << endl; }
#define dearr(be, en)                                                          \
  {                                                                            \
    cerr << #be;                                                               \
    cerr << " = {";                                                            \
    for (auto i = be; i != en; i++)                                            \
      cerr << ' ' << *i;                                                       \
    cerr << " }\n";                                                            \
  }
#define reg register
#define prtime eprintf("\n\nTime used: %dms", clock());
using namespace std;
typedef pair<int, int> pii;
typedef unsigned long long UINT;
typedef unsigned int uint;
typedef long long INT;
typedef long double ldouble;
typedef pair<INT, INT> PII;
typedef stringstream sst;
typedef istringstream iss;
typedef vector<int> vint;
typedef vector<INT> VINT;
typedef vector<pii> vpii;
typedef vector<PII> VPII;
typedef priority_queue<int> pqi;
typedef priority_queue<INT> PQI;
typedef priority_queue<pii> pqp;
typedef priority_queue<PII> PQP;
typedef priority_queue<int, vint, greater<int>> xqi;
typedef priority_queue<INT, VINT, greater<INT>> XQI;
typedef priority_queue<pii, vpii, greater<pii>> xqp;
typedef priority_queue<PII, VPII, greater<PII>> XQP;

int dpz[4007][8007], dpf[4007][8007];
inline int &dp(int a, int b) {
  if (b >= 0)
    return dpz[a][b];
  else
    return dpf[a][-b];
}
string s;
int x, y;
vector<int> jilu[2];

int main() {
  cin >> s >> x >> y;
  int ii = 0, mode = 0;
  while (s[ii] != 'T' && ii < SZ(s))
    x--, ii++;
  for (; ii < SZ(s); ii++) {
    if (s[ii] == 'T') {
      mode = 1 - mode;
      jilu[mode].push_back(0);
    } else
      jilu[mode][SZ(jilu[mode]) - 1]++;
  }
  dp(0, 0) = 1;
  for (int i = 1; i <= SZ(jilu[0]); i++)
    for (int j = -8006; j <= 8006; j++) {
      if (j - jilu[0][i - 1] >= -8006)
        dp(i, j) |= dp(i - 1, j - jilu[0][i - 1]);
      if (j + jilu[0][i - 1] <= 8006)
        dp(i, j) |= dp(i - 1, j + jilu[0][i - 1]);
    }
  if (!dp(SZ(jilu[0]), x))
    OVER("No");
  for (int i = 1; i <= SZ(jilu[0]); i++)
    for (int j = -8006; j <= 8006; j++)
      dp(i, j) = 0;
  dp(0, 0) = 1;
  for (int i = 1; i <= SZ(jilu[1]); i++)
    for (int j = -8006; j <= 8006; j++) {
      if (j - jilu[1][i - 1] >= -8006)
        dp(i, j) |= dp(i - 1, j - jilu[1][i - 1]);
      if (j + jilu[1][i - 1] <= 8006)
        dp(i, j) |= dp(i - 1, j + jilu[1][i - 1]);
    }
  if (!dp(SZ(jilu[1]), y))
    OVER("No");
  OVER("Yes");
}