Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define MAX 8005

char buf[MAX];

deque<int> ar[2];
string s;
bool dp[MAX][MAX * 2];

inline bool reachable(deque<int> &d, int goal) {
  memset(dp, false, sizeof(dp));
  int base = MAX;
  dp[0][base] = true;
  for (int i = 0; i < d.size(); i++) {
    for (int ich = 0; ich < MAX * 2; ich++) {
      if (dp[i][ich] == true) {
        int nex = ich + d[i];
        dp[i + 1][nex] = true;
        nex = ich - d[i];
        dp[i + 1][nex] = true;
      }
    }
  }
  return dp[d.size()][goal + base];
}

int main() {
  scanf("%s", buf);
  int a, b;
  scanf("%d%d", &a, &b);
  s = buf;
  int cur = 0;
  int cnt = 0;
  bool FA = false;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      ar[cur].push_back(cnt);
      cur ^= true;
      cnt = 0;
    } else {
      cnt++;
    }
  }
  ar[cur].push_back(cnt);
  // 0 first
  if (ar[1].size() == 0 && ar[0].size() == 0) {
    puts("No");
    return 0;
  }
  if (ar[0].size()) {
    a -= ar[0].front();
    ar[0].pop_front();
  }
  if (reachable(ar[0], a) && reachable(ar[1], b)) {
    puts("Yes");
  } else {
    puts("No");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define MAX 8005

char buf[MAX];

deque<int> ar[2];
string s;
bool dp[MAX][MAX * 2];

inline bool reachable(deque<int> &d, int goal) {
  if (abs(goal) > 8000) {
    return false;
  }
  memset(dp, false, sizeof(dp));
  int base = MAX;
  dp[0][base] = true;
  for (int i = 0; i < d.size(); i++) {
    for (int ich = 0; ich < MAX * 2; ich++) {
      if (dp[i][ich] == true) {
        int nex = ich + d[i];
        dp[i + 1][nex] = true;
        nex = ich - d[i];
        dp[i + 1][nex] = true;
      }
    }
  }
  return dp[d.size()][goal + base];
}

int main() {
  scanf("%s", buf);
  int a, b;
  scanf("%d%d", &a, &b);
  s = buf;
  int cur = 0;
  int cnt = 0;
  bool FA = false;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      ar[cur].push_back(cnt);
      cur ^= true;
      cnt = 0;
    } else {
      cnt++;
    }
  }
  ar[cur].push_back(cnt);
  // 0 first
  if (ar[1].size() == 0 && ar[0].size() == 0) {
    puts("No");
    return 0;
  }
  if (ar[0].size()) {
    a -= ar[0].front();
    ar[0].pop_front();
  }
  if (reachable(ar[0], a) && reachable(ar[1], b)) {
    puts("Yes");
  } else {
    puts("No");
  }
  return 0;
}