Problem p03488 (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 cntF = 0;
  vector<vector<int>> dist(2, vector<int>(0));
  int index = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s.at(i) == 'F')
      cntF++;
    else {
      dist.at(index % 2).push_back(cntF);
      index++;
      cntF = 0;
    }
  }
  dist.at(index % 2).push_back(cntF);

  vector<vector<bool>> dpX((int)dist.at(0).size() + 1, vector<bool>(16001));
  if ((int)dist.at(0).size() == 0)
    dpX.at(0).at(8000) = true;
  else
    dpX.at(0).at(8000 + dist.at(0).at(0)) = true;
  for (int i = 1; i < (int)dist.at(0).size(); i++) {
    for (int j = 0; j <= 16000; j++) {
      int d = j - dist.at(0).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpX.at(i - 1).at(d))
          dpX.at(i).at(j) = true;
      }

      d = j + dist.at(0).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpX.at(i - 1).at(d))
          dpX.at(i).at(j) = true;
      }
    }
  }
  vector<vector<bool>> dpY((int)dist.at(1).size() + 1, vector<bool>(16001));
  if ((int)dist.at(1).size() == 0)
    dpY.at(0).at(8000) = true;
  else {
    dpY.at(0).at(8000 + dist.at(1).at(0)) = true;
    dpY.at(0).at(8000 - dist.at(1).at(0)) = true;
  }
  for (int i = 1; i < (int)dist.at(1).size(); i++) {
    for (int j = 0; j <= 16000; j++) {
      int d = j - dist.at(1).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpY.at(i - 1).at(d))
          dpY.at(i).at(j) = true;
      }

      d = j + dist.at(1).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpY.at(i - 1).at(d))
          dpY.at(i).at(j) = true;
      }
    }
  }

  if (dpX.at((int)dist.at(0).size() - 1).at(x + 8000) &&
      dpY.at((int)dist.at(1).size() - 1).at(y + 8000))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


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

int main() {
  string s;
  cin >> s;
  int x, y;
  cin >> x >> y;

  int cntF = 0;
  vector<vector<int>> dist(2, vector<int>(0));
  int index = 0;
  for (int i = 0; i < (int)s.size(); i++) {
    if (s.at(i) == 'F')
      cntF++;
    else {
      dist.at(index % 2).push_back(cntF);
      index++;
      cntF = 0;
    }
  }
  dist.at(index % 2).push_back(cntF);
  dist.at((index + 1) % 2).push_back(0);

  vector<vector<bool>> dpX((int)dist.at(0).size() + 1, vector<bool>(16001));
  if ((int)dist.at(0).size() == 0)
    dpX.at(0).at(8000) = true;
  else
    dpX.at(0).at(8000 + dist.at(0).at(0)) = true;
  for (int i = 1; i < (int)dist.at(0).size(); i++) {
    for (int j = 0; j <= 16000; j++) {
      int d = j - dist.at(0).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpX.at(i - 1).at(d))
          dpX.at(i).at(j) = true;
      }

      d = j + dist.at(0).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpX.at(i - 1).at(d))
          dpX.at(i).at(j) = true;
      }
    }
  }
  vector<vector<bool>> dpY((int)dist.at(1).size() + 1, vector<bool>(16001));
  if ((int)dist.at(1).size() == 0)
    dpY.at(0).at(8000) = true;
  else {
    dpY.at(0).at(8000 + dist.at(1).at(0)) = true;
    dpY.at(0).at(8000 - dist.at(1).at(0)) = true;
  }
  for (int i = 1; i < (int)dist.at(1).size(); i++) {
    for (int j = 0; j <= 16000; j++) {
      int d = j - dist.at(1).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpY.at(i - 1).at(d))
          dpY.at(i).at(j) = true;
      }

      d = j + dist.at(1).at(i);
      if (d >= 0 && d <= 16000) {
        if (dpY.at(i - 1).at(d))
          dpY.at(i).at(j) = true;
      }
    }
  }

  if (dpX.at((int)dist.at(0).size() - 1).at(x + 8000) &&
      dpY.at((int)dist.at(1).size() - 1).at(y + 8000))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}