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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct from {
  int fa, fb, fc;
};

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, A, B, C;
  cin >> N >> A >> B >> C;
  chmin(A, 2);
  chmin(B, 2);
  chmin(C, 2);

  ve<string> S(N);
  rep(i, N) cin >> S[i];

  ve<ve<ve<ve<int>>>> dp(N + 1,
                         ve<ve<ve<int>>>(3, ve<ve<int>>(3, ve<int>(3, 0))));
  ve<ve<ve<ve<from>>>> dp_f(
      N + 1, ve<ve<ve<from>>>(3, ve<ve<from>>(3, ve<from>(3, {0, 0, 0}))));

  dp[0][A][B][C] = 1;

  rep(i, N) {

    rep(a, 3) rep(b, 3) rep(c, 3) {
      if (dp[i][a][b][c] == 0)
        continue;

      if (S[i] == "AB") {
        if (b - 1 >= 0) {
          dp[i + 1][min(2, a + 1)][b - 1][c] = 1;
          dp_f[i + 1][min(2, a + 1)][b - 1][c] = {a, b, c};
        }
        if (a - 1 >= 0) {
          dp[i + 1][a - 1][min(b + 1, 2)][c] = 2;
          dp_f[i + 1][a - 1][min(b + 1, 2)][c] = {a, b, c};
        }
      }

      if (S[i] == "AC") {
        if (c - 1 >= 0) {
          dp[i + 1][min(2, a + 1)][b][c - 1] = 1;
          dp_f[i + 1][min(2, a + 1)][b][c - 1] = {a, b, c};
        }
        if (a - 1 >= 0) {
          dp[i + 1][a - 1][b][min(c + 1, 2)] = 3;
          dp_f[i + 1][a - 1][b][min(c + 1, 2)] = {a, b, c};
        }
      }

      if (S[i] == "BC") {
        if (b - 1 >= 0) {
          dp[i + 1][a][b - 1][min(2, c + 1)] = 3;
          dp_f[i + 1][a][b - 1][min(2, c + 1)] = {a, b, c};
        }
        if (c - 1 >= 0) {
          dp[i + 1][a][min(b + 1, 2)][c - 1] = 2;
          dp_f[i + 1][a][min(b + 1, 2)][c - 1] = {a, b, c};
        }
      }
    }
  }

  rep(a, 3) rep(b, 3) rep(c, 3) {
    if (dp[N][a][b][c] != 0) {
      cyes;

      ve<char> D;
      rep(i, N) {
        // cout << dp[N-i][a][b][c] << endl;
        if (dp[N - i][a][b][c] == 1)
          D.pb('A');
        else if (dp[N - i][a][b][c] == 2)
          D.pb('B');
        else
          D.pb('C');

        from aaa = {a, b, c};
        a = dp_f[N - i][aaa.fa][aaa.fb][aaa.fc].fa;
        b = dp_f[N - i][aaa.fa][aaa.fb][aaa.fc].fb;
        c = dp_f[N - i][aaa.fa][aaa.fb][aaa.fc].fc;
      }
      reverse(all(D));
      rep(i, N) cout << D[i] << endl;
      return 0;
    }
  }

  cno;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, A, B, C;
  cin >> N >> A >> B >> C;
  chmin(A, 2);
  chmin(B, 2);
  chmin(C, 2);

  ve<string> S(N);
  rep(i, N) cin >> S[i];

  ve<ve<ve<ve<int>>>> dp(N + 1,
                         ve<ve<ve<int>>>(3, ve<ve<int>>(3, ve<int>(3, 0))));

  dp[0][A][B][C] = 1;

  rep(i, N) {

    rep(a, 3) rep(b, 3) rep(c, 3) {
      if (dp[i][a][b][c] == 0)
        continue;

      if (S[i] == "AB") {
        if (b - 1 >= 0) {
          dp[i + 1][min(2, a + 1)][b - 1][c] = 1;
        }
        if (a - 1 >= 0) {
          dp[i + 1][a - 1][min(b + 1, 2)][c] = 2;
        }
      }

      if (S[i] == "AC") {
        if (c - 1 >= 0) {
          dp[i + 1][min(2, a + 1)][b][c - 1] = 1;
        }
        if (a - 1 >= 0) {
          dp[i + 1][a - 1][b][min(c + 1, 2)] = 3;
        }
      }

      if (S[i] == "BC") {
        if (b - 1 >= 0) {
          dp[i + 1][a][b - 1][min(2, c + 1)] = 3;
        }
        if (c - 1 >= 0) {
          dp[i + 1][a][min(b + 1, 2)][c - 1] = 2;
        }
      }
    }
  }

  rep(a, 3) rep(b, 3) rep(c, 3) {
    if (dp[N][a][b][c] != 0) {
      cyes;

      ve<char> D;
      rep(i, N) {
        // cout << dp[N-i][a][b][c] << endl;
        if (dp[N - i][a][b][c] == 1) {
          D.pb('A');
          if (c < 2) {
            if (dp[N - i - 1][a][b][c + 1] > 0)
              c++;
            if (dp[N - i - 1][a - 1][b][c + 1] > 0)
              c++, a--;
          } else {
            if (dp[N - i - 1][a][b + 1][c] > 0)
              b++;
            if (dp[N - i - 1][a - 1][b + 1][c] > 0)
              b++, a--;
          }
        } else if (dp[N - i][a][b][c] == 2) {
          D.pb('B');
          if (a < 2) {
            if (dp[N - i - 1][a + 1][b][c] > 0)
              a++;
            if (dp[N - i - 1][a + 1][b - 1][c] > 0)
              a++, b--;
          } else {
            if (dp[N - i - 1][a][b][c + 1] > 0)
              c++;
            if (dp[N - i - 1][a][b - 1][c + 1] > 0)
              c++, b--;
          }

        } else {
          D.pb('C');
          if (a < 2) {
            if (dp[N - i - 1][a + 1][b][c] > 0)
              a++;
            if (dp[N - i - 1][a + 1][b][c - 1] > 0)
              a++, c--;
          } else {
            if (dp[N - i - 1][a][b + 1][c] > 0)
              b++;
            if (dp[N - i - 1][a][b + 1][c - 1] > 0)
              b++, c--;
          }
        }
      }
      reverse(all(D));
      rep(i, N) cout << D[i] << endl;
      return 0;
    }
  }

  cno;

  return 0;
}