Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (int i = 0; i < N; ++i) {
    cin >> S[i];
  }
  if (A + B + C >= 3) {
    string seq;
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (S[i] == "AB") {
        if (A >= B) {
          --A;
          ++B;
          seq += "B";
        } else {
          --B;
          ++A;
          seq += "A";
        }
        if (A < 0 || B < 0) {
          ok = false;
          break;
        }
      }
      if (S[i] == "BC") {
        if (B >= C) {
          --B;
          ++C;
          seq += "C";
        } else {
          --C;
          ++B;
          seq += "B";
        }
        if (B < 0 || C < 0) {
          ok = false;
          break;
        }
      }
      if (S[i] == "AC") {
        if (C >= A) {
          --C;
          ++A;
          seq += "A";
        } else {
          --A;
          ++C;
          seq += "C";
        }
        if (C < 0 || A < 0) {
          ok = false;
          break;
        }
      }
    }
    if (ok) {
      cout << "Yes" << '\n';
      for (int i = 0; i < N; ++i) {
        cout << seq[i] << '\n';
      }
    } else {
      cout << "No" << '\n';
    }
  } else {
    vector<map<vector<int>, vector<int>>> dp(N + 1);
    dp[0][{A, B, C}] = {-1, -1, -1};
    for (int i = 0; i < N; ++i) {
      map<vector<int>, vector<int>>::iterator it = dp[i].begin();
      while (it != dp[i].end()) {
        vector<int> v = it->first;
        for (int j = 0; j < 3; ++j) {
          if (j == 0 && S[i] != "AB")
            continue;
          if (j == 1 && S[i] != "BC")
            continue;
          if (j == 2 && S[i] != "AC")
            continue;
          if (v[j] >= 1) {
            vector<int> w = v;
            --w[j];
            ++w[(j + 1) % 3];
            dp[i + 1][w] = v;
          }
          if (v[(j + 1) % 3] >= 1) {
            vector<int> w = v;
            --w[(j + 1) % 3];
            ++w[j];
            dp[i + 1][w] = v;
          }
        }
        ++it;
      }
    }
    if (dp[N].empty()) {
      cout << "No" << endl;
    } else {
      vector<int> p = dp[N].begin()->first;
      string seq;
      for (int i = N - 1; i >= 0; --i) {
        vector<int> np = dp[N][p];
        for (int j = 0; j < 3; ++j) {
          if (np[j] < p[j]) {
            seq += char('A' + j);
          }
        }
        p = np;
      }
      reverse(seq.begin(), seq.end());
      cout << "Yes" << '\n';
      for (int i = 0; i < N; ++i) {
        cout << seq[i] << '\n';
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (int i = 0; i < N; ++i) {
    cin >> S[i];
  }
  if (A + B + C >= 3) {
    string seq;
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (S[i] == "AB") {
        if (A >= B) {
          --A;
          ++B;
          seq += "B";
        } else {
          --B;
          ++A;
          seq += "A";
        }
        if (A < 0 || B < 0) {
          ok = false;
          break;
        }
      }
      if (S[i] == "BC") {
        if (B >= C) {
          --B;
          ++C;
          seq += "C";
        } else {
          --C;
          ++B;
          seq += "B";
        }
        if (B < 0 || C < 0) {
          ok = false;
          break;
        }
      }
      if (S[i] == "AC") {
        if (C >= A) {
          --C;
          ++A;
          seq += "A";
        } else {
          --A;
          ++C;
          seq += "C";
        }
        if (C < 0 || A < 0) {
          ok = false;
          break;
        }
      }
    }
    if (ok) {
      cout << "Yes" << '\n';
      for (int i = 0; i < N; ++i) {
        cout << seq[i] << '\n';
      }
    } else {
      cout << "No" << '\n';
    }
  } else {
    vector<map<vector<int>, vector<int>>> dp(N + 1);
    dp[0][{A, B, C}] = {-1, -1, -1};
    for (int i = 0; i < N; ++i) {
      map<vector<int>, vector<int>>::iterator it = dp[i].begin();
      while (it != dp[i].end()) {
        vector<int> v = it->first;
        for (int j = 0; j < 3; ++j) {
          if (j == 0 && S[i] != "AB")
            continue;
          if (j == 1 && S[i] != "BC")
            continue;
          if (j == 2 && S[i] != "AC")
            continue;
          if (v[j] >= 1) {
            vector<int> w = v;
            --w[j];
            ++w[(j + 1) % 3];
            dp[i + 1][w] = v;
          }
          if (v[(j + 1) % 3] >= 1) {
            vector<int> w = v;
            --w[(j + 1) % 3];
            ++w[j];
            dp[i + 1][w] = v;
          }
        }
        ++it;
      }
    }
    if (dp[N].empty()) {
      cout << "No" << endl;
    } else {
      vector<int> p = dp[N].begin()->first;
      string seq;
      for (int i = N - 1; i >= 0; --i) {
        vector<int> np = dp[i + 1][p];
        for (int j = 0; j < 3; ++j) {
          if (np[j] < p[j]) {
            seq += char('A' + j);
          }
        }
        p = np;
      }
      reverse(seq.begin(), seq.end());
      cout << "Yes" << '\n';
      for (int i = 0; i < N; ++i) {
        cout << seq[i] << '\n';
      }
    }
  }
  return 0;
}