Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i, n) FOR(i, 0LL, n)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define rrep(i, n) RFOR(i, 0LL, n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define COUNT(a, y, x) upper_bound(all(a), y) - lower_bound(all(a), x)
#define UNIQUE(a)                                                              \
  sort(all(a));                                                                \
  a.erase(unique(all(a)), a.end())
#define pb push_back
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef map<ll, ll> MAP;
const ll inf = 1LL << 61;
const ll mod = 1000000007LL;
// const ll mod = 998244353LL;

ll n = 0, m = 0, ans = 0, tmp = 0, ma = -inf, mi = inf;
string s;
bool ok;
ll dx[9] = {0, 1, 0, -1, 0, 1, 1, -1, -1},
   dy[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
#define endl '\n'
#pragma endregion
#define MAX 222222

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;
  ll len = s.size();
  FOR(i, 0, len - 2) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      ans++;
      s[i] = 'B';
      s[i + 1] = 'C';
      s[i + 2] = 'A';
      if (i && s[i - 1] == 'A') {
        i -= 2;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i, n) FOR(i, 0LL, n)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define rrep(i, n) RFOR(i, 0LL, n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define COUNT(a, y, x) upper_bound(all(a), y) - lower_bound(all(a), x)
#define UNIQUE(a)                                                              \
  sort(all(a));                                                                \
  a.erase(unique(all(a)), a.end())
#define pb push_back
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef map<ll, ll> MAP;
const ll inf = 1LL << 61;
const ll mod = 1000000007LL;
// const ll mod = 998244353LL;

ll n = 0, m = 0, ans = 0, tmp = 0, ma = -inf, mi = inf;
string s;
bool ok;
ll dx[9] = {0, 1, 0, -1, 0, 1, 1, -1, -1},
   dy[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
#define endl '\n'
#pragma endregion
#define MAX 222222

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;
  ll len = s.size();
  ll cnt = 0;
  RFOR(i, 0, len) {
    if (s[i] == 'A')
      ans += cnt;
    else if (s[i] == 'B') {
      cnt = 0;
    } else {
      if (i && s[i - 1] == 'B') {
        cnt++;
        i--;
      } else {
        cnt = 0;
      }
    }
  }
  cout << ans << endl;

  return 0;
}