Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename() const { return "Yes"; }
  string_type do_falsename() const { return "No"; }
};

bool dp[2][16384];

bool f(const vector<int> &s, int sum) {
  memset(dp, 0, sizeof(dp));
  dp[0][8192] = true;
  REP(i, s.size()) {
    int v = s[i];
    REP(j, 16384) dp[(i + 1) & 1][j] = false;
    REP(j, 16384) {
      if (dp[i & 1][j]) {
        dp[(i + 1) & 1][j + v] = true;
        dp[(i + 1) & 1][j - v] = true;
      }
    }
  }
  return dp[s.size() & 1][sum + 8192];
}

void solve(string s, ll x, ll y) {
  const int n = s.size();
  int p = 0;
  while (p < n && s[p] == 'F') {
    ++p;
    --x;
  }
  vector<int> vec;
  while (p < n) {
    ++p;
    int c = 0;
    while (p < n && s[p] == 'F') {
      ++p;
      ++c;
    }
    vec.push_back(c);
  }
  vector<int> xs, ys;
  REP(i, vec.size()) {
    if (i % 2)
      xs.push_back(vec[i]);
    else
      ys.push_back(vec[i]);
  }
  cout << (f(xs, x) && f(ys, y)) << endl;
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
  ll x;
  ll y;
  string s;
  cin >> s;
  scanf("%lld", &x);
  scanf("%lld", &y);
  solve(s, x, y);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> int len(const T &x) { return x.size(); }

template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

struct yes_no : numpunct<char> {
  string_type do_truename() const { return "Yes"; }
  string_type do_falsename() const { return "No"; }
};

bool dp[2][16384];

bool f(const vector<int> &s, int sum) {
  if (abs(sum) > 8000)
    return false;
  memset(dp, 0, sizeof(dp));
  dp[0][8192] = true;
  REP(i, s.size()) {
    int v = s[i];
    REP(j, 16384) dp[(i + 1) & 1][j] = false;
    REP(j, 16384) {
      if (dp[i & 1][j]) {
        dp[(i + 1) & 1][j + v] = true;
        dp[(i + 1) & 1][j - v] = true;
      }
    }
  }
  return dp[s.size() & 1][sum + 8192];
}

void solve(string s, ll x, ll y) {
  const int n = s.size();
  int p = 0;
  while (p < n && s[p] == 'F') {
    ++p;
    --x;
  }
  vector<int> vec;
  while (p < n) {
    ++p;
    int c = 0;
    while (p < n && s[p] == 'F') {
      ++p;
      ++c;
    }
    vec.push_back(c);
  }
  vector<int> xs, ys;
  REP(i, vec.size()) {
    if (i % 2)
      xs.push_back(vec[i]);
    else
      ys.push_back(vec[i]);
  }
  cout << (f(xs, x) && f(ys, y)) << endl;
}

int main() {
  locale loc(locale(), new yes_no);
  cout << boolalpha << setprecision(12) << fixed;
  cout.imbue(loc);
  ll x;
  ll y;
  string s;
  cin >> s;
  scanf("%lld", &x);
  scanf("%lld", &y);
  solve(s, x, y);
  return 0;
}