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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long int ll;

void solve1(ll a, ll b, ll c, const vector<string> &inputs, vector<char> &ans) {
  vector<char> f(0);
  for (int i = 0; i < (int)inputs.size(); i++) {
    if (inputs[i] == "AB") {
      if (a == b && i < (int)inputs.size() - 1) {
        if (inputs[i + 1] == "AC") {
          a++;
          b--;
          ans.push_back('A');
        } else {
          a--;
          b++;
          ans.push_back('B');
        }
      } else if (a > b) {
        a--;
        b++;
        ans.push_back('B');
      } else {
        a++;
        b--;
        ans.push_back('A');
      }
    } else if (inputs[i] == "AC") {
      if (a == c && i < (int)inputs.size() - 1) {
        if (inputs[i + 1] == "AB") {
          a++;
          c--;
          ans.push_back('A');
        } else {
          a--;
          c++;
          ans.push_back('C');
        }
      } else if (a > c) {
        a--;
        c++;
        ans.push_back('C');
      } else {
        a++;
        c--;
        ans.push_back('A');
      }
    } else {
      if (b == c && i < (int)inputs.size() - 1) {
        if (inputs[i + 1] == "AB") {
          b++;
          c--;
          ans.push_back('B');
        } else {
          b--;
          c++;
          ans.push_back('C');
        }
      } else if (b > c) {
        b--;
        c++;
        ans.push_back('C');
      } else {
        b++;
        c--;
        ans.push_back('B');
      }
    }
    if (a < 0 || b < 0 || c < 0) {
      ans = f;
      return;
    }
  }
  return;
}

void solve2(ll a, ll b, ll c, const vector<string> &inputs, vector<char> &ans) {
  vector<char> f(0);
  for (int i = 0; i < (int)inputs.size(); i++) {
    if (inputs[i] == "AB") {
      if (a > b) {
        a--;
        b++;
        ans.push_back('B');
      } else {
        a++;
        b--;
        ans.push_back('A');
      }
    } else if (inputs[i] == "AC") {
      if (a > c) {
        a--;
        c++;
        ans.push_back('C');
      } else {
        a++;
        c--;
        ans.push_back('A');
      }
    } else {
      if (b > c) {
        b--;
        c++;
        ans.push_back('C');
      } else {
        b++;
        c--;
        ans.push_back('B');
      }
    }
    if (a < 0 || b < 0 || c < 0) {
      ans = f;
      return;
    }
  }
}

int main() {
  ll n, a, b, c;
  cin >> n;
  cin >> a >> b >> c;
  vector<string> inputs(n);
  for (int i = 0; i < n; i++) {
    cin >> inputs[i];
  }
  vector<char> ans;
  if (a + b + c == 2) {
    solve1(a, b, c, inputs, ans);
  } else {
    solve2(a, b, c, inputs, ans);
  }
  if (ans.size() == 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
    for (int i = 0; i < (int)ans.size(); i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long int ll;

int main() {
  int n;
  cin >> n;
  ll a, b, c;
  cin >> a >> b >> c;
  vector<string> ops(n, "");
  for (int i = 0; i < n; i++) {
    cin >> ops[i];
  }
  map<tuple<ll, ll, ll>, string> memo;

  memo[make_tuple(a, b, c)] = "";
  for (int i = 0; i < n; i++) {
    map<tuple<ll, ll, ll>, string> next;
    for (auto it = memo.begin(); it != memo.end(); it++) {
      ll tmpa = get<0>(it->first);
      ll tmpb = get<1>(it->first);
      ll tmpc = get<2>(it->first);
      string value = it->second;
      if (ops[i] == "AB") {
        if (tmpa > 0) {
          auto key = make_tuple(tmpa - 1, tmpb + 1, tmpc);
          auto newValue = value + 'B';
          if (next.find(key) == next.end()) {
            next[key] = newValue;
          }
        }
        if (tmpb > 0) {
          auto key = make_tuple(tmpa + 1, tmpb - 1, tmpc);
          auto newValue = value + 'A';
          if (next.find(key) == next.end()) {
            next[key] = newValue;
          }
        }
      } else if (ops[i] == "AC") {
        if (tmpa > 0) {
          auto key = make_tuple(tmpa - 1, tmpb, tmpc + 1);
          auto newValue = value + 'C';
          if (next.find(key) == next.end()) {
            next[key] = newValue;
          }
        }
        if (tmpc > 0) {
          auto key = make_tuple(tmpa + 1, tmpb, tmpc - 1);
          auto newValue = value + 'A';
          if (next.find(key) == next.end()) {
            next[key] = newValue;
          }
        }
      } else {
        if (tmpb > 0) {
          auto key = make_tuple(tmpa, tmpb - 1, tmpc + 1);
          auto newValue = value + 'C';
          if (next.find(key) == next.end()) {
            next[key] = newValue;
          }
        }
        if (tmpc > 0) {
          auto key = make_tuple(tmpa, tmpb + 1, tmpc - 1);
          auto newValue = value + 'B';
          if (next.find(key) == next.end()) {
            next[key] = newValue;
          }
        }
      }
    }
    memo = next;
  }
  if (memo.size() > 0) {
    cout << "Yes" << endl;
    auto it = memo.begin();
    string ans = it->second;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << "No" << endl;
  }
  return 0;
}