Problem p03490 (C++)

Buggy submission (status: Runtime Error):
/*
Cerberus97
Hanit Banga
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 2e3 + 10, D = 8e3 + 10;

bool dp[N][D], seen[N][D];

bool solve(vector<int> &a, int i, int j);

int main() {
  string s;
  int x, y;
  cin >> s >> x >> y;
  int n = s.length(), st = 0;
  while (st < n and s[st] == 'F') {
    --x;
    ++st;
  }

  int cur = 0;
  vector<int> moves[2];
  moves[1].push_back(0);
  for (int i = st; i < n; ++i) {
    if (s[i] == 'T') {
      if (moves[cur].empty() or moves[cur].back() > 0) {
        moves[cur].push_back(0);
      }

      while (i < n and s[i] == 'T') {
        cur = 1 - cur;
        ++i;
      }
      --i;
    } else {
      ++moves[cur].back();
    }
  }

  bool ans = solve(moves[0], moves[0].size() - 1, abs(x));
  memset(seen, 0, sizeof(seen));
  ans = ans and solve(moves[1], moves[1].size() - 1, abs(y));
  if (ans) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }
}

bool solve(vector<int> &a, int i, int d) {
  if (i < 0) {
    return !d;
  } else if (d >= D) {
    return false;
  } else if (seen[i][d]) {
    return dp[i][d];
  }

  seen[i][d] = true;
  dp[i][d] = solve(a, i - 1, abs(d + a[i])) or solve(a, i - 1, abs(d - a[i]));
  return dp[i][d];
}

Fixed submission:
/*
Cerberus97
Hanit Banga
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 4e3 + 10, D = 8e3 + 10;

bool dp[N][D], seen[N][D];

bool solve(vector<int> &a, int i, int j);

int main() {
  string s;
  int x, y;
  cin >> s >> x >> y;
  int n = s.length(), st = 0;
  while (st < n and s[st] == 'F') {
    --x;
    ++st;
  }

  int cur = 0;
  vector<int> moves[2];
  moves[1].push_back(0);
  for (int i = st; i < n; ++i) {
    if (s[i] == 'T') {
      if (moves[cur].empty() or moves[cur].back() > 0) {
        moves[cur].push_back(0);
      }

      while (i < n and s[i] == 'T') {
        cur = 1 - cur;
        ++i;
      }
      --i;
    } else {
      ++moves[cur].back();
    }
  }

  bool ans = solve(moves[0], moves[0].size() - 1, abs(x));
  memset(seen, 0, sizeof(seen));
  ans = ans and solve(moves[1], moves[1].size() - 1, abs(y));
  if (ans) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }
}

bool solve(vector<int> &a, int i, int d) {
  if (i < 0) {
    return !d;
  } else if (d >= D) {
    return false;
  } else if (seen[i][d]) {
    return dp[i][d];
  }

  seen[i][d] = true;
  dp[i][d] = solve(a, i - 1, abs(d + a[i])) or solve(a, i - 1, abs(d - a[i]));
  return dp[i][d];
}