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

#include <bits/stdc++.h>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
using namespace std;
constexpr char nl = '\\n';
using ll = long long;
using boost::irange;
namespace rng {
using namespace boost;
using namespace boost::range;
} // namespace rng
namespace adp {
using namespace boost::adaptors;
}

struct init_ {
  init_() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

template <class C>
auto operator>>(istream &in, C &c) ->
    typename enable_if<!is_void<decltype(c.begin())>::value &&
                           !is_same<C, string>::value,
                       istream &>::type {
  for (auto &v : c) {
    in >> v;
  }
  return in;
}
template <class C>
auto operator<<(ostream &out, const C &c) ->
    typename enable_if<!is_void<decltype(c.begin())>::value &&
                           !is_same<C, string>::value,
                       ostream &>::type {
  int i = 0;
  for (const auto &v : c) {
    if (i++) {
      out << ' ';
    }
    out << v;
  }
  return out;
}
template <size_t N, class T, class R>
using when_last =
    typename enable_if<N == tuple_size<remove_reference_t<T>>::value - 1,
                       R>::type;
template <size_t N, class T, class R>
using when_init =
    typename enable_if<N != tuple_size<remove_reference_t<T>>::value - 1,
                       R>::type;
template <size_t N = 0, class T>
auto operator>>(istream &in, T &&t) -> when_last<N, T, istream &> {
  return in >> get<N>(t);
}
template <size_t N = 0, class T>
auto operator>>(istream &in, T &&t) -> when_init<N, T, istream &> {
  return operator>><N + 1>(in >> get<N>(t), forward<T>(t));
}
template <size_t N = 0, class T>
auto operator<<(ostream &out, const T &t) -> when_last<N, T, ostream &> {
  return out << get<N>(t);
}
template <size_t N = 0, class T>
auto operator<<(ostream &out, const T &t) -> when_init<N, T, ostream &> {
  return operator<< <N + 1>(out << get<N>(t) << ' ', t);
}

#ifdef LOCAL
constexpr auto color_s_ = "\\033[2m\\033[33m", color_e_ = "\\033[0m";
template <class T> void trace_(const T &v) { cerr << v; }
template <class T> void trace_(const vector<vector<T>> &m) {
  cerr << "\\n";
  for (const auto &v : m) {
    trace_(v);
    cerr << "\\n";
  }
}
template <class T> void trace_(const char *s, const T &v) {
  cerr << color_s_ << s << "=";
  trace_(v);
  cerr << color_e_ << "\\n";
  cerr.flush();
}
template <class T, class... Ts>
void trace_(const char *s, const T &v, const Ts &...vs) {
  cerr << color_s_;
  for (; *s != ','; ++s) {
    cerr << *s;
  }
  cerr << "=";
  trace_(v);
  cerr << ",";
  trace_(++s, vs...);
}
#define TRACE(...) trace_(#__VA_ARGS__, __VA_ARGS__)
#else
#define TRACE(...)
#endif

char domove(ll &lhs, ll &rhs, char lc, char rc, bool nextHasLc,
            bool nextHasRc) {
  if (lhs == 0 && rhs == 0)
    return '\\0';
  if (lhs == 1 && rhs == 1) {
    if (nextHasLc) {
      ++lhs;
      --rhs;
      return lc;
    } else {
      --lhs;
      ++rhs;
      return rc;
    }
  }
  if (lhs > rhs) {
    --lhs;
    ++rhs;
    return rc;
  } else {
    ++lhs;
    --rhs;
    return lc;
  }
}

int main() {
  int n;
  ll a, b, c;
  cin >> n >> a >> b >> c;
  if (a == 0 && b == 0 && c == 0) {
    cout << "No" << nl;
    return 0;
  }
  vector<string> ss(n);
  cin >> ss;
  if (a == 0 && b == 0 || b == 0 && c == 0 || c == 0 && a == 0) {
    ll ta = a, tb = b, tc = c;
    for (int i : irange(n)) {
      string s = ss[i], next = (i + 1 < n) ? ss[i + 1] : "";
      char res = '\\0';
      if (s == "AB")
        res = domove(ta, tb, 'A', 'B', next.find('A') != string::npos,
                     next.find('B') != string::npos);
      else if (s == "BC")
        res = domove(tb, tc, 'B', 'C', next.find('B') != string::npos,
                     next.find('C') != string::npos);
      else if (s == "AC")
        res = domove(ta, tc, 'A', 'C', next.find('A') != string::npos,
                     next.find('C') != string::npos);
      if (res == '\\0') {
        cout << "No" << nl;
        return 0;
      }
    }
  }
  cout << "Yes" << nl;
  for (int i : irange(n)) {
    string s = ss[i], next = (i + 1 < n) ? ss[i + 1] : "";
    char res = '\\0';
    if (s == "AB")
      res = domove(a, b, 'A', 'B', next.find('A') != string::npos,
                   next.find('B') != string::npos);
    else if (s == "BC")
      res = domove(b, c, 'B', 'C', next.find('B') != string::npos,
                   next.find('C') != string::npos);
    else if (s == "AC")
      res = domove(a, c, 'A', 'C', next.find('A') != string::npos,
                   next.find('C') != string::npos);
    if (res == '\\0') {
      return -1;
    }
    cout << res << nl;
    TRACE(a, b, c);
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/range.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/numeric.hpp>
using namespace std;
constexpr char nl = '\\n';
using ll = long long;
using boost::irange;
namespace rng {
using namespace boost;
using namespace boost::range;
} // namespace rng
namespace adp {
using namespace boost::adaptors;
}

struct init_ {
  init_() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} init_;

template <class C>
auto operator>>(istream &in, C &c) ->
    typename enable_if<!is_void<decltype(c.begin())>::value &&
                           !is_same<C, string>::value,
                       istream &>::type {
  for (auto &v : c) {
    in >> v;
  }
  return in;
}
template <class C>
auto operator<<(ostream &out, const C &c) ->
    typename enable_if<!is_void<decltype(c.begin())>::value &&
                           !is_same<C, string>::value,
                       ostream &>::type {
  int i = 0;
  for (const auto &v : c) {
    if (i++) {
      out << ' ';
    }
    out << v;
  }
  return out;
}
template <size_t N, class T, class R>
using when_last =
    typename enable_if<N == tuple_size<remove_reference_t<T>>::value - 1,
                       R>::type;
template <size_t N, class T, class R>
using when_init =
    typename enable_if<N != tuple_size<remove_reference_t<T>>::value - 1,
                       R>::type;
template <size_t N = 0, class T>
auto operator>>(istream &in, T &&t) -> when_last<N, T, istream &> {
  return in >> get<N>(t);
}
template <size_t N = 0, class T>
auto operator>>(istream &in, T &&t) -> when_init<N, T, istream &> {
  return operator>><N + 1>(in >> get<N>(t), forward<T>(t));
}
template <size_t N = 0, class T>
auto operator<<(ostream &out, const T &t) -> when_last<N, T, ostream &> {
  return out << get<N>(t);
}
template <size_t N = 0, class T>
auto operator<<(ostream &out, const T &t) -> when_init<N, T, ostream &> {
  return operator<< <N + 1>(out << get<N>(t) << ' ', t);
}

#ifdef LOCAL
constexpr auto color_s_ = "\\033[2m\\033[33m", color_e_ = "\\033[0m";
template <class T> void trace_(const T &v) { cerr << v; }
template <class T> void trace_(const vector<vector<T>> &m) {
  cerr << "\\n";
  for (const auto &v : m) {
    trace_(v);
    cerr << "\\n";
  }
}
template <class T> void trace_(const char *s, const T &v) {
  cerr << color_s_ << s << "=";
  trace_(v);
  cerr << color_e_ << "\\n";
  cerr.flush();
}
template <class T, class... Ts>
void trace_(const char *s, const T &v, const Ts &...vs) {
  cerr << color_s_;
  for (; *s != ','; ++s) {
    cerr << *s;
  }
  cerr << "=";
  trace_(v);
  cerr << ",";
  trace_(++s, vs...);
}
#define TRACE(...) trace_(#__VA_ARGS__, __VA_ARGS__)
#else
#define TRACE(...)
#endif

char domove(ll &lhs, ll &rhs, char lc, char rc) {
  if (lhs == 0 && rhs == 0)
    return '\\0';
  if (lhs > rhs) {
    --lhs;
    ++rhs;
    return rc;
  } else {
    ++lhs;
    --rhs;
    return lc;
  }
}

int main() {
  int n;
  ll a, b, c;
  cin >> n >> a >> b >> c;
  if (a == 0 && b == 0 && c == 0) {
    cout << "No" << nl;
    return 0;
  }
  vector<string> ss(n);
  cin >> ss;
  if (a == 0 && b == 0 || b == 0 && c == 0 || c == 0 && a == 0) {
    ll ta = a, tb = b, tc = c;
    for (string s : ss) {
      char res = '\\0';
      if (s == "AB")
        res = domove(ta, tb, 'A', 'B');
      else if (s == "BC")
        res = domove(tb, tc, 'B', 'C');
      else if (s == "AC")
        res = domove(ta, tc, 'A', 'C');
      if (res == '\\0') {
        cout << "No" << nl;
        return 0;
      }
    }
  }
  cout << "Yes" << nl;
  for (string s : ss) {
    char res = '\\0';
    if (s == "AB")
      res = domove(a, b, 'A', 'B');
    else if (s == "BC")
      res = domove(b, c, 'B', 'C');
    else if (s == "AC")
      res = domove(a, c, 'A', 'C');
    if (res == '\\0') {
      return -1;
    }
    cout << res << nl;
    TRACE(a, b, c);
  }
}