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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::io::*;
use std::str::FromStr;

fn main() {
    let mut s: Vec<char> = read::<String>().chars().collect();
    if s.len() < 2 {
        println!("0");
        return;
    }
    
    let mut ans: usize = 0;
    for i in 0..s.len()-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 += 1;
            let mut j = i;
            while j > 0 {
                j -= 1;
                if s[j] == 'A' {
                    ans += 1;
                    s[j] = 'B';
                    s[j+1] = 'C';
                    s[j+2] = 'A';
                } else {
                    break;
                }
            }
            j = i + 1;
            while j < s.len()-3 {
                j += 2;
                if s[j] == 'B' && s[j+1] == 'C' {
                    ans += 1;
                    s[j-1] = 'B';
                    s[j] = 'C';
                    s[j+1] = 'A';
                } else {
                    break;
                }
            }
        }
    }
    println!("{}", ans);
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}