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()
new_s = s.replace('BC', 'X')

count = 0
a = 0
for si in list(new_s):
  if si == 'A':
    count += 1
  elif si == 'X':
    a += count
  else:
    count = 0
print(a)

Solution (Python):
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
import scipy
from collections import deque
from math import sqrt
from math import floor


"""
need to consider the case that ticket is not enough to lower everything
"""


def cost(d):
    tc = 0
    d_cnt = 0
    for c in d:
        if c == 'D':
            tc += d_cnt
        else:
            d_cnt += 1

    return tc


s = gw()
d = []
i = 0
ans = 0
while i < len(s):
    if (s[i] == 'A'):
        d.append(s[i])
        i += 1
    elif (s[i] == 'B' and (i + 1) < len(s) and s[i + 1] == 'C'):
        d.append('D')
        i += 2
    else:
        ans += cost(d)
        d = []
        i += 1

ans += cost(d)

print(ans)

Solution (Python):
s = input()
cnt = 0
flg = 0
ans = 0
for i in range(len(s)-1):
  if s[i] == "A":
    if flg == 0:
      cnt += 1
    else:
      cnt = 1
  elif s[i:i+2] == "BC":
    ans += cnt
    flg = 1
  elif flg == 1:
    flg = 0
    continue
  else:
    cnt = 0
print(ans)

Solution (Python):
s = input()
count = 0
s = s.replace('BC','X')
s = s.replace('B','Z').replace('C','Z')
s = s.split('Z')
for i in s:
    ac = 0
    for j in range(len(i)):
        if i[j] == 'A':
            ac += 1
        if i[j] == 'X':
            count += ac
print(count)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	S = input().rstrip()
	S = [s for s in S]

	
	
	
	

	ans = 0
	sts = 0
	acnt = 0
	i = 0
	while i <= len(S)-2:
		if sts == 0 and S[i] == 'A':
			sts = 1
			acnt += 1
			i += 1
		elif sts == 1 and S[i] == 'A':
			acnt += 1
			i += 1
		elif sts == 1 and S[i]=='B' and S[i+1]=='C':
			ans += acnt
			i += 2
		else:
			sts = 0
			acnt = 0
			i += 1
	
	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):
S = input()
N = len(S)
a = 0
ans = 0
f = 0
for i in range(N-1):
    if S[i] == 'A':
        a += 1
    elif S[i] == 'B':
        if S[i+1] == 'C':
            ans += a
            f = 1
        else:
            a = 0
    else:
        if f != 1:
            a = 0
        else:
            f = 0
print(ans)

Solution (Python):

S = input()

new_s = ""
pointa = 0
while pointa != len(S):
    if pointa == len(S) - 1:
        new_s += S[pointa]
        break
    if S[pointa] == "B" and S[pointa+1] == "C":
        new_s += "D"
        pointa += 2
    else:
        new_s += S[pointa]
        pointa += 1


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

Solution (Python):
S = input()

NS = ""
i = 0
while i < len(S):
    if S[i] == "A":
        NS += "A"
    elif i + 1 < len(S) and S[i] == "B" and S[i + 1] == "C":
        NS += "X"
        i += 1
    else:
        NS += "Z"
    i += 1

NS = NS[::-1]
res = 0
pos = -1

for i, s in enumerate(NS):
    if pos != -1 and s == "A":
        res += i - pos
        pos += 1
    elif pos == -1 and s == "X":
        pos = i
    elif s == "Z":
        pos = -1

print(res)

Solution (Python):
s = input().strip()
x = [s[0]]
for i in range(1,len(s)):
    if s[i]=="C" and s[i-1]=="B":
        x.pop()
        x.append("T")
    else:
        x.append(s[i])
cnt = 0
ca = 0
for i in range(len(x)):
    if x[i]=="A":
        ca += 1
    elif x[i]=="T":
        cnt += ca
    elif x[i]=="B" or x[i]=="C":
        ca = 0
print(cnt)

Solution (Python):
import re

s = input()
ans = 0
s = s.replace("BC", "D")
L = re.split('[BC]', s)
for i in L:
  k = len(i)
  temp = 0
  for j in range(k):
    if i[k-1-j] == "D":
      temp += 1
    else:
      ans += temp
print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



S = str(input())
S = S.replace('BC', 'D')

ans = 0
a = 0
d = 0
flag = False
for s in S[::-1]:
    if s=='D':
        if a>=1:
            ans += a*d
            a = 0
        d += 1
        flag = True
    elif s=='A':
        if flag:
            a += 1
    else:
        ans += a*d
        a,d = 0,0
        flag = False
ans += a*d

print(ans)

Solution (Python):
s=input()
s=s.replace("BC","D")
s=s.replace("B"," ")
s=s.replace("C"," ")
s=list(s.split())
ans=0
for u in s:
    if u:
        u=list(u)
        count=0
        cnt=0
        for i in range(len(u)):
            if u[i]=="D":
                count+=i
                cnt+=1
        ans+=count-cnt*(cnt-1)//2
print(ans)

Solution (Python):
s = input()
ind = 0
len_s = len(s)
string = ""
cnt = 0
ans = 0
while ind < len_s:
    _s = s[ind]
    if _s == "A":
        cnt += 1
        ind += 1
    elif _s == "B" and ind + 1 < len_s and s[ind + 1] == "C":
        ind += 2
        ans += cnt
    else:
        cnt = 0
        ind += 1

print(ans)

Solution (Python):
S=input()
S=list(S.replace("BC","D"))
ans=0
count=0
for x in S:
  if x=="A":
    count+=1
  else:
    if x=="D":
      ans+=count
      continue
    else:
      count=0
print(ans)

Solution (Python):
def sol(S):
  N = len(S)
  nums = [0 for _ in range(N+1)]
  i = N-1
  while i >= 0:
    if S[i-1:i+1] == 'BC':
      nums[i-1] = nums[i+1] + 1
      nums[i] = nums[i+1] + 1
      i -= 2
    else:
      nums[i] = nums[i+1]
      i -= 1
  ans = 0
  for i, s in enumerate(S):
    if s == 'A':
      ans += nums[i]
  return ans

S = input()
ans = 0
N = len(S)
si, i = 0, 0
while i < N:
  if S[i] == 'A':
    i += 1
  elif S[i:i+2] == 'BC':
    i += 2
  else:
    ans += sol(S[si:i+1])
    si = i + 1
    i += 1
ans += sol(S[si:])
print(ans)

Solution (Python):
s = input().replace('BC', 'D')
 
acc = 0
ans = 0
for c in s:
    if c == 'B' or c == 'C':
        acc = 0
        continue
    elif c == 'A':
        acc += 1
    else:
        ans += acc
print(ans)

Solution (Python):
def resolve():
  s = input()
  s = s.replace('BC', 'D')
  s = s.replace('B', 'C')

  ans = 0
  count = 0
  for c in s:
    if c == 'C':
      count = 0
      continue
    if c == 'A':
      count += 1
    if c == 'D':
      ans += count

  print(ans)


if __name__ == "__main__":
  resolve()

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=input().replace("BC","X")
ans=0
acc=0
for i in range(len(s)):
    if s[i]=="B" or s[i]=="C":
        acc=0

    elif s[i]=="A":
        acc+=1
    elif s[i]=="X":
        ans+=acc


print(ans)

Solution (Python):
from sys import stdin
s = (stdin.readline().rstrip())
s = s.replace("BC","D")
ans = 0
acc = 0

for i in s:
    if i == "B" or i == "C":
        acc = 0
        continue
        
    if i == "A":
        acc += 1
    
    if i == "D":
        ans += acc

print(ans)

Solution (Python):
so=input()

s=""

N=len(so)
i=0
while i<N:
    if so[i]=="A":
        s=s+"A"
        i+=1
    elif so[i:i+2]=="BC":
        s=s+"B"
        i+=2
    elif so[i]=="C" or so[i]=="B":
        s=s+"D"
        i+=1

L=len(s)
f=False
cnt=0
a=0
for i in range(L):
    if s[i]=="A":
        f=True
        a+=1
    elif f and s[i]=="B":
        cnt+=a
    elif s[i]=="D":
        f=False
        a=0
print(cnt)

Solution (Python):
s = input()
pos = 0
str_len = len(s)
count = 0
seq_a = 0
seq_bc = 0

while pos < str_len:
    if s[pos] == "A":
        while pos < str_len and s[pos] == "A":
            seq_a += 1
            pos += 1
        if pos+1 < str_len and s[pos] == "B" and s[pos + 1] == "C":
            while pos < str_len and s[pos] == "B" and pos + 1 < str_len and s[pos + 1] == "C":
                seq_bc += 1
                pos += 2
            count += seq_a * seq_bc
            if pos < str_len and s[pos] != "A":
                seq_a = 0
            seq_bc = 0
        else:
            pos += 1
            seq_a = 0
    else:
        pos += 1
        seq_a = 0

print(count)

Solution (Python):
import sys
stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  

S = rs()
S = S.replace('BC', 'D')
cnt_A = 0
answer = 0
for s in S:
    if s == 'A':
        cnt_A += 1
    elif s == 'D':
        answer += cnt_A
    else:
        cnt_A = 0
print(answer)

Solution (Python):
import sys
stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  

S = list(rs())
cur = 'D'
answer = 0
A_cnt = 0
for s in S:
    if cur == 'A':
        if s == 'A':
            A_cnt += 1
        elif s == 'B':
            cur = s
        else:
            cur = 'D'
            A_cnt = 0
    elif cur == 'B':
        if s == 'A':
            A_cnt = 1
            cur = s
        elif s == 'B':
            A_cnt = 0
            cur = 'D'
        elif s == 'C':
            answer += A_cnt
            cur = s
        else:
            cur = 'D'
            A_cnt = 0
    elif cur == 'C':
        if s == 'A':
            A_cnt += 1
            cur = s
        elif s == 'B':
            cur = s
        else:
            cur = 'D'
            A_cnt = 0
    else:
        if s == 'A':
            A_cnt += 1
            cur = s
        else:
            A_cnt = 0
            cur = 'D'

print(answer)

Solution (Python):
s = input()
pos = 0
str_len = len(s)
count = 0
seq_a = 0
seq_bc = 0

while pos < str_len:
    if s[pos] == "A":
        while pos < str_len and s[pos] == "A":
            seq_a += 1
            pos += 1
        if pos + 1 < str_len and s[pos] == "B" and s[pos+1] == "C":
            while pos < str_len and s[pos] == "B" and pos + 1 < str_len and s[pos+1] == "C":
                seq_bc += 1
                pos += 2
            count += seq_a * seq_bc
            if pos < str_len and s[pos] != "A":
                seq_a = 0
            seq_bc = 0
        else:
            pos += 1
            seq_a = 0
    else:
        pos += 1
        seq_a = 0

print(count)

Solution (Python):
s = input()
pos = 0
cnt = 0
acnt = 0
if len(s) < 3:
    print(0)
else:
    while(True):
        if s[pos:pos+2] == "BC" and acnt > 0:
            pos += 2
            cnt += acnt
            
        else:
            if s[pos] == "A":
                acnt += 1
            else:
                acnt = 0
            pos += 1
        if pos >= len(s)-1:
            break
    print(cnt)

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

i = 0
ans = 0

while i < n:
    abc = []
    if s[i] == "A":
        i += 1
        abc.append(0)
        while True:
            if s[i] == "A":
                i += 1
                abc.append(0)
            elif s[i:i+2] == "BC":
                i += 2
                ans += len(abc)
            else:
                break
    i += 1

print(ans)

Solution (Python):
w=input()
count=0
num=len(w)
w1=""
w+=" "
while count<num:
  if w[count]=="A":
    w1+="A"
    count+=1
  elif w[count]=="B":
    if w[count+1]=="C":
      w1+="D"
      count+=2
    else:
      w1+="B"
      count+=1
  else:
    w1+="C"
    count+=1
counta=0
ans=0
num=len(w1)
for i in range(num):
  if w1[i]=="A":
    counta+=1
  elif w1[i]=="D":
    ans+=counta
  else:
    counta=0
print(ans)

Solution (Python):
s=list(input())
n=len(s)
l=[]
i=0
while i<n:
    if i<n-1 and s[i]=='B' and s[i+1]=='C':
        l.append('D')
        i+=2
    else:
        l.append(s[i])
        i+=1
n=len(l)
ans=0
r=[0]*(n+1)
f=0
for i in range(n-1,-1,-1):
    if l[i]=='D':
        f+=1
    elif l[i]=='A':
        ans+=f
    else:
        f=0
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    s=input()
    s2=list(s.replace('BC','D'))
    N=len(s2)
    ans=0
    c=0
    for i in range(N):
        if s2[i]=='A':
            c+=1
        elif c!=0 and s2[i]=='D':
            ans+=c
        else:
            c=0
    print(ans)

if __name__ == '__main__':
    main()

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

target=0
ans=0
for i in range(len(S)):
  if S[i]=="X":
    ans+=(i-target)
    target+=1
  elif S[i]!="A" and S[i]!="X":
    
    target=i+1
print(ans)

Solution (Python):
S=input()
S=S.replace("BC","X")
target=0
ans=0
for i in range(len(S)):
  if S[i]=="X":
    ans+=(i-target)
    target+=1
  elif S[i]!="A" and S[i]!="X":
    target=i+1
print(ans)

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

i = 0
bc = list("BC")

a_cnt = 0
res = 0
i = 0
while i <= len(s)-1:
  if s[i] == "A":
    a_cnt += 1
  elif s[i:i+2] == bc:
    res += a_cnt
    i += 1
  else:
    a_cnt = 0
  i += 1

print(res)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from heapq import heappop, heappush

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

def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])
def sortx(x,n,k):
    if k == 0:x.sort(key=lambda y:y[1,n])
    else:x.sort(reversed=True, key=lambda y:y[1,n])
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a=inp()
        a-=1
        g[i].append(a)
        g[a].append(i)
    return n,g
def graphm():
    n,m=inpm()
    g=[[] for _ in range(n)]
    for _ in range(m):
        a,b,w=inpm()
        a-=1
        b-=1
        g[a].append((b,w))
        g[b].append((a,w))
    return n,m,g

def dijkstra(s,n,g): 
    s -= 1
    que = []
    d = [10**15 for _ in range(n)]
    d[s] = 0
    heappush( que,(0,s) )
    while len(que)>0:
        p =  heappop(que)
        v = p[1]
        if d[v] < p[0]:
            continue
        for i in range(len(g[v])):
            edge = g[v][i]
            if d[edge[0]] > d[v] + edge[1]:
                d[edge[0]] = d[v] + edge[1]
                heappush( que,(d[edge[0]] , edge[0]) )
    return d

def main():
    s=input()
    n=len(s)
    cnt_a=0
    cnt_b=0
    cnt=0
    ans=0
    for i in range(n):
        if s[i]=="A":
            cnt_a+=1
        elif i<n-1 and s[i:i+2]=="BC":
            cnt_b+=1
            cnt+=cnt_a
        elif 0<i and s[i-1:i+1]=="BC":
            continue
        else:
            ans+=cnt
            cnt=0
            cnt_a=0
            cnt_b=0
    ans+=cnt
    print(ans)
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors


s = input()


mode=0
ans=0
anum=0
banum=0
for i in range(len(s)):
    if(mode==0 and s[i]=='A'):
        anum+=1
    elif(mode==1 and s[i]=='A'):
        anum=1
        mode=0
    elif(mode==0 and s[i]=='B'):
        mode=1
    elif(mode==1 and s[i]=='B'):
        mode=0
        anum=0
    elif(mode==0 and s[i]=='C'):
        mode=0
        anum=0
    elif(mode==1 and s[i]=='C'):
        mode=0
        ans += anum
    

print(ans)

Solution (Python):
from collections import deque
import heapq

s = input()
s += " "

S = ""

for i in range(len(s)-1):
    if s[i] == "B" and s[i+1] == "C":
        S += "Z"

    elif i > 0 and s[i] == "C" and s[i-1] == "B":
        continue
    else:
        S += s[i]


la = deque([])
ans = 0
for i in range(len(S)):

    if S[i] == "A":
        la.append(i)

    elif S[i] == "Z" and len(la) > 0:
        lastA = la.popleft()
        ans += i - lastA
        la.append(i)

    else:
        la = deque([])

print (ans)

Solution (Python):
import re


def main():
    s = input().replace('BC', 'X')
    S = re.split('[BC]', s)
    answer = 0

    for i in S:
        tmp = 0
        for j in i:
            if j == 'A':
                tmp += 1
            else:
                answer += tmp

    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
import re

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

S = re.split('[BC]', s)
answer = 0

for i in S:
    tmp = 0
    for j in i:
        if j == 'A':
            tmp += 1
        elif j == 'X':
            answer += tmp

print(answer)

Solution (Python):
s = input()
s_= s.replace("A", "0").replace("BC", "1").replace("B", "2").replace("C", "2")
s_012 = list(map(int, list(s_)))
count = 0
i = 0
ans = 0
while i < len(s_012):
    if s_012[i] == 0:
        count += 1
    elif s_012[i] == 1:
        ans += count
    else:
        count = 0
    i += 1

print(ans)

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

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

ans = 0
a_count = 0
for s in S:
    if s == 'A':
        a_count += 1

    elif s == '>':
        ans += a_count

    else:
        a_count = 0

print(ans)

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

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

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    s = input()
    s = s.replace("BC", "P")
    s = s.replace("B", "T")
    s = s.replace("C", "T")
    s = s.split("T")
    ans = 0
    for ss in s:
        p = 0
        for i in range(len(ss) - 1, -1 ,-1):
            if ss[i] == 'P':
                p += 1
            elif ss[i] == 'A':
                ans += p
    print(ans)





    


if __name__ == '__main__':
    main()

Solution (Python):
s=list(input())
l=[]
f=0
for i in range(len(s)-1):
    if s[i]=="A":
        l.append("A")
        f=0
    elif s[i]=="B":
        if s[i+1]=="C":
            l.append("D")
            f=1
        else:
            l.append("B")
            f=0
    else:
        if f:
            f=0
            continue
        else:
            l.append("C")
ac=0
ans=0
for i in range(len(l)):
    if l[i]=="A":
        ac+=1
    elif l[i]=="D":
        ans+=ac
    else:
        ac=0
print(ans)

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

nS=[]
i=0
while i<len(S)-1:
  if S[i]=="B" and S[i+1]=="C":
    nS.append("D")
    i+=1
  else:
    nS.append(S[i])
  i+=1
    
t=[]
nl=[]
for i in nS:
  if i=="A" or i=="D":
    nl.append(i)
  else:
    if nl:
      t.append(nl)
      nl=[]
if nl:
  t.append(nl)


ans=0
for i in t:
  dn=0
  for j in range(len(i)):
    if i[j]=="D":
      ans+=j-dn
      dn+=1

print(ans)

Solution (Python):
s_ = input().lower()

L_s = []
i=0
while i<len(s_):
    if s_[i]=="a":
        L_s.append("a")
        i+=1
    elif s_[i]=="b":        
        if i<len(s_)-1 and s_[i+1]=="c":
            L_s.append("x")
            i+=2
        else:
            L_s.append("y")
            i+=1
    else:
        L_s.append("y")
        i+=1
        
L_s.append("a")


s = ''.join(L_s)

if s[0]=="a":
    cnt_a = 1
else:
    cnt_a = 0
    
cnt_x = 0

ans = 0

for i in range(1,len(s)):
    if s[i]=="a":
        if s[i-1]=="a":
            cnt_a+=1
        elif s[i-1]=="x":
            ans += cnt_a*cnt_x
            cnt_x=0
            cnt_a+=1
        else:
            cnt_x = 0
            cnt_a = 1
    elif s[i]=="x":
        if s[i-1]=="x":
            cnt_x+=1
        elif s[i-1]=="a":
            ans += cnt_a*cnt_x
            cnt_x+=1
        else:
            cnt_x=1
            cnt_a=0
    else:
        if s[i-1]!="a":
            ans += cnt_a*cnt_x
        cnt_a=0
        cnt_x=0

        
print(ans)

Solution (Python):
s = input()
l = []
i = 0
while i < len(s):
    if s[i] == "A":
        l.append(0)
    elif s[i] == "B":
        if i < len(s)-1 and s[i+1] == "C":
            l.append(1)
            i += 1
        else: l.append(-1)
    else: l.append(-1)
    i += 1

cnt, ans = 0, 0
for x in l:
    if x == 0: cnt += 1
    elif x == 1: ans += cnt
    else: cnt = 0

print(ans)

Solution (Python):
s=list(input())
count=0
count1=0
for i in range(len(s)-2):
    if s[i:i+3]==['A','B','C']:
        count+=1
        s[i]='B'
        s[i+1]='C'
        s[i+2]='A'

            
        for j in range(1,len(s)):
            if i-j<0:
                count+=count1
                break
            if s[i-j]=='A':
                count1+=1
            else:
                count+=count1
                break
            
    if s[i:i+3]==['A','A','A'] or s[i:i+3]==['A','A','B'] or s[i:i+3]==['C','A','A'] or s[i:i+3]==['C','A','B'] or s[i:i+3]==['B','C','A']:
        continue
    else:
        count1=0


print(count)

Solution (Python):



s=input().replace('BC','X')
n=len(s)
cnt=0
ans=0
for i in range(1,n):
    tmp=s[i-1]+s[i]
    if s[i-1]=='A':
        cnt+=1
    if tmp[1]=='X':
        ans+=cnt
    if tmp not in ['AX','AA','XX','XA']:
        cnt=0
print(ans)

Solution (Python):
s=input()
ans=0
check=1
while s:
    if s[:3]=='ABC':
        ans+=check
        s='A'+s[3:]
    else:
        if s[0]=='A':
            check+=1
            s=s[1:]
        else:
            check=1
            s=s[1:]
print(ans)

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

i = 0
while i < len(s)-1:
  if s[i]=='A':
    a += 1
    i += 1
  elif s[i]=='B' and s[i+1]=='C':
    count += a
    i += 2
  else:
    a = 0
    i += 1
print(count)

Solution (Python):
s=input()

ans=0
a=0

i=0
while i<=len(s)-1:
    if s[i]=='A':
        a+=1
        i+=1
    elif i<=len(s)-2 and s[i]+s[i+1]=='BC':
        ans+=a
        i+=2
    else:
        a=0
        i+=1

print(ans)

Solution (Python):
s = input()
n = len(s)
A = []

i = 0
while i < n-1:
    if s[i:i+2] == "BC":
        A.append("D")
        i += 2
    else:
        A.append(s[i])
        i += 1

t = "".join(A)


m = len(t)
i = 0
p = 0
ans = 0
while i < m:
    while i < m and (t[i] == "A" or t[i] == "D"):
        if t[i] == "A":
            p += 1
        else:
            ans += p
        i += 1
    p = 0
    i += 1

print(ans)

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

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

    ans = 0
    count_a = 0
    if s[0] == "A":
        count_a += 1
    for i in range(len(s)-1):
        tmp = s[i:i+2]
        if tmp == "AA":
            count_a += 1
        elif tmp == "AB":
            pass
        elif tmp == "BC":
            ans += count_a
        elif tmp == "CB":
            pass
        elif tmp == "CA":
            count_a += 1
        elif tmp == "BA":
            count_a = 1
        else:
            count_a = 0
        
    print(ans)
        

        


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
n = len(s)
li = [[] for i in range(n)]
ind = 0
i = 0
while True:
    if i >= n:
        break
    if s[i] == "A":
        li[ind].append("A")
        i += 1
        continue
    if i+1 >= n:
        break
    elif s[i] == "B" and s[i+1] == "C":
        li[ind].append("D")
        i += 2
    else:
        i += 1
        ind += 1

ans = 0
for arr in li:
    cnt = 0
    tmp = arr[::-1]
    for j in tmp:
        if j == "D":
            cnt += 1
        else:
            ans += cnt
print(ans)

Solution (Python):
def solve():
  s = input()
  news = ""
  n = len(s)
  pas = False
  for i in range(n-1):
    if pas:
      pas = False
      continue
    if s[i] == 'B' and s[i+1] == "C":
      news += "T"
      pas = True
    else:
      news += s[i]
  ans = 0
  beforea = 0

  n = len(news)
  for i in range(n):
    if news[i] == "A":
      beforea += 1
    elif news[i] == "B" or news[i] == "C":
      beforea = 0
    if news[i] == "T":
      ans += beforea 
  print(ans)
solve()

Solution (Python):



def main():
    s = input()
    
    
    mod_s = list(s.replace('BC', 'D'))[::-1]
    d_count = 0
    ans = 0

    
    
    
    
    
    
    for si in mod_s:
        if si == 'D':
            d_count += 1
        elif si == 'A':
            ans += d_count
        else:
            d_count = 0

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

s = list(input())
ls = len(s)
p = 0
status = 0
a = 0
ans = 0
while p < ls:
    if status == 0:
        if s[p] == "A":
            status = 1
            a = 1

    elif status == 1:
        if s[p] == "A":
            a += 1
        elif s[p] == "B":
            status = 2
        else:
            status = 0
            a = 0

    elif status == 2:
        if s[p] == "C":
            ans += a
            status = 1

        elif s[p] == "A":
            status = 1
            a = 1
        elif s[p] == "B":
            status = 0
    p += 1

print(ans)

Solution (Python):
s=input().replace("BC","X").replace("B","Y").replace("C","Y")
ans=0
for t in s.split("Y"):
	cnt=t.count("X")
	for c in t:
		if c=="A":
			ans+=cnt
		else:
			cnt-=1
print(ans)

Solution (Python):
import re
s = input()
s_ = s.replace("BC", "D")
group = re.findall(r"[AD]+", s_)
ans = 0
for grp in group:
    a = 0
    for g in grp:
        if g=="A":
            a += 1
        if g=="D":
            ans += a
print(ans)

Solution (Python):
s = input().rstrip()
n = len(s)
count_a = 0
count_bc = 0
res = 0
i = 0
while i < n:
    if s[i] == 'A':
        count_a += 1
        i += 1
    elif s[i] == 'B':
        if count_a == 0:
            i += 1
            continue
        if i + 1 == n:
            break
        if i + 1 < n and s[i + 1] != 'C':
            count_a = 0
            i += 1
            continue
        while i + 1 < n and s[i:i + 2] == 'BC':
            count_bc += 1
            i += 2
        res += count_a * count_bc
        count_bc = 0
    else:
        count_a = 0
        i += 1
print(res)

Solution (Python):
s=input()
s=s+"D"+"D"
i=0
lists=[]
add=""
while i<len(s):
    if s[i]=="A":
        add+="A"
        i+=1
    elif s[i]=="B"and s[i+1]=="C":
        i+=2
        add+="X"
    else:
        i+=1
        if add: 
            lists.append(add)
            add=""
        else:
            continue
cnt=0
for some in lists:
    somes=list(some)
    nums=len(some)
    if nums==1:
        continue
    elif nums>=2:
        emp=0
        point=0
        for k in range(nums):
            if somes[k]=="A":
                point+=1
            elif somes[k]=="X":
                emp+=point
        cnt+=emp
print(cnt)

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):

s = list(input())
i = 0
ss = ''

while True:
    if i <= len(s) - 2:
        if s[i] == 'B' and s[i+1] == 'C':
            ss += 'D'
            i += 2
        else:
            ss += s[i]
            i += 1          
    else:
        ss += s[i]
        i += 1
    if i >= len(s):
        break


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

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)

def main():
    s = input().strip()
    s = s.replace("BC", "D")
    ac = 0
    ans = 0
    
    for i in range(len(s)):
        if s[i] == 'A':
            ac += 1
        elif s[i] == 'D':
            ans += ac
        else:
            ac = 0
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):


s=list(input().replace("BC","Z"))[::-1]
ans=0

def seisan(list):
    count=0
    N=len(list)-1
    for index,content in enumerate(list):
        if content=="A":
            count+=(N-index)
            N-=1
    return count

temp=[]
while s:
    this=s.pop(-1)
    if this=="B" or this=="C":
        ans+=seisan(temp)
        temp.clear()
    else:
        temp.append(this)

print(ans+seisan(temp))

Solution (Python):
S = list(input().replace("BC", "D"))
l = []
ans = 0
a_cnt = 1
d_cnt = 1
for i in range(len(S)-1):
    if (S[i] == "A" and S[i + 1] == "D"):
        ans += a_cnt
        S[i] = "D"
        S[i + 1] = "A"
    else:
        if(S[i] == "A" and S[i+1] == "A"):
            a_cnt += 1
        else:
            a_cnt = 1

print(ans)

Solution (Python):
si = list(input())
s = []
for i in range(len(si)):
    if si[i] == 'A':
        s.append('A')
    elif si[i] == 'B':
        if i < len(si) - 1 and si[i+1] == 'C':
            s.append('B')
        else:
            s.append('X')
    elif si[i] == 'C':
        if i > 0 and si[i-1] == 'B':
            pass
        else:
            s.append('X')

result = 0
ac = 0
for i in range(len(s)):
    if s[i] == 'A':
        if i < len(s) - 1 and s[i+1] == 'B':
            result += ac + 1
            s[i+1] = 'A'
        else:
            ac += 1
    else:
        ac = 0

print(result)

Solution (Python):
S=input()

S=S.replace("BC","D")
T=[0]*len(S)


for i in range(len(S)):
  if S[i]=="A":
    T[i]=T[i-1]+1
  elif S[i]=="D":
    T[i]=T[i-1]
  else:
    T[i]=0


ans=0
for i in range(len(S)):
  if S[i]=="D":
    ans+=T[i]
print(ans)

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

def main():
    S = input().strip()
    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)

if __name__ == '__main__':
    main()

Solution (Python):
import re
s = input()
s2 = s.replace("BC", "D")
ss = re.split("B|C", s2)

r = 0
for s in ss:
  n = 0
  for c in s:
    if c == "A":
      n += 1
    if c == "D":
      r += n
print(r)