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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

bool check(string str) {
  if (str == "ABC")
    return true;

  string s = "";
  set<char> st;
  rep(i, str.size()) {
    if (i < str.size() - 2 && str.substr(i, 3) == "ABC") {
      s += "X";
      i += 2;
    } else {
      s += str[i];
      st.insert(str[i]);
    }
  }
  if (st.size() != 2)
    return false;
  rep(i, 3) {
    if (st.count((char)('A' + i)))
      continue;
    rep(j, s.size()) if (s[j] == 'X') s[j] = (char)('A' + i);
  }
  return check(s);
}

int main() {
  cin.sync_with_stdio(false);
  string s;
  cin >> s;

  bool ok = false;
  if (s.size() % 2) {
    ok = check(s);
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define repr(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x " = " << x << endl
#define print(x) cout << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<int, P> PIP;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef set<int> S;

#define INF INT_MAX / 3
#define MAX_N 1000000001

string s;

bool check(string str, int a[3]) {
  if (s == str)
    return true;
  if (str.size() > s.size())
    return false;

  int b[3] = {a[0], a[1], a[2]};
  string tmp;
  bool ok = false;
  rep(i, 3) {
    if (a[i] == 0)
      continue;
    tmp = str;
    rep(j, tmp.size()) {
      if (tmp[j] == (char)('A' + i)) {
        tmp.replace(j, 1, "ABC");
        j += 2;
      }
    }
    b[i]--;
    if (check(tmp, a)) {
      ok = true;
      break;
    }
    b[i]++;
  }
  return ok;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> s;

  bool ok = false;
  if (s.size() % 2) {
    int tmp = (s.size() - 3) / 2, a[3] = {tmp + 1, tmp + 1, tmp + 1};
    rep(i, s.size()) {
      if (s[i] == 'A')
        a[0]--;
      else if (s[i] == 'B')
        a[1]--;
      else
        a[2]--;
    }
    // cout << a[0] << " " <<a[1] << " " << a[2] << endl;
    ok = check("ABC", a);
  }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}