Problem p03490 (C++)

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

using namespace std;

int main(void) {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;
  vector<int> v;
  vector<int> w;
  bool xflag = true;
  int count = 0;
  s += 'T'; // 番兵
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'F') {
      ++count;
    } else if (s[i] == 'T') {
      if (xflag) {
        v.push_back(count);
      } else {
        w.push_back(count);
      }
      xflag ^= true;
      count = 0;
    }
  }

  x -= v[0];
  v[0] = 0;

  const int offset = 10000;
  // dp[i][k] : v[0], ..., v[i]で(k - offset)が作れるか
  vector<vector<bool>> dpv(
      vector<vector<bool>>(v.size(), vector<bool>(offset * 2, false)));
  dpv[0][offset + v[0]] = true;
  dpv[0][offset - v[0]] = true;
  for (int i = 1; i < v.size(); ++i) {
    for (int k = 0; k < offset * 2; ++k) {
      if (dpv[i - 1][k]) {
        dpv[i][k + v[i]] = true;
        dpv[i][k - v[i]] = true;
      }
    }
  }

  if (w.size() != 0) {
    vector<vector<bool>> dpw(
        vector<vector<bool>>(w.size(), vector<bool>(offset * 2, false)));
    dpw[0][offset + w[0]] = true;
    dpw[0][offset - w[0]] = true;
    for (int i = 1; i < w.size(); ++i) {
      for (int k = 0; k < offset * 2; ++k) {
        if (dpw[i - 1][k]) {
          dpw[i][k + w[i]] = true;
          dpw[i][k - w[i]] = true;
        }
      }
    }

    if (dpv[v.size() - 1][offset + x] && dpw[w.size() - 1][offset + y]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  } else {
    if (dpv[v.size() - 1][offset + x] && y == 0) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  }
  return 0;
}


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

using namespace std;

int main(void) {
  string s;
  int x, y;
  cin >> s;
  cin >> x >> y;
  vector<int> v;
  vector<int> w;
  bool xflag = true;
  int count = 0;
  s += 'T'; // 番兵
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'F') {
      ++count;
    } else if (s[i] == 'T') {
      if (xflag) {
        v.push_back(count);
      } else {
        w.push_back(count);
      }
      xflag ^= true;
      count = 0;
    }
  }

  x -= v[0];
  v[0] = 0;

  const int offset = 100000;
  // dp[i][k] : v[0], ..., v[i]で(k - offset)が作れるか
  vector<vector<bool>> dpv(
      vector<vector<bool>>(v.size(), vector<bool>(offset * 2, false)));
  dpv[0][offset + v[0]] = true;
  dpv[0][offset - v[0]] = true;
  for (int i = 1; i < v.size(); ++i) {
    for (int k = 0; k < offset * 2; ++k) {
      if (dpv[i - 1][k]) {
        dpv[i][k + v[i]] = true;
        dpv[i][k - v[i]] = true;
      }
    }
  }

  if (w.size() != 0) {
    vector<vector<bool>> dpw(
        vector<vector<bool>>(w.size(), vector<bool>(offset * 2, false)));
    dpw[0][offset + w[0]] = true;
    dpw[0][offset - w[0]] = true;
    for (int i = 1; i < w.size(); ++i) {
      for (int k = 0; k < offset * 2; ++k) {
        if (dpw[i - 1][k]) {
          dpw[i][k + w[i]] = true;
          dpw[i][k - w[i]] = true;
        }
      }
    }

    if (dpv[v.size() - 1][offset + x] && dpw[w.size() - 1][offset + y]) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  } else {
    if (dpv[v.size() - 1][offset + x] && y == 0) {
      cout << "Yes\n";
    } else {
      cout << "No\n";
    }
  }
  return 0;
}