Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int N;
vector<int> C;
int dp[112345][4][4][4];
vector<string> S;

bool dfs(int p, vector<int> c) {
  REP(i, 3) if (c[i] < 0) return false;
  if (p == N)
    return true;
  int &res = dp[p][c[0]][c[1]][c[2]];
  if (res != -1)
    return res;

  res = 0;
  REP(i, 2) {
    vector<int> nc = c;
    nc[S[p][0] - 'A']++;
    nc[S[p][1] - 'A']--;
    res |= dfs(p + 1, nc);
    swap(S[p][0], S[p][1]);
  }

  return res;
}

int main() {
  cin >> N;
  C.resize(3);
  REP(i, 3) cin >> C[i];
  S.resize(N);
  REP(i, N) cin >> S[i];

  memset(dp, -1, sizeof(dp));
  bool ok = dfs(0, C);
  puts([&] { return ok; }() ? "Yes" : "No");
  if (ok) {
    vector<string> ans;
    REP(i, N) {
      REP(j, 2) {
        vector<int> nc = C;
        nc[S[i][0] - 'A']++;
        nc[S[i][1] - 'A']--;
        if (dfs(i + 1, nc)) {
          ans.emplace_back(S[i].substr(0, 1));
          C = nc;
          break;
        }
        swap(S[i][0], S[i][1]);
      }
    }
    for (string &s : ans)
      cout << s << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int N;
vector<int> C;
int dp[112345][4][4][4];
vector<string> S;

bool dfs(int p, vector<int> c) {
  REP(i, 3) if (c[i] < 0) return false;
  if (p == N)
    return true;
  int &res = dp[p][c[0]][c[1]][c[2]];
  if (res != -1)
    return res;

  res = 0;
  REP(i, 2) {
    vector<int> nc = c;
    nc[S[p][0] - 'A']++;
    nc[S[p][1] - 'A']--;
    res |= dfs(p + 1, nc);
    swap(S[p][0], S[p][1]);
  }

  return res;
}

int main() {
  cin >> N;
  C.resize(3);
  REP(i, 3) {
    cin >> C[i];
    if (C[i] > 3)
      C[i] = 3;
  }
  S.resize(N);
  REP(i, N) cin >> S[i];

  memset(dp, -1, sizeof(dp));
  bool ok = dfs(0, C);
  puts([&] { return ok; }() ? "Yes" : "No");
  if (ok) {
    vector<string> ans;
    REP(i, N) {
      REP(j, 2) {
        vector<int> nc = C;
        nc[S[i][0] - 'A']++;
        nc[S[i][1] - 'A']--;
        if (dfs(i + 1, nc)) {
          ans.emplace_back(S[i].substr(0, 1));
          C = nc;
          break;
        }
        swap(S[i][0], S[i][1]);
      }
    }
    for (string &s : ans)
      cout << s << endl;
  }
  return 0;
}