Problem p03488 (C++)

Buggy submission (status: Memory Limit Exceeded):
// #include<bits/stdc++.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

// #define int long long int
#define double long double
using namespace std;
#define INT_DMAX 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> p;
typedef pair<double, double> dop;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

signed main() {
  std::string s;
  std::cin >> s;
  int x, y;
  std::cin >> x >> y;
  std::vector<int> xstep;
  std::vector<int> ystep;
  int offs = 0;
  int t = 0;
  int flag = 1;
  int xa = 0;
  int ya = 0;
  int buf = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'T') {
      if (flag == 1) {
        xstep.pb(buf);
        xa++;
      } else {
        ystep.pb(buf);
        ya++;
      }
      buf = 0;
      flag *= -1;
      t = 1;
    } else if (t == 0) {
      offs++;
    } else if (flag == 1) {
      buf++;
    } else {
      buf++;
    }
  }
  if (flag == 1) {
    xstep.pb(buf);
    xa++;
  } else {
    ystep.pb(buf);
    ya++;
  }
  int l = s.length();
  std::vector<std::vector<int>> checkx(l + 1, std::vector<int>(2 * l + 1, 0));
  std::vector<std::vector<int>> checky(l + 1, std::vector<int>(2 * l + 1, 0));
  int out = 0;
  int xf = 0;
  int yf = 0;

  checkx[0][l + offs] = 1;
  for (int i = 0; i < xa; i++) {
    for (int j = 0; j < 2 * l + 1; j++) {
      if (j + xstep[i] < 2 * l + 1) {
        if (checkx[i][j] == 1) {
          checkx[i + 1][j + xstep[i]] = 1;
        }
      }
      if (j - xstep[i] >= 0) {
        if (checkx[i][j] == 1) {
          checkx[i + 1][j - xstep[i]] = 1;
        }
      }
    }
  }

  checky[0][l] = 1;
  for (int i = 0; i < ya; i++) {
    for (int j = 0; j < 2 * l + 1; j++) {
      if (j + ystep[i] < 2 * l + 1) {
        if (checky[i][j] == 1) {
          checky[i + 1][j + ystep[i]] = 1;
        }
      }
      if (j - ystep[i] >= 0) {
        if (checky[i][j] == 1) {
          checky[i + 1][j - ystep[i]] = 1;
        }
      }
    }
  }
  xf = checkx[xa][l + x];
  yf = checky[ya][l + y];
  out = xf && yf;
  if (out) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}

Fixed submission:
// #include<bits/stdc++.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

// #define int long long int
#define double long double
using namespace std;
#define INT_DMAX 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> p;
typedef pair<double, double> dop;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

signed main() {
  std::string s;
  std::cin >> s;
  int x, y;
  std::cin >> x >> y;
  std::vector<int> xstep;
  std::vector<int> ystep;
  int offs = 0;
  int t = 0;
  int flag = 1;
  int xa = 0;
  int ya = 0;
  int buf = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'T') {
      if (flag == 1) {
        xstep.pb(buf);
        xa++;
      } else {
        ystep.pb(buf);
        ya++;
      }
      buf = 0;
      flag *= -1;
      t = 1;
    } else if (t == 0) {
      offs++;
    } else if (flag == 1) {
      buf++;
    } else {
      buf++;
    }
  }
  if (flag == 1) {
    xstep.pb(buf);
    xa++;
  } else {
    ystep.pb(buf);
    ya++;
  }
  int l = s.length();
  std::vector<std::vector<bool>> checkx(l + 1, std::vector<bool>(2 * l + 1, 0));
  std::vector<std::vector<bool>> checky(l + 1, std::vector<bool>(2 * l + 1, 0));
  int out = 0;
  int xf = 0;
  int yf = 0;

  checkx[0][l + offs] = 1;
  for (int i = 0; i < xa; i++) {
    for (int j = 0; j < 2 * l + 1; j++) {
      if (j + xstep[i] < 2 * l + 1) {
        if (checkx[i][j] == 1) {
          checkx[i + 1][j + xstep[i]] = 1;
        }
      }
      if (j - xstep[i] >= 0) {
        if (checkx[i][j] == 1) {
          checkx[i + 1][j - xstep[i]] = 1;
        }
      }
    }
  }

  checky[0][l] = 1;
  for (int i = 0; i < ya; i++) {
    for (int j = 0; j < 2 * l + 1; j++) {
      if (j + ystep[i] < 2 * l + 1) {
        if (checky[i][j] == 1) {
          checky[i + 1][j + ystep[i]] = 1;
        }
      }
      if (j - ystep[i] >= 0) {
        if (checky[i][j] == 1) {
          checky[i + 1][j - ystep[i]] = 1;
        }
      }
    }
  }
  xf = checkx[xa][l + x];
  yf = checky[ya][l + y];
  out = xf && yf;
  if (out) {
    std::cout << "Yes" << std::endl;
  } else {
    std::cout << "No" << std::endl;
  }
  return 0;
}