prithivMLmods/Coder-Stat huggingface dataset:
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
// typedef pair<double, double> P;
const int dx[9] = {-1, 0, 0, 1, -1, -1, 1, 1, 0};
const int dy[9] = {0, -1, 1, 0, -1, 1, -1, 1, 0};

string s;
int x, y;
const int Z = 8003;
const int N = 2 * Z + 1;
bool dp[2][2][N];
// char hoge[505050];
//  char s[N];

signed main() {
  cin >> s;
  // scanf("%s", s);
  cin >> x >> y;
  // scanf("%lld%lld", &x, &y);
  int n = s.length();
  // int n = strlen(s);

  // REP(i,8000) s += 'a';

  dp[0][0][Z] = dp[1][0][Z] = 1;

  int pos = 0LL;
  while (pos < n && s[pos] == 'F') {
    pos++;
    x--;
  }

  int t = 0LL;
  while (pos < n) {
    pos++;
    t ^= 1;
    int L = 0;
    while (pos < n && s[pos] == 'F') {
      pos++;
      L++;
    }
    REP(i, N) dp[t][1][i] = 0; // 1の方は計算結果
    REP(i, N) {
      if (!dp[t][0][i])
        continue;
      dp[t][1][i - L] = dp[t][1][i + L] = 1;
    }
    REP(i, N) dp[t][0][i] = dp[t][1][i];
  }

  if (dp[0][0][Z + x] && dp[1][0][Z + y]) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define MOD 1000000007
#define int long long
#ifdef int
const long long INF = LLONG_MAX / 10;
#else
const int INF = 1010101010;
#endif
using namespace std;
typedef long long ll;
typedef vector<vector<ll>> mat;
typedef pair<int, int> P;
// typedef pair<double, double> P;
const int dx[9] = {-1, 0, 0, 1, -1, -1, 1, 1, 0};
const int dy[9] = {0, -1, 1, 0, -1, 1, -1, 1, 0};

string s;
int x, y;
const int Z = 18003;
const int N = 2 * Z + 1;
bool dp[2][2][N];
// char hoge[505050];
//  char s[N];

signed main() {
  cin >> s;
  // scanf("%s", s);
  cin >> x >> y;
  // scanf("%lld%lld", &x, &y);
  int n = s.length();
  // int n = strlen(s);

  // REP(i,8000) s += 'a';

  dp[0][0][Z] = dp[1][0][Z] = 1;

  int pos = 0LL;
  while (pos < n && s[pos] == 'F') {
    pos++;
    x--;
  }

  int t = 0LL;
  while (pos < n) {
    pos++;
    t ^= 1;
    int L = 0;
    while (pos < n && s[pos] == 'F') {
      pos++;
      L++;
    }
    REP(i, N) dp[t][1][i] = 0; // 1の方は計算結果
    REP(i, N) {
      if (!dp[t][0][i])
        continue;
      dp[t][1][i - L] = dp[t][1][i + L] = 1;
    }
    REP(i, N) dp[t][0][i] = dp[t][1][i];
  }

  if (dp[0][0][Z + x] && dp[1][0][Z + y]) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int F[4005];
int A[4005][16005], B[4005][16005];

int main() {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;
  int p = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F')
      F[p]++;
    else
      p++;
  }
  A[0][s.size() + F[0]] = 1;
  B[0][s.size() + F[1]] = 1;
  B[0][s.size() - F[1]] = 1;
  for (int i = 1; i <= p / 2; i++) {
    for (int j = 0; j <= s.size() * 2; j++) {
      if (A[i - 1][j] == 1) {
        A[i][j + F[2 * i]] = 1;
        A[i][j - F[2 * i]] = 1;
      }
      if (B[i - 1][j] == 1) {
        B[i][j + F[2 * i + 1]] = 1;
        B[i][j - F[2 * i + 1]] = 1;
      }
    }
  }
  if (A[p / 2][x + s.size()] == 1 && B[p / 2][y + s.size()] == 1)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int F[8005];
int A[4005][16005], B[4005][16005];

int main() {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;
  int p = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F')
      F[p]++;
    else
      p++;
  }
  A[0][s.size() + F[0]] = 1;
  B[0][s.size() + F[1]] = 1;
  B[0][s.size() - F[1]] = 1;
  for (int i = 1; i <= p / 2; i++) {
    for (int j = 0; j <= s.size() * 2; j++) {
      if (A[i - 1][j] == 1) {
        A[i][j + F[2 * i]] = 1;
        A[i][j - F[2 * i]] = 1;
      }
      if (B[i - 1][j] == 1) {
        B[i][j + F[2 * i + 1]] = 1;
        B[i][j - F[2 * i + 1]] = 1;
      }
    }
  }
  if (A[p / 2][x + s.size()] == 1 && B[p / 2][y + s.size()] == 1)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  cin >> s;
  int x, y;
  scanf("%d %d", &x, &y);
  int cnt = 0;
  vector<int> lr, ud;
  int p = 0;
  while (s[p] == 'F') {
    p++;
    x--;
  }
  bool is_lr = true;
  for (; p < s.size(); p++) {
    if (s[p] == 'F') {
      cnt++;
    } else {
      if (cnt) {
        if (is_lr) {
          lr.push_back(cnt);
        } else {
          ud.push_back(cnt);
        }
        cnt = 0;
      }
      is_lr = !is_lr;
    }
  }
  if (cnt) {
    if (is_lr) {
      lr.push_back(cnt);
    } else {
      ud.push_back(cnt);
    }
  }
  auto calc = [&](int target, const vector<int> &a) {
    const int MID = 8020;
    bitset<MID * 2> cur, prev;
    prev[MID] = 1;
    for (int i = 0; i < a.size(); i++) {
      cur |= prev << a[i];
      cur |= prev >> a[i];
      prev = cur;
      cur.reset();
    }
    return prev[target + MID];
  };
  bool ans = calc(x, lr) && calc(y, ud);
  puts(ans ? "Yes" : "No");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  cin >> s;
  int x, y;
  scanf("%d %d", &x, &y);
  int cnt = 0;
  vector<int> lr, ud;
  int p = 0;
  while (s[p] == 'F') {
    p++;
    x--;
  }
  bool is_lr = true;
  for (; p < s.size(); p++) {
    if (s[p] == 'F') {
      cnt++;
    } else {
      if (cnt) {
        if (is_lr) {
          lr.push_back(cnt);
        } else {
          ud.push_back(cnt);
        }
        cnt = 0;
      }
      is_lr = !is_lr;
    }
  }
  if (cnt) {
    if (is_lr) {
      lr.push_back(cnt);
    } else {
      ud.push_back(cnt);
    }
  }
  auto calc = [&](int target, const vector<int> &a) {
    const int MID = 16060;
    bitset<MID * 2> cur, prev;
    prev[MID] = 1;
    for (int i = 0; i < a.size(); i++) {
      cur |= prev << a[i];
      cur |= prev >> a[i];
      prev = cur;
      cur.reset();
    }
    return prev[target + MID];
  };
  bool ans = calc(x, lr) && calc(y, ud);
  puts(ans ? "Yes" : "No");
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = 999999999;
int x[20000][8001];
int y[20000][8001];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<int> mv;
  string s;
  int ct = 0;
  cin >> s;
  REP(i, s.size()) {
    if (s[i] == 'T') {
      mv.pb(ct);
      ct = 0;
    } else
      ct++;
  }
  mv.pb(ct);

  int _x, _y;
  cin >> _x >> _y;
  memset(x, 0, sizeof(x));
  memset(y, 0, sizeof(y));

  x[10000 + mv[0]][0] = 1;
  y[10000][0] = 1;
  int t = 1;
  FOR(i, 1, mv.size()) {
    if (i % 2 == 0) { // x軸
      REP(j, 20000) {
        x[j][t] =
            (x[min(19999, j + mv[i])][t - 1] | x[max(0, j - mv[i])][t - 1]);
      }
      t++;
    } else { // y軸
      REP(j, 20000) {
        y[j][t] =
            (y[min(19999, j + mv[i])][t - 1] | y[max(0, j - mv[i])][t - 1]);
      }
    }
  }
  if (mv.size() % 2 == 1) {
    if ((x[10000 + _x][t - 1] & y[10000 + _y][t - 1]) == 1)
      puts("Yes");
    else
      puts("No");
  } else {
    if ((x[10000 + _x][t - 1] & y[10000 + _y][t]) == 1)
      puts("Yes");
    else
      puts("No");
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = 999999999;
char x[20000][8001];
char y[20000][8001];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<int> mv;
  string s;
  int ct = 0;
  cin >> s;
  REP(i, s.size()) {
    if (s[i] == 'T') {
      mv.pb(ct);
      ct = 0;
    } else
      ct++;
  }
  mv.pb(ct);

  int _x, _y;
  cin >> _x >> _y;
  memset(x, 0, sizeof(x));
  memset(y, 0, sizeof(y));

  x[10000 + mv[0]][0] = 1;
  y[10000][0] = 1;
  int t = 1;
  FOR(i, 1, mv.size()) {
    if (i % 2 == 0) { // x軸
      REP(j, 20000) {
        x[j][t] =
            (x[min(19999, j + mv[i])][t - 1] | x[max(0, j - mv[i])][t - 1]);
      }
      t++;
    } else { // y軸
      REP(j, 20000) {
        y[j][t] =
            (y[min(19999, j + mv[i])][t - 1] | y[max(0, j - mv[i])][t - 1]);
      }
    }
  }
  if (mv.size() % 2 == 1) {
    if ((x[10000 + _x][t - 1] & y[10000 + _y][t - 1]) == 1)
      puts("Yes");
    else
      puts("No");
  } else {
    if ((x[10000 + _x][t - 1] & y[10000 + _y][t]) == 1)
      puts("Yes");
    else
      puts("No");
  }

  return 0;
}
Problem p03490 (Python)

Buggy submission (status: Runtime Error):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
X, Y = list(map(int, sys.stdin.buffer.readline().split()))

# X 軸と Y 軸は独立に考えられる。
d = 0
cnt = 0
xy = [[], []]
for c in S:
    if c == "F":
        cnt += 1
    else:
        xy[d].append(cnt)
        cnt = 0
        d = d + 1 & 1
xy[d].append(cnt)
X -= xy[0].pop(0)


def can_make(moves, obj):
    obj = 1 << (8000 + obj)
    dp = 1 << 8000
    for mv in moves:
        if mv == 0:
            continue
        dp = (dp << mv) | (dp >> mv)
    return (dp & obj) > 0


ok = can_make(xy[0], X) and can_make(xy[1], Y)
if ok:
    print("Yes")
else:
    print("No")


Fixed submission:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
X, Y = list(map(int, sys.stdin.buffer.readline().split()))

# X 軸と Y 軸は独立に考えられる。
d = 0
cnt = 0
xy = [[], []]
for c in S:
    if c == "F":
        cnt += 1
    else:
        xy[d].append(cnt)
        cnt = 0
        d = d + 1 & 1
xy[d].append(cnt)
X -= xy[0].pop(0)


def can_make(moves, obj):
    if obj < -8000:
        return False
    obj = 1 << (8000 + obj)
    dp = 1 << 8000
    for mv in moves:
        if mv == 0:
            continue
        dp = (dp << mv) | (dp >> mv)
    return (dp & obj) > 0


ok = can_make(xy[0], X) and can_make(xy[1], Y)
if ok:
    print("Yes")
else:
    print("No")
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define EM 100000000
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
LL LINF = 1e18;
int INF = 1e9;
using vint = vector<int>;
using vLL = vector<LL>;
using vvint = vector<vector<int>>;
using vvLL = vector<vector<LL>>;

bool solve(vint a, int b) {
  bitset<16001> bit;
  bit[8000] = 1;
  for (auto d : a)
    bit = (bit << d) | (bit >> d);
  return bit[b];
}
int main() {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;
  vint X, Y;
  bool f = true;
  int start = 0;
  x += 8000, y += 8000;
  while (start < s.size() && s[start] == 'F') {
    x--;
    start++;
  }
  for (int i = start; i < s.size(); i++) {
    int d = 0;
    while (s[i] == 'F') {
      d++;
      i++;
      if (i == s.size())
        break;
    }
    if (d > 0) {
      if (f)
        X.push_back(d);
      else
        Y.push_back(d);
    }
    f = !f;
  }
  bool ans = solve(X, x) && solve(Y, y);
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define EM 100000000
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
LL LINF = 1e18;
int INF = 1e9;
using vint = vector<int>;
using vLL = vector<LL>;
using vvint = vector<vector<int>>;
using vvLL = vector<vector<LL>>;

bool solve(vint a, int b) {
  bitset<16001> bit;
  bit[8000] = 1;
  for (auto d : a)
    bit = (bit << d) | (bit >> d);
  if (b < 0)
    return 0;
  else
    return bit[b];
}
int main() {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;
  vint X, Y;
  bool f = true;
  int start = 0;
  x += 8000, y += 8000;
  while (start < s.size() && s[start] == 'F') {
    x--;
    start++;
  }
  for (int i = start; i < s.size(); i++) {
    int d = 0;
    while (s[i] == 'F') {
      d++;
      i++;
      if (i == s.size())
        break;
    }
    if (d > 0) {
      if (f)
        X.push_back(d);
      else
        Y.push_back(d);
    }
    f = !f;
  }
  bool ans = solve(X, x) && solve(Y, y);
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  int i = 0;
  while (i <= s.size()) {
    if (i == s.size() || s[i] == 'T') {
      x -= i;
      break;
    }
    i++;
  }
  vector<int> dist[2];
  int idx = 0;
  int cur_dist = 0;
  while (i < s.size()) {
    if (s[i] == 'T') {
      if (cur_dist != 0) {
        dist[idx].push_back(cur_dist);
      }
      idx = 1 - idx;
      cur_dist = 0;
    } else {
      cur_dist++;
    }
    i++;
  }
  if (cur_dist != 0) {
    dist[idx].push_back(cur_dist);
  }
  bool feasible = true;
  for (int j = 0; j < 2; j++) {
    vector<bool> possible(16001, false);
    possible[0 + 8000] = true;
    for (int k = 0; k < dist[j].size(); k++) {
      vector<bool> possible2(16001, false);
      int offset = dist[j][k];
      for (int l = -8000; l <= 8000; l++) {
        if (possible[l + 8000]) {
          if (l - offset >= -8000) {
            possible2[l - offset + 8000] = true;
          }
          if (l + offset <= 8000) {
            possible2[l + offset + 8000] = true;
          }
        }
      }
      possible = possible2;
    }
    if (j == 0 && !possible[x + 8000])
      feasible = false;
    if (j == 1 && !possible[y + 8000])
      feasible = false;
  }
  if (feasible) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  int i = 0;
  while (i <= s.size()) {
    if (i == s.size() || s[i] == 'T') {
      x -= i;
      break;
    }
    i++;
  }
  vector<int> dist[2];
  int idx = 0;
  int cur_dist = 0;
  while (i < s.size()) {
    if (s[i] == 'T') {
      if (cur_dist != 0) {
        dist[idx].push_back(cur_dist);
      }
      idx = 1 - idx;
      cur_dist = 0;
    } else {
      cur_dist++;
    }
    i++;
  }
  if (cur_dist != 0) {
    dist[idx].push_back(cur_dist);
  }
  bool feasible = true;
  for (int j = 0; j < 2; j++) {
    vector<bool> possible(16001, false);
    possible[0 + 8000] = true;
    for (int k = 0; k < dist[j].size(); k++) {
      vector<bool> possible2(16001, false);
      int offset = dist[j][k];
      for (int l = -8000; l <= 8000; l++) {
        if (possible[l + 8000]) {
          if (l - offset >= -8000) {
            possible2[l - offset + 8000] = true;
          }
          if (l + offset <= 8000) {
            possible2[l + offset + 8000] = true;
          }
        }
      }
      possible = possible2;
    }
    if (j == 0 && (x < -8000 || x > 8000 || !possible[x + 8000]))
      feasible = false;
    if (j == 1 && !possible[y + 8000])
      feasible = false;
  }
  if (feasible) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int maxN = 8005;

int n, x, y;
vector<int> dx, dy;
char s[maxN];

bool Check(int x, const vector<int> &d) {
  vector<bool> dp[2];
  for (int i = 0; i < 2; ++i)
    dp[i].assign(2 * n + 1, false);
  dp[0][n] = true;

  for (int i = 0; i < d.size(); ++i) {
    for (int j = 0; j < 2 * n; ++j) {
      if (dp[0][j])
        dp[1][j - d[i]] = dp[1][j + d[i]] = true;
    }
    for (int j = 0; j < 2 * n; ++j) {
      dp[0][j] = dp[1][j];
      dp[1][j] = false;
    }
  }

  return dp[0][n + x];
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  scanf("%d%d", &x, &y);

  dx.clear();
  dy.clear();
  int cur = 0, group = 0;
  for (int i = 0; i <= n; ++i) {
    if (i < n && s[i] == 'F')
      ++cur;
    else {
      ++group;
      if (group == 1)
        x -= cur;
      else if (group % 2 == 0)
        dy.push_back(cur);
      else
        dx.push_back(cur);
      cur = 0;
    }
  }

  bool ans = Check(x, dx) && Check(y, dy);
  puts((ans) ? "Yes" : "No");
}


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

using namespace std;

const int maxN = 8005;

int n, x, y;
vector<int> dx, dy;
char s[maxN];

bool Check(int x, const vector<int> &d) {
  if (x < -n || x > n)
    return false;
  vector<bool> dp[2];
  for (int i = 0; i < 2; ++i)
    dp[i].assign(2 * n + 1, false);
  dp[0][n] = true;

  for (int i = 0; i < d.size(); ++i) {
    for (int j = 0; j < 2 * n; ++j) {
      if (dp[0][j])
        dp[1][j - d[i]] = dp[1][j + d[i]] = true;
    }
    for (int j = 0; j < 2 * n; ++j) {
      dp[0][j] = dp[1][j];
      dp[1][j] = false;
    }
  }

  return dp[0][n + x];
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  scanf("%d%d", &x, &y);

  dx.clear();
  dy.clear();
  int cur = 0, group = 0;
  for (int i = 0; i <= n; ++i) {
    if (i < n && s[i] == 'F')
      ++cur;
    else {
      ++group;
      if (group == 1)
        x -= cur;
      else if (group % 2 == 0)
        dy.push_back(cur);
      else
        dx.push_back(cur);
      cur = 0;
    }
  }

  bool ans = Check(x, dx) && Check(y, dy);
  puts((ans) ? "Yes" : "No");
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

using namespace std;

int len;
char s[5];
int x, y;

vector<int> a, b;

bool ok(const vector<int> &a, int x) {
  if (x < -8000 || x > 8000)
    return 0;
  typedef bitset<16001> bs;
  bs cur;
  cur[8000] = true;
  for (int b : a) {
    if (b == 0)
      continue;
    bs tmp = cur << b | cur >> b;
    cur = tmp;
  }
  return cur[8000 + x];
}

int main(void) {
  scanf("%8001s", s);
  len = strlen(s);
  scanf("%d%d", &x, &y);
  int cur = 0;
  int dir = 0;
  REP(i, 0, len + 1) {
    if (i < len && s[i] == 'F')
      cur++;
    else {
      if (dir == 0)
        x -= cur;
      else if (dir == 1)
        b.push_back(cur);
      else
        a.push_back(cur);
      dir = dir == 1 ? 2 : 1;
      cur = 0;
    }
  }
  puts(ok(a, x) && ok(b, y) ? "Yes" : "No");
}


Fixed submission:
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

using namespace std;

int len;
char s[8001];
int x, y;

vector<int> a, b;

bool ok(const vector<int> &a, int x) {
  if (x < -8000 || x > 8000)
    return 0;
  typedef bitset<16001> bs;
  bs cur;
  cur[8000] = true;
  for (int b : a) {
    if (b == 0)
      continue;
    bs tmp = cur << b | cur >> b;
    cur = tmp;
  }
  return cur[8000 + x];
}

int main(void) {
  scanf("%8001s", s);
  len = strlen(s);
  scanf("%d%d", &x, &y);
  int cur = 0;
  int dir = 0;
  REP(i, 0, len + 1) {
    if (i < len && s[i] == 'F')
      cur++;
    else {
      if (dir == 0)
        x -= cur;
      else if (dir == 1)
        b.push_back(cur);
      else
        a.push_back(cur);
      dir = dir == 1 ? 2 : 1;
      cur = 0;
    }
  }
  puts(ok(a, x) && ok(b, y) ? "Yes" : "No");
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

string s;
vector<int> ax[2];
int x[2];
bool dp[2][16001];

int main() {
  cin >> s >> x[0] >> x[1];
  {
    int now = 0;
    int con = 0;
    int i = 0;
    for (; i < s.size(); i++) {
      if (s[i] != 'F') {
        break;
      }
    }
    if (i)
      x[0] -= i;
    for (; i < s.size(); i++) {
      if (s[i] == 'T') {
        if (con > 0)
          ax[now].push_back(con);
        now ^= 1;
        con = 0;
        continue;
      }
      con++;
    }
    if (con > 0)
      ax[now].push_back(con);
  }
  rep(i, 2) {
    memset(dp, 0, sizeof(dp));
    dp[0][8000] = true;
    int ho = 0, ge = 1;
    rep(j, ax[i].size()) {
      rep(k, 16001) {
        if (dp[ho][k]) {
          if (k + ax[i][j] <= 16000)
            dp[ge][k + ax[i][j]] = true;
          if (k - ax[i][j] >= 0)
            dp[ge][k - ax[i][j]] = true;
        }
      }
      swap(ho, ge);
      memset(dp[ge], 0, sizeof(dp[ge]));
    }
    if (!dp[ho][x[i] + 8000]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

string s;
vector<int> ax[2];
int x[2];
bool dp[2][16001];

int main() {
  cin >> s >> x[0] >> x[1];
  {
    int now = 0;
    int con = 0;
    int i = 0;
    for (; i < s.size(); i++) {
      if (s[i] != 'F') {
        break;
      }
    }
    if (i)
      x[0] -= i;
    for (; i < s.size(); i++) {
      if (s[i] == 'T') {
        if (con > 0)
          ax[now].push_back(con);
        now ^= 1;
        con = 0;
        continue;
      }
      con++;
    }
    if (con > 0)
      ax[now].push_back(con);
  }
  if (x[0] < -8000 || x[0] > 8000) {
    cout << "No" << endl;
    return 0;
  }
  rep(i, 2) {
    memset(dp, 0, sizeof(dp));
    dp[0][8000] = true;
    int ho = 0, ge = 1;
    rep(j, ax[i].size()) {
      rep(k, 16001) {
        if (dp[ho][k]) {
          if (k + ax[i][j] <= 16000)
            dp[ge][k + ax[i][j]] = true;
          if (k - ax[i][j] >= 0)
            dp[ge][k - ax[i][j]] = true;
        }
      }
      swap(ho, ge);
      memset(dp[ge], 0, sizeof(dp[ge]));
    }
    if (!dp[ho][x[i] + 8000]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8005, ZERO = 8000;

int p[2];
char str[MAXN];
bitset<MAXN << 1> bs[2];

int main() {
  int i, j, d;
  scanf("%s%d%d", str + 1, &p[0], &p[1]);
  for (i = 1; str[i] == 'F'; ++i)
    --p[0];
  bs[0][ZERO] = bs[1][ZERO] = true;
  for (d = 0; str[i]; i = j) {
    for (j = i; str[j] == 'F'; ++j)
      ;
    bs[d] = (bs[d] << (j - i)) | (bs[d] >> (j - i));
    if (str[j] == 'T')
      ++j, d ^= 1;
  }
  printf("%s\n", bs[0][p[0] + ZERO] && bs[1][p[1] + ZERO] ? "Yes" : "No");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8005, ZERO = 8000;

int p[2];
char str[MAXN];
bitset<MAXN << 1> bs[2];

int main() {
  int i, j, d;
  scanf("%s%d%d", str + 1, &p[0], &p[1]);
  for (i = 1; str[i] == 'F'; ++i)
    --p[0];
  if (p[0] < -ZERO) {
    printf("No\n");
    return 0;
  }
  bs[0][ZERO] = bs[1][ZERO] = true;
  for (d = 0; str[i]; i = j) {
    for (j = i; str[j] == 'F'; ++j)
      ;
    bs[d] = (bs[d] << (j - i)) | (bs[d] >> (j - i));
    if (str[j] == 'T')
      ++j, d ^= 1;
  }
  printf("%s\n", bs[0][p[0] + ZERO] && bs[1][p[1] + ZERO] ? "Yes" : "No");
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define llint long long

using namespace std;
typedef pair<llint, llint> P;

string s;
llint x, y;
vector<llint> vecx, vecy;
bool dp[8005][16005];
llint Zero = 8002;

bool check(vector<llint> vec, llint x) {
  llint n = vec.size();
  for (int i = 0; i <= n; i++) {
    for (int j = -8000; j <= 8000; j++) {
      dp[i][Zero + j] = false;
    }
  }
  dp[0][Zero] = true;

  for (int i = 0; i < n; i++) {
    for (int j = -8000; j <= 8000; j++) {
      if (!dp[i][Zero + j])
        continue;
      if (j + vec[i] <= 8000)
        dp[i + 1][Zero + j + vec[i]] = true;
      if (j - vec[i] >= -8000)
        dp[i + 1][Zero + j - vec[i]] = true;
    }
  }
  return dp[n][x + Zero];
}

int main(void) {
  cin >> s >> x >> y;
  s += "T";

  bool flag = true;
  llint cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F')
      cnt++;
    else {
      if (flag)
        vecx.push_back(cnt);
      else
        vecy.push_back(cnt);
      cnt = 0, flag = !flag;
    }
  }
  x -= vecx.front();
  vecx.erase(vecx.begin());

  if (check(vecx, x) && check(vecy, y))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define llint long long

using namespace std;
typedef pair<llint, llint> P;

string s;
llint x, y;
vector<llint> vecx, vecy;
bool dp[8005][16005];
llint Zero = 8002;

bool check(vector<llint> vec, llint x) {
  if (x < -8000 || x > 8000)
    return false;
  llint n = vec.size();
  for (int i = 0; i <= n; i++) {
    for (int j = -8000; j <= 8000; j++) {
      dp[i][Zero + j] = false;
    }
  }
  dp[0][Zero] = true;

  for (int i = 0; i < n; i++) {
    for (int j = -8000; j <= 8000; j++) {
      if (!dp[i][Zero + j])
        continue;
      if (j + vec[i] <= 8000)
        dp[i + 1][Zero + j + vec[i]] = true;
      if (j - vec[i] >= -8000)
        dp[i + 1][Zero + j - vec[i]] = true;
    }
  }
  return dp[n][x + Zero];
}

int main(void) {
  cin >> s >> x >> y;
  s += "T";

  bool flag = true;
  llint cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F')
      cnt++;
    else {
      if (flag)
        vecx.push_back(cnt);
      else
        vecy.push_back(cnt);
      cnt = 0, flag = !flag;
    }
  }
  x -= vecx.front();
  vecx.erase(vecx.begin());

  if (check(vecx, x) && check(vecy, y))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define sz(c) ((int)(c).size())

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
const int MAX = 30010;

bool step[16001][2];

void ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

bool ok(bool fixed, vi v, int rc) {
  int l = sz(v);
  bitset<20001> step[2010];
  step[0][10000] = 1;
  rep(i, l) rep(x, 20001) if (step[i][x]) {
    step[i + 1][x + v[i]] = 1;
    if (!fixed || i != 0)
      step[i + 1][x - v[i]] = 1;
  }
  if (step[l][rc + 10000])
    return true;
  else
    return false;
}

int main() {
  string S;
  cin >> S;
  int x, y;
  cin >> x >> y;
  vi fx, fy;
  int nowF = 0;
  ;
  int dir = 1;
  bool xfixed = false;
  if (S[0] == 'F')
    xfixed = true;

  rep(i, sz(S)) {
    if (S[i] == 'F')
      nowF++;
    else {
      if (nowF != 0) {
        if (dir == 1)
          fx.push_back(nowF);
        else
          fy.push_back(nowF);
        nowF = 0;
      }
      dir *= -1;
    }
  }
  if (nowF > 0) {
    if (dir == 1)
      fx.push_back(nowF);
    else
      fy.push_back(nowF);
  }

  bool okx = ok(xfixed, fx, x);
  bool oky = ok(false, fy, y);
  bool all = okx & oky;
  ans(all);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define sz(c) ((int)(c).size())

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
const int MAX = 30010;

bool step[16001][2];

void ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

bool ok(bool fixed, vi v, int rc) {
  int l = sz(v);
  bitset<20001> step[4010];
  step[0][10000] = 1;
  rep(i, l) rep(x, 20001) if (step[i][x]) {
    step[i + 1][x + v[i]] = 1;
    if (!fixed || i != 0)
      step[i + 1][x - v[i]] = 1;
  }
  if (step[l][rc + 10000])
    return true;
  else
    return false;
}

int main() {
  string S;
  cin >> S;
  int x, y;
  cin >> x >> y;
  vi fx, fy;
  int nowF = 0;
  ;
  int dir = 1;
  bool xfixed = false;
  if (S[0] == 'F')
    xfixed = true;

  rep(i, sz(S)) {
    if (S[i] == 'F')
      nowF++;
    else {
      if (nowF != 0) {
        if (dir == 1)
          fx.push_back(nowF);
        else
          fy.push_back(nowF);
        nowF = 0;
      }
      dir *= -1;
    }
  }
  if (nowF > 0) {
    if (dir == 1)
      fx.push_back(nowF);
    else
      fy.push_back(nowF);
  }

  bool okx = ok(xfixed, fx, x);
  bool oky = ok(false, fy, y);
  bool all = okx & oky;
  ans(all);
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize "O3"

#define F first
#define S second

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 8100;

string str;
int n, tx, ty;
vector<int> v[2];
bitset<2 * N + 1> vis[2];

bool check(const vector<int> &vi, int t) {
  vis[0].reset();
  bool cur = false;
  vis[0][N] = true;
  for (int x : vi) {
    cur = !cur;
    vis[cur] = (vis[!cur] << x) | (vis[!cur] >> x);
  }
  return vis[cur][t + N];
}

bool solve() {
  cin >> str >> tx >> ty;
  n = str.size();
  int i = 0;
  while (i < n && str[i] == 'F') {
    ++i;
  }
  tx -= i;
  bool d = false;
  while (i < n) {
    int tcnt = 0;
    while (i < n && str[i] == 'T') {
      ++tcnt;
      ++i;
    }
    if (i == n)
      break;
    if (tcnt & 1)
      d = !d;
    int fcnt = 0;
    while (i < n && str[i] == 'F') {
      ++fcnt;
      ++i;
    }
    v[d].push_back(fcnt);
  }
  return (check(v[0], tx) && check(v[1], ty));
}

int main() { puts(solve() ? "Yes" : "No"); }


Fixed submission:
#pragma GCC optimize "O3"

#define F first
#define S second

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 8100;

string str;
int n, tx, ty;
vector<int> v[2];
bitset<2 * N + 1> vis[2];

bool check(const vector<int> &vi, int t) {
  vis[0].reset();
  bool cur = false;
  vis[0][N] = true;
  for (int x : vi) {
    cur = !cur;
    vis[cur] = (vis[!cur] << x) | (vis[!cur] >> x);
  }
  return vis[cur][t + N];
}

bool solve() {
  cin >> str >> tx >> ty;
  n = str.size();
  int i = 0;
  while (i < n && str[i] == 'F') {
    ++i;
  }
  tx -= i;
  bool d = false;
  while (i < n) {
    int tcnt = 0;
    while (i < n && str[i] == 'T') {
      ++tcnt;
      ++i;
    }
    if (i == n)
      break;
    if (tcnt & 1)
      d = !d;
    int fcnt = 0;
    while (i < n && str[i] == 'F') {
      ++fcnt;
      ++i;
    }
    v[d].push_back(fcnt);
  }
  return check(v[0], abs(tx)) && check(v[1], abs(ty));
}

int main() { puts(solve() ? "Yes" : "No"); }
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

string s;
int x, y;
bool a[2][16010];

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> s >> x >> y;
  s += 'T';
  a[0][8000] = true;
  a[1][8000] = true;
  int t = 0, f = 0;
  while (t < s.size() && s[t] == 'F')
    ++t;
  x -= t;
  for (int i = t; i < s.size(); ++i) {
    if (s[i] != 'T')
      continue;
    if (i - t > 1) {
      //			cout << i - t - 1 << "\n";
      int u = i - t - 1;
      bool b[16010] = {};
      for (int j = 0; j < 16010; ++j) {
        if (!a[f][j])
          continue;
        if (0 <= j - u && j - u < 16010)
          b[j - u] = true;
        if (0 <= j + u && j + u < 16010)
          b[j + u] = true;
      }
      for (int j = 0; j < 16010; ++j)
        a[f][j] = b[j];
    }
    t = i;
    f = 1 - f;
  }
  if (a[0][8000 + x] && a[1][8000 + y])
    cout << "Yes\n";
  else
    cout << "No\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

string s;
int x, y;
bool a[2][16010];

int main() {
  //	cin.tie(0);
  //	ios::sync_with_stdio(false);
  cin >> s >> x >> y;
  s += 'T';
  a[0][8000] = true;
  a[1][8000] = true;
  int t = 0, f = 0;
  while (t < s.size() && s[t] == 'F')
    ++t;
  x -= t;
  for (int i = t; i < s.size(); ++i) {
    if (s[i] != 'T')
      continue;
    if (i - t > 1) {
      //			cout << i - t - 1 << "\n";
      int u = i - t - 1;
      bool b[16010] = {};
      for (int j = 0; j < 16010; ++j) {
        if (!a[f][j])
          continue;
        if (0 <= j - u && j - u < 16010)
          b[j - u] = true;
        if (0 <= j + u && j + u < 16010)
          b[j + u] = true;
      }
      for (int j = 0; j < 16010; ++j)
        a[f][j] = b[j];
    }
    t = i;
    f = 1 - f;
  }
  if (0 <= 8000 + x && a[0][8000 + x] && a[1][8000 + y])
    cout << "Yes\n";
  else
    cout << "No\n";
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8005;

vector<int> arr;
int dp[MAXN][2 * MAXN];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  string str;
  cin >> str;
  arr.push_back(0);
  for (int i = 0; i < (int)str.length(); i++) {
    if (str[i] == 'T')
      arr.push_back(0);
    else
      arr.back()++;
  }
  int N = arr.size();

  int x, y;
  cin >> x >> y;
  x -= arr[0];
  x += MAXN;
  y += MAXN;
  dp[0][MAXN] = dp[1][MAXN + arr[1]] = dp[1][MAXN - arr[1]] = 1;
  for (int pos = 2; pos < N; pos++) {
    for (int rem = 0; rem < 2 * MAXN; rem++) {
      int &ret = dp[pos][rem];
      ret = 0;
      if (rem + arr[pos] < 2 * MAXN)
        ret |= dp[pos - 2][rem + arr[pos]];
      if (rem - arr[pos] >= 0)
        ret |= dp[pos - 2][rem - arr[pos]];
    }
  }
  if (N & 1) {
    if (dp[N - 1][x] && dp[N - 2][y]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  } else {
    if (dp[N - 1][y] && dp[N - 2][x]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 8005;

vector<int> arr;
int dp[MAXN][2 * MAXN];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  string str;
  cin >> str;
  arr.push_back(0);
  for (int i = 0; i < (int)str.length(); i++) {
    if (str[i] == 'T')
      arr.push_back(0);
    else
      arr.back()++;
  }
  int N = arr.size();

  int x, y;
  cin >> x >> y;

  if (arr.size() == 1) {
    if (x == arr[0]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
    return 0;
  }

  x -= arr[0];
  x += MAXN;
  y += MAXN;
  dp[0][MAXN] = dp[1][MAXN + arr[1]] = dp[1][MAXN - arr[1]] = 1;
  for (int pos = 2; pos < N; pos++) {
    for (int rem = 0; rem < 2 * MAXN; rem++) {
      int &ret = dp[pos][rem];
      ret = 0;
      if (rem + arr[pos] < 2 * MAXN)
        ret |= dp[pos - 2][rem + arr[pos]];
      if (rem - arr[pos] >= 0)
        ret |= dp[pos - 2][rem - arr[pos]];
    }
  }
  if (N & 1) {
    if (dp[N - 1][x] && dp[N - 2][y]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  } else {
    if (dp[N - 1][y] && dp[N - 2][x]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  }
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int m[32005][4005];
vector<int> p;

bool solve(int val, int ind) {
  if (ind == p.size()) {
    return (val == 0);
  }
  int mval = val + 16002;
  assert(!(mval < 0 || mval >= 32005 || ind >= 2005));
  if (m[mval][ind] != -1) {
    return m[mval][ind];
  }
  return m[mval][ind] =
             (solve(val - p[ind], ind + 1) || solve(val + p[ind], ind + 1));
}

int main() {
  string s;
  int x, y, n;
  cin >> s >> x >> y;
  s.push_back('T');
  n = s.size();
  vector<int> a[2];
  bool fi = 1;
  int dir = 0, steps = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'T') {
      if (fi) {
        x -= steps;
        fi = 0;
      } else {
        a[dir].push_back(steps);
      }
      dir = 1 - dir;
      steps = 0;
    } else {
      steps++;
    }
  }
  memset(m, -1, sizeof(m));
  p = a[0];
  if (!solve(x, 0)) {
    cout << "No" << endl;
    return 0;
  }
  memset(m, -1, sizeof(m));
  p = a[1];
  if (!solve(y, 0)) {
    cout << "No" << endl;
    return 0;
  } else {
    cout << "Yes" << endl;
  }
  return 0;
}


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

using namespace std;

int m[32005][4005];
vector<int> p;

bool solve(int val, int ind) {
  if (ind == p.size()) {
    return (val == 0);
  }
  int mval = val + 16002;
  // assert(!(mval<0 || mval >= 32005 || ind >= 2005));
  if (m[mval][ind] != -1) {
    return m[mval][ind];
  }
  return m[mval][ind] =
             (solve(val - p[ind], ind + 1) || solve(val + p[ind], ind + 1));
}

int main() {
  string s;
  int x, y, n;
  cin >> s >> x >> y;
  s.push_back('T');
  n = s.size();
  vector<int> a[2];
  bool fi = 1;
  int dir = 0, steps = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'T') {
      if (fi) {
        x -= steps;
        fi = 0;
      } else {
        a[dir].push_back(steps);
      }
      dir = 1 - dir;
      steps = 0;
    } else {
      steps++;
    }
  }
  memset(m, -1, sizeof(m));
  p = a[0];
  if (!solve(x, 0)) {
    cout << "No" << endl;
    return 0;
  }
  memset(m, -1, sizeof(m));
  p = a[1];
  if (!solve(y, 0)) {
    cout << "No" << endl;
    return 0;
  } else {
    cout << "Yes" << endl;
  }
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <iostream>
#define M 8000
using namespace std;
string s;
int x, y, z, c;
bitset<M * 2> bs[2];
int main() {
  cin >> s >> x >> y;
  s += "T";
  c = s.find("T");
  s = string(s.begin() + c, s.end());
  x -= c;
  bs[0][M] = bs[1][M] = 1;
  for (int i = c = 0; s[i]; i++) {
    if (s[i] == 'F')
      c++;
    else {
      bs[z] = (bs[z] << c) | (bs[z] >> c);
      z = !z;
      c = 0;
    }
  }
  cout << (bs[0][x + M] & bs[1][y + M] ? "Yes" : "No");
  return 0;
}

Fixed submission:
#include <bitset>
#include <iostream>
#define M 16000
using namespace std;
string s;
int x, y, z, c;
bitset<M * 2> bs[2];
int main() {
  cin >> s >> x >> y;
  s += "T";
  c = s.find("T");
  s = string(s.begin() + c, s.end());
  x -= c;
  bs[0][M] = bs[1][M] = 1;
  for (int i = c = 0; s[i]; i++) {
    if (s[i] == 'F')
      c++;
    else {
      bs[z] = (bs[z] << c) | (bs[z] >> c);
      z = !z;
      c = 0;
    }
  }
  cout << (bs[0][x + M] & bs[1][y + M] ? "Yes" : "No");
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <bitset>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  vector<int> ary;
  int cnt = 0;
  s.push_back('T');
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F') {
      cnt++;
    } else {
      ary.push_back(cnt);
      cnt = 0;
    }
  }
  int xsum = 0, ysum = 0;
  bitset<8001> xbs(0), ybs(0);
  xbs[0] = 1, ybs[0] = 1;
  for (int i = 2; i < ary.size(); i += 2) {
    xbs = xbs | xbs << ary[i];
    xsum += ary[i];
  }
  for (int i = 1; i < ary.size(); i += 2) {
    ybs = ybs | ybs << ary[i];
    ysum += ary[i];
  }
  int xdif = xsum + ary[0] - x;
  int ydif = ysum - y;
  if (xdif % 2 | ydif % 2 | xdif / 2 > 8000 | ydif / 2 > 8000 | xdif < 0 |
      ydif < 0 | xbs[xdif / 2] == 0 | ybs[ydif / 2] == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
  return 0;
}

Fixed submission:
#include <bitset>
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;
  vector<int> ary;
  int cnt = 0;
  s.push_back('T');
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'F') {
      cnt++;
    } else {
      ary.push_back(cnt);
      cnt = 0;
    }
  }
  int xsum = 0, ysum = 0;
  bitset<8001> xbs(0), ybs(0);
  xbs[0] = 1, ybs[0] = 1;
  for (int i = 2; i < ary.size(); i += 2) {
    xbs = xbs | xbs << ary[i];
    xsum += ary[i];
  }
  for (int i = 1; i < ary.size(); i += 2) {
    ybs = ybs | ybs << ary[i];
    ysum += ary[i];
  }
  int xdif = xsum + ary[0] - x;
  int ydif = ysum - y;
  if (xdif % 2 | ydif % 2 | xdif / 2 > 8000 | ydif / 2 > 8000 | xdif < 0 |
      ydif < 0) {
    cout << "No" << endl;
  } else if (xbs[xdif / 2] == 0 | ybs[ydif / 2] == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

bool f(vector<int> vec, int target) {
  int s = 0;
  rep(i, vec.size()) { s += vec[i]; }
  if (s < target)
    return false;
  if (s == target)
    return true;
  int dist = s - target;
  if (dist % 2 == 1)
    return false;
  assert(0);
  dist /= 2;
  sort(vec.begin(), vec.end());
  bool dp[vec.size() + 1][dist + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;
  rep(i, vec.size()) {
    rep(j, dist + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j - vec[i] >= 0)
        dp[i + 1][j] |= dp[i][j - vec[i]];
    }
  }
  return dp[vec.size()][dist];
}

signed main() {
  string S;
  cin >> S;
  int X, Y;
  cin >> X >> Y;
  int k = 0;
  while (S[k] == 'F') {
    k++;
    --X;
  }
  X = abs(X);
  Y = abs(Y);
  vector<int> lr, ud;
  bool nowX = true;
  int length = 0;
  for (;;) {
    while (k < S.size() && S[k] == 'F') {
      k++;
      length++;
    }
    k++;
    if (nowX && length != 0)
      lr.push_back(length);
    if (!nowX && length != 0)
      ud.push_back(length);
    nowX = !nowX;
    length = 0;
    if (k >= S.size())
      break;
  }
  if (f(lr, X) && f(ud, Y)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

bool f(vector<int> vec, int target) {
  int s = 0;
  rep(i, vec.size()) { s += vec[i]; }
  if (s < target)
    return false;
  if (s == target)
    return true;
  int dist = s - target;
  if (dist % 2 == 1)
    return false;
  dist /= 2;
  sort(vec.begin(), vec.end());
  bool dp[vec.size() + 1][dist + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;
  rep(i, vec.size()) {
    rep(j, dist + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j - vec[i] >= 0)
        dp[i + 1][j] |= dp[i][j - vec[i]];
    }
  }
  return dp[vec.size()][dist];
}

signed main() {
  string S;
  cin >> S;
  int X, Y;
  cin >> X >> Y;
  int k = 0;
  while (S[k] == 'F') {
    k++;
    --X;
  }
  X = abs(X);
  Y = abs(Y);
  vector<int> lr, ud;
  bool nowX = true;
  int length = 0;
  for (;;) {
    while (k < S.size() && S[k] == 'F') {
      k++;
      length++;
    }
    k++;
    if (nowX && length != 0)
      lr.push_back(length);
    if (!nowX && length != 0)
      ud.push_back(length);
    nowX = !nowX;
    length = 0;
    if (k >= S.size())
      break;
  }
  if (f(lr, X) && f(ud, Y)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}
Problem p03490 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define F first
#define S second
#define PB push_back
using namespace std;

const int maxn = 1e4;
int dp[2][maxn][2 * maxn];
vector<int> v, u[2];

int main() {
  string s;
  int x, y;
  cin >> s >> x >> y;
  int cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      v.PB(cnt);
      cnt = 0;
    } else {
      cnt++;
      if (i == s.size() - 1)
        v.PB(cnt);
    }
  }
  int n = v.size();
  u[1].PB(0);
  u[0].PB(0);
  for (int i = 0; i < n; i++)
    u[i % 2].push_back(v[i]);
  dp[0][0][10 * 1000] = 1;
  dp[1][0][10 * 1000] = 1;
  for (int i = 0; i < u[0].size() - 1; i++) {
    for (int j = 0; j < 2 * maxn; j++) {
      if (dp[0][i][j]) {
        dp[0][i + 1][j + u[0][i + 1]] = 1;
        if (i == 0)
          continue;
        dp[0][i + 1][j - u[0][i + 1]] = 1;
      }
    }
  }
  for (int i = 0; i < u[1].size() - 1; i++) {
    for (int j = 0; j < 2 * maxn; j++) {
      if (dp[1][i][j]) {
        dp[1][i + 1][j + u[1][i + 1]] = 1;
        dp[1][i + 1][j - u[1][i + 1]] = 1;
      }
    }
  }
  int p = u[0].size() - 1;
  int q = u[1].size() - 1;
  if (dp[0][p][x + 1000 * 10] && dp[1][q][y + 1000 * 10])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define F first
#define S second
#define PB push_back
using namespace std;

const int maxn = 1e4;
bool dp[2][maxn][2 * maxn];
vector<int> v, u[2];

int main() {
  string s;
  int x, y;
  cin >> s >> x >> y;
  int cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'T') {
      v.PB(cnt);
      cnt = 0;
    } else {
      cnt++;
      if (i == s.size() - 1)
        v.PB(cnt);
    }
  }
  int n = v.size();
  u[1].PB(0);
  u[0].PB(0);
  for (int i = 0; i < n; i++)
    u[i % 2].push_back(v[i]);
  dp[0][0][10 * 1000] = 1;
  dp[1][0][10 * 1000] = 1;
  for (int i = 0; i < u[0].size() - 1; i++) {
    for (int j = 0; j < 2 * maxn; j++) {
      if (dp[0][i][j]) {
        dp[0][i + 1][j + u[0][i + 1]] = 1;
        if (i == 0)
          continue;
        dp[0][i + 1][j - u[0][i + 1]] = 1;
      }
    }
  }
  for (int i = 0; i < u[1].size() - 1; i++) {
    for (int j = 0; j < 2 * maxn; j++) {
      if (dp[1][i][j]) {
        dp[1][i + 1][j + u[1][i + 1]] = 1;
        dp[1][i + 1][j - u[1][i + 1]] = 1;
      }
    }
  }
  int p = u[0].size() - 1;
  int q = u[1].size() - 1;
  if (dp[0][p][x + 1000 * 10] && dp[1][q][y + 1000 * 10])
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}