Problem p02692 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

struct chain {
  char car;
  shared_ptr<chain> cdr;
};

string solve(int n, int a0, int b0, int c0, const vector<string> &ops) {
  map<tuple<int, int, int>, shared_ptr<chain>> cur, prv;
  cur[make_tuple(min(5, a0), min(5, b0), min(5, c0))] = nullptr;
  for (string op : ops) {
    cur.swap(prv);
    cur = map<tuple<int, int, int>, shared_ptr<chain>>();
    for (auto it : prv) {
      auto [a, b, c] = it.first;
      shared_ptr<chain> as = make_shared<chain>((chain){'A', it.second});
      shared_ptr<chain> bs = make_shared<chain>((chain){'B', it.second});
      shared_ptr<chain> cs = make_shared<chain>((chain){'C', it.second});
      auto use = [&](int da, int db, int dc) {
        auto [a, b, c] = it.first;
        auto ns = da == 1 ? as : db == 1 ? bs : cs;
        cur[make_tuple(a + da, b + db, c + dc)] = ns;
      };
      if (op == "AB") {
        if (a >= 2) {
          use(-1, 1, 0);
        } else if (b >= 2) {
          use(1, -1, 0);
        } else {
          if (a)
            use(-1, 1, 0);
          if (b)
            use(1, -1, 0);
        }
      } else if (op == "BC") {
        if (c >= 2) {
          use(0, 1, -1);
        } else if (b >= 2) {
          use(0, -1, 1);
        } else {
          if (c)
            use(0, 1, -1);
          if (b)
            use(0, -1, 1);
        }
      } else if (op == "AC") {
        if (a >= 2) {
          use(-1, 0, 1);
        } else if (c >= 2) {
          use(1, 0, -1);
        } else {
          if (a)
            use(-1, 0, 1);
          if (c)
            use(1, 0, -1);
        }
      } else {
        assert(false);
      }
    }
  }
  if (cur.empty())
    return "";
  string ans;
  for (auto s = cur.begin()->second; s != nullptr; s = s->cdr) {
    ans += s->car;
  }
  reverse(ALL(ans));
  return ans;
}

// generated by online-judge-template-generator v4.1.0
// (https://github.com/kmyk/online-judge-template-generator)
const string YES = "Yes";
const string NO = "No";
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr char endl = '\\n';
  int64_t N;
  int64_t A, B, C;
  cin >> N;
  vector<string> s(N);
  cin >> A >> B >> C;
  REP(i, N) { cin >> s[i]; }
  auto ans = solve(N, A, B, C, s);
  if (ans.empty()) {
    cout << NO << endl;
  } else {
    cout << YES << endl;
    for (char c : ans) {
      cout << c << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

struct chain {
  char car;
  shared_ptr<chain> cdr;
};

string solve(int n, int a0, int b0, int c0, const vector<string> &ops) {
  set<tuple<int, int, int, shared_ptr<chain>>> cur, prv;
  cur.emplace(min(5, a0), min(5, b0), min(5, c0), nullptr);
  for (string op : ops) {
    cur.swap(prv);
    cur = set<tuple<int, int, int, shared_ptr<chain>>>();
    for (auto it : prv) {
      auto [a, b, c, s] = it;
      shared_ptr<chain> as = make_shared<chain>((chain){'A', s});
      shared_ptr<chain> bs = make_shared<chain>((chain){'B', s});
      shared_ptr<chain> cs = make_shared<chain>((chain){'C', s});
      auto use = [&](int da, int db, int dc) {
        auto [a, b, c, s] = it;
        auto ns = da == 1 ? as : db == 1 ? bs : cs;
        cur.emplace(a + da, b + db, c + dc, ns);
      };
      if (op == "AB") {
        if (a >= 2) {
          use(-1, 1, 0);
        } else if (b >= 2) {
          use(1, -1, 0);
        } else {
          if (a)
            use(-1, 1, 0);
          if (b)
            use(1, -1, 0);
        }
      } else if (op == "BC") {
        if (c >= 2) {
          use(0, 1, -1);
        } else if (b >= 2) {
          use(0, -1, 1);
        } else {
          if (c)
            use(0, 1, -1);
          if (b)
            use(0, -1, 1);
        }
      } else if (op == "AC") {
        if (a >= 2) {
          use(-1, 0, 1);
        } else if (c >= 2) {
          use(1, 0, -1);
        } else {
          if (a)
            use(-1, 0, 1);
          if (c)
            use(1, 0, -1);
        }
      } else {
        assert(false);
      }
    }
  }
  if (cur.empty())
    return "";
  string ans;
  for (auto s = get<3>(*cur.begin()); s != nullptr; s = s->cdr) {
    ans += s->car;
  }
  reverse(ALL(ans));
  return ans;
}

// generated by online-judge-template-generator v4.1.0
// (https://github.com/kmyk/online-judge-template-generator)
const string YES = "Yes";
const string NO = "No";
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr char endl = '\\n';
  int64_t N;
  int64_t A, B, C;
  cin >> N;
  vector<string> s(N);
  cin >> A >> B >> C;
  REP(i, N) { cin >> s[i]; }
  auto ans = solve(N, A, B, C, s);
  if (ans.empty()) {
    cout << NO << endl;
  } else {
    cout << YES << endl;
    for (char c : ans) {
      cout << c << endl;
    }
  }
  return 0;
}