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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
int main() {
  string s;
  cin >> s;
  bool f = false;
  while (s.length() >= 3) {
    if (s == "ABC") {
      f = true;
      break;
    }
    int len = s.length();
    string memo;
    bool a[3];
    fill(a, a + 3, false);
    rep(i, len) {
      if (i + 2 < len && s.substr(i, 3) == "ABC") {
        i += 2;
      } else {
        a[s[i] - 'A'] = true;
      }
    }
    char chk;
    if (!a[0] && a[1] && a[2])
      chk = 'A';
    else if (a[0] && !a[1] && a[2])
      chk = 'B';
    else if (a[0] && a[1] && !a[2])
      chk = 'C';
    else {
      break;
    }
    rep(i, len) {
      if (i + 2 < len && s.substr(i, 3) == "ABC") {
        i += 2;
        memo += chk;
      } else {
        memo += s[i];
      }
    }
    s = memo;
  }
  if (f) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
struct alf {
  string x;
  int a, b, c;
};
alf trans(alf x, char k) {
  string res;
  int count[3] = {};
  int len = x.x.length();
  rep(i, len) {
    if (x.x[i] != k) {
      res += x.x[i];
      count[x.x[i] - 'A']++;
    } else {
      res += "ABC";
      count[0]++;
      count[1]++;
      count[2]++;
    }
  }
  return {res, count[0], count[1], count[2]};
}
int cnt[3] = {};
string s;
bool safe(alf x) {
  if (x.a > cnt[0] || x.b > cnt[1] || x.c > cnt[2])
    return false;
  return true;
}
int main() {
  cin >> s;
  bool f = false;
  int len = s.length();
  rep(i, len) { cnt[s[i] - 'A']++; }
  queue<alf> q;
  q.push({"ABC", 1, 1, 1});
  while (!q.empty()) {
    alf now = q.front();
    q.pop();
    if (now.x == s) {
      f = true;
      break;
    }
    alf A = trans(now, 'A');
    alf B = trans(now, 'B');
    alf C = trans(now, 'C');
    if (safe(A))
      q.push(A);
    if (safe(B))
      q.push(B);
    if (safe(C))
      q.push(C);
  }
  if (f)
    cout << "Yes" << endl;
  else {
    cout << "No" << endl;
  }
  return 0;
}