Problem p03488 (C++)

Buggy submission (status: Runtime Error):
// 2020年4月17日現在

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

template <class... A> void prints() { cout << "\n"; }
template <class... A> void prints_rest() { cout << "\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &...rest) {
  cout << " " << first;
  prints_rest(rest...);
}
template <class T, class... A> void prints(const T &first, const A &...rest) {
  cout << first;
  prints_rest(rest...);
}
template <class T, class... A> void printd(const T &first, const A &...rest) {
  cout << fixed << setprecision(15) << first;
  prints_rest(rest...);
}

namespace {
// printElems(&a[0], &a[n]);　みたいな感じで使う。
void printElems(const int *begin, const int *end) {
  for (const int *p = begin; p != end; ++p) {
    cout << *p << ' ';
  }
  cout << endl;
}
} // namespace

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i > 0; i--)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define souatari(i, j, n) rep1(i, n - 1) rep(j, i)
#define souatari1(i, j, n)                                                     \
  for (int i = 2; i < n + 1; i++)                                              \
  rep1(j, i - 1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int, 3> arr3;
typedef long double D;

const ll llinf = numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const ll BIG = 1LL << 60;
const string yes = "Yes";
const string no = "No";

//----------------------------------------------

set<int> xable[10000];
set<int> yable[10000];
stack<int> xq;
stack<int> yq;

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  int sx = 0;
  int pos = 0;
  while (s[pos] != 'T') {
    pos++;
    sx++;
  }
  bool flag = true; // falseならy、trueならx
  int cnttmp = 0;
  while (pos < s.size()) {

    if (s[pos] == 'T') {
      //			cout << cnttmp << ' ';
      if (flag) {
        xq.push(cnttmp);
      } else {
        yq.push(cnttmp);
      }
      flag = !flag;
      cnttmp = 0;

    } else {
      cnttmp++;
    }
    pos++;
  }
  //	prints();
  if (flag) {
    xq.push(cnttmp);
  } else {
    yq.push(cnttmp);
  }

  flag = !flag;
  cnttmp = 0;
  int xcnt = 0;
  int ycnt = 0;
  xable[0].insert(x);
  yable[0].insert(y);

  while (!xq.empty()) {
    xcnt++;
    int tmp = xq.top();
    xq.pop();
    for (auto i : xable[xcnt - 1]) {
      xable[xcnt].insert(i + tmp);
      xable[xcnt].insert(i - tmp);
    }
  }
  while (!yq.empty()) {
    ycnt++;
    int tmp = yq.top();
    yq.pop();
    //		cout << tmp << ' ';
    for (auto i : yable[ycnt - 1]) {
      yable[ycnt].insert(i + tmp);
      yable[ycnt].insert(i - tmp);
    }
  }
  //	prints();
  //		for(auto i : yable[ycnt]){
  //			cout << i << ' ';
  //		}
  //	prints();
  auto itx = xable[xcnt].find(sx);
  if (itx != xable[xcnt].end()) {
    auto ity = yable[ycnt].find(0);
    if (ity != yable[ycnt].end()) {
      prints(yes);
    } else {
      prints(no);
    }
  } else {
    prints("No");
  }

  return 0;
}


Fixed submission:
// 2020年4月17日現在

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

template <class... A> void prints() { cout << "\n"; }
template <class... A> void prints_rest() { cout << "\n"; }
template <class T, class... A>
void prints_rest(const T &first, const A &...rest) {
  cout << " " << first;
  prints_rest(rest...);
}
template <class T, class... A> void prints(const T &first, const A &...rest) {
  cout << first;
  prints_rest(rest...);
}
template <class T, class... A> void printd(const T &first, const A &...rest) {
  cout << fixed << setprecision(15) << first;
  prints_rest(rest...);
}

namespace {
// printElems(&a[0], &a[n]);　みたいな感じで使う。
void printElems(const int *begin, const int *end) {
  for (const int *p = begin; p != end; ++p) {
    cout << *p << ' ';
  }
  cout << endl;
}
} // namespace

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i > 0; i--)
#define FOR(i, j, n) for (int i = j; i < n; i++)
#define souatari(i, j, n) rep1(i, n - 1) rep(j, i)
#define souatari1(i, j, n)                                                     \
  for (int i = 2; i < n + 1; i++)                                              \
  rep1(j, i - 1)
#define mp make_pair
#define F first
#define S second
#define elif else if

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef array<int, 3> arr3;
typedef long double D;

const ll llinf = numeric_limits<ll>::max();
const ll _llinf = numeric_limits<ll>::lowest();
const int inf = numeric_limits<int>::max();
const int _inf = numeric_limits<int>::lowest();
const double dinf = numeric_limits<double>::infinity();
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const ll BIG = 1LL << 60;
const string yes = "Yes";
const string no = "No";

//----------------------------------------------

set<int> xable[10000];
set<int> yable[10000];
stack<int> xq;
stack<int> yq;

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  int sx = 0;
  int pos = 0;

  xq.push(0);
  yq.push(0);

  while (s[pos] != 'T' && pos < s.size()) {
    pos++;
    sx++;
  }
  bool flag = true; // falseならy、trueならx
  int cnttmp = 0;
  while (pos < s.size()) {

    if (s[pos] == 'T') {
      //			cout << cnttmp << ' ';
      if (flag) {
        xq.push(cnttmp);
      } else {
        yq.push(cnttmp);
      }
      flag = !flag;
      cnttmp = 0;

    } else {
      cnttmp++;
    }
    pos++;
  }
  //	prints();
  if (flag) {
    xq.push(cnttmp);
  } else {
    yq.push(cnttmp);
  }

  flag = !flag;
  cnttmp = 0;
  int xcnt = 0;
  int ycnt = 0;
  xable[0].insert(x);
  yable[0].insert(y);

  while (!xq.empty()) {
    xcnt++;
    int tmp = xq.top();
    xq.pop();
    for (auto i : xable[xcnt - 1]) {
      xable[xcnt].insert(i + tmp);
      xable[xcnt].insert(i - tmp);
    }
  }
  while (!yq.empty()) {
    ycnt++;
    int tmp = yq.top();
    yq.pop();
    //		cout << tmp << ' ';
    for (auto i : yable[ycnt - 1]) {
      yable[ycnt].insert(i + tmp);
      yable[ycnt].insert(i - tmp);
    }
  }
  //	prints();
  //		for(auto i : yable[ycnt]){
  //			cout << i << ' ';
  //		}
  //	prints();
  auto itx = xable[xcnt].find(sx);
  if (itx != xable[xcnt].end()) {
    auto ity = yable[ycnt].find(0);
    if (ity != yable[ycnt].end()) {
      prints(yes);
    } else {
      prints(no);
    }
  } else {
    prints("No");
  }

  return 0;
}