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 (Python):
s = input()
n = len(s)
stat = 0
a = 0
bc = 0
abc = 0
ans = 0
def count():
    global ans, a, bc, abc
    newa = a+abc
    cnt = abc
    while cnt>0:
        ans += cnt
        cnt -= 1
        if a>0:
            a -= 1
            cnt += 1
        if bc>0:
            bc -= 1
            cnt += 1
    a = newa
    bc = 0
    abc = 0
i = 0
while i<n:
    if stat==0:
        if s[i:i+3]=='ABC':
            a = 0
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a = 1
            stat = 1
            i += 1
        else:
            i += 1
    elif stat==1:
        if s[i:i+3]=='ABC':
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a += 1
            i += 1
        else:
            stat = 0
    elif stat==2:
        if s[i:i+3]=='ABC':
            abc += 1
            i += 3
        elif s[i:i+2]=='BC':
            bc = 1
            stat = 3
            i += 2
        else:
            count()
            stat = 1
    else:
        if s[i:i+2]=='BC':
            bc += 1
            i += 2
        else:
            count()
            stat = 1
if stat>=2:
    count()
print(ans)

Solution (Python):
s = input().replace("BC", "D")
ans = 0
a = 0
for i in range(len(s)):
    if s[i] == "A":
        a += 1
    elif s[i] == "D":
        ans += a
    else:
        a = 0
print(ans)

Solution (Python):
s=input()

s=s.replace("BC","D")

count=0

c=0
while c<len(s):
    if s[c]=="A" or s[c]=="D":
        d=c
        while s[d]=="A" or s[d]=="D":
            d+=1
            if d==len(s):
                break
        an=0
        for i in range(c,d):
            if s[i]=="A":
                an+=1
            else:
                count+=an
        c=d
    else:
        c+=1

print(count)

Solution (Python):
S = input().replace('BC', 'X')

cnt = 0
ans = 0
for s in S[::-1]:
    if s == 'X':
        cnt += 1
    elif s == 'A':
        ans += cnt
    else:
        cnt = 0

print(ans)

Solution (Python):
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    s = [c for c in gete()]
    ans = 0
    a = 0
    cur = 0
    while cur < len(s) - 2:

        if "".join(s[cur:cur + 3]) == "ABC":
            ans += a + 1
            cur += 2
            s[cur] = "A"
        else:
            if s[cur] == "A":
                a += 1
            else:
                a = 0
            cur += 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s=list(input())
S=[]
n=0
while n+1<len(s):
    if s[n:n+2]==["B","C"]:
        S.append("D")
        n+=1
    else:
        S.append(s[n])
    n+=1
a=0
ans=0
for i in range(len(S)):
    if S[i]=="D":
        ans+=a
    elif S[i]=="A":
        a+=1
    else:
        a=0
print(ans)

Solution (Python):
def main():
    import re
    s = input()
    s = s.replace('BC', 'D')

    obj = re.findall('[AD]+', s)
    ans = 0

    for x in obj:
        cnt = 0
        for i in x:
            if i == 'A':
                cnt += 1
            if i == 'D':
                ans += cnt

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10**7)

ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

s = ns()

s = s.replace("BC", "D")

cnt = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'A':
        cnt += 1
    elif s[i] == 'D':
        ans += cnt
    else:
        cnt = 0

print(ans)

Solution (Python):






s = input()
n = len(s)
i = 0
ans = 0
count_a = 0

while i < n:
    if s[i] == 'A':
        count_a += 1
    elif i < n - 1 and s[i:i+2] == 'BC' and count_a > 0:
        ans += count_a
        i += 1
    else:
        count_a = 0
    i += 1
print(ans)

Solution (Python):
s = input()
ans = 0

a_stack = 0
i = 0
while i < len(s):
    v = s[i]
    if v == 'A':
        a_stack += 1
        pass
    elif v == 'B' and i < len(s)-1 and s[i+1] == 'C':
        ans += a_stack
        i += 1
    else:
        a_stack = 0
    i+=1
print(ans)

Solution (Python):
import sys
readline = sys.stdin.readline

S = readline().rstrip()
S = S.replace("BC","X")
S = S.replace("B",".").replace("C",".")
arr = S.split(".")
ans = 0
for a in arr:
  cnt = 0
  for i in range(len(a)):
    if a[i] == "X":
      ans += (i - cnt)
      cnt += 1
  
print(ans)

Solution (Python):
from collections import deque

s=input()
s=s.replace("BC","D")
N=len(s)

cnt=0
cntA=0
cntD=0

for i in range(N):
  if s[i]=="A":
    cntA += 1
  elif s[i]=="D":
    cnt += cntA
  else:
    cntA=0
    cntD=0
    
print(cnt)

Solution (Python):
s = input()
n = len(s)
ans = 0
cntA = 0
for i in range(n-1):
    if s[i] == 'A':
        cntA += 1
    elif s[i: i+2] == 'BC':
        ans += cntA
    elif i > 0 and s[i-1: i+1] == 'BC':
        continue
    else:
        cntA = 0

print(ans)

Solution (Python):
s = input()
n = len(s)
p = []
flag = False
for i in range(n-1):
    if flag:
        flag = False
        continue 
    if s[i] == "B" and s[i+1] == "C":
        flag = True
        p.append("
    elif s[i] == "A":
        p.append(s[i])
    else:
        p.append("!")

nowa = 0
ans = 0
for i in range(len(p)):
    if p[i] == "A":
        nowa += 1
    elif p[i] == "
        ans += nowa
    elif p[i] == "!":
        nowa = 0
print(ans)

Solution (Python):
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    s = input()
    i = 0
    cc = output = 0
    while i < len(s)-1:
        if s[i] == 'A':
            cc += 1
            i += 1
        elif s[i] == 'B' and s[i+1] == 'C':
            output += cc
            i += 2
        else:
            cc = 0
            i += 1
    print(output)

    return

if __name__ == '__main__':
    main()

Solution (Python):
import sys


import math
import collections

import bisect

import fractions

import copy
import heapq
import decimal

import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    s = list(input())
    target = ["A", "B", "C"]
    next = ["B", "C"]

    idx = 0
    ans = 0
    a_cnt = 0

    while len(s) - 3 >= idx:
        if [s[idx], s[idx + 1], s[idx + 2]] == target:
            ans += a_cnt
            ans += 1
            a_cnt += 1

            idx += 3
            while len(s) - 3 >= idx:
                if [s[idx], s[idx + 1], s[idx + 2]] == target:
                    ans += a_cnt + 1
                    idx += 3
                    a_cnt += 1
                else:
                    break

            while len(s) - 2 >= idx:
                if [s[idx], s[idx + 1]] == next:
                    ans += a_cnt
                    idx += 2
                else:
                    break

        elif s[idx] != "A":
            a_cnt = 0
            idx += 1
        elif s[idx] == "A":
            a_cnt += 1
            idx += 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
l = len(s)
t = [0]*l
c=0
ch=0
for i in range(l):
    if s[l-1-i]=='C' and c == 0:
        c=1
    elif s[l-i-1]=='B' and c == 1:
        c=0
        ch+=1
    elif s[l-i-1]=='A' and c == 1:
        c=0
        ch = 0
    elif s[l-i-1]=='A':
        c=0
    elif s[l-i-1]=='C' and c==1:
        ch=0
        c=1
    else:
        ch=0
        c=0
    t[l-i-1]=ch
ans = 0
for i in range(l):
    if s[i]=='A':
        ans += t[i]
print(ans)

Solution (Python):
s = input()

ss = s.replace('BC', '0').replace('A','1')

cnt = 0
ans = 0

for i in range(len(ss)-1, -1, -1):
    if ss[i] == 'B' or ss[i] == 'C':
        cnt = 0
    elif ss[i] == '1': ans += cnt
    else : cnt += 1
        
print(ans)

Solution (Python):
from collections import Counter

s = list(input())
chain = {}
abcs = []
kugiri = 0
countA = 0
ans = 0

for i in range(1,len(s)):
    if not((s[i] == "B" and s[i-1] == "C") or (s[i] == "B" and s[i-1] == "A") or (s[i] == "C" and s[i-1] == "B") or (s[i] == "A" and s[i-1] == "C") or (s[i] == "A" and s[i-1] == "A")):
        kugiri = i
        countA = 0
    elif s[i-1] == "A":
        countA += 1
    if s[i] == "C" and s[i-1] == "B":
        ans += countA

print(ans)

Solution (Python):
s = input().replace('BC','D')
d = 0
ans = 0
for c in s[-1::-1]:
    if c == 'D':
        d += 1
    elif c == 'A':
        ans += d
    else:
        d = 0
print(ans)

Solution (Python):
s = input()
s = s.replace("BC", "X")
cnt, ans = 0, 0
for i in s:
    if i == "A":
        cnt += 1
    elif i == "X":
        ans += cnt
    else:
        cnt = 0
print(ans)

Solution (Python):
s = input()
n = len(s)

i = 0
numA = 0
res = 0
while i < n-1:
    if s[i] == 'A':
        numA += 1
    elif s[i] == 'B' and s[i+1] == 'C':
        res += numA
        i += 1
    else:
        numA = 0
    i += 1
print(res)

Solution (Python):
s = input()
n = len(s)
s += "ZZ"
def make(s,i):
    cnt = 0
    a,bc = [],[]
    while True:
        if s[i] == "A":
            a.append(cnt)
            i += 1
        elif s[i:(i+2)] == "BC":
            bc.append(cnt)
            i += 2
        else:
            while s[i] != "A":
                if s[i] == "Z":
                    break
                i += 1
            return a,bc,i
        cnt += 1

def cnt(bc):
    c = 0
    for i in range(len(bc)):
        c += bc[i]-i
    return c

i = 0
ans = 0
while i < n:
    a,bc,i = make(s,i)
    ans += cnt(bc)
print(ans)

Solution (Python):
s = input()

S = []
N = len(s)
i = 0
while i < N:
    if i < N - 1 and s[i:i + 2] == 'BC':
        S.append('D')
        i += 2
    else:
        S.append(s[i])
        i += 1

ans = 0
c = 0
for l in S:
    if l == 'A':
        c += 1
    elif l == 'D':
        ans += c
    else:
        c = 0
print(ans)

Solution (Python):


s = input()
s = s.replace('BC', 'X')
ans = 0
a = 0
for i in range(len(s)):
    if s[i] == 'A':
        a += 1
    elif s[i] == 'X':
        ans += a
    else:
        a = 0
print(ans)

Solution (Python):
s = input()
s = s.replace('BC', 'T')
a_count = 0
ans = 0
for c in s:
    if c == 'A':
        a_count += 1
    elif c == 'T':
        ans += a_count
    else:
        a_count = 0
print(ans)

Solution (Python):
def main():

    s = list(input())
    ans = 0
    h = 0
    i = 0
    while i < len(s):
        if s[i] == "A":
            h += 1
            
            if s[i:i+3] == ["A", "B", "C"]:
                ans += h
                h -= 1
                i += 2
                s[i] = "A"
            else:
                i += 1
        else:
            h = 0
            i += 1
    return ans

if __name__ == '__main__':
    print(main())

Solution (Python):
def main():
    S = input()
    S = S.replace("BC", "D")

    ans = 0
    count_A = 0
    for c in S:
        if c == "A":
            count_A += 1
        elif c == "D":
            ans += count_A
        else:
            count_A = 0
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x,'b') 
def to_oct(x: int) -> str: return format(x,'o') 
def to_hex(x: int) -> str: return format(x,'x') 
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res

MOD=10**9+7

import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left

def main():
    s = IS()
    n = len(s)
    s = s[::-1]
    cb_num = 0
    a_num = 0
    cnt = 0
    i = 0
    while i < n:
        if cb_num == 0:
            if s[i:i+2] == 'CB':
                cb_num += 1
                i += 2
                continue
        if s[i:i+2] == 'CB':
            cb_num += 1
            i += 2
        elif s[i] == 'A':
            cnt += cb_num
            i += 1
        else:
            cb_num = 0
            i += 1
    print(cnt)

if __name__ == '__main__':
    main()

Solution (Python):
s = input()
s = s.replace("BC", "D")
gomi = 0
c = 0
for i in s:
  if i == "A":
      c += 1
  elif i == "D":
      gomi += c
  else:
      c = 0
print(gomi)

Solution (Python):
S = input()
S = S.replace("BC", "D")
result = 0
temp = 0
for s in S:
    if s == "A":
        temp += 1
    elif s == "D":
        result += temp
    else:
        temp = 0
print(result)

Solution (Python):
s = list(input()) 

def mergecount(A):
    cnt = 0
    n = len(A)
    if n>1:
        A1 = A[:n>>1]
        A2 = A[n>>1:]
        cnt += mergecount(A1)
        cnt += mergecount(A2)
        i1=0
        i2=0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt

ans=0
ss = []
i=0
finish_calc = 0
while i<len(s):
    if s[i]=='A':
        ss.append(1)
        i+=1
        finish_calc=0
    elif i+1<len(s):
        if s[i]=='B' and s[i+1]=='C':
            ss.append(0)
            finish_calc=0
            i+=2
            continue
        if finish_calc==0:
            ans += mergecount(ss)
            ss=[]
            i+=1
            finish_calc=1
        else:
            i+=1
    elif finish_calc==0:
        ans += mergecount(ss)
        ss=[]
        i+=1
        finish_calc=1
    else:
        i+=1

if finish_calc==0:
    ans += mergecount(ss)

print(ans)

Solution (Python):
s = input()

s = s.replace("BC", "D")

ans = 0
a_cnt = 0
for e in s:
    if e == "A":
        a_cnt += 1

    elif e == "D":
        ans += a_cnt

    else:
        a_cnt = 0

print(ans)

Solution (Python):
S = input()
newS = S.replace("BC","D")
A_count = 0
ans = 0
for i in range(len(newS)):
    if newS[i] == "A":
        A_count += 1
    elif newS[i] == "D":
        ans += A_count
    else:
        A_count = 0
print(ans)

Solution (Python):
s = input().replace('BC', 'o').replace('B', 'x').replace('C', 'x')

ans = cnt = 0
for c in s[::-1]:
    if c == 'x':
        cnt = 0
        continue
    elif c == 'o':
        cnt += 1
    else:
        ans += cnt
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    len_s=len(s)
    i=0
    cunt_a=0
    ans=0
    while i<len_s:
        if s[i]=='A':  
            cunt_a+=1
        elif i<len_s-1 and s[i:i+2]=="BC" and cunt_a>0:
            ans+=cunt_a
            i+=1
        else:
            cunt_a=0
        i+=1
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    len_s=len(s)
    i=0
    cunt_a=0
    ans=0
    while i<len_s:
        if s[i]=='A':  
            cunt_a+=1
        elif i<len_s-1 and s[i:i+2]=="BC" and cunt_a>0:
            ans+=cunt_a
            i+=1
        else:
            cunt_a=0
        i+=1
    print(ans)
if __name__=='__main__':
    main()

Solution (Python):
s=input()
ans=0
bc=0
i=len(s)-1
while i>=0:
  if s[i]=="A":
    ans+=bc
    i-=1
  elif i>0 and s[i-1:i+1]=="BC":
    bc+=1
    i-=2
  else:
    bc=0
    i-=1
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = '?' + read().rstrip().decode()[::-1]
ans = 0
cnt = 0
index = 0
for _ in range(len(s) + 1):
    if s[index] == 'C' and s[index + 1] == 'B':
        cnt += 1
        index += 1
    elif s[index] == 'A':
        ans += cnt
    else:
        cnt = 0
    index += 1
    if index >= len(s) - 1:
        break
if s[-1] == 'A':
    ans += cnt
print(ans)

Solution (Python):
S = input()
ans = 0
idx = 0
a_count = 0
while idx < len(S):
    if S[idx] == "A":
        a_count += 1
    elif S[idx:idx+2] == "BC":
        ans += a_count
        idx += 1
    else:
        a_count = 0
    idx += 1
print(ans)

Solution (Python):
s = input()
n = len(s)

res = 0
ac = 0
i = 0
while i<n:
    if s[i]=="A":
        ac += 1
    elif s[i]=="B" and i+1<n and s[i+1]=="C":
        res += ac
        i += 1
    else:
        ac = 0
    i += 1
print(res)

Solution (Python):
s = input()

s = s.replace('BC','D')

a = 0
ans = 0

for i in range(len(s)):
    if s[i] == 'A':
        a += 1
    elif s[i] == 'D':
        if a != 0:
            ans += a
    else:
        a = 0

print(ans)

Solution (Python):
s=input()
n=len(s)

cnt=0
ans=0

for i in range(n-1):
    if s[i]=="A":
        cnt+=1
    elif s[i:i+2]=="BC":
        ans+=cnt
    elif i>0 and s[i-1:i+1]=="BC":
        pass
    else:
        cnt=0

print(ans)

Solution (Python):
S=input().replace("BC","

cnt,point=0,0
for i in range(len(S)):
    if S[i]=="A":point +=1
    elif S[i]=="
    else:point=0
print(cnt)

Solution (Python):
def main(s):
  n=len(s)
  ans=0
  na=0
  i=0
  while i<n:
    if s[i]=='A':
      na+=1
      i+=1
    elif s[i]=='B' and i<n-1 and s[i+1]=='C' and na>0:
      ans+=na
      i+=2
    else:
      na=0
      i+=1
  print(ans)
if __name__=='__main__':
  s=list(input())
  main(s)

Solution (Python):
from collections import deque
s=list(input())
d=deque()
n=len(s)
acount=0
cnt=0
for i in range(n):
    d.append(s[i])
i=0
while i<n:
    if s[i]=="A":
        acount=acount+1
    elif (i<n-1)and(acount>0)and(s[i]=="B")and(s[i+1]=="C"):
        cnt=cnt+acount
        i=i+1
    else:
        acount=0
    i=i+1
    
    
print(cnt)

Solution (Python):
s=input() 
n=len(s)
cnt=0
temp=[0]*n
JinA=[] 
for i in range(n-1):
    if s[i]=="A":
        if i>0:
            temp[i]+=temp[i-1]+1 
        else:
            temp[i]+=1
    if s[i]=="B" and s[i+1]=="C" and i>0:
        temp[i+1]+=temp[i-1]
        cnt+=temp[i-1] 

print(cnt)

Solution (Python):
s = input()
s = s.replace("BC", "X")

ans = 0
a_count = 0

for i in range(len(s)):
    if s[i] == "A":
        a_count += 1
    elif s[i] == "X":
        ans += a_count
    else:
        a_count = 0

print(ans)

Solution (Python):
s = input().replace('BC', 'D')

a = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'A':
        a += 1
    elif s[i] == 'D':
        ans += a
    else:
        a = 0

print(ans)

Solution (Python):
s = input()
n = len(s)

s = s.replace("BC","X")
s += "DD"
nn = len(s)

alen = 0
ans = 0
for i in range(nn):
    if s[i] == "A":
        alen += 1
    elif s[i] == "X":
        ans += alen
    else:
        alen = 0
        
print(ans)

Solution (Python):
s=input()
s=s.replace("BC","D")
n=len(s)
ans=0
cnt=0
for i in range(n-1,-1,-1):
    if s[i]=="B" or s[i]=="C":
        cnt=0
    elif s[i]=="D":
        cnt+=1
    else:
        ans+=cnt
print(ans)

Solution (Python):
S = input()
N = len(S)
ans = 0
cnt = 0
i = 0
while i < N:
  if S[i] == "A":
    cnt += 1
    i += 1
  elif "".join(S[i:i+2]) == "BC":
    ans += cnt
    i += 2
  else:
    cnt = 0
    i += 1
print(ans)

Solution (Python):
s=input()
n=0
c=0
i=0
while i<len(s):
  if s[i]=="A":
    c+=1
  elif s[i:i+2]=="BC":
    n+=c
    i+=1
  else:
    c=0
  i+=1
print(n)

Solution (Python):
sl = list(input())
dl = []

n = len(sl)
i = 0
while i < n:

    if i < n-1 and sl[i] == "B" and sl[i+1] == "C":
        dl.append("D")
        i += 2
    else:
        dl.append(sl[i])
        i += 1

ans = 0
c = 0
for d in dl:
    if d == "A":
        c += 1
    elif d == "D":
        ans += c
    else:
        c = 0


print(ans)

Solution (Python):
s = list(input())
t = []
ls = len(s)
for i in range(ls):
    if s[i] == "A":
        t.append("A")
    elif s[i] == "B":
        if not i == ls - 1:
            if s[i + 1] == "C":
                t.append("D")
            else:
                t.append("B")
    else:
        if not i == 0:
            if not s[i - 1] == "B":
                t.append("C")
t = "".join(t).split("B")
for u in range(len(t)):
    t[u] = t[u].split("C")
ans = 0
for u in t:
    for v in u:
        dcnt = 0
        v = list(v)
        for i in range(len(v)):
            if v[i] == "D":
                ans += (i - dcnt)
                dcnt += 1
print(ans)

Solution (Python):

"""
ABC→BCA
ABCBC→BCABC→BCBCA
AABC→ABCA→BCAA

"BC"の文字列は絶対に不変で、これが"A"を左から右にキャリーする役割を持つ。
それ以外の文字列、Bの後にCが無い状態もしくは、Cの前にBが無い時、キャリーが終わる。
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

s = readline().rstrip().decode('utf-8')
ans = 0 
a_ct = 0
bc_ct = 0
flag = 0
for i in range(len(s)-1):
    if flag:
        flag = 0
        continue
    if s[i] == "A":
        a_ct += 1
    elif s[i] == "B":
        if s[i+1] == "C":
            bc_ct += 1
            ans += a_ct
            flag = 1
        else:
            a_ct = 0
            bc_ct = 0
    else:
        a_ct = 0
        bc_ct = 0

print(ans)

Solution (Python):





s = input()

numBC = 0
total = 0
i = len(s)-1
while i >= 0:    
    if s[i] == 'A':
        total += numBC
    elif s[i] == 'C' and i > 0 and s[i-1] == 'B':
            numBC += 1
            i -= 1
    else:
        numBC = 0
    i-=1

print(total)

Solution (Python):
import sys
import re
import math  
import bisect  
import queue  


def input():
    return sys.stdin.readline().rstrip()



















def main():
    S = input()

    S = re.sub(r'(BC)', 'X', S)

    ans = 0
    cnt = 0
    for s in S[::-1]:
        if s == 'X':
            cnt += 1
        elif s == 'A':
            ans += cnt
        else:
            cnt = 0

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for i in range(len(l)-1,-1,-1):
    if l[i]!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Solution (Python):
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for c in l[::-1]:
    if c!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Solution (Python):
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for c in l[::-1]:
    if c!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Solution (Python):
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for i in range(len(l)-1,-1,-1):
    if l[i]!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Solution (Python):
s=input().replace("BC","D")

ans=0
count=0
for i in range(len(s)):
    if s[i]=="A":
        count+=1
    elif s[i]=="D":
        ans+=count
    else:
        count=0

print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    S = input().strip()
    a_cnt = 0
    state = 0
    ans = 0
    for s in S:
        if s == 'A':
            if state == 0:
                a_cnt += 1
            else:
                a_cnt = 1
                state = 0
        elif s == 'B':
            if state == 0:
                state = 1
            else:
                a_cnt = 0
        elif s == 'C':
            if state == 1:
                ans += a_cnt
                state = 0
            else:
                a_cnt = 0

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    S = input().strip()
    a_cnt = 0
    state = 0
    ans = 0
    for s in S:
        if s == 'A':
            if state == 0:
                a_cnt += 1
            else:
                a_cnt = 1
                state = 0
        elif s == 'B':
            if state == 0:
                state = 1
            else:
                a_cnt = 0
        elif s == 'C':
            if state == 1:
                ans += a_cnt
                state = 0
            else:
                a_cnt = 0

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
s = sys.stdin.readline().rstrip()
N = len(s)

def next_abc(ac, start):
    for i in range(start, N):
        if ac and i < N-1 and s[i] == 'B' and s[i+1] == 'C':
            return ac, i+2
        elif s[i] == 'A':
            ac += 1
        else:
            ac = 0
    return 0, N

ans = ac = i = 0
while i < N:
    ac, i = next_abc(ac, i)
    ans += ac
print(ans)

Solution (Python):
import re
s=input()
ans=0

s=re.sub('BC', 'D', s)

re_ad=re.compile(r'[AD]+')
ad=re.findall(re_ad,s)


for adi in ad:
    adi_a=0
    for adij in adi:
        if(adij=='A'):
            adi_a+=1
        else:
            ans+=adi_a

print(ans)

Solution (Python):
s = input()
s = s.replace("BC","Z")
cnt = 0
c = 0
for i in s:
	if i == "A":
		c += 1
	elif i == "Z":
		cnt += c
	else:
		c = 0
print (cnt)

Solution (Python):
s = str(input())
lis = []
cnt = 0

while cnt < len(s):
    if cnt+1 < len(s) and (s[cnt] == "B" and s[cnt+1] == "C"):
        lis.append("D")
        cnt += 1
    else:
        lis.append(s[cnt])
    cnt += 1
  

ans = 0
num = 0
for i in range(len(lis)):
    if lis[i] == "A":
        num += 1
    elif lis[i] == "D":
        ans += num
    else:
        num = 0
print(ans)

Solution (Python):
s=input()
N=len(s)
ans=0
data=[]
i=0
while N>i:
    if s[i]=='A':
        data.append('X')
        i+=1
    elif s[i]=='B' and i<len(s)-1 and s[i+1]=='C':
        data.append('A')
        i+=2
    else:
        index=[]
        count=0
        for j in range(0,len(data)):
            if data[j]=='A':
                index.append(j)
                count+=1

        for j in range(count):
            ans+=index[j]-j
        data=[]
        i+=1
else:
    if data:
        index=[]
        count=0
        for j in range(0,len(data)):
            if data[j]=='A':
                index.append(j)
                count+=1

        s=0
        for j in range(count):
            s+=index[j]-j

        ans+=s

print(ans)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    S = input()
    S = S.replace("BC", "X")
    cnt_a = 0
    answer = 0
    for s in S:
        if s == "A":
            cnt_a += 1
        elif s == "X":
            answer += cnt_a
        else:
            cnt_a = 0
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    S = input()
    S = S.replace("BC", "X")
    S = S.replace("C", "B")
    S = S.split("B")
    answer = 0
    for s in S:
        place = []
        c = 0
        n = len(s)
        for i in range(n):
            if s[i] == "A":
                c += 1
                place.append(i + 1)
        for p in place:
            c -= 1
            answer += n - c - p
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
S = input()
S = S.replace("BC", "X")
n = len(S)
ans = 0
cnt_a = 0

for s in S:
    if s == "X":
        ans += cnt_a
    elif s == "A":
        cnt_a += 1
    else:
        cnt_a = 0

print(ans)

Solution (Python):
S = input()
S = S.replace("BC", "X")
ans = 0
lo = 0
while True:
    while lo < len(S) and (S[lo] == "B" or S[lo] == "C"): lo += 1
    if lo == len(S): break
    hi = lo
    X = 0
    while hi < len(S) and S[hi] != "B" and S[hi] != "C":
        if S[hi] == "X": 
            ans += hi - lo
            lo += 1 
        hi += 1
    lo = hi
print(ans)

Solution (Python):
s=input()

while 'BC' in s:
    s=s.replace('BC','D')
ans=0
a=0
for i in range (len(s)):
    if s[i]!='A' and s[i]!='D':
        a=0
        continue
    elif s[i]=='A':
        a+=1
    elif s[i]=='D':
        ans+=a
print(ans)

Solution (Python):
s = input()
n = len(s)
i = 0
j = 0
ans = 0
while i < n:
    if s[i] == "A":
        j += 1
        i += 1
    elif i + 1 < n and s[i:i + 2] == "BC":
        ans += j
        i += 2
    else:
        j = 0
        i += 1
print(ans)

Solution (Python):
s=list(input())
x=[]
for i in range(len(s)):
    if s[i]=='A':
        x.append(0)
    elif s[i]=='B':
        x.append(1)
    elif i>0 and s[i]=='C' and s[i-1]=='B':
        x[-1]=3
    else:
        x.append(2)
ans=0
tmp0=0
for i in range(len(x)):
    if x[i]==0:
        tmp0+=1
    if x[i]==3:
        ans+=tmp0
    if x[i]!=0 and x[i]!=3:
        tmp0=0
print(ans)

Solution (Python):
s = input()
s = s.replace("BC","D")
a,ans = 0,0

for i in s:
    if i == "A":
        a += 1
    elif i == "D":
        ans += a
    else:
        a = 0
print(ans)

Solution (Python):
s=input()
s=s.replace("BC","D")
s=s.replace("B","E")
s=s.replace("C","E")
s=s.split("E")

t=[]
for ss in s:
  if len(ss)!=0:
    t.append(ss)
ans=0

for tt in t:
  d_cnt=0
  for i,ttt in enumerate(tt):
    if ttt=="D":
      ans+=i-d_cnt
      d_cnt+=1
print(ans)

Solution (Python):
s = input()


r = list(reversed(list(s)))
r = ['X'] + r + ['X']

BC_cnt = 0
A_cnt = 0
cnt = 0
for i in range(1, len(s) + 1):
    if r[i] == 'A':
        cnt += BC_cnt
        
    elif r[i] == 'C' and r[i + 1] == 'B':
        BC_cnt += 1
    elif r[i - 1] == 'C' and r[i] == 'B':
        pass
    else:
        BC_cnt = 0
print(cnt)