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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define eps 1e-8
// #define eq(x,y) (fabs((x)-(y)) < eps)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll mod = 998244353;
long double PI = acosl(-1);
const ll infl = 3e18 + 10;
const int inf = 1e9 + 10;
const int nmax = 1e5 + 5;
const int MAXLG = log2(nmax) + 1;
// mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
// typedef tree< int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ost;

void NA() {
  cout << "No";
  exit(0);
}

bool dp[nmax][3][3][3];  /// n, a, b, c
char dp2[nmax][3][3][3]; /// n, a, b, c

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, a, b, c;
  cin >> n;
  cin >> a >> b >> c;
  vector<string> s(n);
  for (auto &z : s)
    cin >> z;

  ll sam = a + b + c;
  string ans;
  if (sam == 2) {
    int hui = 0;
    dp[hui][a][b][c] = true;
    for (string z : s) {
      hui++;
      if (z == "AB") {
        for (int i = 0; i <= 2; i++)
          for (int j = 0; j <= 2; j++)
            for (int k = 0; k <= 2; k++)
              if (dp[hui - 1][i][j][k]) {
                if (i > 0)
                  dp[hui][i - 1][j + 1][k] = true,
                                     dp2[hui][i - 1][j + 1][k] = 'B';
                if (j > 0)
                  dp[hui][i + 1][j - 1][k] = true,
                                     dp2[hui][i + 1][j - 1][k] = 'A';
              }
      } else if (z == "AC") {
        for (int i = 0; i <= 2; i++)
          for (int j = 0; j <= 2; j++)
            for (int k = 0; k <= 2; k++)
              if (dp[hui - 1][i][j][k]) {
                if (i > 0)
                  dp[hui][i - 1][j][k + 1] = true,
                                        dp2[hui][i - 1][j][k + 1] = 'C';
                if (k > 0)
                  dp[hui][i + 1][j][k - 1] = true,
                                        dp2[hui][i + 1][j][k - 1] = 'A';
              }
      } else {
        for (int i = 0; i <= 2; i++)
          for (int j = 0; j <= 2; j++)
            for (int k = 0; k <= 2; k++)
              if (dp[hui - 1][i][j][k]) {
                if (j > 0)
                  dp[hui][i][j - 1][k + 1] = true,
                                        dp2[hui][i][j - 1][k + 1] = 'C';
                if (k > 0)
                  dp[hui][i][j + 1][k - 1] = true,
                                        dp2[hui][i][j + 1][k - 1] = 'B';
              }
      }
      //            for(int i=0; i<2; i++) for(int j=0; j<2; j++) for(int k=0;
      //            k<2; k++) if(dp[hui][i][j][k]){
      //                cout<<"possibul "<<hui<<" "<<i<<" "<<j<<" "<<k<<endl;
      //            }
    }
    for (int i = 0; i <= 2; i++)
      for (int j = 0; j <= 2; j++)
        for (int k = 0; k <= 2; k++)
          if (dp[n][i][j][k]) {
            while (n) {
              ans += dp2[n][i][j][k];
              n--;
              if (ans.back() == 'A' and s[n] == "AB")
                i--, j++;
              else if (ans.back() == 'B' and s[n] == "AB")
                i++, j--;
              else if (ans.back() == 'A' and s[n] == "AC")
                i--, k++;
              else if (ans.back() == 'C' and s[n] == "AC")
                i++, k--;
              else if (ans.back() == 'B' and s[n] == "BC")
                j--, k++;
              else if (ans.back() == 'C' and s[n] == "BC")
                j++, k--;
            }
            reverse(ans.begin(), ans.end());
            cout << "Yes\\n";
            for (auto z : ans)
              cout << z << "\\n";
            exit(0);
          }
    NA();
    //        assert(false);
  } else {
    for (string z : s) {
      if (z == "AB") {
        if (a == 0 and b == 0)
          NA();
        if (a > b)
          a--, b++, ans += 'B';
        else
          a++, b--, ans += 'A';
      } else if (z == "AC") {
        if (a == 0 and c == 0)
          NA();
        if (a > c)
          a--, c++, ans += 'C';
        else
          a++, c--, ans += 'A';
      } else {
        if (c == 0 and b == 0)
          NA();
        if (c > b)
          c--, b++, ans += 'B';
        else
          c++, b--, ans += 'C';
      }
    }
    cout << "Yes\\n";
    for (auto z : ans)
      cout << z << "\\n";
  }
}
/*
3
1 1 0
AB
AC
BC
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define eps 1e-8
// #define eq(x,y) (fabs((x)-(y)) < eps)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll mod = 998244353;
long double PI = acosl(-1);
const ll infl = 3e18 + 10;
const int inf = 1e9 + 10;
const int nmax = 1e5 + 5;
const int MAXLG = log2(nmax) + 1;
// mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
// typedef tree< int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ost;

void NA() {
  cout << "No";
  exit(0);
}

bool dp[nmax][3][3][3];  /// n, a, b, c
char dp2[nmax][3][3][3]; /// n, a, b, c

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll n, a, b, c;
  cin >> n;
  cin >> a >> b >> c;
  vector<string> s(n);
  for (auto &z : s)
    cin >> z;

  ll sam = a + b + c;
  string ans;
  if (sam == 2) {
    int hui = 0;
    dp[hui][a][b][c] = true;
    for (string z : s) {
      hui++;
      if (z == "AB") {
        for (int i = 0; i <= 2; i++)
          for (int j = 0; j <= 2; j++)
            for (int k = 0; k <= 2; k++)
              if (dp[hui - 1][i][j][k]) {
                if (i > 0)
                  dp[hui][i - 1][j + 1][k] = true,
                                     dp2[hui][i - 1][j + 1][k] = 'B';
                if (j > 0)
                  dp[hui][i + 1][j - 1][k] = true,
                                     dp2[hui][i + 1][j - 1][k] = 'A';
              }
      } else if (z == "AC") {
        for (int i = 0; i <= 2; i++)
          for (int j = 0; j <= 2; j++)
            for (int k = 0; k <= 2; k++)
              if (dp[hui - 1][i][j][k]) {
                if (i > 0)
                  dp[hui][i - 1][j][k + 1] = true,
                                        dp2[hui][i - 1][j][k + 1] = 'C';
                if (k > 0)
                  dp[hui][i + 1][j][k - 1] = true,
                                        dp2[hui][i + 1][j][k - 1] = 'A';
              }
      } else {
        for (int i = 0; i <= 2; i++)
          for (int j = 0; j <= 2; j++)
            for (int k = 0; k <= 2; k++)
              if (dp[hui - 1][i][j][k]) {
                if (j > 0)
                  dp[hui][i][j - 1][k + 1] = true,
                                        dp2[hui][i][j - 1][k + 1] = 'C';
                if (k > 0)
                  dp[hui][i][j + 1][k - 1] = true,
                                        dp2[hui][i][j + 1][k - 1] = 'B';
              }
      }
      //            for(int i=0; i<2; i++) for(int j=0; j<2; j++) for(int k=0;
      //            k<2; k++) if(dp[hui][i][j][k]){
      //                cout<<"possibul "<<hui<<" "<<i<<" "<<j<<" "<<k<<endl;
      //            }
    }
    for (int i = 0; i <= 2; i++)
      for (int j = 0; j <= 2; j++)
        for (int k = 0; k <= 2; k++)
          if (dp[n][i][j][k]) {
            while (n) {
              ans += dp2[n][i][j][k];
              n--;
              if (ans.back() == 'A' and s[n] == "AB")
                i--, j++;
              else if (ans.back() == 'B' and s[n] == "AB")
                i++, j--;
              else if (ans.back() == 'A' and s[n] == "AC")
                i--, k++;
              else if (ans.back() == 'C' and s[n] == "AC")
                i++, k--;
              else if (ans.back() == 'B' and s[n] == "BC")
                j--, k++;
              else if (ans.back() == 'C' and s[n] == "BC")
                j++, k--;
            }
            reverse(ans.begin(), ans.end());
            cout << "Yes\\n";
            for (auto z : ans)
              cout << z << "\\n";
            exit(0);
          }
    assert(false);
  } else {
    for (string z : s) {
      if (z == "AB") {
        if (a == 0 and b == 0)
          NA();
        if (a > b)
          a--, b++, ans += 'B';
        else
          a++, b--, ans += 'A';
      } else if (z == "AC") {
        if (a == 0 and c == 0)
          NA();
        if (a > c)
          a--, c++, ans += 'C';
        else
          a++, c--, ans += 'A';
      } else {
        if (c == 0 and b == 0)
          NA();
        if (c > b)
          c--, b++, ans += 'B';
        else
          c++, b--, ans += 'C';
      }
    }
    cout << "Yes\\n";
    for (auto z : ans)
      cout << z << "\\n";
  }
}
/*
3
1 1 0
AB
AC
BC
*/