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()

Amemo = 0
result = 0
A = False
B = False
C = False
for c in s:
    if c == 'A':
        if B:
            Amemo = 0
        A = True
        B = False
        C = False
        Amemo += 1
    elif c == 'B':
        if A or C:
            A = False
            B = True
            C = False
        else:
            Amemo = 0
            A = False
            B = False
            C = False
    elif c == 'C':
        if B:
            result += Amemo
            A = False
            B = False
            C = True
        else:
            Amemo = 0
            A = False
            B = False
            C = False
print(result)

Solution (Python):
s = input()
K = []
for i, l in enumerate(s):
    if l == "A":
        K.append(1)
    elif i+1 < len(s) and l+s[i+1] == "BC":
        K.append(2)
    elif i > 0 and s[i-1]+l == "BC":
        continue
    else:
        K.append(0)

ans = 0
ct_1 = 0
for k in K:
    if k == 1:
        ct_1 += 1
    elif k == 2:
        ans += ct_1
    else:
        ct_1 = 0

print(ans)

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

import re
SSS = re.split('[BC]', SS)

ans = 0
for i in SSS:
    acnt = 0
    for g in i:
        if g == 'A':
            acnt += 1
        else:
            ans += acnt
print(ans)

Solution (Python):
import numpy as np
from collections import defaultdict
from functools import reduce

s = input()
N = len(s)
old_idx = 0
res = 0

lis = []

i = 0
while i < N:
    if s[i] == 'A':
        cnt = 1
        while i + cnt < N and s[i+cnt] == 'A':
            cnt += 1
        lis.append((s[i], cnt))
        i += cnt
    else:
        lis.append((s[i], 1))
        i += 1

lisN = len(lis)

i = 0
while i < lisN-2:
    if lis[i][0] == 'A' and lis[i+1][0] == 'A':
        lis[i+1] = ('A', lis[i][1]+lis[i+1][1])
        
    if lis[i][0] == 'A' and lis[i+1][0] == 'B' and lis[i+2][0] == 'C':
        res += lis[i][1]
        lis[i+2] = ('A', lis[i][1])
        lis[i] = ('B', 1)
        lis[i+1] = ('C', 1)
        

    i += 1

print(res)

Solution (Python):
S=input()
lst=[]
i=0
while i<len(S):
    if S[i]=='A':lst+=[1]
    elif S[i]=='B' and i<len(S)-1 and S[i+1]=='C':lst+=[2];i+=1
    else:lst+=[0]
    i+=1
lll=[]
i=0
while i<len(lst):
    c=i
    while i<len(lst) and lst[c]==lst[i]:
        i+=1
    lll+=[[lst[c],i-c]]
r=0
i=0
while i<len(lll)-1:
    if lll[i][0]==1 and lll[i+1][0]==2:
        r+=lll[i][1]*lll[i+1][1]
        lll[i],lll[i+1]=lll[i+1],lll[i]
        if i<len(lll)-2 and lll[i+2][0]==1:
            lll[i+2][1]+=lll[i+1][1]
    i+=1
print(r)

Solution (Python):
S = list(input())
LI = [[]]
for i in range(len(S)):
    if S[i] == 'A':
        LI[-1].append(0)
    elif S[i] == 'B':
        if i != len(S)-1:
            if S[i+1] == 'C':
                LI[-1].append(1)
            else:
                LI.append([])
    elif S[i] == 'C':
        if i != 0:
            if S[i-1] != 'B':
                LI.append([])
ansl = []
for i in range(len(LI)):
    cnt = 0
    for j in range(len(LI[i])):
        if LI[i][j] == 0:
            ansl.append(0)
            cnt += 1
        else:
            ansl.append(cnt)
ans = sum(ansl)
print(ans)

Solution (Python):
s=input()
t=s.replace("BC","D")
"""print(t)"""
u=""
cnt=0
dcnt=0
for i in range(1,len(t)+1):
  num=(-1)*i
  """print("t",t[num])"""
  if t[num]=="B" or t[num]=="C":
    dcnt=0
  if t[num]=="D":
    dcnt+=1
  if t[num]=="A":
    cnt+=dcnt
  
print(cnt)

Solution (Python):
import numpy as np
words = lambda t : list(map(t, input().split()))

str = input()
s = [c for c in str]
total = 0
i = 0
preva = 0
while i < len(s):
    
    cnta = preva
    preva = 0
    cntbc = 0
    if s[i] == 'A':

        while i < len(s):
            
            if s[i] == 'A':
                cnta += 1
                i+=1
            else:
                break
        while i < len(s) - 1:
            
            if s[i] == 'B' and s[i+1] == 'C':
                cntbc += 1
                i += 2
            else:
                break
        if cnta > 0 and cntbc > 0:
            total += cnta * cntbc
            preva = cnta
        else:
            preva = 0
    else:
        i += 1
print(total)

Solution (Python):
s=list(input())
s_new=[]
ans=0
flag=False
for i in range(len(s)-1):
  if flag==True:
    flag=False
    continue
  if s[i]=="B" and s[i+1]=="C":
    s_new.append("P")
    flag=True
  else:
    s_new.append(s[i])
s_new="".join(s_new)
import re
s_new=re.split("[BC]",s_new)
s_new=[list(i) for i in s_new if i]
for i in range(len(s_new)):
  count=0
  for j in range(len(s_new[i])):
    if s_new[i][-j-1]=="P":
      count+=1
    else:
      ans+=count
print(ans)

Solution (Python):
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
N = len(s)

cntA = 0
ans = 0
i = 0
while i < N:
  if s[i] == "A":
    cntA += 1
  elif s[i:i+2] == "BC":
    ans += cntA
    i += 1
  else:
    cntA = 0
  i += 1

print(ans)

Solution (Python):

import sys
from collections import Counter


def solve(S: str):
    tmp = []
    i = 0
    while i < len(S):
        j = i+1
        if S[i: i+3] == 'ABC':
            j = i
            while True:
                if S[j:j+3] == 'ABC':
                    j += 3
                elif S[j:j+2] == 'BC':
                    j += 2
                else:
                    break
            jl = i
            while jl >= 0:
                if S[jl] != 'A':
                    break
                jl -= 1
            jl += 1


            if len(tmp)>0 and tmp[-1][1] == jl:
                tmp[-1][0] += S[jl: j]
                tmp[-1][1] = j
            else:
                tmp.append([S[jl: j], j])

        i = j
    ans = 0
    for elem, _ in tmp:
        cnt_a = Counter(elem)['A']
        for i in range(len(elem)):
            if elem[i] == 'A':
                cnt_a -= 1
                ans += (len(elem)-1-i-cnt_a)//2
                assert (len(elem)-1-i-cnt_a)%2==0

    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  
    solve(s)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
line = 0
Acount = 0
ans = 0
leng = len(S)
while line<leng-1:
    if S[line] == "A":
        Acount += 1
        line += 1
    elif S[line]== "B" and S[line+1]=="C":
        ans += Acount
        line += 2
    else:
        Acount = 0
        line += 1
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()

aa = 0 
i = 0
ans = 0

while i < len(s):
    if s[i] == 'A':
        aa += 1
    elif s[i] == 'B':
        if i+1 != len(s) and s[i+1] == 'C':
            ans += aa
            i += 1
        else:
            aa = 0
    elif s[i] == 'C':
        aa = 0
    i += 1

print(ans)

Solution (Python):


import math

def main():
    s = list(raw_input())
    substr = ''
    total = []
    cnt = 0
    m = False
    for i in s:
        if i == 'A' and m == False:
            cnt += 1
        elif i == 'A':
            cnt = 1
            m = False
        elif i == 'B' and m == False:
            m = True
        elif i == 'C' and m == True:
            total.append(cnt)
            m = False
        else:
            m = False
            cnt = 0
    result = 0
    for i in total:
        result += i
    print result

if __name__ == "__main__":
    main()

Solution (Python):
S = list(input())
N = len(S)

i = 0
ans = 0
T = list(reversed(S))
bc_cnt = 0
bc = False
for i in range(N):
    t = T[i]
    if bc:
        if t == "A":
            ans += bc_cnt
        elif t == "C" and i < N - 1 and T[i + 1] == "B":
            bc_cnt += 1
        elif t == "B" and T[i-1] == "C":
            continue
        else:
            bc_cnt = 0
            bc = False

    elif t == "C" and i < N - 1 and T[i + 1] == "B":
        bc = True
        bc_cnt += 1
print(ans)

Solution (Python):
s=list(input())
n=len(s)
a=0
b=[]
i=0
def ac(b):
  a=b[::-1]
  t,c=0,0
  for i in range(len(b)):
    if a[i]==0:c+=i-t;t+=1
  return c
while i<n:
  f=True
  if s[i]=="A":b.append(0);f=False
  if i<n-1:
    if s[i]+s[i+1]=="BC":b.append(1);f=False;i+=1
  if f:
    a+=ac(b)
    b=[]
  i+=1
print(a+ac(b))

Solution (Python):
s = str(input())
nexts = ''
flag1 = 0
count1 = 0
count2=0
ans = 0
que1 = ''
bigque = []
for i in range(len(s)):
    if flag1 == 1:
        if s[i] == 'A':
            flag1 = 0
            nexts += 'b'
            nexts += 'a'
        if s[i] == 'B':
            nexts += 'b'
        if s[i] == 'C':
            nexts += 'd'
            flag1 = 0
    else:
        if s[i] == 'A':
            nexts += 'a'
        if s[i] == 'B':
            flag1 += 1
        if s[i] == 'C':
            nexts += 'c'
for i in range(len(nexts)):
    if nexts[i] == 'a':
        que1 += 'a'
        count2+=1
    elif nexts[i] == 'd':
        que1 += 'd'
        count2+=1
    else:
        if count2>0:
            bigque.append(que1)
            count2=0
        que1 = ''
if len(que1)!=0:
  bigque.append(que1)
for i in range(len(bigque)):
    for j in range(len(bigque[i])):
        if bigque[i][j] == 'a':
            count1+=1
        if bigque[i][j] == 'd':
            ans += count1
    count1 = 0
print(ans)

Solution (Python):

import sys
INF = float("inf")


def solve(s: str):

    ans = 0
    A = 0
    B = 0
    process = []
    for c in s:
        if c == "A":
            if B == 0:
                A += 1
            else:
                A = 1
                B = 0
        elif A > 0 and c == "B":
            if B == 0:
                B = 1
            else:
                B = 0
                A = 0
        elif A > 0 and B > 0 and c == "C":
            ans += A
            B = 0
        else:
            A = 0
            B = 0
    print(ans)
    

    
    
    
    
    
    

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  
    solve(s)


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
L = [" "]
for s in S:
  if s == 'A':
    L.append('0')
  elif s == 'B':
    L.append('B')
  elif s == 'C' and L[-1] == 'B':
    L[-1] = '1'
  else:
    L.append('C')

def func(a,b):

  return a * b

ans = 0
a,b = 0,0
i = 0
while i < len(S):
  if S[i] == 'A':
    if b != 0:
      ans += func(a,b)
      b = 0
    a += 1
  elif S[i:i + 2] == 'BC':
    b += 1
  elif S[i - 1:i + 1] == "BC":
    ()
  else:
    ans += func(a,b)
    a,b = 0,0
  i += 1
print(ans + func(a,b))

Solution (Python):

import sys

sys.setrecursionlimit(210000)
S = list(input()) + ['D']*100
N = len(S)-10




memo = [-1] * (N+100) 



def check(k):
    if memo[k] != -1:
        return memo[k]
    if S[k+1] == 'B' and S[k+2] == 'C':
        memo[k] = check(k+2) + 1
        return memo[k]
    if S[k+1] == 'A':
        memo[k] = check(k+1)
        return memo[k]
    memo[k] = 0
    return memo[k]

ans = 0
for i in range(N):
    if S[i] =='A':
        ans += check(i)
print (ans)

Solution (Python):
S=input()
N=len(S)
i=0
ans=0
d=0

p=""
while i<N:
  if S[i]=="A":
    if p=="B":
      d=0
    d+=1
  elif S[i]=="B":
    if p=="B":
      d=0
  else:
    if p=="B":
      ans+=d
    else:
      d=0
      
  p=S[i]
  i+=1
print(ans)

Solution (Python):
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

def main():
    s = SI().strip()

    if len(s) <= 2:
        print(0)
        return
    
    prepre = s[0]
    previous = s[1]
    if previous == 'A' and prepre == 'A': A_counter = 2
    elif previous == 'A' or (prepre == 'A' and previous == 'B'): A_counter = 1
    else: A_counter = 0
    jumps = 0
    for char in s[2:]:
        if char == 'A':
            if previous == 'A':
                A_counter += 1
                prepre = previous
            elif previous == 'B':
                prepre = previous
                A_counter = 1
                previous = 'A'
            elif previous == 'C':
                prepre = previous
                A_counter += 1
                previous = 'A'
        elif char == 'B':
            if previous == 'A':
                prepre = previous
                previous = 'B'
            elif previous == 'B':
                prepre = previous
                A_counter = 0
            elif previous == 'C':
                
                if prepre != 'B':
                    A_counter = 0
                prepre = previous
                previous = 'B'
        elif char == 'C':
            if previous == 'A':
                A_counter = 0
                prepre = previous
                previous = 'C'
            elif previous == 'B':
                jumps += A_counter
                prepre = previous
                previous = 'C'
            elif previous == 'C':
                A_counter = 0
                prepre = previous
    print(jumps)

main()

Solution (Python):
s = input()
ans = 0
pre = 0
while s.rfind('A') >= 0:
    rec = 0
    index = s.rfind('A')
    t = s[index+1:]
    while t[:2]=='BC':
        rec += 1
        t=t[2:]
    if len(t)==0:
        rec += pre
    pre = rec
    ans += rec
    s = s[:index]
print(ans)

Solution (Python):
S = input()
ans = 0
bc = 0
c_flag = 0
for i in range(len(S) - 1, -1, -1):
    if S[i] == "A" and c_flag == 0:
        ans += bc
    elif S[i] == "C" and c_flag == 0:
        c_flag = 1
    elif S[i] == "B" and c_flag == 1:
        bc += 1
        c_flag = 0
    else:
        bc = 0
        if S[i] == "C":
            c_flag = 1
        else:
            c_flag = 0
print(ans)

Solution (Python):
def give(list):
    i=0
    count=0
    count1=0
    while i<len(list)-2:
        if list[i]=='A':
            count1+=1
            if list[i]=='A' and list[i+1]=='B' and list[i+2]=='C':
                list[i+2]='A'
                
                i+=2
                count+=count1
                count1-=1
                
            else:
                i+=1
        else:
            count1=0
            i+=1
    return count
s=list(input())
print(give(s))

Solution (Python):








def f(S):
    a = 0
    n = 0
    b = False

    for c in S:
        if c == 'A':
            if b:
                a = 1
                b = False
            else:
                a += 1

        elif c == 'B':
            if b:
                a = 0
            else:
                b = True
                
        else:
            if b:
                n += a
                b = False
            else:
                a = 0
                
    return n


def main():
    S = input()

    print(f(S))
 


if __name__ == '__main__':
    main()

Solution (Python):
import re
s=re.split("B|C",input().replace("BC","D"))
c,cd=0,0
for i in s:
    for j in range(len(i)):
        if i[j]=="D":
            c+=j-cd
            cd+=1
    cd=0
print(c)

Solution (Python):
s=input().replace('BC','X')
i=0
count=0
while i<len(s):
    if s[i]=='A':
        p=i
        i+=1
        while i<len(s) and( s[i]=='A' or s[i]=='X'):
            if s[i]=='X':
                count+=i-p
                p+=1
            i+=1
    else:
        i+=1
print(count)

Solution (Python):
import sys
INPUT = sys.stdin.readline
S = list(input())
S.reverse()
L = len(S)
A, B, C = 'A', 'B', 'C'

followingAs = 0
searchABC = 0
ans = 0

while S:

    last = S.pop()

    if last == A:
        if searchABC == 0:
            searchABC = 1
            followingAs += 1
            continue
        
        elif searchABC == 1:
            followingAs += 1
            continue
        
        else: 
            searchABC = 1
            followingAs = 1
            continue
    
    elif last == B:
        if searchABC == 0:
            continue

        elif searchABC == 1:
            searchABC += 1
            continue

        else: 
            searchABC = 0
            followingAs = 0
            continue

    else: 

        if searchABC == 0:
            continue

        elif searchABC == 1:
            followingAs = 0
            searchABC = 0
            continue

        else:
            ans += followingAs
            searchABC = 1
            continue

print(ans)

Solution (Python):

s = input()


s_int = [1 if c == "A" else 2 if c == "B" else 3 for c in s ]

s_len = len(s_int)

write_count = 0

s_pointer = 0

a_count = 0

while (s_pointer + 1 < s_len):
    
    if s_int[s_pointer] == 1:
        a_count += 1
    elif s_int[s_pointer] == 2 and s_int[s_pointer + 1] == 3:
        
        write_count += a_count
        s_pointer += 1
    else:
        
        a_count = 0
    s_pointer += 1

print(write_count)

Solution (Python):

s = list(str(input()))
slen = len(s)
Anum = 0
cflag = 0
ans = 0
for i in range(slen):
    if s[i] == "A":
        if cflag == 0:
            Anum+=1
        else:
            Anum = 1
            cflag = 0
    elif s[i] == "B":
        if i > 0:
            if s[i-1] == "A":
                cflag = 1
            else:
                Anum = 0
                cflag = 0
        else:
            pass
    elif s[i] == "C":
        if cflag == 1:
            ans+=Anum
            s[i] = "A"
            cflag = 0
        else:
            Anum = 0
            
print(ans)

Solution (Python):
s = input()

s = s.replace('BC','F')
s = s.replace('C','B')

s = s.split('B')

def cnt(st):
	i = 0
	res = 0
	for s in st:
		if s == "A":
			i += 1
		if s == "F":
			res += i
	return res

ans = 0
for part in s:
	ans += cnt(part)

print(ans)

Solution (Python):
s = input().rstrip()

concurrent_BC_cnt = 0
old1 = ""
cnt = 0
for tmp in (reversed(s)):
	if tmp == "A":
		if old1 == "B":
			cnt += concurrent_BC_cnt
		else:
			concurrent_BC_cnt = 0
			old1 = "A"
		
	else:
		if tmp == "B" and old1 == "C":
			concurrent_BC_cnt += 1
		elif tmp == "C" and old1 == "B":
			pass
		else:
			concurrent_BC_cnt = 0
			
		old1=tmp
	
print(cnt)

Solution (Python):
s=input()
a=0
r=0
for i in range(len(s)-1):
  if s[i]=="A":
    a=a+1
    if s[i+1]=="C":
      a=0
  elif s[i]=="B":
    if s[i+1]=="C":
      r=r+a
    else:
      a=0
  elif s[i]=="C":
    if s[i+1]=="C":
      a=0
print(int(r))

Solution (Python):
S = input()
cnt = 0
length = len(S)
mem = 0
for i, s in enumerate(S):
    w = 1
    if (i < mem): continue
    while(s == "A"):
        if (i+3 > length):
            mem = i
            break
        elif (S[i+1] == "A"):
            i += 1
            w += 1
        elif (S[i+1:i+3] == "BC"):
            i += 2
            cnt += w
        else:
            mem = i
            break
print(cnt)

Solution (Python):
S = input()

w = ""
l = ""
L = []
for s in S:
    w += s
    if w == "A":
        l += w
        w = ""
    elif w == "B":
        continue
    elif w == "C":
        w = ""
        if l != "":
            L.append(l)
            l = ""

    elif w == "BC":
        l += "X"
        w = ""
    elif w == "BA":
        w = ""
        if l != "":
            L.append(l)
            l = ""
        l += "A"
    elif w == "BB":
        w = ""
        if l != "":
            L.append(l)
            l = ""

if l != "":
    L.append(l)
    l = ""


def count(S):
    T = S[::-1]

    res = 0
    x_count = 0
    for t in T:
        if t == "X":
            x_count += 1
        elif t == "A":
           res += x_count
    return res

print(sum(count(l) for l in L))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n,a,b,c,d = LI()
    s = input()
    a -= 1
    b -= 1
    c -= 1
    d -= 1
    if c == d:
        print("No")
    else:
        if c < d:
            k = s[b:d+1]
            for i in range(d-b):
                if k[i] == k[i+1] == "
                    print("No")
                    quit()
            k = s[a:c+1]
            for i in range(c-a):
                if k[i] == k[i+1] == "
                    print("No")
                    quit()
            print("Yes")
        else:
            k = s[b:d+1]
            for i in range(d-b):
                if k[i] == k[i+1] == "
                    print("No")
                    quit()
            k = s[a:c+1]
            for i in range(c-a):
                if k[i] == k[i+1] == "
                    print("No")
                    quit()
            k = s[b-1:d+2]
            for i in range(len(k)-2):
                if k[i] == k[i+1] == k[i+2] == ".":
                    print("Yes")
                    quit()
            print("No")
    return


def B():
    def f(n):
        return n*(n+1)//2
    s = S()
    n = len(s)
    ans = 0
    i = 0
    while i < n-2:
        if s[i] == "A":
            if s[i+1] == "B":
                if s[i+2] == "C":
                    j = i
                    while j < n-2 and s[j] == "A" and s[j+1] == "B" and s[j+2] == "C":j += 3
                    j -= 3
                    l = i
                    r = j+2
                    while l >= 0 and s[l] == "A":l -= 1
                    while r < n and s[r-1] == "B" and s[r] == "C":r += 2
                    l += 1
                    r -= 2
                    a = i-l
                    b = (j-i)//3+1
                    c = (r-j)//2-1
                    for k in range(b+c):
                        s[2*k+l] = "B"
                        s[2*k+l+1] = "C"
                    for k in range(a+b):
                        s[k+l+2*(b+c)] = "A"
                    ans += f(b)+a*b+b*c+c*a
                    i = r
        i += 1
    print(ans)
    return


def C():
    n = I()

    return


def D():
    n = I()

    return


def E():
    n = I()

    return


def F():
    n = I()

    return


if __name__ == "__main__":
    B()

Solution (Python):

S = input()

preS = ''

a_cnt = 0
bc_cnt = 0
ans = 0

for i, v in enumerate(S):
  if preS == 'A':
    if v == 'C':
      ans += a_cnt * bc_cnt
      a_cnt = 0
      bc_cnt = 0
  elif preS == 'B':
    if v == 'C':
      bc_cnt += 1
    else:
      ans += a_cnt * bc_cnt
      a_cnt = 0
      bc_cnt = 0      
  elif preS == 'C':
    if v == 'A' and a_cnt > 0 and bc_cnt > 0:
      ans += a_cnt * bc_cnt
      bc_cnt = 0      
    elif v != 'B':
      ans += a_cnt * bc_cnt
      a_cnt = 0
      bc_cnt = 0
  if v == 'A':
    a_cnt += 1

  preS = v

else:
  ans += a_cnt * bc_cnt

print(ans)

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

ans = 0
acc_A = 0
for s in S:
    if s=='A':
        acc_A += 1
    elif s=='X':
        ans += acc_A
    else:
        acc_A = 0

print(ans)

Solution (Python):
s = input()
counter = 0
n = s.find('BC')
while n != -1:
    k = 1
    while k <= n and s[n-k] == 'A':
        k += 1
    a_num = k-1
    counter += a_num
    s = s[n+2:]
    while True:
        n = s.find('BC')
        if n == -1:
            break
        else:
            if s[:n].count('A') == n:
                a_num += n
                counter += a_num
                s = s[n+2:]
            else:
                break
print(counter)

Solution (Python):
s = input()

A = 'A'
BC = 'BC'
n_A = 0
n_BC = 0

def get_next(j):
  if s[j]==A:
    return A
  elif s[j]=='B' and s[j+1]=='C':
    return BC
  else:
    return 0

total = 0

flag = False
  
for j in range(len(s)-1):
  if flag:
    flag = False
    continue
  c = get_next(j)
  if c == A:
    n_A += 1
  elif c == BC:
    flag = True
    n_BC += 1
    total += n_A
  else:
    n_A = 0
    n_BC = 0
  
print(total)

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


AD_s = ""
index = 0
while index < len(s):
    if s[index:index+2] == "BC":
        AD_s += "D"
        index += 1
    else:
        AD_s += s[index]
    index += 1

index = 0
l = []
s_part = ""
while index < len(AD_s):
    if AD_s[index] == "B" or AD_s[index] == "C" or index == len(AD_s) - 1:
        if index == len(AD_s) - 1:
            s_part += AD_s[index]
        if s_part != "":
            l.append(s_part)
            s_part = ""
    else:
        s_part += AD_s[index]
    index += 1

ans = 0

for part in l:
    
    d_count = 0
    for i, c in enumerate(list(part)):
        if c == "D":
            ans += i - d_count
            d_count += 1

print(ans)

Solution (Python):
def main():
    s = raw_input().strip()
    s = s.replace("BC", "T")
    ans = t = 0
    for c in reversed(s):
        if c == 'A':
            ans += t
        elif c == 'T':
            t += 1
        else:
            t = 0
    print ans
main()

Solution (Python):
A = 0
B = 1
C = 2

table = {"A": A, "B": B, "C": C}

s = [table[c] for c in input()]
s.reverse()

total = 0
bc_count = 0
prev = None
for c in s:
	if c == A:
		if prev == C:
			bc_count = 0
		else:
			total += bc_count
	elif c == B:
		if prev == C:
			bc_count += 1
		else:
			bc_count = 0
	elif c == C:
		if prev == C:
			bc_count = 0
	prev = c

print(total)

Solution (Python):
import sys
input = sys.stdin.readline
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD=10**9+7

S = input()
len_S = len(S)

set_map = []
state = 0
st = 0
en = 0

for i in range(len_S):
    s = S[i]
    if state == 0:
        if s == 'A':
            state = 1
            st = i
    elif state == 1:
        if s == 'B':
            state = 2
        elif s == 'C':
            state = 0
    elif state == 2:
        if s == 'A':
            state = 1
            st = i
        elif s == 'B':
            state = 0
        elif s == 'C':
            state = 3
            en = i
    elif state == 3:
        if s == 'A':
            state = 5
        elif s == 'B':
            state = 4
        elif s == 'C':
            set_map.append([st,en])
            state = 0
    elif state == 4:
        if s == 'A':
            set_map.append([st,en])
            state = 1
            st = i
        elif s == 'B':
            set_map.append([st,en])
            state = 0
        elif s == 'C':
            state = 3
            en = i
    elif state == 5:
        if s == 'B':
            state = 6
        elif s == 'C':
            set_map.append([st,en])
            state = 0
    elif state == 6:
        if s == 'A':
            set_map.append([st,en])
            state = 1
            st = i
        elif s == 'B':
            set_map.append([st,en])
            state = 0
        elif s == 'C':
            state = 7
            en = i
    elif state == 7:
        if s == 'A':
            state = 5
        elif s == 'B':
            state = 8
        elif s == 'C':
            set_map.append([st,en])
            state = 0
    elif state == 8:
        if s == 'A':
            set_map.append([st,en])
            state = 1
            st = i
        elif s == 'B':
            set_map.append([st,en])
            state = 0
        elif s == 'C':
            state = 7
            en = i

if state >= 3:
    set_map.append([st,en])

ans = 0
for st,en in set_map:
    cnt = 0
    for i in range(st,en):
        if S[i] == 'A':
            cnt += 1
        elif S[i] == 'B':
            ans += cnt

print(ans)

Solution (Python):
s=input()
s=s.replace("BC","1")
s=s.replace("A","0")
s=s.replace("B","2")
s=s.replace("C","2")
l=[]
cnt1=0
ans=0
for ss in reversed(s):
  if ss=="0":
    ans+=cnt1
  elif ss=="1":
    cnt1+=1
  else:
    cnt1=0
print(ans)

Solution (Python):


import math
import sys


DEBUG = False


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


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


def solve(S):
    N = len(S)
    a = 0
    ans = 0
    i = 0
    while i < N:
        if S[i] == 'A':
            a += 1
            i += 1
            continue
        if S[i:i + 2] == 'BC':
            ans += a
            i += 2
            continue
        a = 0
        i += 1
    return ans


def main():
    print(solve(inp()))


if __name__ == '__main__':
    main()

Solution (Python):
S = input()
ans = 0
bc = 0
reset = set(['AC','BA','BB','CC'])
for i in range(len(S) + 1):

    if S[len(S) - i - 2 : len(S) - i] == 'BC':
        bc += 1
    elif S[len(S) - i - 2 : len(S) - i] in reset:
        bc = 0
    if len(S) - i - 2 >= 0 and S[len(S) - i - 2] == 'A':
        ans += bc
print(ans)

Solution (Python):
s = input()

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


seqs = []
seq = ''
for si in s:
    if si in ('A', 'X'):
        seq += si
    else:
        seqs.append(seq)
        seq = ''
else:
    if len(seq) > 0:
        seqs.append(seq)
        seq = ''
        
ans = 0
for seq in seqs:
    Ni = len(seq)
    cnttab = [None] * Ni
    cnt = seq.count('X')
    for i, ci in enumerate(seq):
        if ci == 'X':
            cnt -= 1
        cnttab[i] = cnt
    
    
    for i, ci in enumerate(seq):
        if ci == 'A':
            ans += cnttab[i]

print(ans)

Solution (Python):
s = list(input())
a_cnt = 0
b_flag = 0
total = 0

for c in s:

  if c == "A":
    if b_flag == 1:
      b_flag = 0
      a_cnt = 1
    else:
      a_cnt += 1
  elif c == "B":
    if b_flag:
      b_flag = 0
      a_cnt = 0
    elif a_cnt:
      b_flag = 1
  elif c == "C":
    if b_flag:
      total += a_cnt
      b_flag = 0

    else:
      a_cnt = 0
      
print(total)

Solution (Python):
s = input()
s = s.replace('BC', 'X')
s = s[::-1]

ans = 0
cnt_x = 0

for i, c in enumerate(s):
    if c == 'X':
        cnt_x += 1
    
    elif c == 'A':
        ans += cnt_x

    elif c in {'B', 'C'}:
        cnt_x = 0

print(ans)