Problem p03018 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize(                                                          \
    "O3,Ofast,inline,fast-math,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
#define int int64_t
#define double long double
#define MAXC numeric_limits<C>::max()
#define MINC numeric_limits<C>::min()
const int MAX = numeric_limits<int>::max();
const int MIN = numeric_limits<int>::min();
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
// typedef
#define vdef(t, T)                                                             \
  typedef vector<t> V##T;                                                      \
  typedef vector<V##T> VV##T;                                                  \
  typedef vector<VV##T> VVV##T;
#define pdef(t1, t2, T)                                                        \
  typedef pair<t1, t2> P##T;                                                   \
  vdef(P##T, P##T)
vdef(int, I) vdef(double, D) vdef(char, C) vdef(string, S) pdef(int, int, II)
    pdef(int, PII, III) pdef(string, PII, SII)
// template
#define TCC template <class C>
#define TCC12 template <class C1, class C2>
#define TCC12C template <class C1, class C2, class CC>
#define TCCC template <class C, class CC>
#define VCL vector<C>
#define VVCL vector<VCL>
#define PCC pair<C1, C2>
#define PCI pair<C, int>
#define GC graph<C>
// define use frequently
#define emb emplace_back
#define pub push_back
#define pob pop_back
#define mp make_pair
#define mp3(a, b, c) mp(a, mp(b, c))
#define fi first
#define se second
#define fir fi
#define sec se.fi
#define thi se.se
#define fou(i, a, n) for (int i = a; i < n; i++)
#define fod(i, a, n) for (int i = n - 1; i >= a; i--)
#define rep(n) fou(_, 0, n)
#define tra(a, x) for (auto &a : x)
#define elif(c) else if (c)
#define si(vec) ((int)(vec).size())
#define all(vec) vec.begin(), vec.end()
#define lla(vec) vec.rbegin(), vec.rend()
#define range(vec, x, y) vec.begin() + x, vec.begin() + y
// make_pair,vector calculation
#define opall(calc)                                                            \
  calc(+) calc(-) calc(*) calc(/) calc(%) calc(<<) calc(>>) calc(&) calc(|)    \
      calc(^)
#define p1c(op)                                                                \
  TCC12C PCC operator op(const PCC &l, const CC &r) {                          \
    return mp(l.fi op r, l.se op r);                                           \
  }
#define p2c(op)                                                                \
  TCC12 PCC operator op(const PCC l, const PCC &r) {                           \
    return mp(l.fi op r.fi, l.se op r.se);                                     \
  }
#define v1c(op)                                                                \
  TCCC VCL operator op(VCL l, const CC &r) {                                   \
    tra(le, l) le = le op r;                                                   \
    return l;                                                                  \
  }
#define v2c(op)                                                                \
  TCC VCL operator op(VCL l, const VCL &r) {                                   \
    fou(i, 0, min(si(l), si(r))) l[i] = l[i] op r[i];                          \
    return l;                                                                  \
  }
#define opeq(op)                                                               \
  TCC12 C1 operator op##=(C1 &l, const C2 &r) { return l = l op r; }
        opall(p1c) opall(p2c) opall(v1c) opall(v2c) opall(opeq)
// iostream
#define in(T, ...)                                                             \
  T __VA_ARGS__;                                                               \
  _in(__VA_ARGS__)
            void _in() {
}
template <class Head, class... Tail> void _in(Head &head, Tail &&...tail) {
  cin >> head;
  _in(forward<Tail>(tail)...);
}
template <class Head> void out(const Head &head) { cout << head << endl; }
template <class Head, class... Tail> void out(const Head &head, Tail... tail) {
  cout << head << " ";
  out(forward<Tail>(tail)...);
}
TCC void vout(const VCL &vec) { tra(v, vec) out(v); }
TCC12 istream &operator>>(istream &is, PCC &p) { return is >> p.fi >> p.se; }
TCC istream &operator>>(istream &is, VCL &vec) {
  tra(v, vec) is >> v;
  return is;
}
TCC12 ostream &operator<<(ostream &os, const PCC &p) {
  os << p.fi << " " << p.se;
  return os;
}
TCC ostream &operator<<(ostream &os, const VCL &vec) {
  if (vec.empty())
    return os;
  ;
  fou(i, 0, si(vec) - 1) os << vec[i] << " ";
  os << vec[si(vec) - 1];
  return os;
}
// math
TCC C sign(C a) { return (a > 0) - (a < 0); }
int gcds(const int &a, const int &b) { return b ? gcds(b, a % b) : a; }
int gcd(const int &a, const int &b) { return gcds(max(a, b), min(a, b)); }
int lcm(const int &a, const int &b) { return a / gcd(a, b) * b; }
int fact(const int &n, const int &prod) {
  return (n == 0) ? prod : fact(n - 1, n * prod);
}
int fact(const int &n) { return fact(n, 1); }
PII comb(const int &m, const int &n, const PII &nd) {
  return (n == 0) ? nd : comb(m - 1, n - 1, mp(m, n) * nd);
}
int comb(const int &m, const int &n) {
  PII p = comb(m, min(n, m - n), mp((int)1, (int)1));
  return p.fi / p.se;
}
// mod
int modnum(const int &a) { return a % MOD + (a < 0 ? MOD : 0); }
int modinv(int a) {
  a = modnum(a);
  int b = MOD, u = 0, v = 1;
  while (a > 1) {
    u -= b / a * v;
    b %= a;
    swap(a, b);
    swap(u, v);
  }
  return modnum(v);
}
#define modcalc(opstr, op)                                                     \
  int mod##opstr(const int &a, const int &b) {                                 \
    return modnum(modnum(a) op modnum(b));                                     \
  }
modcalc(add, +) modcalc(sub, -)
    modcalc(mul, *) int moddiv(const int &a, const int &b) {
  return modmul(a, modinv(b));
}
int modpow(int a, int b) {
  int r = 1;
  while (b > 0) {
    if (b & 1)
      r = modmul(r, a);
    a = modmul(a, a);
    b >>= 1;
  }
  return r;
}
// vector,matrix
TCC C vecsum(const VCL &vec, const int &l, const int &r) {
  C sum = 0;
  fou(i, l, r) sum += vec[i];
  return sum;
}
TCC C dot(const VCL &vec1, const VCL &vec2) {
  int sum = 0;
  fou(i, 0, min(si(vec1), si(vec2))) sum += vec1[i] * vec2[i];
  return sum;
}
TCC VVCL dot(const VVCL &mat1, const VVCL &mat2) {
  int h = si(mat1), w = si(mat2[0]);
  VVCL mat(h, VI(w));
  fou(i, 0, h) fou(j, 0, w) fou(k, 0, min(si(mat1[0]), si(mat2))) mat[i][j] +=
      mat1[i][k] * mat2[k][j];
  return mat;
}
VVI eye(const int &n) {
  VVI e(n, VI(n));
  fou(i, 0, n) e[i][i] = 1;
  return e;
}
TCC VVCL matpow(VVCL mat, int n) {
  int s = si(mat);
  VVCL r = eye(s);
  while (n > 0) {
    if (n & 1)
      r = dot(r, mat);
    mat = dot(mat, mat);
    n >>= 1;
  }
  return r;
}
TCC void sortup(VCL &vec) { sort(all(vec)); }
TCC void sortdown(VCL &vec) { sort(lla(vec)); }
TCC void erase_unique(VCL &vec) { vec.erase(unique(all(vec)), vec.end()); }
TCC void rotateleft(VCL &vec, const int &n) {
  rotate(vec.begin(), vec.begin() + n, vec.end());
}
TCC void rotateright(VCL &vec, const int &n) {
  rotate(vec.rbegin(), vec.rbegin() + n, vec.rend());
}
TCC void mini(C &a, const C &b) { a = min(a, b); }
TCC void maxi(C &a, const C &b) { a = max(a, b); }
TCC int mine(VCL &vec) {
  C m = 0;
  fou(i, 1, si(vec)) if (vec[i] < vec[m]) m = i;
  return m;
}
TCC int maxe(VCL &vec) {
  C m = 0;
  fou(i, 1, si(vec)) if (vec[i] > vec[m]) m = i;
  return m;
}
// cumulated sum
TCC void cumsum(VCL &vec) { fou(i, 1, si(vec)) vec[i] += vec[i - 1]; }
TCC void cumsum(VVCL &vec) {
  fou(i, 1, si(vec)) vec[i] += vec[i - 1];
  fou(i, 0, si(vec)) cumsum(vec[i]);
}
TCC C partsum(VCL &vec, const int &l, const int &r) {
  return (l == 0) ? vec[r - 1] : vec[r - 1] - vec[l - 1];
}
TCC C partsum(VVCL &vec, const int &l1, const int &r1, const int &l2,
              const int &r2) {
  return (vec[r1 - 1][r2 - 1]) - ((l1 == 0) ? 0 : vec[l1 - 1][r2 - 1]) -
         ((l2 == 0) ? 0 : vec[r1 - 1][l2 - 1]) +
         ((l1 == 0 || l2 == 0) ? 0 : vec[l1 - 1][l2 - 1]);
}
TCC void decumsum(VCL &vec) {
  int s = si(vec);
  fod(i, 1, s) vec[i] -= vec[i - 1];
}
TCC void decumsum(VVCL &vec) {
  fod(i, 1, si(vec)) vec[i] -= vec[i - 1];
  fod(i, 0, si(vec)) decumsum(vec[i]);
}
// string
int leven(string str1, string str2, int insert = 1, int erase = 1,
          int replace = 1) {
  int s1 = si(str1), s2 = si(str2);
  VVI d(s1 + 1, VI(s2 + 1));
  fou(i, 0, s1 + 1) d[i][0] = i * insert;
  fou(j, 0, s2 + 1) d[0][j] = j * erase;
  fou(i, 1, s1 + 1) fou(j, 1, s2 + 1) {
    int diff = ((str1[i - 1] == str2[j - 1]) ? 0 : 1);
    d[i][j] = min({d[i - 1][j] + insert, d[i][j - 1] + erase,
                   d[i - 1][j - 1] + replace * diff});
  }
  return d[s1][s2];
}
int lcs(string str1, string str2) {
  int s1 = si(str1), s2 = si(str2);
  VVI dp(s1 + 1, VI(s2 + 1));
  fou(i, 0, s1) fou(j, 0, s2) {
    if (str1[i] == str2[j])
      dp[i + 1][j + 1] = dp[i][j] + 1;
    else
      dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
  }
  return dp[s1][s2];
}
// algorithm
TCC class BIT {
  VCL tree;
  int n;

public:
  BIT(int _n) : n(_n) { tree.resize(n); }
  void add(int x, C v) {
    while (x < n) {
      tree[x] += v;
      x |= (x + 1);
    }
  }
  C sum(int x) {
    C v = 0;
    while (x >= 0) {
      v += tree[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
  C part(int x, int y) { return sum(y - 1) - sum(x - 1); }
  C get(int x) { return part(x, x + 1); }
  void set(int x, int y) { add(x, -get(x) + y); }
};
TCC class unionfind {
  VI par;
  VCL wei;
  int n;

public:
  unionfind(int _n) : n(_n) {
    par.resize(n);
    iota(all(par), 1);
    wei.resize(n);
  }
  int get(const int &x) {
    if (par[x] == (x + 1))
      return x + 1;
    else {
      int parent = sign(par[x]) * get(abs(par[x]) - 1);
      wei[x] += wei[abs(par[x]) - 1];
      return par[x] = parent;
    }
  }
  void get(int &x, int &y) {
    x = get(x);
    y = get(y);
  }
  int weight(int x) {
    get(x);
    return wei[x];
  }
  void unite(int x, int y, C w) {
    w += weight(x) - weight(y);
    get(x, y);
    if (abs(x) != abs(y)) {
      par[abs(y) - 1] = x * sign(y);
      wei[abs(y) - 1] = w;
    }
  }
  void separate(int x, int y, C w) {
    w += weight(x) - weight(y);
    get(x, y);
    if (abs(x) != abs(y)) {
      par[abs(y) - 1] = -x * sign(y);
      wei[abs(y) - 1] = w;
    }
  }
  int same(int x, int y) {
    get(x, y);
    return (x == y) - (x == -y);
  }
  C diff(int x, int y) { return weight(y) - weight(x); }
};

TCC int binarysearch(const VCL &vec, const C &n, const char &c) {
  int left = -1;
  int right = si(vec);
  bool outleft = (c == 'd' || c == 'U');
  int getmin = (c == 'u' || c == 'd') ? 1 : -1;
  while (right - left > 1) {
    int mid = left + (right - left) / 2;
    bool OK = vec[mid] * getmin >= n * getmin;
    if (outleft == OK)
      left = mid;
    else
      right = mid;
  }
  return outleft ? left : right;
}

TCC class graph {
public:
  struct edge {
    int from, to;
    C cost;
  };
  int n;
  vector<edge> edges;
  VVI g;
  function<bool(int)> ignore;
  graph(int _n) : n(_n) {
    g.resize(n);
    ignore = nullptr;
  }
  virtual int add(int from, int to, C cost) = 0;
  virtual void set_ignore_edge_rule(const function<bool(int)> &f) {
    ignore = f;
  }
  virtual void reset_ignore_edge_rule() { ignore = nullptr; }
};
TCC class undigraph : public GC {
public:
  using GC::edges;
  using GC::g;
  using GC::n;
  undigraph(int n) : GC(n) {}
  int add(int from, int to, C cost = 1) {
    int id = si(edges);
    g[from].emb(id);
    g[to].emb(id);
    edges.pub({from, to, cost});
    return id;
  }
};
TCC class digraph : public GC {
public:
  using GC::edges;
  using GC::g;
  using GC::ignore;
  using GC::n;
  digraph(int _n) : GC(_n) {}
  int add(int from, int to, C cost = 1) {
    int id = si(edges);
    g[from].emb(id);
    edges.pub({from, to, cost});
    return id;
  }
  digraph<C> reverse() const {
    digraph<C> rev(n);
    tra(e, edges) rev.add(e.to, e.from, e.cost);
    if (ignore != nullptr) {
      rev.set_ignore_edge_rule([&](int id) { return ignore(id); });
    }
    return rev;
  }
};

TCC VCL dijkstra(const GC &g, const int &start) {
  VCL d(g.n, MAXC);
  priority_queue<PCI, vector<PCI>, greater<PCI>> s;
  d[start] = 0;
  s.emplace(d[start], start);
  while (!s.empty()) {
    int expected = s.top().fi, from = s.top().se;
    s.pop();
    if (d[from] != expected)
      continue;
    tra(id, g.g[from]) {
      auto e = g.edges[id];
      int to = e.from ^ e.to ^ from;
      if (d[from] + e.cost < d[to]) {
        d[to] = d[from] + e.cost;
        s.emplace(d[to], to);
      }
    }
  }
  return d;
}
TCC VCL bellman(const GC &g, const int &start) {
  VCL d(g.n, MAXC);
  d[start] = 0;
  bool nl = false;
  VI upd_b(1, start);
  int i = 0;
  while (!upd_b.empty() && i < g.n) {
    VI upd;
    tra(from, upd_b) {
      tra(id, g.g[from]) {
        auto e = g.edges[id];
        int to = e.from ^ e.to ^ from;
        if (d[to] > d[from] + e.cost) {
          d[to] = d[from] + e.cost;
          upd.emb(to);
          if (i == g.n - 1) {
            d[to] = MINC;
            nl = true;
          }
        }
      }
    }
    upd_b = upd;
    i++;
  }
  if (nl) {
    i = 0;
    while (!upd_b.empty() && i < g.n - 1) {
      VI upd;
      tra(from, upd_b) {
        tra(id, g.g[from]) {
          auto e = g.edges[id];
          int to = e.from ^ e.to ^ from;
          if (d[to] != MINC) {
            d[to] = MINC;
            upd.emb(to);
          }
        }
      }
      upd_b = upd;
      i++;
    }
  }
  return d;
}

class time_measure {
  chrono::system_clock::time_point start;

public:
  time_measure() : start(chrono::system_clock::now()) {}
  void time() {
    out(chrono::duration_cast<chrono::milliseconds>(
            chrono::system_clock::now() - start)
            .count(),
        "ms");
  }
};

// todo
// graph行列表現
// ２部グラフ
// フローグラフ
// kruskal
//  文字列抜き出し

// TCC VVCL warshall(const GC&g){
//   VVCL mat(g.n,VCL(g.n,MAXC));
//   fou(i,0,g.n)mat[i][i]=0;
//   fou(from,0,si(g.n)){
//     tra(id,g.g[from]){
//       auto e=g.edges[id];int to=e.from^e.to^from;
//     }
//   }
//   return mat;
// }

signed main() {
  IOS;
  in(string, s);
  string ss;
  fou(i, 0, si(s)) {
    if (s[i] == 'A')
      ss.pub('A');
    if (i + 1 != si(s))
      if (s[i] == 'B' && s[i + 1] == 'C') {
        ss.pub('X');
        i++;
        continue;
      }
    if (s[i] == 'B')
      ss.pub('B');
    if (s[i] == 'C')
      ss.pub('C');
  }

  if (si(ss) < 2) {
    out(0);
    return 0;
  }
  VI before(si(ss) - 1);
  int ans = 0;

  fou(i, 1, si(ss)) {
    if (ss[i] == 'X') {
      fod(j, 0, i) {
        if (ss[j] == 'A') {
          ss[j] = 'X';
          ss[j + 1] = 'A';
          ans++;
        } else
          break;
      }
    }
  }
  out(ans);
}

Fixed submission:
#pragma GCC optimize(                                                          \
    "O3,Ofast,inline,fast-math,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
#define int int64_t
#define double long double
#define MAXC numeric_limits<C>::max()
#define MINC numeric_limits<C>::min()
const int MAX = numeric_limits<int>::max();
const int MIN = numeric_limits<int>::min();
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
// typedef
#define vdef(t, T)                                                             \
  typedef vector<t> V##T;                                                      \
  typedef vector<V##T> VV##T;                                                  \
  typedef vector<VV##T> VVV##T;
#define pdef(t1, t2, T)                                                        \
  typedef pair<t1, t2> P##T;                                                   \
  vdef(P##T, P##T)
vdef(int, I) vdef(double, D) vdef(char, C) vdef(string, S) pdef(int, int, II)
    pdef(int, PII, III) pdef(string, PII, SII)
// template
#define TCC template <class C>
#define TCC12 template <class C1, class C2>
#define TCC12C template <class C1, class C2, class CC>
#define TCCC template <class C, class CC>
#define VCL vector<C>
#define VVCL vector<VCL>
#define PCC pair<C1, C2>
#define PCI pair<C, int>
#define GC graph<C>
// define use frequently
#define emb emplace_back
#define pub push_back
#define pob pop_back
#define mp make_pair
#define mp3(a, b, c) mp(a, mp(b, c))
#define fi first
#define se second
#define fir fi
#define sec se.fi
#define thi se.se
#define fou(i, a, n) for (int i = a; i < n; i++)
#define fod(i, a, n) for (int i = n - 1; i >= a; i--)
#define rep(n) fou(_, 0, n)
#define tra(a, x) for (auto &a : x)
#define elif(c) else if (c)
#define si(vec) ((int)(vec).size())
#define all(vec) vec.begin(), vec.end()
#define lla(vec) vec.rbegin(), vec.rend()
#define range(vec, x, y) vec.begin() + x, vec.begin() + y
// make_pair,vector calculation
#define opall(calc)                                                            \
  calc(+) calc(-) calc(*) calc(/) calc(%) calc(<<) calc(>>) calc(&) calc(|)    \
      calc(^)
#define p1c(op)                                                                \
  TCC12C PCC operator op(const PCC &l, const CC &r) {                          \
    return mp(l.fi op r, l.se op r);                                           \
  }
#define p2c(op)                                                                \
  TCC12 PCC operator op(const PCC l, const PCC &r) {                           \
    return mp(l.fi op r.fi, l.se op r.se);                                     \
  }
#define v1c(op)                                                                \
  TCCC VCL operator op(VCL l, const CC &r) {                                   \
    tra(le, l) le = le op r;                                                   \
    return l;                                                                  \
  }
#define v2c(op)                                                                \
  TCC VCL operator op(VCL l, const VCL &r) {                                   \
    fou(i, 0, min(si(l), si(r))) l[i] = l[i] op r[i];                          \
    return l;                                                                  \
  }
#define opeq(op)                                                               \
  TCC12 C1 operator op##=(C1 &l, const C2 &r) { return l = l op r; }
        opall(p1c) opall(p2c) opall(v1c) opall(v2c) opall(opeq)
// iostream
#define in(T, ...)                                                             \
  T __VA_ARGS__;                                                               \
  _in(__VA_ARGS__)
            void _in() {
}
template <class Head, class... Tail> void _in(Head &head, Tail &&...tail) {
  cin >> head;
  _in(forward<Tail>(tail)...);
}
template <class Head> void out(const Head &head) { cout << head << endl; }
template <class Head, class... Tail> void out(const Head &head, Tail... tail) {
  cout << head << " ";
  out(forward<Tail>(tail)...);
}
TCC void vout(const VCL &vec) { tra(v, vec) out(v); }
TCC12 istream &operator>>(istream &is, PCC &p) { return is >> p.fi >> p.se; }
TCC istream &operator>>(istream &is, VCL &vec) {
  tra(v, vec) is >> v;
  return is;
}
TCC12 ostream &operator<<(ostream &os, const PCC &p) {
  os << p.fi << " " << p.se;
  return os;
}
TCC ostream &operator<<(ostream &os, const VCL &vec) {
  if (vec.empty())
    return os;
  ;
  fou(i, 0, si(vec) - 1) os << vec[i] << " ";
  os << vec[si(vec) - 1];
  return os;
}
// math
TCC C sign(C a) { return (a > 0) - (a < 0); }
int gcds(const int &a, const int &b) { return b ? gcds(b, a % b) : a; }
int gcd(const int &a, const int &b) { return gcds(max(a, b), min(a, b)); }
int lcm(const int &a, const int &b) { return a / gcd(a, b) * b; }
int fact(const int &n, const int &prod) {
  return (n == 0) ? prod : fact(n - 1, n * prod);
}
int fact(const int &n) { return fact(n, 1); }
PII comb(const int &m, const int &n, const PII &nd) {
  return (n == 0) ? nd : comb(m - 1, n - 1, mp(m, n) * nd);
}
int comb(const int &m, const int &n) {
  PII p = comb(m, min(n, m - n), mp((int)1, (int)1));
  return p.fi / p.se;
}
// mod
int modnum(const int &a) { return a % MOD + (a < 0 ? MOD : 0); }
int modinv(int a) {
  a = modnum(a);
  int b = MOD, u = 0, v = 1;
  while (a > 1) {
    u -= b / a * v;
    b %= a;
    swap(a, b);
    swap(u, v);
  }
  return modnum(v);
}
#define modcalc(opstr, op)                                                     \
  int mod##opstr(const int &a, const int &b) {                                 \
    return modnum(modnum(a) op modnum(b));                                     \
  }
modcalc(add, +) modcalc(sub, -)
    modcalc(mul, *) int moddiv(const int &a, const int &b) {
  return modmul(a, modinv(b));
}
int modpow(int a, int b) {
  int r = 1;
  while (b > 0) {
    if (b & 1)
      r = modmul(r, a);
    a = modmul(a, a);
    b >>= 1;
  }
  return r;
}
// vector,matrix
TCC C vecsum(const VCL &vec, const int &l, const int &r) {
  C sum = 0;
  fou(i, l, r) sum += vec[i];
  return sum;
}
TCC C dot(const VCL &vec1, const VCL &vec2) {
  int sum = 0;
  fou(i, 0, min(si(vec1), si(vec2))) sum += vec1[i] * vec2[i];
  return sum;
}
TCC VVCL dot(const VVCL &mat1, const VVCL &mat2) {
  int h = si(mat1), w = si(mat2[0]);
  VVCL mat(h, VI(w));
  fou(i, 0, h) fou(j, 0, w) fou(k, 0, min(si(mat1[0]), si(mat2))) mat[i][j] +=
      mat1[i][k] * mat2[k][j];
  return mat;
}
VVI eye(const int &n) {
  VVI e(n, VI(n));
  fou(i, 0, n) e[i][i] = 1;
  return e;
}
TCC VVCL matpow(VVCL mat, int n) {
  int s = si(mat);
  VVCL r = eye(s);
  while (n > 0) {
    if (n & 1)
      r = dot(r, mat);
    mat = dot(mat, mat);
    n >>= 1;
  }
  return r;
}
TCC void sortup(VCL &vec) { sort(all(vec)); }
TCC void sortdown(VCL &vec) { sort(lla(vec)); }
TCC void erase_unique(VCL &vec) { vec.erase(unique(all(vec)), vec.end()); }
TCC void rotateleft(VCL &vec, const int &n) {
  rotate(vec.begin(), vec.begin() + n, vec.end());
}
TCC void rotateright(VCL &vec, const int &n) {
  rotate(vec.rbegin(), vec.rbegin() + n, vec.rend());
}
TCC void mini(C &a, const C &b) { a = min(a, b); }
TCC void maxi(C &a, const C &b) { a = max(a, b); }
TCC int mine(VCL &vec) {
  C m = 0;
  fou(i, 1, si(vec)) if (vec[i] < vec[m]) m = i;
  return m;
}
TCC int maxe(VCL &vec) {
  C m = 0;
  fou(i, 1, si(vec)) if (vec[i] > vec[m]) m = i;
  return m;
}
// cumulated sum
TCC void cumsum(VCL &vec) { fou(i, 1, si(vec)) vec[i] += vec[i - 1]; }
TCC void cumsum(VVCL &vec) {
  fou(i, 1, si(vec)) vec[i] += vec[i - 1];
  fou(i, 0, si(vec)) cumsum(vec[i]);
}
TCC C partsum(VCL &vec, const int &l, const int &r) {
  return (l == 0) ? vec[r - 1] : vec[r - 1] - vec[l - 1];
}
TCC C partsum(VVCL &vec, const int &l1, const int &r1, const int &l2,
              const int &r2) {
  return (vec[r1 - 1][r2 - 1]) - ((l1 == 0) ? 0 : vec[l1 - 1][r2 - 1]) -
         ((l2 == 0) ? 0 : vec[r1 - 1][l2 - 1]) +
         ((l1 == 0 || l2 == 0) ? 0 : vec[l1 - 1][l2 - 1]);
}
TCC void decumsum(VCL &vec) {
  int s = si(vec);
  fod(i, 1, s) vec[i] -= vec[i - 1];
}
TCC void decumsum(VVCL &vec) {
  fod(i, 1, si(vec)) vec[i] -= vec[i - 1];
  fod(i, 0, si(vec)) decumsum(vec[i]);
}
// string
int leven(string str1, string str2, int insert = 1, int erase = 1,
          int replace = 1) {
  int s1 = si(str1), s2 = si(str2);
  VVI d(s1 + 1, VI(s2 + 1));
  fou(i, 0, s1 + 1) d[i][0] = i * insert;
  fou(j, 0, s2 + 1) d[0][j] = j * erase;
  fou(i, 1, s1 + 1) fou(j, 1, s2 + 1) {
    int diff = ((str1[i - 1] == str2[j - 1]) ? 0 : 1);
    d[i][j] = min({d[i - 1][j] + insert, d[i][j - 1] + erase,
                   d[i - 1][j - 1] + replace * diff});
  }
  return d[s1][s2];
}
int lcs(string str1, string str2) {
  int s1 = si(str1), s2 = si(str2);
  VVI dp(s1 + 1, VI(s2 + 1));
  fou(i, 0, s1) fou(j, 0, s2) {
    if (str1[i] == str2[j])
      dp[i + 1][j + 1] = dp[i][j] + 1;
    else
      dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
  }
  return dp[s1][s2];
}
// algorithm
TCC class BIT {
  VCL tree;
  int n;

public:
  BIT(int _n) : n(_n) { tree.resize(n); }
  void add(int x, C v) {
    while (x < n) {
      tree[x] += v;
      x |= (x + 1);
    }
  }
  C sum(int x) {
    C v = 0;
    while (x >= 0) {
      v += tree[x];
      x = (x & (x + 1)) - 1;
    }
    return v;
  }
  C part(int x, int y) { return sum(y - 1) - sum(x - 1); }
  C get(int x) { return part(x, x + 1); }
  void set(int x, int y) { add(x, -get(x) + y); }
};
TCC class unionfind {
  VI par;
  VCL wei;
  int n;

public:
  unionfind(int _n) : n(_n) {
    par.resize(n);
    iota(all(par), 1);
    wei.resize(n);
  }
  int get(const int &x) {
    if (par[x] == (x + 1))
      return x + 1;
    else {
      int parent = sign(par[x]) * get(abs(par[x]) - 1);
      wei[x] += wei[abs(par[x]) - 1];
      return par[x] = parent;
    }
  }
  void get(int &x, int &y) {
    x = get(x);
    y = get(y);
  }
  int weight(int x) {
    get(x);
    return wei[x];
  }
  void unite(int x, int y, C w) {
    w += weight(x) - weight(y);
    get(x, y);
    if (abs(x) != abs(y)) {
      par[abs(y) - 1] = x * sign(y);
      wei[abs(y) - 1] = w;
    }
  }
  void separate(int x, int y, C w) {
    w += weight(x) - weight(y);
    get(x, y);
    if (abs(x) != abs(y)) {
      par[abs(y) - 1] = -x * sign(y);
      wei[abs(y) - 1] = w;
    }
  }
  int same(int x, int y) {
    get(x, y);
    return (x == y) - (x == -y);
  }
  C diff(int x, int y) { return weight(y) - weight(x); }
};

TCC int binarysearch(const VCL &vec, const C &n, const char &c) {
  int left = -1;
  int right = si(vec);
  bool outleft = (c == 'd' || c == 'U');
  int getmin = (c == 'u' || c == 'd') ? 1 : -1;
  while (right - left > 1) {
    int mid = left + (right - left) / 2;
    bool OK = vec[mid] * getmin >= n * getmin;
    if (outleft == OK)
      left = mid;
    else
      right = mid;
  }
  return outleft ? left : right;
}

TCC class graph {
public:
  struct edge {
    int from, to;
    C cost;
  };
  int n;
  vector<edge> edges;
  VVI g;
  function<bool(int)> ignore;
  graph(int _n) : n(_n) {
    g.resize(n);
    ignore = nullptr;
  }
  virtual int add(int from, int to, C cost) = 0;
  virtual void set_ignore_edge_rule(const function<bool(int)> &f) {
    ignore = f;
  }
  virtual void reset_ignore_edge_rule() { ignore = nullptr; }
};
TCC class undigraph : public GC {
public:
  using GC::edges;
  using GC::g;
  using GC::n;
  undigraph(int n) : GC(n) {}
  int add(int from, int to, C cost = 1) {
    int id = si(edges);
    g[from].emb(id);
    g[to].emb(id);
    edges.pub({from, to, cost});
    return id;
  }
};
TCC class digraph : public GC {
public:
  using GC::edges;
  using GC::g;
  using GC::ignore;
  using GC::n;
  digraph(int _n) : GC(_n) {}
  int add(int from, int to, C cost = 1) {
    int id = si(edges);
    g[from].emb(id);
    edges.pub({from, to, cost});
    return id;
  }
  digraph<C> reverse() const {
    digraph<C> rev(n);
    tra(e, edges) rev.add(e.to, e.from, e.cost);
    if (ignore != nullptr) {
      rev.set_ignore_edge_rule([&](int id) { return ignore(id); });
    }
    return rev;
  }
};

TCC VCL dijkstra(const GC &g, const int &start) {
  VCL d(g.n, MAXC);
  priority_queue<PCI, vector<PCI>, greater<PCI>> s;
  d[start] = 0;
  s.emplace(d[start], start);
  while (!s.empty()) {
    int expected = s.top().fi, from = s.top().se;
    s.pop();
    if (d[from] != expected)
      continue;
    tra(id, g.g[from]) {
      auto e = g.edges[id];
      int to = e.from ^ e.to ^ from;
      if (d[from] + e.cost < d[to]) {
        d[to] = d[from] + e.cost;
        s.emplace(d[to], to);
      }
    }
  }
  return d;
}
TCC VCL bellman(const GC &g, const int &start) {
  VCL d(g.n, MAXC);
  d[start] = 0;
  bool nl = false;
  VI upd_b(1, start);
  int i = 0;
  while (!upd_b.empty() && i < g.n) {
    VI upd;
    tra(from, upd_b) {
      tra(id, g.g[from]) {
        auto e = g.edges[id];
        int to = e.from ^ e.to ^ from;
        if (d[to] > d[from] + e.cost) {
          d[to] = d[from] + e.cost;
          upd.emb(to);
          if (i == g.n - 1) {
            d[to] = MINC;
            nl = true;
          }
        }
      }
    }
    upd_b = upd;
    i++;
  }
  if (nl) {
    i = 0;
    while (!upd_b.empty() && i < g.n - 1) {
      VI upd;
      tra(from, upd_b) {
        tra(id, g.g[from]) {
          auto e = g.edges[id];
          int to = e.from ^ e.to ^ from;
          if (d[to] != MINC) {
            d[to] = MINC;
            upd.emb(to);
          }
        }
      }
      upd_b = upd;
      i++;
    }
  }
  return d;
}

class time_measure {
  chrono::system_clock::time_point start;

public:
  time_measure() : start(chrono::system_clock::now()) {}
  void time() {
    out(chrono::duration_cast<chrono::milliseconds>(
            chrono::system_clock::now() - start)
            .count(),
        "ms");
  }
};

// todo
// graph行列表現
// ２部グラフ
// フローグラフ
// kruskal
//  文字列抜き出し

// TCC VVCL warshall(const GC&g){
//   VVCL mat(g.n,VCL(g.n,MAXC));
//   fou(i,0,g.n)mat[i][i]=0;
//   fou(from,0,si(g.n)){
//     tra(id,g.g[from]){
//       auto e=g.edges[id];int to=e.from^e.to^from;
//     }
//   }
//   return mat;
// }

signed main() {
  IOS;
  in(string, s);
  string ss;
  fou(i, 0, si(s)) {
    if (s[i] == 'A')
      ss.pub('A');
    if (i + 1 != si(s))
      if (s[i] == 'B' && s[i + 1] == 'C') {
        ss.pub('X');
        i++;
        continue;
      }
    if (s[i] == 'B')
      ss.pub('B');
    if (s[i] == 'C')
      ss.pub('C');
  }

  if (si(ss) < 2) {
    out(0);
    return 0;
  }
  VI before(si(ss) - 1);
  int ans = 0;

  int cuma = 0;
  fou(i, 0, si(ss)) {
    if (ss[i] == 'A')
      cuma++;
    if (ss[i] == 'B')
      cuma = 0;
    if (ss[i] == 'C')
      cuma = 0;
    if (ss[i] == 'X')
      ans += cuma;
  }
  out(ans);
}