Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  int st = 5 * 5 * 5;
  vector<vecint> dp(n + 1, vecint(st));
  a = min(a, 4);
  b = min(b, 4);
  c = min(c, 4);
  auto id = [](int x, int y, int z) {
    x = min(x, 4);
    y = min(y, 4);
    z = min(z, 4);
    return x * 25 + y * 5 + z;
  };
  dp[0][id(a, b, c)] = 1;
  vector<string> vq(n);
  REP(i, n) {
    string s;
    cin >> s;
    vq[i] = s;
    if (s == "AB") {
      REP(j, st) {
        if (!dp[i][j])
          continue;
        int x = j / 25;
        int y = (j / 5) % 5;
        int z = j % 5;
        if (x == 0 && y == 0)
          continue;
        if (x <= y) {
          dp[i + 1][id(x + 1, y - 1, z)] = 1;
        }
        if (x >= y) {
          dp[i + 1][id(x - 1, y + 1, z)] = 1;
        }
      }
    } else if (s == "AC") {
      REP(j, st) {
        if (!dp[i][j])
          continue;
        int x = j / 25;
        int y = (j / 5) % 5;
        int z = j % 5;
        if (x == 0 && z == 0)
          continue;
        if (x <= z) {
          dp[i + 1][id(x + 1, y, z - 1)] = 1;
        }
        if (x >= z) {
          dp[i + 1][id(x - 1, y, z + 1)] = 1;
        }
      }
    } else {
      REP(j, st) {
        if (!dp[i][j])
          continue;
        int x = j / 25;
        int y = (j / 5) % 5;
        int z = j % 5;
        if (y == 0 && z == 0)
          continue;
        if (y <= z) {
          dp[i + 1][id(x, y + 1, z - 1)] = 1;
        }
        if (y >= z) {
          dp[i + 1][id(x, y - 1, z + 1)] = 1;
        }
      }
    }
  }
  int cur = find(ALL(dp[n]), 1) - begin(dp[n]);
  if (cur == st) {
    cout << "No\\n";
    return 0;
  }
  vector<string> res;
  REP(i, n) {
    int j = n - i;
    string s = vq[j - 1];
    int x = cur / 25;
    int y = (cur / 5) % 5;
    int z = cur % 5;
    int *p;
    int *q;
    if (s == "AB") {
      p = &x;
      q = &y;
    } else if (s == "AC") {
      p = &x;
      q = &z;
    } else {
      p = &y;
      q = &z;
    }
    bool first = false;
    if (*p > 0 && *q < 4) {
      ++*q;
      --*p;
      if (dp[j - 1][id(x, y, z)]) {
        first = true;
      } else if (*p == 3) {
        ++*p;
        if (dp[j - 1][id(x, y, z)]) {
          first = true;
        } else {
          --*q;
        }
      } else {
        --*q;
        ++*p;
      }
    }
    if (!first) {
      --*q;
      ++*p;
      if (!dp[j - 1][id(x, y, z)] && *q == 3) {
        ++*q;
      }
    }
    if (first) {
      if (s == "BC") {
        res.push_back("B");
      } else {
        res.push_back("A");
      }
    } else {
      if (s == "AB") {
        res.push_back("B");
      } else {
        res.push_back("C");
      }
    }
    cur = id(x, y, z);
  }
  cout << "Yes\\n";
  reverse(ALL(res));
  for (auto &&ch : res) {
    cout << ch << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
#define FOR(i, k, n) for (int i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using namespace boost::adaptors;
using namespace std::string_literals;
using ll = int64_t;
using vecint = vector<int>;
using vecll = vector<ll>;
using boost::irange;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  int st = 5 * 5 * 5;
  vector<vecint> dp(n + 1, vecint(st));
  a = min(a, 4);
  b = min(b, 4);
  c = min(c, 4);
  auto id = [](int x, int y, int z) { return x * 25 + y * 5 + z; };
  dp[0][id(a, b, c)] = 1;
  vector<string> vq(n);
  REP(i, n) {
    string s;
    cin >> s;
    vq[i] = s;
    if (s == "AB") {
      REP(j, st) {
        if (!dp[i][j])
          continue;
        int x = j / 25;
        int y = (j / 5) % 5;
        int z = j % 5;
        if (x == 0 && y == 0)
          continue;
        if (x <= y) {
          dp[i + 1][id(x + 1, y - 1, z)] = 1;
        }
        if (x >= y) {
          dp[i + 1][id(x - 1, y + 1, z)] = 1;
        }
      }
    } else if (s == "AC") {
      REP(j, st) {
        if (!dp[i][j])
          continue;
        int x = j / 25;
        int y = (j / 5) % 5;
        int z = j % 5;
        if (x == 0 && z == 0)
          continue;
        if (x <= z) {
          dp[i + 1][id(x + 1, y, z - 1)] = 1;
        }
        if (x >= z) {
          dp[i + 1][id(x - 1, y, z + 1)] = 1;
        }
      }
    } else {
      REP(j, st) {
        if (!dp[i][j])
          continue;
        int x = j / 25;
        int y = (j / 5) % 5;
        int z = j % 5;
        if (y == 0 && z == 0)
          continue;
        if (y <= z) {
          dp[i + 1][id(x, y + 1, z - 1)] = 1;
        }
        if (y >= z) {
          dp[i + 1][id(x, y - 1, z + 1)] = 1;
        }
      }
    }
  }
  int cur = find(ALL(dp[n]), 1) - begin(dp[n]);
  if (cur == st) {
    cout << "No\\n";
    return 0;
  }
  vector<string> res;
  REP(i, n) {
    int j = n - i;
    string s = vq[j - 1];
    int x = cur / 25;
    int y = (cur / 5) % 5;
    int z = cur % 5;
    if (s == "AB") {
      if (x > 0 && dp[j - 1][id(x - 1, y + 1, z)]) {
        res.push_back("A");
        cur = id(x - 1, y + 1, z);
      } else {
        res.push_back("B");
        cur = id(x + 1, y - 1, z);
      }
    } else if (s == "AC") {
      if (x > 0 && dp[j - 1][id(x - 1, y, z + 1)]) {
        res.push_back("A");
        cur = id(x - 1, y, z + 1);
      } else {
        res.push_back("C");
        cur = id(x + 1, y, z - 1);
      }
    } else {
      if (y > 0 && dp[j - 1][id(x, y - 1, z + 1)]) {
        res.push_back("A");
        cur = id(x, y - 1, z + 1);
      } else {
        res.push_back("C");
        cur = id(x, y + 1, z - 1);
      }
    }
  }
  cout << "Yes\\n";
  reverse(ALL(res));
  for (auto &&ch : res) {
    cout << ch << "\\n";
  }
  return 0;
}