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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>

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

const int N = 1e5 + 10;
string s[N];
int n, a, b, c;

vector<char> res;

void solve1() {
  f(i, n) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0)
        bye();
      else if (a == 1)
        a = 0, b = 1, res.pb('B');
      else
        a = 1, b = 0, res.pb('A');
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        bye();
      else if (a == 1)
        a = 0, c = 1, res.pb('C');
      else
        a = 1, c = 0, res.pb('A');
    }

    if (s[i] == "BC") {
      if (c == 0 && b == 0)
        bye();
      else if (b == 1)
        c = 1, b = 0, res.pb('C');
      else
        c = 0, b = 1, res.pb('B');
    }
  }
}

int dis[N][3];

void process() {
  f(i, n) f(j, 3) dis[i][j] = N;

  for (int i = n - 2; i >= 0; i--) {
    if (s[i + 1] == "AB")
      dis[i][0] = i;
    else if (s[i + 1] == "AC")
      dis[i][1] = i;
    else
      dis[i][2] = i;

    f(j, 3) dis[i][j] = min(dis[i][j], dis[i + 1][j]);
  }
}

void solve2() {
  process();

  for (int i = 0; i < n; i++) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0)
        bye();

      if (a == 0)
        a++, b--, res.pb('A');
      else if (b == 0)
        b++, a--, res.pb('B');
      else {
        if (dis[i][1] < dis[i][2]) {
          a++, b--, res.pb('A');
        } else
          a--, b++, res.pb('B');
      }
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        bye();

      if (a == 0)
        a++, c--, res.pb('A');
      else if (c == 0)
        c++, a--, res.pb('C');
      else {
        if (dis[i][0] < dis[i][2]) {
          a++, c--, res.pb('A');
        } else
          a--, c++, res.pb('C');
      }
    }

    if (s[i] == "BC") {
      if (c == 0 && b == 0)
        bye();

      if (c == 0)
        c++, b--, res.pb('C');
      else if (b == 0)
        b++, c--, res.pb('B');
      else {
        if (dis[i][0] < dis[i][1]) {
          b++, c--, res.pb('B');
        } else
          b--, c++, res.pb('C');
      }
    }
  }
}

void solve3() {
  f(i, n) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0)
        bye();
      else if (a == 0)
        a++, b--, res.pb('A');
      else if (b == 0)
        a--, b++, res.pb('B');
      else if (a > 1)
        a--, b++, res.pb('B');
      else
        a++, b--, res.pb('A');
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        bye();
      else if (a == 0)
        a++, c--, res.pb('A');
      else if (c == 0)
        a--, c++, res.pb('C');
      else if (a > 1)
        a--, c++, res.pb('C');
      else
        a++, c--, res.pb('A');
    }

    if (s[i] == "BC") {
      if (b == 0 && c == 0)
        bye();
      else if (c == 0)
        c++, b--, res.pb('C');
      else if (b == 0)
        c--, b++, res.pb('B');
      else if (c > 1)
        c--, b++, res.pb('B');
      else
        c++, b--, res.pb('C');
    }
  }
}

void solve() {
  cin >> n >> a >> b >> c;

  f(i, n) cin >> s[i];

  if (a == 0 && b == 0 && c == 0)
    bye();

  if (a + b + c == 1)
    solve1();
  else if (a + b + c == 2) {
    solve2();
  } else
    solve3();

  cout << "Yes\\n";
  for (auto x : res)
    cout << x << '\\n';
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}

Error: 0

Fixed submission:

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>

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

const int N = 1e5 + 10;
string s[N];
int n, a, b, c;

vector<char> res;

void solve1() {
  f(i, n) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0)
        bye();
      else if (a == 1)
        a = 0, b = 1, res.pb('B');
      else
        a = 1, b = 0, res.pb('A');
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        bye();
      else if (a == 1)
        a = 0, c = 1, res.pb('C');
      else
        a = 1, c = 0, res.pb('A');
    }

    if (s[i] == "BC") {
      if (c == 0 && b == 0)
        bye();
      else if (b == 1)
        c = 1, b = 0, res.pb('C');
      else
        c = 0, b = 1, res.pb('B');
    }
  }
}

int dis[N][3];

void process() {
  f(i, n) f(j, 3) dis[i][j] = N;

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == "AB")
      dis[i][0] = i;
    else if (s[i] == "AC")
      dis[i][1] = i;
    else
      dis[i][2] = i;

    f(j, 3) dis[i][j] = min(dis[i][j], dis[i + 1][j]);
  }
}

void solve2() {
  process();

  for (int i = 0; i < n; i++) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0)
        bye();

      if (a == 0)
        a++, b--, res.pb('A');
      else if (b == 0)
        b++, a--, res.pb('B');
      else {
        if (dis[i][1] < dis[i][2]) {
          a++, b--, res.pb('A');
        } else
          a--, b++, res.pb('B');
      }
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        bye();

      if (a == 0)
        a++, c--, res.pb('A');
      else if (c == 0)
        c++, a--, res.pb('C');
      else {
        if (dis[i][0] < dis[i][2]) {
          a++, c--, res.pb('A');
        } else
          a--, c++, res.pb('C');
      }
    }

    if (s[i] == "BC") {
      if (c == 0 && b == 0)
        bye();

      if (c == 0)
        c++, b--, res.pb('C');
      else if (b == 0)
        b++, c--, res.pb('B');
      else {
        if (dis[i][0] < dis[i][1]) {
          b++, c--, res.pb('B');
        } else
          b--, c++, res.pb('C');
      }
    }
  }
}

void solve3() {
  f(i, n) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0)
        bye();
      else if (a == 0)
        a++, b--, res.pb('A');
      else if (b == 0)
        a--, b++, res.pb('B');
      else if (a > 1)
        a--, b++, res.pb('B');
      else
        a++, b--, res.pb('A');
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        bye();
      else if (a == 0)
        a++, c--, res.pb('A');
      else if (c == 0)
        a--, c++, res.pb('C');
      else if (a > 1)
        a--, c++, res.pb('C');
      else
        a++, c--, res.pb('A');
    }

    if (s[i] == "BC") {
      if (b == 0 && c == 0)
        bye();
      else if (c == 0)
        c++, b--, res.pb('C');
      else if (b == 0)
        c--, b++, res.pb('B');
      else if (c > 1)
        c--, b++, res.pb('B');
      else
        c++, b--, res.pb('C');
    }
  }
}

void solve() {
  cin >> n >> a >> b >> c;

  f(i, n) cin >> s[i];

  if (a == 0 && b == 0 && c == 0)
    bye();

  if (a + b + c == 1)
    solve1();
  else if (a + b + c == 2) {
    assert(1 == 0);
    solve2();
  } else
    solve3();

  cout << "Yes\\n";
  for (auto x : res)
    cout << x << '\\n';
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}