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 <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>
#include <assert.h>
#include <unordered_set>
#include <random>



using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)














int main(){
    string s;
    cin >> s;
    ll ans = 0, abc = 0, a = 0, p = 0;
    bool f = true;
    for(ll i = 0; i < s.size(); i++){
        if(f){
            if(s[i] == 'A'){
                a++;
            }
            else if(s[i] == 'B'){
                if(a > 0){
                    f = false;
                    a--;
                    p = 2;
                }
            }
            else{
                a = 0;
            }
        }
        else{
            if(p == 2){
                if(s[i] == 'C'){
                    abc++;
                    p = 0;
                }
                else{
                    if(abc > 0){
                        ans += abc * (abc + 1) / 2;
                        ans += a * abc;
                    }
                    a = 0;
                    if(s[i] == 'A') a = 1;
                    abc = 0;
                    f = true;
                }
            }
            else if(p == 1){
                if(s[i] == 'B'){
                    p = 2;
                }
                else{
                    if(abc > 0){
                        ans += abc * (abc + 1) / 2;
                        ans += a * abc;
                    }
                    if(s[i] == 'A'){
                        a = a + abc + 2;
                    }
                    else{
                        a = 0;
                    }
                    abc = 0;
                    f = true;
                }
            }
            else{
                if(s[i] == 'A'){
                    p = 1;
                }
                else{
                    if(abc > 0){
                        ans += abc * (abc + 1) / 2;
                        ans += a * abc;
                    }
                    if(s[i] == 'B'){
                        p = 2;
                        a = abc - 1 + a;
                    }
                    else{
                        f = true;
                        a = 0;
                    }
                    abc = 0;
                }
            }
        }
        // cerr << i << " " << a << " " << abc << " " << ans << endl;
    }
    if(abc > 0){
        ans += abc * (abc + 1) / 2;
        ans += a * abc;
    }
    cout << ans << endl;
}