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

/*input
2 1 3 0
AB
AC
*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \\
                                                    |     |   )   \\
                                                    |_____|_______)

*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 2147483647;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define out(container)                                                         \\
  for (auto it : container)                                                    \\
    cout << it << " ";                                                         \\
  cout << endl;

template <typename T> T GCD(T a, T b) {
  ll t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }

const int N = 1e5 + 5;
int n;
vector<int> vec;

void fun(int ind, int a, int b, int c, string &str) {
  if (a < 0 || b < 0 || c < 0)
    return;
  if (ind == n) {
    cout << "Yes" << endl;
    for (auto it : str) {
      cout << it << endl;
    }
    exit(0);
  }
  if (vec[ind] == 1) { // AB
    str += "A";
    fun(ind + 1, a + 1, b - 1, c, str);
    str.pop_back();
    str += "B";
    fun(ind + 1, a - 1, b + 1, c, str);
    str.pop_back();
  } else if (vec[ind] == 2) { // BC
    str += "C";
    fun(ind + 1, a, b - 1, c + 1, str);
    str.pop_back();
    str += "B";
    fun(ind + 1, a, b + 1, c - 1, str);
    str.pop_back();
  } else { // AC
    str += "A";
    fun(ind + 1, a + 1, b, c - 1, str);
    str.pop_back();
    str += "C";
    fun(ind + 1, a - 1, b, c + 1, str);
    str.pop_back();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int a, b, c;
  cin >> n >> a >> b >> c;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "AB")
      vec.pb(1);
    else if (str == "BC")
      vec.pb(2);
    else
      vec.pb(3);
  }
  str = "";
  fun(0, a, b, c, str);
  cout << "No" << endl;

  return 0;
}


Error: TLE

Fixed submission:

/*input
2 1 3 0
AB
AC
*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \\
                                                    |     |   )   \\
                                                    |_____|_______)

*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
int mod = 1000000007;
const int inf = 2147483647;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define out(container)                                                         \\
  for (auto it : container)                                                    \\
    cout << it << " ";                                                         \\
  cout << endl;

template <typename T> T GCD(T a, T b) {
  ll t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T> string toString(T a) { return to_string(a); }
template <typename T> void toInt(string s, T &x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, ll b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1)
      x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }

const int N = 1e5 + 5;
int n;
vector<int> vec;
map<string, int> mp[N];

void fun(int ind, int a, int b, int c, string str) {
  if (a < 0 || b < 0 || c < 0)
    return;
  if (ind == n) {
    cout << "Yes" << endl;
    for (auto it : str) {
      cout << it << endl;
    }
    exit(0);
  }
  if (vec[ind] == 1) { // AB
    fun(ind + 1, a + 1, b - 1, c, str + "A");
    fun(ind + 1, a - 1, b + 1, c, str + "B");
  } else if (vec[ind] == 2) { // BC
    fun(ind + 1, a, b - 1, c + 1, str + "C");
    fun(ind + 1, a, b + 1, c - 1, str + "B");
  } else { // AC
    fun(ind + 1, a + 1, b, c - 1, str + "A");
    fun(ind + 1, a - 1, b, c + 1, str + "C");
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int a, b, c;
  cin >> n >> a >> b >> c;
  string str;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "AB")
      vec.pb(1);
    else if (str == "BC")
      vec.pb(2);
    else
      vec.pb(3);
  }
  fun(0, a, b, c, "");
  cout << "No" << endl;

  return 0;
}