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

while "BC" in s:
    s = s.replace("BC", "D")

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

print(ret)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

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





S = input()
S = S.replace('BC', 'P')
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "P":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

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

count =  0
AP_streak = 0
P_pos = []
P_count = 0

S=S.replace('BC', 'P')

N = len(S)

for i in range (0, N):
	V = S[i]
	if V != 'P' and V!= 'A':
		count+=sum(P_pos)-(P_count*(P_count+1))//2
		AP_streak = 0
		P_pos = []
		P_count = 0
	if V == 'A':
		AP_streak+=1
	if V == 'P':
		if AP_streak > 0:
			AP_streak+=1
			P_count+=1
			P_pos.append(AP_streak)

count+=sum(P_pos)-(P_count*(P_count+1))//2

print(count)

Solution (Python):






s = input()
t = s.replace("BC", "X", len(s))
ans = 0
left, right = 0, 0
while left < len(t):
    
    if t[left] not in ["A", "X"]:
        left += 1
        continue
    a_indicies = []
    a_count = 0
    a_idx = 0
    if t[left] == "A":
        a_indicies.append(a_idx)
        a_count += 1
    right = left + 1
    a_idx += 1
    while right < len(t) and t[right] in ["A", "X"]:
        if t[right] == "A":
            a_indicies.append(a_idx)
            a_count += 1
        right += 1
        a_idx += 1
    
    if right - left != a_count and a_count != 0:
        for i in range(a_count):
            ans += ((right - left) - (a_count - i)) - a_indicies[i]  
    left = right + 1
print(ans)

Solution (Python):
s = input()
n = len(s)
d = [s[0]]
for i in range(1,n):
  if s[i]=="C" and s[i-1]=='B':
    d.pop()
    d.append("x")
  else:
    d.append(s[i])
q = "".join(d)
cou = 0
cou_a = 0
l = len(q)
for i in range(l):
  if q[i]=="A":
    cou_a += 1
  elif q[i]=="x":
    cou +=  cou_a
  else:
    cou_a = 0
print(cou)

Solution (Python):
def f():
  ans,c=0,0
  for t in input().replace('BC','y'):
    if t=='y':ans+=c
    elif t=='A':c+=1
    else:c=0
  print(ans)
if __name__ == "__main__":f()

Solution (Python):
def f():
    ans,c=0,0
    for t in input().replace('BC','y').replace('A','x'):
        if t=='y':ans+=c
        elif t=='x':c+=1
        else:c=0
    print(ans)
if __name__ == "__main__":
    f()

Solution (Python):
ans=0
for x in input().replace('BC','y').replace('A','x').replace('B',' ').replace('C',' ').split():
    c=0
    for t in x:
        if t=='y':
            ans+=c
        else:c+=1
print(ans)

Solution (Python):
def solve(s):
    n = len(s)
    res = 0
    i = 0
    num = 0
    while i < n-1:
        if s[i] == "A":
            num += 1
            i += 1
        elif s[i:i+2] == "BC":
            res += num
            i += 2
        else:
            num = 0
            i += 1
    return res

s = input()
print(solve(s))

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()

    ans = 0
    A_count = 0
    i = 0
    N = len(S)
    while i < N - 1:
        if S[i] == "A":
            A_count += 1
            i += 1
        elif S[i:i + 2] == "BC":
            ans += A_count
            i += 2
        else:
            A_count = 0
            i += 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s=input()

s=list(s.replace("BC", "D"))
ans=0
cnt_a=0
for i in range(len(s)):
    if s[i]=="A":
        cnt_a+=1
    elif s[i]=="D":
        ans+=cnt_a
    else:
        cnt_a=0

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

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

cnt = 0
ans = 0
for x in s:
	if x == "A":
		cnt += 1
	elif x == "D":
		ans += cnt
	else:
		cnt = 0

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

s = input()
t = ""

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

D_cnt = []
cnt = 0
for x in t[::-1]:
	if x == "D":
		cnt += 1
		D_cnt.append(cnt)
	elif x == "A":
		D_cnt.append(cnt)
	else:
		cnt = 0
		D_cnt.append(cnt)

D_acc = D_cnt[::-1]

ans = 0
for i in range(len(t)):
	if t[i] == "A":
		ans += D_acc[i]

print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


s = input()
n = len(s)
v = []
i = 0
tmp = []
while i<n:
    if s[i]=="A":
        tmp.append(0)
        i += 1
    elif i<n-1 and s[i]=="B" and s[i+1]=="C":
        tmp.append(1)
        i += 2
    else:
        i += 1
        if tmp:
            v.append(tmp)
        tmp = []
if tmp:
    v.append(tmp)
ans = 0
for l in v:
    val = 0
    for c in l:
        if c==1:
            ans += val
        else:
            val += 1
print(ans)

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

Solution (Python):
s=input().replace('BC','D')
a=[]
b=''
for c in s:
    if c=='B' or c=='C':
        a.append(b)
        b=''
    else:
        b+=c
a.append(b)
ans=0
for i in a:
    ac=0
    for j in i:
        if j=='A':
            ac+=1
        else:
            ans+=ac
print(ans)

Solution (Python):
def main():
  S = str(input())
  s = []
  i = 0
  while i <= len(S)-1:
    if i == len(S)-1:
      s.append(S[i])
      break
    if S[i] == "B" and S[i+1] == "C":
      s.append("D")
      i += 2
    else:
      s.append(S[i])
      i += 1
  ans = 0
  flag = True
  for i in range(len(s)-1, -1, -1):
    if flag and s[i] == "D":
      posi = i
      flag = False
    if not flag and s[i]=="A":
      ans += posi-i
      posi-= 1
    if s[i] == "B" or s[i] == "C":
      flag = True
  print(ans)
  
  
if __name__ == "__main__":
  main()

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

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

ans = 0
cnt = 0
for i in s[::-1]:
    if i == "D":
        cnt += 1
    elif i == "A":
        ans += cnt
    else:
        cnt = 0



print(ans)

Solution (Python):
def test(s):

	res = 0
	aAdd = 0
	last = ''
	tmp = ''
	for i in xrange(len(s)):
		if tmp and tmp > i:
			continue
		if s[i] == 'A':
			aAdd += 1
		elif s[i:i+2] == 'BC':
			
			res = res + aAdd
			tmp = i + 2
		else:
			aAdd = 0
			last = s[i]


	return res
s=raw_input()
print test(s)

Solution (Python):
s = raw_input()
count = 0
num = 0
i = 0
length = len(s)
while i < length:
	if s[i] == 'A':
		num += 1
		i += 1
	elif i+1 < len(s) and s[i] == 'B' and s[i+1] == 'C':
		count += num
		i += 2
	else:
		num = 0
		i += 1
print count

Solution (Python):
s = input().replace('BC', 'X')
cnt_a = 0
ans = 0
for c in s:
    if c == 'A':
        cnt_a += 1
    if c == 'X':
        ans += cnt_a
    if c in 'BC':
        cnt_a = 0
print(ans)

Solution (Python):
s = raw_input()

count = 0
a = 0
i = 0
l = len(s)
while i < len(s):
    if s[i] == 'A':
        a += 1
        i += 1
        continue

    if s[i] == 'B' and i < l - 1 and s[i + 1] == 'C':
        count += a
        i += 2
    else:
        a = 0
        i += 1

print count

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def t(S):

    N = len(S)

    a = [0] * (N + 1)
    d = [0] * (N + 1)
    for i in range(N):
        if S[i] == 'A':
            a[i + 1] = 1
        else:
            d[i + 1] = 1

    for i in range(N):
        a[i + 1] += a[i]
        d[i + 1] += d[i]

    ans = 0
    for i in range(1, N):
        if S[i] == 'D':
            ans += a[i]

    return ans


def main():

    S = in_s()
    S = S.replace('BC', 'D')
    S = S.replace('B', ' ')
    S = S.replace('C', ' ')

    ans = 0
    for ts in S.split():
        ans += t(ts)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

from itertools import accumulate

s = input()
l = s.split('BC')[:-1]
l = l[::-1]

ans = 0
i = 0

while i < len(l):
    anum = []
    flag = True
    while flag and i < len(l):
        M = len(l[i])
        tmp = 0
        for j in range(M):
            if l[i][M-j-1] == 'A':
                tmp += 1
            else:
                flag = False
                break
        anum.append(tmp)
        if flag:
            i += 1
    ans += sum(accumulate(anum[::-1]))
    i += 1
print(ans)

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

cnt=0
ans=0
for i in range(len(new)-1,-1,-1):
    if new[i]=="A" and cnt > 0:
        ans+=cnt
    elif new[i]=="K":
        cnt+=1
    else:
        cnt=0
    
print(ans)

Solution (Python):
s=input()
t=''
i=0
while(i<len(s)):
    if i<len(s)-1 and s[i:i+2]=='BC':
        t+='D'
        i+=2
    else:
        t+=s[i]
        i+=1
t=t[::-1]
N=len(t)
i=0
ans=0
while(i<N):
    if t[i]=='D':
        j=i
        cnt=0
        while(j<N and t[j]!='B' and t[j]!='C'):
            if t[j]=='D':
                cnt+=1
            else:
                ans+=cnt
            j+=1
        i=j
    i+=1
print(ans)

Solution (Python):
def main():
    S = list(input())
    N = len(S)
    if N <= 2:
        print(0)
        exit()
    T = []
    flag = False
    for i in range(N-1):
        if flag:
            flag = False
            continue

        if S[i] == 'B' and S[i+1] == 'C':
            T.append('D')
            flag = True
        else:
            T.append(S[i])
        
    if not (S[i-2] == 'B' and S[i-1] == 'C'):
        T.append(S[i-1])
    
    M = len(T)
    d = [0] * T.count('D')
    ans = 0
    a = 0
    for i in range(M):
        if T[i] == 'A':
            a += 1
        elif T[i] == 'D':
            ans += a
        else:
            a = 0
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
s = input()
ans = 0
cnt = 0
idx = len(s)

while idx >= 0:
    if s[idx - 2 : idx] == "BC":
        cnt += 1
        idx -= 2
    elif s[idx - 1 : idx] == "A":
        ans += cnt
        idx -= 1
    else:
        idx -= 1
        cnt = 0

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    S = input().replace("BC", "D")

    res = 0
    cnt = 0
    for s in S:
        if s == "A":
            cnt += 1
        elif s == "D":
            res += cnt
        else:
            cnt = 0
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):



def main():
    S = input()
    S = S.replace('BC','Z')
    S = S.replace('B',' ')
    S = S.replace('C',' ')
    S = S[::-1]
    S = S.split()
    ret = 0
    for s in S:
        zcn = 0
        for si in s:
            zcn += (si == 'Z')
            ret += zcn * (si == 'A')
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
S = input()
S = S.replace("BC", "D")
segs = []
prev = "a"
for c in S:
    if c in "AD":
        if prev not in "AD":
            segs.append([c])
        else:
            segs[-1].append(c)
    prev = c

ans = 0
for seg in segs:
    pos = []
    for i in range(len(seg)):
        if seg[i] == "D":
            pos.append(i)
    for i, p in enumerate(pos):
        ans += p - i

print(ans)

Solution (Python):
s = input()
l = s.replace("BC","D").replace("B"," ")
L = l.replace("C"," ").split()
ans = 0
for i in L:
    numa = 0
    for j in i:
        if j == "A":
            numa += 1
        else:
            ans += numa
print(ans)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""
def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

s = input()
ss = []
ind = 0
while ind < len(s)-1:
    if s[ind] == 'B' and s[ind+1] == 'C':
        ss.append('D')
        ind+=2
    else:
        ss.append(s[ind])
        ind+=1
if ind == len(s)-1:
    ss.append(s[ind])
ind = 0
A = 0
ans = 0
while ind < len(ss):
    if ss[ind] == 'A':
        A+=1
    elif ss[ind] == 'D':
        ans+=A
    else:
        A = 0
    ind+=1
print(ans)

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

Solution (Python):
s = input()
s = s.replace("BC", "D")
acnt = 0
mode = 1
ans = 0
"""
AD -> DA
AAD -> ADA -> DAA
ADAD -> DAAD -> DADA -> DDAA

Dを見る→前にあるAは何個？（その分だけ数える）
"""
for i in range(len(s)):
	if s[i] == "A":
		acnt += 1
	elif s[i] == "D":
		ans += acnt
	else:
		acnt = 0
print(ans)

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

s=input()
n=len(s)
i=0
k=0
l=[[]]
while i<n:
    if s[i]=="A":
        l[k].append(0)
    elif i<n-1:
        if s[i:i+2]=="BC":
            l[k].append(1)
            i+=1
        else:
            l.append([])
            k+=1
    i+=1
ans=0
for lis in l:
    m=len(lis)
    if m>0:
        n1=[0]*m
        c=0
        for i in range(m):
            if lis[-1-i]==1:
                c+=1
            n1[-1-i]=c
        for i in range(m):
            if lis[i]==0:
                ans+=n1[i]
print(ans)

Solution (Python):
import re

S = 'S' + input()
new_s = list(S.replace('BC', 'D'))


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

Solution (Python):
def rep(s):
    a,b='',[]
    for i in range(len(s)):
        if s[i]=='C' or s[i]=='B':
            b.append(a)
            a=''
        else:
            a+=s[i]
    b.append(a)
    a=[]
    for i in range(len(b)):
        if b[i]!='':
            a.append(b[i])
    return a
s=rep(input().replace('BC','D'))
b=0
for i in s:
    a=0
    for j in i:
        if j=='A':
            a+=1
        else:
            b+=a
print(b)

Solution (Python):
s = input()
i = 0
p = []
tmp =[]
n = len(s)
while i<n-1:
    if(s[i]=="A" or s[i:i+2]=="BC"):
        if s[i]=="A":
            
            tmp.append(1)
            i+=1
        else:
            tmp.append(0)
            i+=2

    else:
        p.append(tmp)
        tmp = []
        i+=1
p.append(tmp)
res = 0
for i in range(len(p)):
    anum = 0
    bcnum = 0
    for j in range(len(p[i])):
        if p[i][j]:anum+=1
        else:
            res += (anum)
print(res)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(str(input()))
length = len(S)

abc_cnt = 0
a_cnt = 0
ans = 0
for i in range(length):
    s = S[i]
    if s=='A':
        if abc_cnt!=1:
            a_cnt = 0
        a_cnt += 1
        abc_cnt = 1
    elif s=='B':
        if abc_cnt==1:
            abc_cnt += 1
        else:
            a_cnt = 0
            abc_cnt = 0
    else:
        if abc_cnt==2:
            abc_cnt = 1
            ans += a_cnt
        else:
            a_cnt = 0
            abc_cnt = 0
print(ans)

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

cnt = 0
ans = []
for i in range(len(s)):
    if s[i] == "A": cnt += 1
    elif s[i] == "D":ans.append(cnt)
    else: cnt = 0
print(sum(ans))

Solution (Python):
s=input()
s=s.replace("BC","D")
m=[]
is_seq=False
t=""
for ss in s:
  if is_seq:
    if ss=="A" or ss=="D":
      t+=ss
    else:
      m.append(t)
      t=""
      is_seq=False
  else:
    if ss=="A":
      is_seq=True
      t+=ss
if t!="":
  m.append(t) 

ans=0
for mm in m:
  cnt_a=mm.count("A")
  for i in range(len(mm)):
    if mm[i]=="A":
      ans+=len(mm)-cnt_a-i
      cnt_a-=1
print(ans)

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

S = readline().rstrip().replace("BC","X")





parts = S.replace("B",",").replace("C",",").split(",")

ans = 0
for part in parts:
  
  cnt = 0
  for i in range(len(part)):
    if part[i] == "X":
      ans += (i - cnt)
      cnt += 1

print(ans)

Solution (Python):
s=input()
n=len(s)
ans=0
current=0
flag=0
for i in range(n-1):
  if flag==1:
    flag=0
    continue
  if s[i]=='B' and s[i+1]=='C':
    ans+=current
    flag=1
  elif s[i]=='A':
    current+=1
  else:
    current=0
print(ans)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


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


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





S = S().replace("BC","X")

ans = []
prev = "Z"
for s in S:
  if s in "AX":
    if prev in "AX":
      ans[-1].append(s)
    else:
      ans.append([s])
  prev = s
res = 0
for D in ans:
  n = len(D)
  count = 0
  place = 0
  for i in range(n):
    if D[i] == "A":
      count += 1
      place += i+1
  res += count*(n+(n-count+1))//2 - place
print(res)

Solution (Python):
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
U=0

I=0
K=0
while I<N-2:
    if (S[I]==0) and (S[I+1]==1) and (S[I+2]==2):
        U+=1
        T+=U
        I+=3
    elif S[I]==0:
        U+=1
        I+=1
    elif (S[I]==1) and (S[I+1]==2):
        T+=U
        I+=2
    else:
        U=0
        I+=1

if (S[-2]==1 and S[-1]==2) and S[-3]!=0:
    T+=U
    
print(T)

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

print(ans)

Solution (Python):
import sys

readline = sys.stdin.buffer.readline


sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    S = readline().decode('utf-8')
    S = S.replace('BC', 'D')

    count_a = 0
    ans = 0

    for s in S:
        if s == 'A':
            count_a += 1
        elif s == 'D':
            ans += count_a
        else:
            count_a = 0

    print(ans)


if __name__ == '__main__':
    main()

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


if __name__ == "__main__":
    main()

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

cnt = 0
ans = 0


for x in s:
    if x == 'A':
        cnt += 1
    elif x == 'X':
        ans += cnt
    else:
        cnt = 0

print(ans)

Solution (Python):
s = str(input())
ans = 0
i = 0
cnt = 0
while i < len(s) - 1:
    if s[i] == 'A':
        cnt += 1
        i += 1
    elif s[i] == 'B':
        if s[i+1] == 'C':
            ans += cnt
            i += 2
        else:
            cnt = 0
            i += 1
    else:
        cnt = 0
        i += 1
print(ans)

Solution (Python):
s = input()
l = len(s)
s += "D"*10
cnt = 0
ans = 0
i = 0
while i < l:
    if s[i] == "A": cnt += 1
    elif s[i:i+2] == "BC":
        ans += cnt
        i += 1
    else: cnt = 0
    i += 1
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    s = rl().rstrip()
    t = s.replace('BC', 'D')
    
    cnt = 0
    i = 0
    ans = 0
    while i < len(t):
        if t[i] == 'B' or t[i] == 'C':
            cnt = 0
        elif t[i] == 'A':
            cnt += 1
        else:
            ans += cnt
        i += 1
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
s = input()
ans = 0
ds = []
tmp = 0
b = ""
while tmp < len(s):
    if s[tmp] == 'A':
        b += 'A'
        tmp += 1
    elif s[tmp:tmp+2] == "BC":
        b += 'D'
        tmp += 2
    else:
        ds.append(b)
        b = ""
        tmp += 1
ds.append(b) if b else None
ans = 0
def inver(s):
    tmp = 0
    res = 0
    for i in s:
        if i == 'A':
            tmp += 1
        else:
            res += tmp
    return res
for i in ds:
    ans += inver(i)
print(ans)

Solution (Python):
s = input() + '?'

ans = 0
n_a = 0
n_bc = 0
i = 0
while(i<len(s)):
    if(s[i:i+2]=='BC'):
        n_bc += 1
        i += 2
        continue
    else:
        ans += n_a * n_bc
        n_bc = 0
        if(s[i]=='A'):
            n_a += 1
        else:
            n_a = 0
        i += 1

print(ans)

Solution (Python):
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    S = list(input())
    S = [1 if c == 'A' else 2 if c=='B' else 3 for c in S]
    N = len(S)
    ans = 0
    continuous_one = 0
    i = 0

    while i < N-1:
        if S[i] == 1:
            continuous_one += 1
            i += 1
        elif S[i] == 2 and S[i+1] == 3:
            ans += continuous_one
            i += 2
        else:
            continuous_one = 0
            i += 1
    

    



    print(ans)



    
main()

Solution (Python):
s = input()
s = s.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)