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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.InputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {

  public static void main(String[] args) {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    Scanner sc = new Scanner(is);

    /* read */
    String s = sc.next();

    os.println(search(s));

  }

//  private static int search(String s) {
//
//    int n = s.length();
//
//    int sum = 0;
//    int j = 0;
//    while (0 <= j && j < n) {
//      // for each, find A
//      if (s.charAt(j) != 'A') {
//        j = s.indexOf("A", j);
//        continue;
//      }
//      // found A
//
//      int bcCount = 0;
//      int i = j + 1;
//      while (i < n - 1) {
//        if (s.charAt(i) == 'A') {
//          i++;
//        } else if (s.charAt(i) == 'B' && s.charAt(i + 1) == 'C') {
//          bcCount++;
//          i += 2;
//        } else {
//          break;
//        }
//      }
//      sum += bcCount;
//      j++;
//    }
//    return sum;
//  }

  private static int search(String s) {

    int n = s.length();
    char[] c = s.toCharArray();

    int sum = 0;
    int j = 0;
    while (0 <= j && j < n) {

      // for each, find A
      // found A
      if (c[j] != 'A') {
        j = s.indexOf("A", j + 1);
        continue;
      }

      int aCount = 0;
      int bcCount = 0;
      int i = j;
      while (i < n - 1) {
        if (c[i] == 'A') {
          aCount++;
          i++;
        } else if (c[i] == 'B' && c[i + 1] == 'C') {
          bcCount++;
          i += 2;
          sum += aCount;
        } else {
          break;
        }
      }
      j = i;
    }
    return sum;
  }

//  private static int search(String s) {
//
//    int n = s.length();
//
//    int sum = 0;
//    for (int j = 0; j < n; j++) {
//
//      // for each, find A
//      // found A
//      if (s.charAt(j) != 'A') continue;
//
//      int bcCount = 0;
//      int i = j + 1;
//      while (i < n - 1) {
//        if (s.charAt(i) == 'A') {
//          i++;
//        } else if (s.charAt(i) == 'B' && s.charAt(i + 1) == 'C') {
//          bcCount++;
//          i += 2;
//        } else {
//          break;
//        }
//      }
//      sum += bcCount;
//    }
//    return sum;
//  }
}