Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rt "\n"
#define sp " "
#define test(n) cout << "test" << n << endl;
#define fixsp(n) fixed << setprecision(n)
#define defsp(n) defaultfloat << setprecision(n)
#define kyopro                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL)
#define MOD 1000000007
#define iikae auto &
#define vector3D(K, N, M, DP)                                                  \
  vector<vector<vector<ll>>> DP(K, vector<vector<ll>>(N, vector<ll>(M)));
#define vector4D(K, N, M, O, DP)                                               \
  vector<vector<vector<vector<ll>>>> DP(                                       \
      K, vector<vector<vector<ll>>>(N, vector<vector<ll>>(M, vector<ll>(O))));
#define zenbu(a) a.begin(), a.end()
#define rzenbu(a) a.rbegin(), a.rend()
#define rep(i, s, N) for (ll i{s}; i < N; i++)
#define rem(i, N, s) for (ll i{N}; i > s; i--)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
template <typename T> T gcd(T a, T b) { return b != 0 ? gcd(b, a % b) : a; }
template <typename T> struct UnionFind {
  vector<T> par;
  UnionFind(T n) : par(n, -1) {}
  void init(T n) { par.assign(n, -1); }
  T root(T x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  bool issame(T x, T y) { return root(x) == root(y); }
  bool merge(T x, T y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
template <typename T> ll combpm(T N_, T C_) {
  const int NUM_ = 400001;
  static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD;
}

/* ここからコード開始 */

int main() {
  kyopro;
  string s;
  cin >> s;
  ll exist{1}, sum{};
  while (exist) {
    exist = 0;
    rep(i, 0, max((ll)s.size(), 0LL) - 2) {
      if (s.substr(i, 3) == "ABC") {
        s.replace(i, 3, "BCA");
        exist = 1;
        sum++;
      }
    }
  }
  cout << sum << rt;
}

Fixed submission:
#include <bits/stdc++.h>
#define rt "\n"
#define sp " "
#define test(n) cout << "test" << n << endl;
#define fixsp(n) fixed << setprecision(n)
#define defsp(n) defaultfloat << setprecision(n)
#define kyopro                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL)
#define MOD 1000000007
#define iikae auto &
#define vector3D(K, N, M, DP)                                                  \
  vector<vector<vector<ll>>> DP(K, vector<vector<ll>>(N, vector<ll>(M)));
#define vector4D(K, N, M, O, DP)                                               \
  vector<vector<vector<vector<ll>>>> DP(                                       \
      K, vector<vector<vector<ll>>>(N, vector<vector<ll>>(M, vector<ll>(O))));
#define zenbu(a) a.begin(), a.end()
#define rzenbu(a) a.rbegin(), a.rend()
#define rep(i, s, N) for (ll i{s}; i < N; i++)
#define rem(i, N, s) for (ll i{N}; i > s; i--)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
template <typename T> T gcd(T a, T b) { return b != 0 ? gcd(b, a % b) : a; }
template <typename T> struct UnionFind {
  vector<T> par;
  UnionFind(T n) : par(n, -1) {}
  void init(T n) { par.assign(n, -1); }
  T root(T x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  bool issame(T x, T y) { return root(x) == root(y); }
  bool merge(T x, T y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
};
template <typename T> ll combpm(T N_, T C_) {
  const int NUM_ = 400001;
  static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD;
}

/* ここからコード開始 */

int main() {
  kyopro;
  string s;
  cin >> s;
  rep(i, 0, (ll)s.size() - 1) {
    if (s.substr(i, 2) == "BC") {
      s.replace(i, 2, "X");
    }
  }
  ll sum{}, bA{};
  rep(i, 0, (ll)s.size()) {
    if (s[i] == 'A') {
      bA++;
    } else if (s[i] == 'X') {
      sum += bA;
    } else {
      bA = 0;
    }
  }
  cout << sum << rt;
}