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

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
// struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

#define yes puts("Yes")
#define no puts("No")

void solve(string s) {

  while (s.size() > 3) {
    bool f = false;
    for (int i = 0; i < s.size(); i++) {
      if (s.substr(i, 3) == "ABC") {
        f = true;
        break;
      }
    }
    if (!f)
      break;
    string tmp = "";
    string c;
    if (s.substr(0, 3) == "ABC") {
      c = "A";
    } else if (s.substr(s.size() - 3, 3) == "ABC") {
      c = "C";
    } else {
      f = true;
      for (int i = 1; i < s.size() - 1; i++) {
        if (s[i] == 'B') {
          if (!(s[i - 1] == 'A' and s[i + 1] == 'C')) {
            no;
            return;
          }
        }
      }
      c = "B";
    }

    for (int i = 0; i < s.size(); i++)
      if (s.substr(i, 3) == "ABC") {
        s.replace(i, 3, c);
      }
  }

  if (s == "ABC")
    yes;
  else
    no;
}

int main() {
  while (1) {
    string s;
    cin >> s;
    if (s == "0")
      break;
    // cout << s << " -> ";
    solve(s);

    break;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*{{{*/ // template
#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 29
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define all(v) ALL(v)
#define debug(x) cerr << #x << ":" << x << endl
#define debug2(x, y) cerr << #x << "," << #y ":" << x << "," << y << endl
#define CININIT cin.tie(0), ios::sync_with_stdio(false)
struct fin {
  fin() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fin_;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream &operator<<(ostream &os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

constexpr ll mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
/*}}}*/

#define yes puts("Yes")
#define no puts("No")

int main() {
  string s;
  cin >> s;
  if (s == "ABC") {
    yes;
    return 0;
  }

  while (s.size() > 3) {
    string tmp = "";
    string c;
    if (s.substr(0, 3) == "ABC") {
      c = "A";
    } else if (s.substr(s.size() - 3, 3) == "ABC") {
      c = "C";
    } else {
      c = "B";
    }

    for (int i = 0; i < s.size(); i++)
      if (s.substr(i, 3) == "ABC") {
        s.replace(i, 3, c);
      }
  }

  if (s == "ABC")
    yes;
  else
    no;
}