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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  string s;
  cin >> s;
  bool h = true;
  while (s != "ABC") {
    if (s.size() <= 3) {
      h = false;
      break;
    }
    int co[3] = {0};
    rep(i, s.size()) co[s[i] - 'A']++;
    int c = 0;
    rep(i, s.size() - 2) if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C')
        c++;
    if (c == 0) {
      h = false;
      break;
    }
    rep(i, 4) {
      if (i == 3) {
        s = "ABC";
        h = false;
        break;
      }
      if (co[i] == c) {
        string t;
        rep(j, s.size()) {
          if (j < s.size() - 2 && s[j] == 'A' && s[j + 1] == 'B' &&
              s[j + 2] == 'C') {
            j += 2;
            t += (char)('A' + i);
          } else
            t += s[j];
        }
        s = t;
        break;
      }
    }
  }
  if (h)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  string s;
  cin >> s;
  bool h = true;
  while (s != "ABC") {
    if (s.size() < 3) {
      h = false;
      break;
    }
    int co[3] = {0};
    rep(i, s.size()) co[s[i] - 'A']++;
    int c = 0;
    rep(i, s.size() - 2) if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C')
        c++;
    rep(i, 4) {
      if (i == 3) {
        s = "ABC";
        h = false;
        break;
      }
      if (co[i] == c) {
        string t;
        rep(j, s.size()) {
          if (j < s.size() - 2 && s[j] == 'A' && s[j + 1] == 'B' &&
              s[j + 2] == 'C') {
            j += 2;
            t += (char)('A' + i);
          } else
            t += s[j];
        }
        s = t;
        break;
      }
    }
  }
  if (h)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}