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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll>;
using tlll = tuple<ll, ll, ll>;
using vs = vector<string>;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) range(i, 0, n)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define range(i, a, n) for (int i = (a); i < (n); i++)

#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S> S acm(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
void YN(bool b) { cout << (b ? "YES" : "NO") << "\\n"; }
void Yn(bool b) { cout << (b ? "Yes" : "No") << "\\n"; }
void yn(bool b) { cout << (b ? "yes" : "no") << "\\n"; }

ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
template <class T> void puta(T &&t) { cout << t << "\\n"; }
template <class H, class... T> void puta(H &&h, T &&...t) {
  cout << h << ' ';
  puta(t...);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  bool fail = false;
  cin >> s;
  while (s.size() > 3 && (!fail)) {
    string t;
    if (s[0] == 'A' && s[1] == 'B' && s[2] == 'C') {
      rep(i, s.size()) {
        if (s[i] == 'A') {
          if (i < s.size() - 2 && s.substr(i, 3) == "ABC") {
            s[i + 1] = '*';
            s[i + 2] = '*';
          } else
            fail = true;
        }
      }
    } else if (s[s.size() - 3] == 'A' && s[s.size() - 2] == 'B' &&
               s[s.size() - 1] == 'C') {
      rep(i, s.size()) {
        if (s[i] == 'C') {
          if (i > 1 && i < s.size() && s.substr(i - 2, 3) == "ABC") {
            s[i - 1] = '*';
            s[i - 2] = '*';
          } else
            fail = true;
        }
      }
    } else {
      rep(i, s.size()) {
        if (s[i] == 'B') {
          if (i > 0 && i < s.size() - 1 && s.substr(i - 1, 3) == "ABC") {
            s[i - 1] = '*';
            s[i + 1] = '*';
          } else
            fail = true;
        }
      }
    }
    rep(i, s.size()) if (s[i] != '*') t.push_back(s[i]);
    fail |= s.size() == t.size();
    swap(s, t);
  }
  Yn(!fail && s == "ABC");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll>;
using tlll = tuple<ll, ll, ll>;
using vs = vector<string>;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define rep(i, n) range(i, 0, n)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define range(i, a, n) for (int i = (a); i < (n); i++)

#define LINF ((ll)1ll << 60)
#define INF ((int)1 << 30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout << setprecision(a) << fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

int dx[] = {1, 0, -1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S> S acm(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
void YN(bool b) { cout << (b ? "YES" : "NO") << "\\n"; }
void Yn(bool b) { cout << (b ? "Yes" : "No") << "\\n"; }
void yn(bool b) { cout << (b ? "yes" : "no") << "\\n"; }

ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
template <class T> void puta(T &&t) { cout << t << "\\n"; }
template <class H, class... T> void puta(H &&h, T &&...t) {
  cout << h << ' ';
  puta(t...);
}
template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) {
  os << "[" << p.first << ", " << p.second << "]";
  return os;
};
template <class S> auto &operator<<(ostream &os, vector<S> t) {
  bool a = 1;
  for (auto s : t) {
    os << (a ? "" : " ") << s;
    a = 0;
  }
  return os;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  bool fail = false;
  cin >> s;
  while (s.size() > 3 && !fail) {
    string t;
    if (s[0] == 'A' & s[1] == 'B' && s[2] == 'C') {
      rep(i, s.size()) {
        t.push_back(s[i]);
        if (s[i] == 'A') {
          i += 2;
          if (i >= s.size())
            fail = true;
        }
      }
    } else if (s[s.size() - 3] == 'A' & s[s.size() - 2] == 'B' &&
               s[s.size() - 1] == 'C') {
      rep(i, s.size()) {
        t.push_back(s[i]);
        if (s[i] == 'C') {
          if (t.size() < 2)
            fail = true;
          else {
            t.pop_back();
            t.pop_back();
            t.pop_back();
            t.push_back(s[i]);
          }
        }
      }
    } else {
      rep(i, s.size()) {
        t.push_back(s[i]);
        if (s[i] == 'B') {
          if (t.size() < 2)
            fail = true;
          else {
            t.pop_back();
            t.pop_back();
            t.push_back(s[i]);
            i++;
            if (i >= s.size())
              fail = true;
          }
        }
      }
    }
    swap(s, t);
    // cout<<s<<endl;
  }
  Yn(!fail && s == "ABC");
}