Problem:
Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#x<<" = "<<(x)<<endl;
#define PRINTA(a,first,last)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<(first)<<", "<<(last)<<")"<<endl;\
    for (int i=(first);i<(last);++i){cout<<#a<<"["<<i<<"] = "<<(a)[i]<<endl;}
#define PRINTP(p)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#p<<" = ("<<(p.first)<<", "<<(p.second)<<")"<<endl;
#define PRINTI(a,i)\
    cout<<"func "<<__func__<<": line "<<__LINE__<<": "<<#a<<"["<<#i<<"] = "<<#a<<"["<<(i)<<"] = "<<(a)[i]<<endl;
#else
#define PRINT(x)
#define PRINTA(a,first,last)
#define PRINTP(p)
#define PRINTI(a,i)
#endif

#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T& x, U y) {if (x < y) x = y;}
template <class T, class U> void amin(T& x, U y) {if (x > y) x = y;}

const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    ll A = -100;
    ll BC = -100;
    int n = S.size();
    bool reading_A = false;
    bool reading_BC = false;
    ll result = 0;
    S.append("BB");
    REP(i, n + 2) {
        if (S[i] == 'A') {
            if (reading_BC) {
                assert(A >= 0);
                assert(BC >= 0);
                result += A * BC;
                //printf("add %d * %d\n", A, BC);
                BC = 0;
                reading_BC = false;
                if (S[i - 1] == 'B') {
                    A = 0;
                }
            } else if (!reading_A) {
                A = 0;
                BC = 0;
            }
            A++;
            reading_A = true;
        } else if (S[i] == 'B') {
            if (i - 1 >= 0 && S[i - 1] == 'A') {
                reading_A = false;
                reading_BC = true;
            } else if (reading_BC) {
                if (S[i - 1] == 'C') {
                } else if (S[i - 1] == 'B') {
                    assert(A >= 0);
                    assert(BC >= 0);
                    result += A * BC;
                    //printf("add %d * %d\n", A, BC);
                    A = 0;
                    BC = 0;
                    reading_A = false;
                    reading_BC = false;
                }
            } else {
                A = -100;
                BC = -100;
                reading_A = false;
                reading_BC = false;
            }
        } else if (S[i] == 'C') {
            if (reading_BC) {
                if (S[i - 1] == 'B') {
                    BC++;
                } else if (S[i - 1] == 'C') {
                    assert(A >= 0);
                    assert(BC >= 0);
                    result += A * BC;
                    //printf("add %d * %d\n", A, BC);
                    A = 0;
                    BC = 0;
                    reading_A = false;
                    reading_BC = false;
                }
            } else {
                A = -100;
                BC = -100;
                reading_A = false;
                reading_BC = false;
            }
        }
        PRINT(i);
        PRINT(A);
        PRINT(BC);
        PRINT(reading_A);
        PRINT(reading_BC);
    }
    cout << result << endl;
    return 0;
}