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

#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
#define vall(x) (x).begin(), (x).end()
using namespace std;
using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
template <class T1, class T2> void vadd(vector<T1> &v, T2 x);

int state[200005][3][3][3] = {};
int f(int u) { return min(2, u + 1); }
void solve(long long N, long long A, long long B, long long C,
           std::vector<std::string> s) {

  bool ex = A > 1 || B > 1 || C > 1;
  bool ex2 = A == 1 || B == 1 || C == 1;
  //  if ((A > 0 && B > 0 && C > 0) || (ex && ex2)) {
  // if ((A > 0 && B > 0 && C > 0)) {

  //   cout << "Yes" << endl;
  //   for (auto ss : s) {
  //     string an;
  //     if ("AB" == ss) {
  //       an = (A ? "B" : "A");
  //       if (A)
  //         A--, B++;
  //       else
  //         B--, A++;
  //     }
  //     if ("BC" == ss) {
  //       an = (B ? "C" : "B");
  //       if (B)
  //         B--, C++;
  //       else
  //         C--, B++;
  //     }
  //     if ("AC" == ss) {

  //       an = (A ? "C" : "A");
  //       if (A)
  //         A--, C++;
  //       else
  //         C--, A++;
  //     }
  //     cout << an << endl;
  //   }
  //   return;
  // }
  // 110, 300 のどっちか
  A = min(A, 2ll);
  B = min(B, 2ll);
  C = min(C, 2ll);

  state[0][A][B][C] = true;
  rep(i, N) {
    rep(j, 3) rep(k, 3) rep(l, 3) {
      if (!state[i][j][k][l])
        continue;
      bool p = state[i][j][k][l];
      if (s[i] == "AB") {
        if (j)
          state[i + 1][j - 1][min(2ll, k + 1)][l] = true;
        if (k)
          state[i + 1][min(2ll, j + 1)][k - 1][l] = true;
      }
      if (s[i] == "BC") {
        if (k)
          state[i + 1][j][k - 1][min(2ll, l + 1)] = true;
        if (l)
          state[i + 1][j][f(k)][l - 1] = true;
      }
      if (s[i] == "AC") {
        if (j)
          state[i + 1][j - 1][k][f(l)] = true;
        if (l)
          state[i + 1][f(j)][k][l - 1] = true;
      }
    }
  }
  int curj, curk, curl;
  bool found = false;
  rep(j, 3) rep(k, 3) rep(l, 3) {
    if (state[N][j][k][l]) {
      found = true;
      curj = j, curk = k, curl = l;
      break;
    }
  }
  if (!found) {
    cout << "No" << endl;
    return;
  }
  vector<string> ans;

  cout << "Yes" << endl;
  rrep(i, N) {

    bool found = false;
    rep(j, 3) rep(k, 3) rep(l, 3) {
      if (!state[i][j][k][l])
        continue;
      bool p = state[i][j][k][l];
      if (found)
        break;
      if (s[i] == "AB") {
        if (curj == j - 1 && curk == f(k) && curl == l) {
          ans.push_back("B");
          found = true;
        } else if (curj == f(j) && curk == k - 1 && curl == l) {
          ans.push_back("A");
          found = true;
        }
      }
      if (s[i] == "BC") {
        if (curj == j && curk == k - 1 && curl == f(l)) {
          ans.push_back("C");
          found = true;
        } else if (curj == j && curk == f(k) && curl == l - 1) {
          ans.push_back("B");
          found = true;
        }
      }
      if (s[i] == "AC") {
        if (curj == j - 1 && curk == k && curl == f(l)) {
          ans.push_back("C");
          found = true;
        } else if (curj == f(j) && curk == k && curl == l - 1) {
          ans.push_back("A");
          found = true;
        }
      }
      if (found) {
        curj = j, curk = k, curl = l;
      }
    }
  }
  rrep(i, N) { cout << ans[i] << endl; }
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long A;
  scanf("%lld", &A);
  long long B;
  scanf("%lld", &B);
  long long C;
  scanf("%lld", &C);
  std::vector<std::string> s(N);
  for (int i = 0; i < N; i++) {
    std::cin >> s[i];
  }
  solve(N, A, B, C, std::move(s));
  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
  return (l < r) ? (l = r, true) : false;
}

template <class T1, class T2> void vadd(vector<T1> &v, T2 x) {
  for (auto &s : v)
    s += T2(x);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
#define vall(x) (x).begin(), (x).end()
using namespace std;
using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
template <class T1, class T2> void vadd(vector<T1> &v, T2 x);

int state[200005][3][3][3] = {};
int f(int u) { return min(2, u + 1); }
void solve(long long N, long long A, long long B, long long C,
           std::vector<std::string> s) {

  bool ex = A > 1 || B > 1 || C > 1;
  bool ex2 = A == 1 || B == 1 || C == 1;
  //  if ((A > 0 && B > 0 && C > 0) || (ex && ex2)) {
  if ((A > 0 && B > 0 && C > 0)) {

    cout << "Yes" << endl;
    for (auto ss : s) {
      string an;
      if ("AB" == ss) {
        an = (A ? "B" : "A");
        if (A)
          A--, B++;
        else
          B--, A++;
      }
      if ("BC" == ss) {
        an = (B ? "C" : "B");
        if (B)
          B--, C++;
        else
          C--, B++;
      }
      if ("AC" == ss) {

        an = (A ? "C" : "A");
        if (A)
          A--, C++;
        else
          C--, A++;
      }
      cout << an << endl;
    }
    return;
  }
  // 110, 300 のどっちか
  A = min(A, 2ll);
  B = min(B, 2ll);
  C = min(C, 2ll);

  state[0][A][B][C] = true;
  rep(i, N) {
    rep(j, 3) rep(k, 3) rep(l, 3) {
      if (!state[i][j][k][l])
        continue;
      bool p = state[i][j][k][l];
      if (s[i] == "AB") {
        if (j)
          state[i + 1][j - 1][min(2ll, k + 1)][l] = true;
        if (k)
          state[i + 1][min(2ll, j + 1)][k - 1][l] = true;
      }
      if (s[i] == "BC") {
        if (k)
          state[i + 1][j][k - 1][min(2ll, l + 1)] = true;
        if (l)
          state[i + 1][j][f(k)][l - 1] = true;
      }
      if (s[i] == "AC") {
        if (j)
          state[i + 1][j - 1][k][f(l)] = true;
        if (l)
          state[i + 1][f(j)][k][l - 1] = true;
      }
    }
  }
  int curj, curk, curl;
  bool found = false;
  rep(j, 3) rep(k, 3) rep(l, 3) {
    if (state[N][j][k][l]) {
      found = true;
      curj = j, curk = k, curl = l;
      break;
    }
  }
  if (!found) {
    cout << "No" << endl;
    return;
  }
  vector<string> ans;

  cout << "Yes" << endl;
  rrep(i, N) {

    bool found = false;
    rep(j, 3) rep(k, 3) rep(l, 3) {
      if (!state[i][j][k][l])
        continue;
      bool p = state[i][j][k][l];
      if (found)
        break;
      if (s[i] == "AB") {
        if (curj == j - 1 && curk == f(k) && curl == l) {
          ans.push_back("B");
          found = true;
        } else if (curj == f(j) && curk == k - 1 && curl == l) {
          ans.push_back("A");
          found = true;
        }
      }
      if (s[i] == "BC") {
        if (curj == j && curk == k - 1 && curl == f(l)) {
          ans.push_back("C");
          found = true;
        } else if (curj == j && curk == f(k) && curl == l - 1) {
          ans.push_back("B");
          found = true;
        }
      }
      if (s[i] == "AC") {
        if (curj == j - 1 && curk == k && curl == f(l)) {
          ans.push_back("C");
          found = true;
        } else if (curj == f(j) && curk == f(k) && curl == l - 1) {
          ans.push_back("A");
          found = true;
        }
      }
      if (found) {
        curj = j, curk = k, curl = l;
      }
    }
  }
  rrep(i, N) { cout << ans[i] << endl; }
}

signed main() {
  long long N;
  scanf("%lld", &N);
  long long A;
  scanf("%lld", &A);
  long long B;
  scanf("%lld", &B);
  long long C;
  scanf("%lld", &C);
  std::vector<std::string> s(N);
  for (int i = 0; i < N; i++) {
    std::cin >> s[i];
  }
  solve(N, A, B, C, std::move(s));
  return 0;
}

// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
  return (l > r) ? (l = r, true) : false;
}

template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
  return (l < r) ? (l = r, true) : false;
}

template <class T1, class T2> void vadd(vector<T1> &v, T2 x) {
  for (auto &s : v)
    s += T2(x);
}