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()
S=[]
i=0
leng=len(s)
while(i<leng):
    if s[i]=="A":
        S.append("A")
        i+=1
    elif i+1<leng and s[i]=="B" and s[i+1]=="C":
        S.append("B")
        i+=2
    else:
        S.append("C")
        i+=1
T=S[::-1]
l=[0]*len(T)
ans=0
t=len(T)
for i in range(len(T)-1):
    if T[i]=="B":
        l[i+1]=l[i]+1
    elif T[i]=="A":
        l[i+1]=l[i]
        ans+=l[i]
if T[-1]=="A":
    ans+=l[-1]
print(ans)

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

Solution (Python):
"""
B,"s/BC/X"したうえで、
置換できないB,Cで区切って、(re.finditer(r"[AX+"])
各区間でAをすべて右側に寄せるAの移動回数の和 例: AXAXAXA -> XXXAAAA にするには6回
"""
import re
s = input()
s = s.replace("BC", "X")
m = re.search(r"[AX]+", s)
res = 0
for m in re.finditer(r"([AX]+)", s): 
    count = 0 
    
    
    t = ''.join(list(reversed(str(m.groups()[0]))))
    for i in range(len(t)):
        if t[i] == "A":
            
            
            
            
            res += (i - count)
            count += 1
print(res)

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    S = input().strip()
    S = S.replace('BC', 'D')

    ans = 0
    cnt = 0
    for c in S:
        if c == 'A':
            cnt += 1
        elif c == 'D':
            ans += cnt
        else:
            cnt = 0

    return ans


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

Solution (Python):
li = list(input())
count = 0



counta = 0
i = 0
while(i<len(li)-2):
  
  if(li[i]=='A'):
    counta += 1
    
    if(li[i+1]=='B' and li[i+2]=='C'):
      count += counta
      li[i]='B'
      li[i+1]='C'
      li[i+2]='A'
      i = i+2
      counta -=1
    else:
      i += 1
  else:
    counta = 0
    i = i+1

print(count)

Solution (Python):
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
t = ""
a = 0
ans = 0
for i,e in enumerate(s):
    if e == "A":
        a += 1
    elif s[i:i+2] == "BC":
        ans += a
    elif s[i-1:i+1] == "BC":
        continue
    else:
        a = 0
print(ans)

Solution (Python):
s = input()

s = s.replace('BC', 'D')
a_count = 0
count = 0
for c in s:
    if c == 'A':
        a_count += 1
    elif c == 'D':
        count += a_count
    else:
        a_count = 0

print(count)

Solution (Python):
S = input()
T = S.replace('BC','X')
res = 0
cnt = 0
for s in T:
    if s == 'A' :
        cnt += 1
    elif s == 'X':
        res += cnt
    else:
        cnt = 0
print(res)

Solution (Python):
s = list(input())
count_abc = 0
a_count = 0
i = 0
while(i < len(s) - 1):
    if s[i] == "A":
        a_count += 1
    elif s[i] == "B" and s[i+1] == "C":
        if(i < len(s)-2):
            i += 1
        count_abc += a_count
    else:
        a_count = 0
    i += 1
print(count_abc)

Solution (Python):
import re
s=input()
ss=s.replace('BC','D')
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):
s = input()
tot = 0
cnt_A = 0
s = s.replace("BC", "D")
for c in s:
    if c == "A":
        cnt_A += 1
    elif c == "D":
        tot += cnt_A
    else:
        cnt_A = 0
print(tot)

Solution (Python):

def main():
    data = input()
    data = data.replace('BC', 'D')
    count_a = 0
    ans = 0
    for i in range(len(data)):
        now_str = data[i]
        if now_str == 'A':
            count_a += 1
        elif now_str == 'D':
            ans += count_a
        else:
            count_a = 0

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
s=input()
S=""
i=0
leng=len(s)
while(i<leng):
    if s[i]=="A":
        S+="A"
        i+=1
    elif i+1<leng and s[i]=="B" and s[i+1]=="C":
        S+="B"
        i+=2
    else:
        S+="C"
        i+=1
T=S[::-1]
l=[0]*len(T)
ans=0
t=len(T)
for i in range(len(T)-1):
    if T[i]=="B":
        l[i+1]=l[i]+1
    elif T[i]=="A":
        l[i+1]=l[i]
        ans+=l[i]
if T[-1]=="A":
    ans+=l[-1]
print(ans)

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    S = input().strip()
    n = len(S)

    T = ''
    i = 0
    while i < n:
        if S[i] == 'A':
            T += 'A'
            i += 1
        elif S[i:i+2] == 'BC':
            T += 'D'
            i += 2
        else:
            T += S[i]
            i += 1

    ans = 0
    cnt = 0
    for c in T:
        if c == 'A':
            cnt += 1
        elif c == 'D':
            ans += cnt
        else:
            cnt = 0

    return ans


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

Solution (Python):
def main(s):
    n = len(s)
    l = []
    i = 0
    while i < n:
        if s[i] == 'A':
            i += 1
            l.append('a')
        elif i < n-1 and s[i] == 'B' and s[i+1] == 'C':
            l.append('bc')
            i += 2
        else:
            l.append('b')
            i += 1
    n = len(l)
    cum = [0] * (n+1)
    ans = 0
    for i in range(n):
        if l[i] == 'a':
            cum[i+1] = cum[i] + 1
        elif l[i] == 'bc':
            cum[i+1] = cum[i]
            ans += cum[i]
        else:
            cum[i+1] = 0
    print(ans)
    
    
    
    
    
    
    
    
    
    
    
    
    
    

s=input()
main(s)

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

Solution (Python):

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

main()

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

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

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

def f(s):
  ans = 0
  add = 0
  for i in range(len(s)):
    if s[i] == "A":
      add += 1
    if s[i] == "D":
      ans += add
  return ans

ans = 0
j = 0
for i in range(1, len(s)):
  if s[i] == "B" or s[i] == "C":
    ans += f(s[j:i])
    j = i
ans += f(s[j:])

print(ans)

Solution (Python):
s = input().replace("BC", "D")
ans, cnt = 0, 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):
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
a,bc=0,0
i=0
while i<len(s)-1:
    if s[i]=="A": a+=1
    elif s[i]=="B" and s[i+1]=="C":
        bc+=1
        i+=1
        ans+=a
    
    else:
        a,bc=0,0
    i+=1
print(ans)

Solution (Python):
s = input()
t = s.replace('BC', 'D')
a = 0
x = 0
for i in t:
	if i == 'A':
		a += 1
	elif i == 'D':
		x += a
	else:
		a = 0
print(x)

Solution (Python):
s = input()
ans = 0
s = s.replace("BC", "D")
cnt = 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 = list(input())

cnt = 0
cnA = 0
i = 0
while i < len(s) - 1:
    
    if s[i:i + 2] == ["B", "C"]:
        cnt += cnA
        i += 1
    elif s[i] == "A":
        cnA += 1
    else:
        cnA = 0
    i += 1

print(cnt)

Solution (Python):
s = list(input())
count = 0
abc = 0

a_series = 0

for i in range(len(s)):
  
  if s[i] == 'A':
    if abc != 1:
      a_series = 0
    a_series += 1
    abc = 1
  elif s[i] == 'B':
    if abc == 1:
      abc = 2
    else:
      abc = 0
      a_series = 0
  elif s[i] == 'C':
    if abc == 2:
      abc = 1
      count += a_series
    else:
      abc = 0
      a_series = 0
      
print(count)

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

ans = 0
a = 0
for s in S:
    if s =="A":
        a += 1
    elif s =="D":
        ans += a
    else:
        a = 0

print(ans)

Solution (Python):



















import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    s = ST().replace("BC", "X")
    ans = 0
    a_cnt=0
    for st in s:
        if st == "A":
            a_cnt += 1
        elif st == "X":
            ans += a_cnt
        else:
            a_cnt=0
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
inp = input()
changed_input = inp.replace('BC', 'D')
A_count = 0
total_ans = 0
for st in changed_input:
    if st == 'A':
        A_count += 1
    elif st == 'D':
        total_ans += A_count
    else:
        A_count = 0
print(total_ans)

Solution (Python):
import sys
import re
sys.setrecursionlimit(10 ** 7)
 
 
def LI(): return [int(x) for x in sys.stdin.readline().split()]
 
 
def SI(): return input()
 
 
def main():
    S = SI()
    ans = 0
    S = S.replace("BC", "1")
    a = 0
    b = 0
    for s in S:
        if s == "A":
            a += 1
        elif s == "1":
            ans += a
            b = 1
        else:
            a = 0
    print(ans)
 
 
main()

Solution (Python):
s = list(input())
ans = 0
ls = len(s)
i = 0
ca = 0
while i < ls-2:
    if s[i:i+3] == ['A', 'B', 'C']:
        ans += 1 + ca
        
        s[i+2] = 'A'
        if ca >= 1:
            ca -= 2
            s[i+1] = 'A'
        else:
            ca = 0
        continue
    elif s[i] == 'A':
        ca += 1
    else:
        ca = 0
    i += 1

print(ans)

Solution (Python):
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    s = input()
    ope_num = 0
    index = 0
    a_stock = 0
    b_stock = 0
    s = list(s)
    while index < len(s):
        
        
        
        
        si = s[index]
        if si == "A":
            if b_stock > 0:
                a_stock = 1
            else:
                a_stock = a_stock + 1
            b_stock = 0
        elif a_stock > 0:
            if b_stock == 0:
                if si == "B":
                    b_stock = 1
                else:
                    a_stock = 0
                    b_stock = 0
            elif si == "C":
                ope_num = ope_num + a_stock
                
                
                
                
                a_stock = a_stock
                b_stock = 0
            else:
                a_stock = 0
                b_stock = 0
        else:
            a_stock = 0
            b_stock = 0
        index = index + 1
    print(ope_num)

Solution (Python):

S =input() +"ZZZ"
n =0
nowa = 0
karia = 0
ans = 0
while n < len(S):
    if S[n]=="A":
        karia += 1
        n += 1
    elif S[n:n+2]=="BC":
        ans += karia + nowa
        nowa += karia
        karia = 0
        n += 2
    else:
        nowa = 0
        karia = 0
        n += 1
print(ans)

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

a = 0
cnt = 0
for s in S:
    if s != 'A' and s != '*':
        a = 0
    elif s == 'A':
        a += 1
    elif s == '*':
        cnt += a

print(cnt)

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

margin = 0
length = len(s)
while length > 0:

    if s[length-1] == "A":
       count += margin
       s = s[:length-1]
    elif s[length-2:] == "BC":
        margin += 1
        s = s[:length-2]
    else:
        s = s[:length-1]
        margin = 0

    length = len(s)

print(count)

Solution (Python):
import sys
sys.setrecursionlimit(500000)
s=list(input())
ans=0
cA=1
L=[]
def dfs(v):
    if 0<=v+1<=len(s)-1 and s[v+1]=='A':
        s[v+1]='
        global cA
        cA+=1
        dfs(v+1)
    elif 0<=v+2<=len(s)-1 and s[v+1]=='B' and s[v+2]=='C':
        global ans
        ans+=cA
        dfs(v+2)
    else:
        cA=1
        return

for i,I in enumerate(s):
    if I=='A':
        L.append(i)
for l in L:
    if (0<=l+1<=len(s)-1 and s[l]=='A'and s[l+1]=='A'):
        dfs(l)
    elif (0<=l+2<=len(s)-1 and s[l]=='A'and s[l+1]=='B'and s[l+2]=='C'):
        dfs(l)
    else:
        cA=1
print(ans)

Solution (Python):
s = input()

arr = s.replace('BC','D').replace('B','C').split('C')

cnt = 0

for sub in arr:
  if len(sub) <= 1:
    continue
  ds = 0
  for i in range(len(sub)):
    if sub[i] is 'D':
      ds += 1
      cnt += i - ds + 1
print(cnt)

Solution (Python):
S = raw_input()

count = 0
idx = 0
a_count = 0
while idx < len(S):
    if a_count > 0:
        if S[idx: idx+2] == 'BC':
            count += a_count
            idx += 2
            continue
    if S[idx] == 'A':
        a_count += 1
    else:
        a_count = 0
    idx += 1

print count

Solution (Python):
s=input()
import re
import copy

s1=re.sub('(A)','1',s)
s2=re.sub('(BC)','2',s1)
s3=re.sub('\D+','3',s2)

s=list(s3.split('3'))

ans=0
for i in s:
    count=0
    tmp=map(int,list(i))
    tmp=list(tmp)
    tmp2=copy.copy(tmp.reverse())
    for j in tmp:
        if j==2:
            count+=1
        elif j==1:
            ans+=count
print(ans)

Solution (Python):
s=input()


i=0
c=0
flag=0
while True:
	if i>len(s)-2: break
	if s[i]=="A":
		flag+=1
		i+=1
	elif flag and s[i:i+2]=="BC":
		c+=flag
		i+=2
	else:
		flag=0
		i+=1

print(c)

Solution (Python):
s = input()

lis = []
l = len(s)
judge = True
i = 0
while (i < l):
    if judge:
        if (s[i] == 'A'):
            lis.append(0)
            i += 1
        elif (s[i:i+2] == 'BC'):
            lis.append(1)
            i += 2
        else:
            judge = False
            lis.append(2)
            i += 1
    else:
        if (s[i] == 'A'):
            judge = True
            lis.append(0)
        i += 1

sum1 = 0    
cnt = 0
for i in lis:
        if i == 0:
            cnt += 1
        elif i == 1:
            sum1 += cnt
        else:
            cnt = 0

print(sum1)

Solution (Python):
S = "BB" + input()
N = len(S)

ans = 0
keep = 0

i = -2
while -1 * N <= i:
    s = S[i] + S[i + 1]
    if s == "AA":
        ans += keep * 2
        i -= 2
    elif s == "AB":
        i -= 2
        keep = 0
    elif s == "AC":
        i -= 2
        keep = 0
    elif s == "BA":
        ans += keep
        keep = 0
        i -= 2
    elif s == "BB":
        i -= 2
        keep = 0
    elif s == "BC":
        keep += 1
        i -= 2
        flag = True
    elif s == "CA":
        ans += keep
        i -= 1
    elif s == "CB":
        i -= 1
        keep = 0
    elif s == "CC":
        i -= 1
        keep = 0

print(ans)

Solution (Python):
import re
s = input()
s = s.replace("BC", "X")
m = re.search(r"[AX]+", s)
count = 0
res = 0
for m in re.finditer(r"([AX]+)", s):
    t = str(m.groups()[0])
    t = ''.join(list(reversed(t)))
    count = 0
    for i in range(len(t)):
        if t[i] == "A":
            res += (i - count)
            count += 1
print(res)

Solution (Python):
def main():
    s = input()
    c = 0
    pos = 0
    a = 0

    while pos < len(s):
        if s[pos] == 'A':
            a += 1
            pos += 1
        elif s[pos:pos+2] == 'BC':
            c += a
            pos += 2
        else:
            a = 0
            pos += 1

    print(c)
        


main()

Solution (Python):

"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()


def main():
    s = read()
    ans = 0
    cnt_a = 0
    last_ch = 'C'
    for ch in s:
        if ch == 'A':
            if last_ch != 'A':
                cnt_a = 0
            cnt_a += 1
            last_ch = ch
        elif ch == 'B':
            if last_ch != 'A':
                cnt_a = 0
            last_ch = ch
        elif ch == 'C':
            if last_ch == 'B':
                ans += cnt_a
                cnt_a = cnt_a
                last_ch = 'A'
            else:
                cnt_a = 0
                last_ch = ch
    print(ans)


def slow(s):
    ans = 0
    while True:
        x = s.count('ABC')
        if x:
            ans += x
            s = s.replace('ABC', 'BCA')
        else:
            break
    return ans



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        if self.buffer.tell():
            return self.buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")


def read():
    return input()


def read_array(sep=None, maxsplit=-1):
    return read().split(sep, maxsplit)


def read_int():
    return int(read())


def read_int_array(sep=None, maxsplit=-1):
    return [int(a) for a in read_array(sep, maxsplit)]



if __name__ == "__main__":
    main()

Solution (Python):
s=input()
ans=0
a=[0 for i in range(len(s))]
bc=[0 for i in range(len(s))]
if len(s)<3:
  print(0)
else:
  for i in range(len(s)-2):
    if s[i]=="A":
      a[i]+=1
    if s[i+1]=="B" and s[i+2]=="C":
      a[i+2]+=a[i]
    if s[i+1]=="A":
      a[i+1]+=a[i]
      
  for i in range(len(s)-1):
    if s[i]=="B" and s[i+1]=="C":
      bc[i]=1
  
   
    
     
  
   
    
  
   
    
     
  
   
    
     
  
  for i in range(len(s)-2):
    if a[i]>0 and bc[i+1]>0:
      ans+=a[i]*bc[i+1]
  print(ans)

Solution (Python):
import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    S = S.replace("A", "0")
    S = S.replace("BC", "1")
    a = 0
    b = 0
    for s in S:
        if s == "0":
            a += 1
        elif s == "1":
            ans += a
            b = 1
        else:
            a = 0
    print(ans)


main()

Solution (Python):
from collections import deque


def calc(i):
    while True:
        a = 0
        while i < M and S[i] == 'A':
            i += 1
            a += 1
        if a == 0:
            break
        else:
            dq.appendleft(a)
        bc = 0
        while i < M - 1 and S[i] == 'B' and S[i + 1] == 'C':
            bc += 1
            i += 2
        if bc == 0:
            dq.popleft()
            break
        else:
            dq.appendleft(bc)

    asum = 0
    while dq:
        global ans
        asum += dq.pop()
        ans += asum * dq.pop()
    return i


S = input()
M = len(S)
i = 0
dq = deque()
ans = 0
while i < M:
    if S[i] == 'A':
        i = calc(i)
    i += 1
print(ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S=list(input())
N=len(S)

cnt=cnta=0
i=0
while i<N:
    if S[i]=='A':
        cnta+=1
    else:
        cnta=0
    if i+2<N:
        if S[i]=='A' and S[i+1]=='B' and S[i+2]=='C':
            cnt+=cnta
            S[i]='B'
            S[i+1]='C'
            S[i+2]='A'
            i+=2
            cnta-=1
            continue
    i+=1
print(cnt)

Solution (Python):
s=list(input())
l=[]
x=-100
for i in range(len(s)-1):
  if x==i:
    pass
  else:
    if s[i]=='B' and s[i+1]=='C':
      l.append('D')
      x=i+1
    else:
      l.append(s[i])
f=0
ct=0
for i in range(len(l)):
  if l[i]=='A':
    f+=1
  if l[i]=='B' or l[i]=='C':
    f=0
  if l[i]=='D':
    ct+=f
print(ct)

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

ans = 0
flag = 0
ABCflag = 0

i = 0
while i < N-2:

    if S[i]+S[i+1]+S[i+2] == 'ABC':
        if flag:
            ans += flag
        ans += 1
        S[i+2] = 'A'
        i += 1

    elif S[i] == 'A':
        flag += 1

    else:
        flag = 0

    i += 1
print(ans)

Solution (Python):
s = input()
Acnt = 0 
cnt = 0
BC = False
for i in range(len(s)-1):
    if BC :
        BC = False
        continue
    if s[i] == 'A':
        Acnt += 1
        BC = False
    elif s[i:i+2] == 'BC':
        cnt += Acnt
        BC= True
    else:
        Acnt = 0
        BC = False

print(cnt)

Solution (Python):
s = input()
path = []

i = 0
while True:
    if s[i] == 'A':
        path.append(0)
        i += 1
    elif i+1 < len(s) and s[i] == 'B' and s[i+1] == 'C':
        path.append(1)
        i += 2
    else:
        path.append(2)
        i += 1
    
    if i >= len(s):
        break


path.reverse()

ans = 0
c1 = 0
for i in range(len(path)):
    if path[i] == 1:
        c1 += 1
    elif path[i] == 2:
        c1 = 0
    else:
        ans += c1

print(ans)

Solution (Python):
s = list(input())
def change(i):
    global s
    
    if s[i : i + 3] == ["A", "B", "C"]:
        s[i:i + 3] = ["B", "C", "A"]
        
        return 1
    return 0

cnt = 0
cnA = 0
i = 0
while i < len(s) - 1:
    
    if s[i:i + 2] == ["B", "C"]:
        cnt += cnA
        i += 1
    elif s[i] == "A":
        cnA += 1
    else:
        cnA = 0
    i += 1

print(cnt)

Solution (Python):
S = list(input())
S = S[::-1]

t = 0
ans = 0
bc_score = 0
flag = 1

if len(S) <= 2:
    print(0)
    exit()

while t <= len(S)-2:
    if S[t]=="C" and S[t+1] == "B":
        bc_score = 0
        bc_score += 1
        t += 2
        flag = 1
        if t == len(S)-1:
            if S[t] == "A":
                ans += 1
            t += 1
        while flag == 1 and t <= len(S) - 1:
            flag = 0
            if S[t]=="C" and S[t+1] == "B":
                bc_score += 1
                t += 2
                flag = 1
            if t <= len(S) - 1:
                if S[t] == "A":
                    ans += bc_score
                    t += 1
                    flag = 1
    else:
        t += 1

print(ans)

Solution (Python):
L=[x for x in input()]
r=0
acount=0
for i in range(len(L)-2):
    if L[i]=="A":
        acount+=1
        if L[i:i+3]==["A","B","C"]:
            r+=acount
            L[i+2]="A"
            acount-=1
        elif L[i+1]!="A":
            acount=0
    
print(r)

Solution (Python):
S = list(input())
N = len(S)
import sys
if N < 3:
    print(0)
    sys.exit()

cnt = 0
BClist = [0] * N
Alist = [0] * N
idx = N-1
while idx-1 >= 0:
    if S[idx-1] == 'B' and S[idx] == 'C':
        cnt += 1
        BClist[idx] = cnt
        BClist[idx-1] = cnt
        idx -= 1
    elif S[idx] != 'A':
        BClist[idx] = 0
        cnt = 0
        
    idx -= 1
    
idx = 0
cnt = 0
while idx < N:
    if S[idx] == 'A':
        cnt += 1
        Alist[idx] = cnt
    else:
        cnt = 0
        
    idx += 1
    
idx = 0
ans = 0
flag = False
while idx < N:
    if Alist[idx] <= 0:
        idx += 1
        continue
    while Alist[idx] > 0:
        idx += 1
        if idx >= N:
            flag = True
            break
    if flag:break
    tmp = Alist[idx-1]
    if BClist[idx] <= 0:
        idx += 1
    else:
        ans += tmp * BClist[idx]
        idx += 1
    
print(ans)

Solution (Python):
import re

S = input()
ct = 0

na = 0
bf = False
ct = 0
for s in S:
  if s =='A':
    if bf==True:
      na = 1
    else:
      na += 1
    bf = False
      
  if s == 'B':
    if na > 0 and bf==False:
      bf = True
    else:
      na = 0
      bf = False
  if s == 'C':
    if bf:
      ct += na
    else:
      na = 0
    bf = False
print(ct)

Solution (Python):
import re

S = input()
ct = 0

na = 0
bf = False
ct = 0
for s in S:
  if s =='A':
    if bf==True:
      na = 1
    else:
      na += 1
    bf = False
      
  if s == 'B':
    if na > 0 and bf==False:
      bf = True
    else:
      na = 0
      bf = False
  if s == 'C':
    if bf:
      ct += na
    else:
      na = 0
    bf = False
print(ct)

Solution (Python):
s = input()
n = len(s)
s = s+"xxx"

def cal_subop(l):
    cum_a = [0 for i in range(len(l))] 
    tmp = 0
    for i in range(len(l)):
        tmp += (1 if l[i]=='A' else 0)
        cum_a[i] = tmp

    n_op = 0
    for i in range(len(l)):
        if l[i] == 'B':
            n_op += cum_a[i]
    return n_op

cursor = 0
ans = 0
tmp  = []

while cursor < n:
    if s[cursor] == 'A':
        tmp.append('A')
        cursor += 1
    elif s[cursor:cursor+2] == 'BC':
        tmp.append('B')
        cursor += 2
    else:
        ans += cal_subop(tmp)
        tmp = []
        cursor += 1

ans += cal_subop(tmp)
print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


s = ns()

cur = 0
anum = 0
bcnum = 0
ans = 0

while cur < len(s):
    if s[cur:cur+1] == "A":
        anum += 1
        cur += 1

    elif s[cur:cur+2] == "BC":
        bcnum += 1
        ans += anum
        cur += 2

    else:
        anum = 0
        bcnum = 0
        cur += 1

print(ans)

Solution (Python):
s = list(input())
a,b = 0,0
bc = 0
ans = 0
for i in s:
    if i == "A":
        if b == 1:
            a, b = 1, 0
        else:
            a += 1
            a_now = 1
    elif i == "B":
        if b == 1 or a == 0:
            a, b = 0, 0
        else:
            b = 1
    else:
        if b == 1:
            ans += a
            b = 0
        else:
            a, b = 0, 0
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 = str(input())
_s = []
s.count('BC')
p = 0
for i in range(len(s)-1):
    if p == 1:
        p = 0
    elif s[i:i+2]=='BC':
        _s+=['D']
        p = 1
    else:
        _s+=[s[i]]


len_s = len(_s)
cnt = 0
ans = 0
for i in range(1, len_s+1):
    
    if _s[-i] == 'A':
        ans += cnt
    elif _s[-i] == 'D':
        cnt += 1
    elif _s[-i] in ('B','C'):
        cnt = 0

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):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()

  
  
  
  
  
  
  

  
  

  s=list(s)
  s.reverse()
  s=''.join(s)

  cb_c=0
  sm=0
  f=False
  for i in range(len(s)):
    if f:
      f=False
      continue
    if s[i:i+2]=='CB':
      cb_c+=1
      f=True
    elif s[i]=='A':
      sm+=cb_c
    else:
      cb_c=0

  return sm

print(main())

Solution (Python):
S = input()

A = [[] for i in range(len(S)+1)]
ans = 0
j = 0
b = False
for i in range(len(S)):
    if b == True and S[i] == "A":
        j += 1
        A[j].append(0)
        b = False
    elif S[i] == "A":
        A[j].append(0)
        b = False
    elif b == False and S[i] == "B":
        b = True
    elif b == True and S[i] == "C":
        A[j].append(1)
        b = False
    else:
        b = False
        j += 1

for i in range(j+1):
    li = A[i]
    length = 0
    n = 0
    for k in range(len(li)):
        length += k*li[k]
        n += li[k]
    length -= n*(n-1)//2
    ans += length
print(ans)

Solution (Python):
from collections import deque
S = list(input())
N = len(S)
S = deque(S)


def solve(L):
    if 'BC' not in ''.join(L):
        return 0
    while L[-1] == 'A':
        L.pop()
    A_place = []
    n = len(L) - 1
    for i in range(n):
        if L[i] == 'A':
            A_place.append(i)
    a = len(A_place)
    return int(((a * n - sum(A_place)) - (a * (a - 1) / 2)) / 2)


ans = 0
part_S = []
num = 0
while S:
    if part_S == []:
        if S[0] == 'A':
            part_S.append('A')
            S.popleft()
        else:
            S.popleft()
    else:
        if len(S) >= 2:
            if S[0] == 'A':
                part_S.append('A')
                S.popleft()
            elif S[0] + S[1] == 'BC':
                part_S.append('B')
                part_S.append('C')
                S.popleft()
                S.popleft()
            else:
                ans += solve(part_S)
                S.popleft()
                part_S = []
        else:
            break
ans += solve(part_S)
print(ans)

Solution (Python):
def main():
    s = input().replace('BC', 'X')[::-1]
    N = len(s)
    count = 0
    tmp = 0
    for i in range(N):
        if s[i] == 'X':
            tmp += 1
        elif s[i] == 'A':
            count += tmp
        else:
            tmp = 0
    print(count)

if __name__ == '__main__':
    main()