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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

class Program
{
    static void Main(string[] args)
    {
        new Program().Calc();
    }

    Scanner cin;

    public Program() { }

    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    void Calc()
    {
        cin = new Scanner();

        string S = cin.next();

        int index = 0;
        int count = 0;

        while (true)
        {
            int findIndex = S.IndexOf("ABC", index);

            if (findIndex == -1)
            {
                WriteLine(count);
                return;
            }

            ++count;

            for (int i = findIndex - 1; i >= index; --i)
            {
                if (S[i] == 'A')
                {
                    ++count;
                }
                else
                {
                    break;
                }
            }

            index = findIndex;

            index += 3;

            int tmpCount = 0;
            int bcBonus = 1;

            while (index + 1 < S.Length)
            {
                if (S[index] == 'B' && S[index + 1] == 'C')
                {
                    count += bcBonus;
                    index += 2;
                }
                else if(index + 2 < S.Length && S[index] == 'A' && S[index + 1] == 'B' && S[index + 2] == 'C')
                {
                    count += 2 + tmpCount;
                    index += 3;
                    bcBonus += 1;

                    tmpCount = 0;
                }
                else if (S[index] == 'A')
                {
                    if (0 <= index - 3 && S[index - 3] == 'A' && S[index - 2] == 'B' && S[index - 1] == 'C')
                    {
                        tmpCount += 2;
                        index += 1;
                        bcBonus += 1;
                    }
                    else
                    {
                        tmpCount += 1;
                        index += 1;
                        bcBonus += 1;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
}

public static class StringExtension
{
    public static char GetChar1(this string str, int indexOneBase)
    {
        return str[indexOneBase - 1];
    }
}

class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}