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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007ll
#define INF 1000000000ll
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \\
  REP(d, a.size()) {                                                           \\
    cout << a[d];                                                              \\
    if (d != a.size() - 1)                                                     \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  bool ans = false;
  while (1) {
    if ((ll)s.size() == 3 && s == "ABC") {
      ans = true;
      break;
    }
    bool a = true, b = true, c = true;
    ll cnta = 0, cntb = 0, cntc = 0;
    REP(j, (ll)s.size()) {
      if (s[j] == 'A') {
        cnta++;
        if (!(j < (ll)s.size() - 2 && s[j + 1] == 'B' && s[j + 2] == 'C'))
          a = false;
      }
      if (s[j] == 'B') {
        cntb++;
        if (!(j > 0 && j < (ll)s.size() - 1 && s[j - 1] == 'A' &&
              s[j + 1] == 'C'))
          b = false;
      }
      if (s[j] == 'C') {
        cntc++;
        if (!(j > 1 && s[j - 1] == 'B' && s[j - 2] == 'A'))
          c = false;
      }
    }
    string t = "";
    if (cnta && a && !b && !c) {
      REP(j, (ll)s.size()) {
        t += s[j];
        if (s[j] == 'A')
          j += 2;
      }
    } else if (cntb && !a && b && !c) {
      REP(j, (ll)s.size()) {
        if (j < (ll)s.size() - 1 && s[j + 1] == 'B') {
          t += 'B';
          j += 2;
        } else
          t += s[j];
      }
    } else if (cntc && !a && !b && c) {
      REP(j, (ll)s.size()) {
        if (j < (ll)s.size() - 2 && s[j + 2] == 'C') {
          t += 'C';
          j += 2;
        } else
          t += s[j];
      }
    } else
      break;
    s = t;
  }
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 1000000007ll
#define INF 1000000000ll
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \\
  REP(d, a.size()) {                                                           \\
    cout << a[d];                                                              \\
    if (d != a.size() - 1)                                                     \\
      cout << " ";                                                             \\
    else                                                                       \\
      cout << endl;                                                            \\
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \\
  sort(v.begin(), v.end());                                                    \\
  v.erase(unique(v.begin(), v.end()), v.end());
#define pb push_back

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  bool ans = false;
  while (1) {
    if ((ll)s.size() == 3 && s == "ABC") {
      ans = true;
      break;
    }
    bool a = true, b = true, c = true;
    REP(j, (ll)s.size()) {
      if (s[j] == 'A') {
        if (!(j < (ll)s.size() - 2 && s[j + 1] == 'B' && s[j + 2] == 'C'))
          a = false;
      }
      if (s[j] == 'B') {
        if (!(j > 0 && j < (ll)s.size() - 1 && s[j - 1] == 'A' &&
              s[j + 1] == 'C'))
          b = false;
      }
      if (s[j] == 'C') {
        if (!(j > 1 && s[j - 1] == 'B' && s[j - 2] == 'A'))
          c = false;
      }
    }
    string t = "";
    if (a && !b && !c) {
      REP(j, (ll)s.size()) {
        t += s[j];
        if (s[j] == 'A')
          j += 2;
      }
    } else if (!a && b && !c) {
      REP(j, (ll)s.size()) {
        if (j < (ll)s.size() - 1 && s[j + 1] == 'B') {
          t += 'B';
          j += 2;
        } else
          t += s[j];
      }
    } else if (!a && !b && c) {
      REP(j, (ll)s.size()) {
        if (j < (ll)s.size() - 2 && s[j + 2] == 'C') {
          t += 'C';
          j += 2;
        } else
          t += s[j];
      }
    } else
      break;
    s = t;
  }
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}