prithivMLmods/Coder-Stat huggingface dataset:
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < s.length() - 1; i++) {
    if (s[i] != 'A')
      continue;
    int p = i + 1;
    while (p < s.length() - 1) {
      assert(p <= s.length() - 2);
      if (s[p] == 'B' && s[p + 1] == 'C') {
        ans++;
        p++;
      } else if (s[p] != 'A')
        break;
      p++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  long long ans = 0;
  vector<int> count(s.length());
  int t = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    if (i == s.length() - 1)
      count[i] = 0;
    else if (s[i] == 'B' && s[i + 1] == 'C')
      t++;
    else if (s[i] == 'A' || (s[i] == 'C' && s[i - 1] == 'B' && i >= 2))
      count[i] = t;
    else
      t = 0;
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'A')
      ans += count[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5, mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int ans = 0, ls = s.size(), p = s.find("BC", 0);
  while (p != string::npos) {
    s.erase(p, 2);
    int t = p - 1;
    while (t >= 0 && s[t] == 'A')
      --t, ++ans;
    s.insert(t + 1, "BC");
    p = s.find("BC", p + 2);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5, mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int ls = s.size(), lm = 0;
  ll ans = 0;
  for (int i = 0; i < ls - 1; ++i) {
    if (s[i] == 'B' && s[i + 1] == 'C') {
      ans += i - lm;
      ++lm, ++lm, ++i;
    } else if (s[i] != 'A')
      lm = i + 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll h[200010], n, tt;
char s[200010];
bool bo[200010];

inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x;
}

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  tt = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == 'A') {
      tt++;
      int id = 0;
      for (int j = i + 1; j <= n - 1; j += 2)
        if (s[j] == 'B' && s[j + 1] == 'C')
          h[tt]++;
        else {
          id = j;
          break;
        }
      if (id && s[id] == 'A')
        bo[tt] = true;
    }
  for (int i = tt - 1; i; i--)
    if (bo[i])
      h[i] += h[i + 1];
  ll ans = 0;
  for (int i = 1; i <= tt; i++)
    ans += h[i];
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll h[200010], n, tt;
char s[200010];
bool bo[200010];

inline int rd() {
  int x = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    ;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x;
}

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  tt = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == 'A') {
      tt++;
      int id = 0;
      for (int j = i + 1; j <= n - 1; j += 2)
        if (s[j] == 'B' && s[j + 1] == 'C')
          h[tt]++;
        else {
          id = j;
          break;
        }
      if (id && s[id] == 'A')
        bo[tt] = true;
    }
  for (int i = tt - 1; i > 0; i--)
    if (bo[i])
      h[i] += h[i + 1];
  ll ans = 0;
  for (int i = 1; i <= tt; i++)
    ans += h[i];
  printf("%lld\n", ans);
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main() {
  string s;
  cin >> s;

  int cnt = 0, flag = 0, sz = s.size();
  queue<int> que;
  for (int i = 0; i < sz - 2; i++) {
    if (s.substr(i, 3) == "ABC")
      que.push(i);
  }
  /*while(flag==0){
      flag=1;
      for(int i=0; i<sz-2; i++){
          if(s.substr(i,3)=="ABC"){
              cnt++;
              flag=0;
              s[i]='B';s[i+1]='C';s[i+2]='A';
              i++;
          }
      }
  }*/
  while (!que.empty()) {
    int ind = que.front();
    que.pop();
    cnt++;
    s[ind] = 'B', s[ind + 1] = 'C', s[ind + 2] = 'A';
    if (ind > 0) {
      if (s[ind - 1] == 'A')
        que.push(ind - 1);
    }
    if (ind + 4 < sz) {
      if (s.substr(ind + 3, 2) == "BC")
        que.push(ind + 2);
    }
  }
  cout << cnt;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int main() {
  string s;
  cin >> s;
  ll cnt = 0;
  int sz = s.size();
  int numa1 = 0; // iまでに連なるaの数
  int numa2 = 0; // iがbのとき前に連なるaの数
  for (int i = 0; i < sz; i++) {
    // 最後がbのときcがきたらnuma2を足す
    if (s[i] == 'A') {
      numa1++;
      numa2 = 0;
    } else if (s[i] == 'B') {
      numa2 = numa1;
      numa1 = 0;
    } else {
      numa1 = numa2;
      cnt += numa2;
      numa2 = 0;
      if (i > 0 && s[i - 1] != 'B')
        numa1 = 0;
    }
  }
  cout << cnt;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long a[100];
int main() {
  string s;
  cin >> s;
  int n = s.size();
  int ans = 0, co = 0;
  for (int i = n - 2; i >= 0; i--) {
    if (s.substr(i, 2) == "BC") {
      for (int j = i - 1; j >= 0; j--) {
        if (j == 0) {
          if (s[j] == 'A') {
            co++;
          }
          ans += co;
          co = 0;
          break;
        }
        if (s[j] == 'A') {
          co++;
        } else {
          if (s.substr(j - 1, 2) == "BC") {
            j--;
            continue;
          } else {
            ans += co;
            co = 0;
            break;
          }
        }
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long a[100];
int main() {
  string s;
  cin >> s;
  long long n = s.size();
  long long ans = 0, co = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'A') {
      co++;
    } else if (s.substr(i, 2) == "BC") {
      ans += co;
      i++;
      continue;
    } else {
      co = 0;
    }
  }
  cout << ans << endl;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

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

  string s;
  cin >> s;
  int i = 0, ans = 0;
  while (i < (int)s.size() - 2) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      s[i] = 'B';
      s[i + 1] = 'C';
      s[i + 2] = 'A';
      ans++;
      if (i > 0)
        i--;
    } else {
      i++;
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

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

  string s;
  cin >> s;
  int A = 0, B = 0;
  ll ans = 0;
  REP(i, s.size()) {
    if (s[i] == 'A') {
      if (B == 0)
        A++;
      else {
        B = 0;
        A = 1;
      }
    }
    if (s[i] == 'B') {
      if (B == 0 && A > 0)
        B++;
      else if (B == 1)
        A = 0;
    }
    if (s[i] == 'C') {
      if (B == 0)
        A = 0;
      else {
        B = 0;
        ans += A;
      }
    }
  }
  cout << ans << '\n';
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  cin >> s;

  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] == 'B' && s[i + 1] == 'C') {
      s.replace(i, 2, 1, 'S');
    }
  }

  int count = 0;
  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'S') {
      char tmp = s[i];
      s[i] = s[i + 1];
      s[i + 1] = tmp;
      i = -1;
      count++;
    }
  }

  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  cin >> s;

  for (int i = 0; i < s.size() - 1; i++) {
    if (s[i] == 'B' && s[i + 1] == 'C') {
      s.replace(i, 2, 1, 'S');
    }
  }

  long int count = 0;
  long int a_count = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'S')
      count += a_count;
    else if (s[i] == 'A')
      a_count++;
    else
      a_count = 0;
  }

  cout << count << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long arr[1000000];
int main() {
  string s;
  long long n, i, j, k, m, x, y, z;
  cin >> s;
  x = 0;
  y = 0;
  i = 0;
  while (i < s.length() - 1) {
    if (s[i] == 'A') {
      x++;
      i++;
    } else if (s[i] == 'B' && s[i + 1] == 'C') {
      if (x > 0) {
        y = x + y;
        x = 1;
        i = i + 2;
      } else {
        i = i + 2;
      }
    } else {
      x = 0;
      i++;
    }
  }
  i = s.length() - 1;
  while (i >= 0) {
    if (s[i] == 'A') {
      x++;
      i++;
    } else if (s[i] == 'B' && s[i + 1] == 'C') {
      if (x > 0) {
        y = x + y;
        x = x;
        i = i + 2;
      } else {
        i = i + 2;
      }
    } else {
      x = 0;
      i++;
    }
  }
  cout << y << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long arr[1000000];
int main() {
  string s;
  long long n, i, j, k, m, x, y, z;
  cin >> s;
  x = 0;
  y = 0;
  i = 0;
  while (i < s.length() - 1) {
    if (s[i] == 'A') {
      x++;
      i++;
    } else if (s[i] == 'B' && s[i + 1] == 'C') {
      if (x > 0) {
        y = x + y;
        x = x;
        i = i + 2;
      } else {
        i = i + 2;
      }
    } else {
      x = 0;
      i++;
    }
  }
  cout << y << endl;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define reps(i, a, b) for (long long int i = a; i < b; i++)
#define rep(i, a) for (long long int i = 0; i < a; i++)
typedef long long int ll;
using namespace std;
ll MOD = 1000000007;

const int MAX = 510000;

struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

int main() {
  string S;
  cin >> S;
  vector<vector<int>> a(100000);
  ll ans = 0;
  int imabasho = 0;
  rep(i, S.size()) {
    if (S[i] == 'A') {
      a[imabasho].push_back(1);
    } else {
      if (S[i] == 'B' && S[i + 1] == 'C') {
        a[imabasho].push_back(2);
        i++;
      } else {
        if (S[i] == 'B') {
          imabasho++;
        } else {
          imabasho++;
        }
      }
    }
  }

  rep(i, imabasho + 1) {
    int count = 0;
    rep(j, a[i].size()) {
      if (a[i][j] == 2) {
        ans += ll(j) - count;
        count++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define reps(i, a, b) for (long long int i = a; i < b; i++)
#define rep(i, a) for (long long int i = 0; i < a; i++)
typedef long long int ll;
using namespace std;
ll MOD = 1000000007;

const int MAX = 510000;

struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

int main() {
  string S;
  cin >> S;
  vector<vector<int>> a(200000);
  ll ans = 0;
  int imabasho = 0;
  rep(i, S.size()) {
    if (S[i] == 'A') {
      a[imabasho].push_back(1);
    } else {
      if (S[i] == 'B' && S[i + 1] == 'C') {
        a[imabasho].push_back(2);
        i++;
      } else {
        if (S[i] == 'B') {
          imabasho++;
        } else {
          imabasho++;
        }
      }
    }
  }

  rep(i, imabasho + 1) {
    int count = 0;
    rep(j, a[i].size()) {
      if (a[i][j] == 2) {
        ans += ll(j) - count;
        count++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long
#define S second
#define F first

typedef long long ll;
typedef long double ld;

using namespace std;

const int INF = 1e9;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

int n;
int A[maxn];
string s;
vector<int> v;
int dp[maxn];

signed main() {
  cin >> s;
  n = s.size();
  if (s[0] == 'A')
    A[0] = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == 'C' && s[i - 1] == 'B')
      v.push_back(i);
    if (s[i] == 'A')
      A[i] = A[i - 1] + 1;
  }

  if (v.empty())
    return cout << 0, 0;

  ll ans = 0;
  dp[0] = A[v[0] - 2];
  ans += dp[0];
  for (int i = 1; i < v.size(); i++) {
    dp[i] = A[v[i] - 2];
    if (A[v[i] - 2] == v[i] - v[i - 1] - 2)
      dp[i] += dp[i - 1];
    ans += dp[i];
  }

  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>

#define int long long
#define S second
#define F first

typedef long long ll;
typedef long double ld;

using namespace std;

const int INF = 1e9;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;

int n;
int A[maxn];
string s;
vector<int> v;
int dp[maxn];

signed main() {
  cin >> s;
  n = s.size();
  if (s[0] == 'A')
    A[0] = 1;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == 'C' && s[i - 1] == 'B')
      v.push_back(i);
    if (s[i] == 'A')
      A[i] = A[i - 1] + 1;
  }

  if (v.empty())
    return cout << 0, 0;

  ll ans = 0;
  dp[0] = A[v[0] - 2];
  ans += dp[0];
  for (int i = 1; i < v.size(); i++) {
    dp[i] = A[v[i] - 2];
    if (A[v[i] - 2] == v[i] - v[i - 1] - 2)
      dp[i] += dp[i - 1];
    ans += dp[i];
  }

  cout << ans;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define pb push_back
#define sz size
#define f first
#define s second

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string s;
  cin >> s;
  ll a, t;
  a = t = 0;
  F0R(i, (int)s.size() - 1) {
    if (s[i] == 'A')
      a++;
    else if (s[i] == 'B' && s[i + 1] == 'C') {
      i++;
      t += a;
    } else
      a = 0;
  }
  cout << t;

  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define pb push_back
#define sz size
#define f first
#define s second

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  string s;
  cin >> s;
  ll a, t;
  a = t = 0;
  F0R(i, (int)s.size() - 1) {
    if (s[i] == 'A')
      a++;
    else if (s[i] == 'B' && s[i + 1] == 'C') {
      i++;
      t += a;
    } else
      a = 0;
  }
  cout << t;

  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int ans = 0;
  bool ck = true;
  while (ck) {
    ck = false;
    for (int i = 0; i < n - 2; i++) {
      if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
        ans++;
        ck = true;
        s[i] = 'B';
        s[i + 1] = 'C';
        s[i + 2] = 'A';
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  ll ans = 0;
  ll cnt = 0;
  for (int i = 0; i < n - 2; i++) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      ans += cnt + 1;
      s[i + 2] = 'A';
      i++;
    } else if (s[i] == 'A') {
      cnt++;
    } else {
      cnt = 0;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef double db;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define pb push_back
#define mp make_pair
#define MAX 1000000
#define mod 1000000007
#define all(_) _.begin(), _.end()
#define F first
#define S second
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n, i, ans = 0;
  string s;
  cin >> s;
  n = s.size();
  char prev = 'Z';
  i = 0;
  while (i < n) {
    if (s[i] == 'A') {
      ll j = i;
      while (j < n) {
        if (prev == 'A' && s[j] == 'C')
          break;
        if (prev == 'C' && s[j] == 'C')
          break;
        if ((prev == 'B' && s[j] == 'B') || (prev == 'B' && s[j] == 'A'))
          break;
        prev = s[j++];
      }
      ll ca = 0;
      while (i < j) {
        if (s[i] == 'A')
          ca++;
        if (s[i] == 'C')
          ans += ca;
        i++;
      }
    } else
      i++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef double db;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define pb push_back
#define mp make_pair
#define MAX 1000000
#define mod 1000000007
#define all(_) _.begin(), _.end()
#define F first
#define S second
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  ll n, i, ans = 0;
  string s;
  cin >> s;
  n = s.size();
  char prev = 'Z';
  i = 0;
  while (i < n) {
    if (s[i] == 'A') {
      ll j = i;
      while (j < n) {
        if (prev == 'A' && s[j] == 'C')
          break;
        if (prev == 'C' && s[j] == 'C')
          break;
        if ((prev == 'B' && s[j] == 'B') || (prev == 'B' && s[j] == 'A'))
          break;
        prev = s[j++];
      }
      ll ca = 0;
      while (i < j) {
        if (s[i] == 'A')
          ca++;
        if (s[i] == 'C')
          ans += ca;
        i++;
      }
    } else
      i++;
    prev = 'Z';
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces1148C
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ll x, ll y, ll a, ll b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (int k = 0; k < n; k++) {
    cout << a[k] << " ";
  }
  cerr << "\n";
}
ll mod = 1000000007;

int main() {
  char s[222222];
  int n;
  scanf("%s%n", s, &n);
  ll ans = 0;
  ll flag = 0;
  for (int k = 0; k < n - 2; k++) {
    if (s[k] == 'A' && s[k + 1] == 'B' && s[k + 2] == 'C') {
      ans++;
      if (flag) {
        ans += flag;
      } else {
        ll cur = k - 1;
        while (cur >= 0 && s[cur] == 'A') {
          s[cur + 2] = 'A';
          s[cur] = 'B';
          s[cur + 1] = 'C';
          cur--;
          ans++;
          flag++;
        }
        s[k + 2] = 'A';
      }
    } else {
      flag = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
/* بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ */
// codeforces1148C
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define mp make_pair
#define pb push_back
void parseArray(ll *A, ll n) {
  for (ll K = 0; K < n; K++) {
    cin >> A[K];
  }
}
ll modInverse(ll a, ll b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ld dist(ll x, ll y, ll a, ll b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(ll *a, ll n) {
  for (int k = 0; k < n; k++) {
    cout << a[k] << " ";
  }
  cerr << "\n";
}
ll mod = 1000000007;

int main() {
  char s[222222];
  int n;
  scanf("%s%n", s, &n);
  ll ans = 0;
  ll flag = 0;
  for (int k = 0; k < n - 1; k++) {
    if (s[k] == 'A') {
      flag++;
    } else if (s[k] == 'B' && s[k + 1] == 'C') {
      ans += flag;
      k++;
    } else {
      flag = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int main() {
  // cout.precision(10);
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  S = "###" + S + "###";
  int ans = 0;
  queue<int> que;
  for (int i = 0; i < S.size(); i++) {
    if (S.substr(i, 3) == "ABC")
      que.push(i);
  }
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    ans++;
    S[now] = 'B';
    S[now + 1] = 'C';
    S[now + 2] = 'A';
    if (S.substr(now - 1, 3) == "ABC")
      que.push(now - 1);
    if (S.substr(now + 2, 3) == "ABC")
      que.push(now + 2);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int main() {
  // cout.precision(10);
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  S = S + "###";
  ll ans = 0;
  ll nowa = 0;
  for (ll i = 0; i < S.size(); i++) {
    if (S[i] == 'A') {
      nowa++;
      continue;
    }
    if (S[i] == 'B' && S.substr(i, 2) == "BC") {
      ans += nowa;
      i++;
      continue;
    }
    nowa = 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string S;
  cin >> S;

  int N = S.size();
  int ans = 0;
  for (int i = N - 3; i >= 0; i--) {
    if (S[i] == 'A' && S[i + 1] == 'B' && S[i + 2] == 'C') {
      ans++;
      S[i] = 'B';
      S[i + 1] = 'C';
      S[i + 2] = 'A';
      int ii = i + 2;
      while (ii + 2 < N && S[ii] == 'A' && S[ii + 1] == 'B' &&
             S[ii + 2] == 'C') {
        ans++;
        S[ii] = 'B';
        S[ii + 1] = 'C';
        S[ii + 2] = 'A';
        ii += 2;
      }
    }
  }

  cout << ans << endl;
}


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

int main() {
  string S;
  cin >> S;

  int N = S.size();
  string s;
  for (int i = 0; i < N; i++) {
    if (i + 1 < N && S[i] == 'B' && S[i + 1] == 'C') {
      s += 'D';
      i++;
    } else
      s += S[i];
  }
  int n = s.size();
  int a = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'A')
      a++;
    else if (s[i] == 'D')
      ans += a;
    else
      a = 0;
  }

  cout << ans << endl;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  string s;
  getline(cin, s);
  vector<char> SS;
  for (auto i = 0; i < s.size() - 1; i++) {
    if (s[i] == 'A') {
      SS.push_back('A');
    } else {
      if (s[i] == 'B' && s[i + 1] == 'C') {
        SS.push_back('D');
        i++;
      } else {
        if (SS.back() != '|')
          SS.push_back('|');
      }
    }
  }

  stack<int> mys;

  SS.push_back('|');

  long long cc = 0;

  for (auto i = 0; i < SS.size(); i++) {
    auto c = SS[i];
    if (c == '|') {
      auto j = i - 1;
      while (!mys.empty()) {
        auto t = mys.top();
        cc += (j - t);
        mys.pop();
        j--;
      }
      continue;
    }

    if (c == 'A')
      mys.push(i);
  }

  cout << cc << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <stack>
#include <stdio.h>
#include <vector>

using namespace std;

int main() {
  string s;
  getline(cin, s);
  vector<char> SS;
  for (auto i = 0; i < s.size() - 1; i++) {
    if (s[i] == 'A') {
      SS.push_back('A');
    } else {
      if (s[i] == 'B' && s[i + 1] == 'C') {
        SS.push_back('D');
        i++;
      } else {
        if (SS.size() >= 1) {
          if (SS.back() != '|')
            SS.push_back('|');
        }
      }
    }
  }

  stack<int> mys;

  SS.push_back('|');

  long long cc = 0;

  for (auto i = 0; i < SS.size(); i++) {
    auto c = SS[i];
    if (c == '|') {
      auto j = i - 1;
      while (!mys.empty()) {
        auto t = mys.top();
        cc += (j - t);
        mys.pop();
        j--;
      }
      continue;
    }

    if (c == 'A')
      mys.push(i);
  }

  cout << cc << endl;
  return 0;
}
Problem p03018 (Python)

Buggy submission (status: Time Limit Exceeded):
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    s = input()
    ope_num = 0
    index = 0
    a_stock = 0
    b_stock = 0
    s = list(s)
    while index < len(s):
        # print(s)
        # print(index)
        # print(ope_num)
        # print()
        si = s[index]
        if si == "A":
            if b_stock > 0:
                a_stock = 1
            else:
                a_stock = a_stock + 1
            b_stock = 0
        elif a_stock > 0:
            if b_stock == 0:
                if si == "B":
                    b_stock = 1
                else:
                    a_stock = 0
                    b_stock = 0
            elif si == "C":
                ope_num = ope_num + a_stock
                # s[index - 2 - a_stock + 1] = "B"
                # s[index - 1 - a_stock + 1] = "C"
                for i in range(a_stock - 1, -1, -1):
                    s[index - i] = "A"
                index = index - a_stock
                a_stock = 0
                b_stock = 0
            else:
                a_stock = 0
                b_stock = 0
        else:
            a_stock = 0
            b_stock = 0
        index = index + 1
    print(ope_num)

# ABCの数
# ABCの前にあるAの分だけ追加で操作できる
# ABCの後にある(BC)の分だけ追加で操作できる

# Aがあれば覚えておく
# ABCと来たら、それより前側に隣接しているAの回数だけ追加で操作できる
# 見つかったABCをBCAに置き換える
# BCAのAから走査を継続する


Fixed submission:
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    s = input()
    ope_num = 0
    index = 0
    a_stock = 0
    b_stock = 0
    s = list(s)
    while index < len(s):
        # print(s)
        # print(index)
        # print(ope_num)
        # print()
        si = s[index]
        if si == "A":
            if b_stock > 0:
                a_stock = 1
            else:
                a_stock = a_stock + 1
            b_stock = 0
        elif a_stock > 0:
            if b_stock == 0:
                if si == "B":
                    b_stock = 1
                else:
                    a_stock = 0
                    b_stock = 0
            elif si == "C":
                ope_num = ope_num + a_stock
                # s[index - 2 - a_stock + 1] = "B"
                # s[index - 1 - a_stock + 1] = "C"
                # s[index] = "A"
                # index = index - 1
                a_stock = a_stock
                b_stock = 0
            else:
                a_stock = 0
                b_stock = 0
        else:
            a_stock = 0
            b_stock = 0
        index = index + 1
    print(ope_num)

# ABCの数
# ABCの前にあるAの分だけ追加で操作できる
# ABCの後にある(BC)の分だけ追加で操作できる

# Aがあれば覚えておく
# ABCと来たら、それより前側に隣接しているAの回数だけ追加で操作できる
# 見つかったABCをBCAに置き換える
# BCAのAから走査を継続する
Problem p03018 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
ans = 0
cntA = 0
for i in range(0, len(s) - 1):
    if s[i] == "A":
        cntA += 1
    elif s[i : i + 2] == "BC":
        cntANext = 0
        for j in range(0, cntA):
            ans += 1
            cntANext += 1
        cntA = cntANext
    elif i > 0 and s[i - 1 : i + 1] == "BC":
        continue
    else:
        cntA = 0
print(ans)
# print(*data, sep='\n')


Fixed submission:
s = input()
ans = 0
cntA = 0
for i in range(0, len(s) - 1):
    if s[i] == "A":
        cntA += 1
    elif s[i : i + 2] == "BC":
        ans += cntA
    elif i > 0 and s[i - 1 : i + 1] == "BC":
        continue
    else:
        cntA = 0
print(ans)
# print(*data, sep='\n')
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int sum = 0;
  while (true) {
    if (s.find("ABC") == std::string::npos)
      break;
    else {
      int a = s.find("ABC");
      s.replace(a, 3, "BCA");
      sum += 1;
    }
  }
  cout << sum << endl;
}

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

int main() {
  string s;
  cin >> s;
  long sum = 0;
  long litsum = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'A')
      litsum += 1;
    else if (s[i] == 'B') {
      if (i == s.length() - 1)
        break;
      else if (s[i + 1] == 'C') {
        sum += litsum;
      } else if (s[i + 1] == 'A') {
        litsum = 0;
      } else
        litsum = 0;
    } else if (s[i] == 'C') {
      if (i == 0)
        continue;
      else if (s[i - 1] == 'B')
        continue;
      else
        litsum = 0;
    }
  }
  cout << sum << endl;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << -1 << endl;                                                          \
  return 0
#define Endl endl

signed main(void) {
  string S;
  cin >> S;
  int Ans = 0;
  int N = S.size();
  vector<pair<char, int>> E;
  int r = 0;
  int M = N;
  REP(i, N) {
    if (S[i] == 'A') {
      r++;
    } else {
      if (r != 0) {
        M -= (r - 1);
        E.push_back({'A', r});
      }
      r = 0;
      E.push_back({S[i], 1});
    }
  }

  /*cout << M << endl;
  REP(i,M){cout << E[i].first <<" ";}cout << endl;
  REP(i,M){cout << E[i].second <<" ";}cout << endl;
  */
  pair<char, int> T;
  REP(i, M - 2) {
    if (E[i].first == 'A' && E[i + 1].first == 'B' && E[i + 2].first == 'C') {
      T = E[i];
      E[i] = E[i + 1];
      E[i + 1] = E[i + 2];
      E[i + 2] = T;
      Ans += T.second;
      i = max(i - 1, 0LL);
      i = max(i - 1, 0LL);
    }
  }
  cout << Ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  cout << -1 << endl;                                                          \
  return 0
#define Endl endl

signed main(void) {
  string S;
  cin >> S;
  int Ans = 0;
  int N = S.size();
  vector<pair<char, int>> E;
  int r = 0;
  int M = N;
  REP(i, N) {
    if (S[i] == 'A') {
      r++;
    } else {
      if (r != 0) {
        M -= (r - 1);
        E.push_back({'A', r});
      }
      r = 0;
      E.push_back({S[i], 1});
    }
  }

  if (r != 0) {
    M -= (r - 1);
    E.push_back({'A', r});
  }
  /*
  cout << M << endl;
  cout << E.size() << endl;
  REP(i,M){cout << E[i].first <<" ";}cout << endl;
  REP(i,M){cout << E[i].second <<" ";}cout << endl;
  */
  pair<char, int> T;
  REP(i, M - 2) {
    if (E[i].first == 'A' && E[i + 1].first == 'B' && E[i + 2].first == 'C') {
      T = E[i];
      E[i] = E[i + 1];
      E[i + 1] = E[i + 2];
      E[i + 2] = T;
      Ans += T.second;
      i = max(i - 1, 0LL);
      i = max(i - 1, 0LL);
    }
  }
  cout << Ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MOD = 1000000007;
const int INF = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  string s;
  cin >> s;
  int ans = 0;
  rep(i, s.length() - 2) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      ans++;
      if (i - 1 >= 0 && s[i - 1] == 'A') {
        s[i] = 'B';
        s[i + 1] = 'C';
        s[i + 2] = 'A';
        i -= 2;
      } else {
        s[i + 2] = 'A';
        i++;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int MOD = 1000000007;
const int INF = 1e9;
#define PI 3.14159265369;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  string s;
  cin >> s;
  int nowA = 0;
  ll ans = 0;
  rep(i, s.length()) {
    if (s[i] == 'A')
      nowA++;
    else if (s.substr(i, 2) == "BC") {
      ans += nowA;
      i++;
    } else
      nowA = 0;
  }
  cout << ans << endl;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
// RAY
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1000000007;
int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  ll count = 0, temp = 0;
  ll n = s.size(), x = 0;
  ll acount = 0;
  for (ll i = 0; i < n - 1; i++) {

    if (s[i] == 'A')
      acount++;

    else if (s[i] == 'B' && s[i + 1] == 'C') {
      count += acount;
      i++;
    } else
      acount = 0;
  }

  cout << count;

  return 0;
}


Fixed submission:
// RAY
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1000000007;
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  ll count = 0, temp = 0;
  ll n = s.size(), x = 0;
  ll acount = 0;
  for (ll i = 0; i < n - 1; i++) {

    if (s[i] == 'A')
      acount++;

    else if (s[i] == 'B' && s[i + 1] == 'C') {
      count += acount;
      i++;
    } else
      acount = 0;
  }

  cout << count;

  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i <= n; ++i)
bool judge_bc(string s, int k, int len) {
  if (k == len)
    return false;
  return s[k] == 'B' && s[k + 1] == 'C';
}
int main() {
  string s;
  cin >> s;
  int n = (int)s.length();
  ll ans = 0;
  int sum_a = 0;
  rep(i, n) {
    if (s[i] == 'A')
      sum_a++;
    else if (judge_bc(s, i, n - 1)) {
      ans += sum_a;
      i++;
    } else {
      sum_a = 0;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i <= n; ++i)
bool judge_bc(string s, int k, int len) {
  if (k == len)
    return false;
  return s[k] == 'B' && s[k + 1] == 'C';
}
int main() {
  string s;
  cin >> s;
  int n = (int)s.length();
  ll ans = 0;
  int sum_a = 0;
  rep(i, n) {
    if (s[i] == 'A')
      sum_a++;
    else if (s[i] == 'B' && s[i + 1] == 'C') {
      if (i < n - 1) {
        ans += sum_a;
        i++;
      }
    } else {
      sum_a = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

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

  string s;
  cin >> s;

  if (s.size() < 3) {
    cout << 0 << endl;
    return 0;
  }

  long long ans = 0LL;
  for (int i = 0; i < s.size() - 2; i++) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      s[i] = 'B';
      s[i + 1] = 'C';
      s[i + 2] = 'A';
      if (i >= 1) {
        i -= 2;
      }
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

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

  string s;
  cin >> s;

  if (s.size() < 3) {
    cout << 0 << endl;
    return 0;
  }

  long long ans = 0LL;
  int A_num = 0;
  for (int i = 0; i < s.size() - 1; i++) {

    if (s[i] == 'B' && s[i + 1] == 'C' && A_num > 0) {
      ans += A_num;
      i++;
    } else if (s[i] == 'A') {
      A_num++;
    } else {
      A_num = 0;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define eps LDBL_EPSILON
#define moder 1000000007
#define double long double
#define INF LLONG_MAX / 10000
#define P pair<int, int>
#define prique priority_queue
using namespace std;
string s;
int ans = 0;
signed main() {
  cin >> s;
  for (int i = (int)s.size() - 3; i >= 0; i--) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
      int p = i;
    kaage:;
      s[p] = 'B';
      s[p + 1] = 'C';
      s[p + 2] = 'A';
      ans++;
      if (p + 4 < (int)s.size() && s[p + 2] == 'A' && s[p + 3] == 'B' &&
          s[p + 4] == 'C') {
        p += 2;
        goto kaage;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define int long long
#define ll long long
#define eps LDBL_EPSILON
#define moder 1000000007
#define double long double
#define INF LLONG_MAX / 10000
#define P pair<int, int>
#define prique priority_queue
using namespace std;
string s;
int ans = 0;
signed main() {
  cin >> s;
  int cnt = 0;
  rep(i, s.size()) {
    if (s[i] == 'A')
      cnt++;
    else if (i != s.size() - 1 && s[i] == 'B' && s[i + 1] == 'C') {
      ans += cnt;
      i++;
    } else
      cnt = 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const ll inf = 1e9;
const int MAX = 510000;
const ll MOD = 998244353;

inline ll mod(ll a, ll m) { return (a % m + m) % m; }

int main() {
  string s;
  cin >> s;
  ll n = s.size(), ans = 0;
  rep(i, 0, n - 2) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {

      ll l = 0;
      while (s[i - l] == 'A' && i - l >= 0) {
        l++;
      }

      s[i - l + 1] = 'B';
      s[i - l + 2] = 'C';
      rep(j, i - l + 3, i + 3) { s[j] = 'A'; }

      ans += l;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const ll inf = 1e9;
const int MAX = 510000;
const ll MOD = 998244353;

inline ll mod(ll a, ll m) { return (a % m + m) % m; }

int main() {
  string s;
  cin >> s;
  ll n = s.size(), ans = 0;
  string t = "";
  if (n < 3) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, 0, n - 1) {
    if (s[i] == 'B' && s[i + 1] == 'C') {
      t += "Z";
      i++;
    } else
      t += s[i];
  }
  ll m = t.size();
  ll cnt = 0;
  rep(i, 0, m) {
    if (t[i] == 'Z')
      ans += cnt;
    else if (t[i] == 'A')
      cnt++;
    else
      cnt = 0;
  }
  cout << ans << endl;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
/* ****GT_18**** */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 100005
ll ans = 0, n;
string s;
void solve(ll x, ll d) {
  if (x >= n - 2 || x < 0)
    return;
  if (d == 1) {
    for (ll i = x; i < n - 2; i++) {
      if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
        ans++;
        s[i] = 'B';
        s[i + 1] = 'C';
        s[i + 2] = 'A';
        solve(i - 1, 1);
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //    cin>>TESTS;
  while (TESTS--) {
    ll i, j, k, l, m;
    cin >> s;
    n = s.size();
    solve(0, 1);
    cout << ans;
  }
  return 0;
}

Fixed submission:
/* ****GT_18**** */

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
using namespace std;

#define N 100005
ll ans = 0, n;
string s;
void solve(ll x, ll d) {
  if (x >= n - 2 || x < 0)
    return;
  ll sum = 0;
  for (ll i = 0; i < n - 1; i++) {
    if (s[i] == 'B' && s[i + 1] == 'C') {
      ans += sum;
      i += 1;
    } else {
      if (s[i] == 'A')
        sum++;
      else {
        sum = 0;
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  //    cin>>TESTS;
  while (TESTS--) {
    ll i, j, k, l, m;
    cin >> s;
    n = s.size();
    solve(0, 1);
    cout << ans;
  }
  return 0;
}
Problem p03018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int countab = 0;
  ll tot = 0, c1;
  vector<int> cnt[205];
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'A') {
      cnt[countab].push_back(1);
    } else if (s[i] == 'B' && i != s.size() - 1 && s[i + 1] == 'C') {
      cnt[countab].push_back(0);
      i++;
    } else {
      countab++;
    }
  }
  for (int i = 0; i <= countab; ++i) {
    c1 = 0;
    for (int j = 0; j < cnt[i].size(); ++j) {
      if (cnt[i][j] == 1) {
        c1++;
      } else {
        tot += c1;
      }
    }
  }
  cout << tot << '\n';
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int countab = 0;
  ll tot = 0, c1;
  vector<int> cnt[200005];
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'A') {
      cnt[countab].push_back(1);
    } else if (s[i] == 'B' && i != s.size() - 1 && s[i + 1] == 'C') {
      cnt[countab].push_back(0);
      i++;
    } else {
      countab++;
    }
  }
  for (int i = 0; i <= countab; ++i) {
    c1 = 0;
    for (int j = 0; j < cnt[i].size(); ++j) {
      if (cnt[i][j] == 1) {
        c1++;
      } else {
        tot += c1;
      }
    }
  }
  cout << tot << '\n';
}