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

#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
  void solve() {
    int n, a, b, c;
    cin >> n >> a >> b >> c;
    vector<string> ans(n);
    bool f2 = (a + b + c == 2);
    vector<string> q(n);
    for (int i = 0; i < n; i++)
      cin >> q[i];
    for (int i = 0; i < n; i++) {
      if (q[i] == "AB") {
        if (a == 1 && b == 1 && f2 && i != n - 1) {
          if (q[i + 1] == "AB" || q[i + 1] == "AC") {
            a++;
            b--;
            ans[i] = "A";
          } else {
            b++;
            a--;
            ans[i] = "B";
          }
        } else if (a == 0 && b == 0) {
          cout << "No\\n";
          return;
        } else if (a < b) {
          ans[i] = "A";
          a++;
          b--;
        } else {
          ans[i] = "B";
          b++;
          a--;
        }
      } else if (q[i] == "BC") {
        if (b == 1 && c == 1 && f2 && i != n - 1) {
          if (q[i + 1] == "AB" || q[i + 1] == "BC") {
            b++;
            c--;
            ans[i] = "B";
          } else {
            c++;
            b--;
            ans[i] = "C";
          }
        } else if (b == 0 && c == 0) {
          cout << "No\\n";
          return;
        } else if (b < c) {
          ans[i] = "B";
          b++;
          c--;
        } else {
          ans[i] = "C";
          c++;
          b--;
        }
      } else {
        if (a == 1 && c == 1 && f2 && i != n - 1) {
          if (q[i + 1] == "AB" || q[i + 1] == "AC") {
            a++;
            c--;
            ans[i] = "A";
          } else {
            c++;
            a--;
            ans[i] = "C";
          }
        } else if (a == 0 && c == 0) {
          cout << "No\\n";
          return;
        } else if (a < c) {
          ans[i] = "A";
          a++;
          c--;
        } else {
          ans[i] = "C";
          c++;
          a--;
        }
      }
    }
    cout << "Yes\\n";
    for (int i = 0; i < n; i++)
      cout << ans[i] << "\\n";

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
  int idx(int a, int b, int c) { return a << 4 | b << 2 | c; }
  void solve2(int x, int y, int z, int n) {
    vector<vector<string>> dp(n + 1, vector<string>(64));
    dp[0][idx(x, y, z)] = "_";
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < 64; j++) {
        if (dp[i][j] == "")
          continue;
        int a = j >> 4;
        int b = (j >> 2) & 3;
        int c = j & 3;
        if (s == "AB") {
          if (a > 0)
            dp[i + 1][idx(a - 1, b + 1, c)] = dp[i][j] + "B";
          if (b > 0)
            dp[i + 1][idx(a + 1, b - 1, c)] = dp[i][j] + "A";
        } else if (s == "BC") {
          if (b > 0)
            dp[i + 1][idx(a, b - 1, c + 1)] = dp[i][j] + "C";
          if (c > 0)
            dp[i + 1][idx(a, b + 1, c - 1)] = dp[i][j] + "B";
        }
        if (s == "AC") {
          if (a > 0)
            dp[i + 1][idx(a - 1, b, c + 1)] = dp[i][j] + "C";
          if (c > 0)
            dp[i + 1][idx(a + 1, b, c - 1)] = dp[i][j] + "A";
        }
      }
    }
    for (int i = 0; i < 64; i++) {
      if (dp[n][i] != "") {
        cout << "Yes\\n";
        for (int j = 1; j <= n; j++) {
          cout << dp[n][i][j] << endl;
        }
        return;
      }
    }
    cout << "No\\n";
  }

  void solve() {
    int n, a, b, c;
    cin >> n >> a >> b >> c;
    if (a + b + c == 2) {
      return solve2(a, b, c, n);
    }
    vector<string> ans(n);
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      if (s == "AB") {
        if (a == 0 && b == 0) {
          cout << "No\\n";
          return;
        }
        if (a < b) {
          ans[i] = "A";
          a++;
          b--;
        } else {
          ans[i] = "B";
          b++;
          a--;
        }
      } else if (s == "BC") {
        if (b == 0 && c == 0) {
          cout << "No\\n";
          return;
        }
        if (b < c) {
          ans[i] = "B";
          b++;
          c--;
        } else {
          ans[i] = "C";
          c++;
          b--;
        }
      } else if (s == "AC") {
        if (c == 0 && a == 0) {
          cout << "No\\n";
          return;
        }
        if (c < a) {
          ans[i] = "C";
          c++;
          a--;
        } else {
          ans[i] = "A";
          a++;
          c--;
        }
      }
    }
    cout << "Yes\\n";
    for (int i = 0; i < n; i++)
      cout << ans[i] << "\\n";

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}