Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD =
    1000000000 + 7; // ;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
constexpr long long DINF = 1000000000000;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<Edge>> g;
vector<edge> ed;
vector<Pll> pv;
map<LL, LL> ma;
set<LL> st;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
LL n, m, k, p, q, w, h, ans, cnt, sum, a[3100], b[3100];
string str;
bool f[1100];
char c;
int main() {
  cin >> str;
  rep(i, str.size() - 2) {
    if (str[i] == 'A' && str[i + 1] == 'B' && str[i + 2] == 'C') {
      cnt = i;
      while (cnt >= 0) {
        if (str[cnt] == 'A')
          ans++;
        else
          break;
        cnt--;
        if (cnt < 0)
          break;
      }
      str[i] = 'B', str[i + 1] = 'C', str[i + 2] = 'A';
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD =
    1000000000 + 7; // ;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = numeric_limits<LL>::max();
constexpr long long DINF = 1000000000000;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourth;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Rrep(i, mf) for (LL i = mf - 1; i >= 0; i--)
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
struct Edge {
  LL to, cost;
};
struct edge {
  LL from, to, cost;
};
vector<vector<Edge>> g;
vector<edge> ed;
vector<Pll> pv;
map<LL, LL> ma;
set<LL> st;
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
LL n, m, k, p, q, w, h, ans, cnt, sum, a[3100], b[3100];
string str;
bool f[1100];
char c;
int main() {
  cin >> str;
  rep(i, str.size() - 1) {
    if (str[i] == 'B' && str[i + 1] == 'C') {
      ans += cnt;
      i = i + 1;
    } else if (str[i] == 'A')
      cnt++;
    else
      cnt = 0;
  }
  cout << ans << endl;
  return 0;
}