Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *    Author: "Robin Singh" !!
 *    rob_in_1
 **/

#include <bits/stdc++.h>
using namespace std;

#define dbg(args...)                                                           \
  {                                                                            \
    vector<string> _v = split(#args, ',');                                     \
    err(_v.begin(), args);                                                     \
    cerr << '\n';                                                              \
  }
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\t';
  err(++it, args...);
}

#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define MP make_pair
#define szz(v) ((int)(v).size())
#define fr(i, j, k) for (int i = j; i < k; i++)
#define bk(i, j, k) for (int i = j - 1; i >= k; i--)
#define mem(ptr, val) memset(ptr, val, sizeof ptr)
#define sbit(n) __builtin_popcount(n)
#define nl "\n"

typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
const long double eps = 1e-9;
const int mod = 1e9 + 7;

pii dir[8] = {{-1, 0}, {1, 0},   {0, -1}, {0, 1},
              {1, 1},  {-1, -1}, {-1, 1}, {1, -1}};

//*********************************!Template Ends
//Here!**********************************//

const int nn = 2e5 + 5;

void robin() {
  string s;
  cin >> s;
  int ans = 0, n = szz(s);

  for (int i = n - 3; i >= 0; i--) {
    if (s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C') {
      ans++;
      s[i] = 'B';
      s[i + 1] = 'C';
      s[i + 2] = 'A';
      i = i + 3;
    }
  }
  cout << ans;
}

int32_t main() {
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    // cout << "Case #" << tc << ": ";
    robin();
  }
  return 0;
}

Fixed submission:
/**
 *    Author: "Robin Singh" !!
 *    rob_in_1
 **/

#include <bits/stdc++.h>
using namespace std;

#define dbg(args...)                                                           \
  {                                                                            \
    vector<string> _v = split(#args, ',');                                     \
    err(_v.begin(), args);                                                     \
    cerr << '\n';                                                              \
  }
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\t';
  err(++it, args...);
}

#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define MP make_pair
#define szz(v) ((int)(v).size())
#define fr(i, j, k) for (int i = j; i < k; i++)
#define bk(i, j, k) for (int i = j - 1; i >= k; i--)
#define mem(ptr, val) memset(ptr, val, sizeof ptr)
#define sbit(n) __builtin_popcount(n)
#define nl "\n"

typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
const long double eps = 1e-9;
const int mod = 1e9 + 7;

pii dir[8] = {{-1, 0}, {1, 0},   {0, -1}, {0, 1},
              {1, 1},  {-1, -1}, {-1, 1}, {1, -1}};

//*********************************!Template Ends
//Here!**********************************//

const int nn = 2e5 + 5;

void robin() {
  string s;
  cin >> s;
  int ans = 0, n = szz(s);
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (i - 1 >= 0 and s[i] == 'C' and s[i - 1] == 'B') {
      cnt++;
      i--;
    } else if (cnt and s[i] == 'A') {
      ans += cnt;
    } else
      cnt = 0;
  }
  cout << ans;
}

int32_t main() {
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    // cout << "Case #" << tc << ": ";
    robin();
  }
  return 0;
}