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



Sample input:
ABCABC


Sample output:
3

Submission status: Accepted

Solution code:
import re
import sys
input = sys.stdin.readline

s = input()
s = s.replace('BC', 'D')
s = re.split('[BC]', s)
res = 0

for kk in s:
  if not kk:
    continue
  d = 0
  for a in kk[::-1]:
    if a == 'D':
      d += 1
    else:
      res += d
    
print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

s = input()
s = s[::-1]
ans = 0
a = -1
s = (s.replace('CB', 'D').replace('C', '\t').replace('B', '\t')).split()
for i in range(len(s)):
    for j in range(s[i].count('A')):
        a = s[i].find('A',a+1)
        ans += a-j
    a = -1
print(ans)

Submission status: Accepted

Solution code:
s = input()
s = s.replace("BC", "D")
#print(s)

ans = 0
cnt_a = 0
for i in range(len(s)):
    if s[i] in ["B", "C"]:
        cnt_a = 0
    elif s[i] == "A":
        cnt_a += 1
    elif s[i] == "D":
        ans += cnt_a

print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
ans = 0
X = [0] * (N+2)
for i in range(N)[::-1]:
    if S[i:i+2] == "BC":
        X[i] = X[i+2] + 1
    elif S[i] == "A":
        X[i] = X[i+1]
        ans += X[i]
    else:
        X[i] = 0

print(ans)

Submission status: Accepted

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

cnt = 0
num_a = 0
for i in range(len(s)):
    if s[i] in ["B", "C"]:
        num_a = 0
    elif s[i] == "A":
        num_a += 1
    elif s[i] == "D":
        cnt += num_a

print(cnt)

Submission status: Accepted

Solution code:
s = list(input())

t = []

i = 0
while i < len(s) - 1:
    if s[i] == 'A':
        t.append('A')
        i += 1
    elif s[i] == 'B':
        if s[i+1] == 'C':
            t.append('D')
            i += 2
        else:
            t.append('B')
            i += 1
    else:
        t.append('C')
        i += 1

mode = 0
ans = 0
count = 0

for x in t:
    if mode == 0:
        if x == 'A':
            count = 1
            mode = 1
    else:
        if x == 'A':
            count += 1
        elif x == 'D':
            ans += count
        else:
            mode = 0

print(ans)

Submission status: Accepted

Solution code:
s=input()
t=''
skip=False
for i in range(len(s)-1):
    if skip:
        skip=False
        continue
    if s[i:i+2]=='BC':
        t+='D'
        skip=True
    else:
        t+=s[i]

ans,cnt=0,0
for i in range(len(t)):
    if t[i]=='A':
        cnt+=1
    if t[i] in ['B','C']:
        cnt=0
    if t[i]=='D':
        ans+=cnt
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = 0
A = 0
BC = 0
prv = 0
if S[0] == 'A':
    A += 1
    prv = 'A'
else:
    prv = S[0]
for i in S[1:]:
    if prv == 'A':
        if i == 'A':
            A += 1
        elif i == 'B':
            prv = 'B'
        else:
            ans += A*BC
            A = 0
            BC = 0
            prv = 'C'
    elif prv == 'B':
        if i == 'A':
            ans += A*BC
            A = 1
            BC = 0
            prv = 'A'
        elif i == 'B':
            ans += A*BC
            A = 0
            BC = 0
        else:
            BC += 1
            prv = 'C'
    else:
        if i == 'A':
            ans += A*BC
            A += 1
            BC = 0
            prv = 'A'
        elif i == 'B':
            prv = 'B'
        else:
            ans += A*BC
            A = 0
            BC = 0
ans += A*BC
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

S = input()[:-1]
S = S.replace("BC", "X")  # BC->X
S = S.replace("B", "C")  # BCという連続になってない、[AX]の連続区間のB,Cは区切りとなる。区切り文字を"C"にまとめる

L = S.split("C")
ans = 0
# 区切り文字で分割して、それぞれを見て転倒数を数えていく
for l in L:
    X_num = l.count('X')
    for c in l:
        if c == "X":
            X_num -= 1
        else:
            ans += X_num
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

S = input()[:-1]
S = S.replace("BC", "X")
S = S.replace("B", "C")

L = S.split("C")
ans = 0
for l in L:
    X_num = l.count('X')
    for c in l:
        if c == "X":
            X_num -= 1
        else:
            ans += X_num
print(ans)

Submission status: Accepted

Solution code:
s = input()
num_a = 0
num_ab = 0
val = 0
for i in range(len(s)):
    if s[i] == 'A':
        num_a += 1
    elif s[i] == 'B':
        num_ab = num_a
        num_a = 0
    else:
        if num_a == 0:
            val += num_ab
            num_a = num_ab
            num_ab = 0
        else:
            num_a = 0
            num_ab = 0
print(val)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
total = 0
i = 0
inRange = False
range_start = 0
range_minus = 0
range_cnt = 0
 
while i < len(s)-1:
    # BC=>0
    if s[i] == 'B' and s[i+1] == 'C':
        if inRange:
            total += (range_cnt - range_minus)
            range_cnt += 1
            range_minus += 1
        i += 1
    elif s[i] == 'A':
        inRange = True
        range_cnt += 1
    else:
        inRange = False
        range_start = 0
        range_minus = 0
        range_cnt = 0
    i+=1
    
print(total)

Submission status: Accepted

Solution code:
s = input()
total = 0
i = 0
inRange = False
range_start = 0
range_minus = 0
range_cnt = 0

while i < len(s)-1:
    # BC=>0
    if s[i] == 'B' and s[i+1] == 'C':
        if inRange:
            total += (range_cnt - range_minus)
            range_cnt += 1
            range_minus += 1
        i += 1
    elif s[i] == 'A':
        inRange = True
        range_cnt += 1
    else:
        inRange = False
        range_start = 0
        range_minus = 0
        range_cnt = 0
    i+=1
    
print(total)

Submission status: Accepted

Solution code:
s = input()

count = 0
pre = ''
movable = 0
for i in reversed(s):
    if pre == '':
        pre = i
    elif pre == 'CB' and i == 'A':
        count += movable
    elif pre == 'CB' and i == 'C':
        pre = 'C'    
    elif pre == 'C' and i == 'B':
        movable += 1
        pre = 'CB'
    else:
        movable = 0
        pre = i   
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def b():
    import re
    s = input()
    s = re.sub(r'BC', 'D', s)
    ls = range(len(s))
    ac, c = 0, 0
    for i in ls:
        if s[i] == 'A':
            ac += 1
        elif s[i] == 'D':
            c += ac
        else:
            ac = 0
    return c


ans = b()
print(ans)

Submission status: Accepted

Solution code:
def b():
    import re
    s = input()
    s = re.sub(r'BC', 'D', s)
    ls = range(len(s))
    ac, c = 0, 0
    for i in ls:
        if s[i] == 'A':
            ac += 1
        elif s[i] == 'D':
            c += ac
        else:
            ac = 0
    return c


ans = b()
print(ans)

Submission status: Accepted

Solution code:
S = input()

ans = 0
cnt = 0
flg = 0

for i in range(len(S)-1) :
    if S[i] == "A" :
        cnt += 1
    elif S[i]+S[i+1] == "BC" :
        ans += cnt
        flg = 1
    elif flg :
        flg = 0
    else :
        cnt = 0

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()
s = s.replace('BC', 'D')

a_num = 0
ans = 0
for c in s:
    if c == 'A':
        a_num += 1
    elif c == 'D':
        ans += a_num
    else:
        a_num = 0

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()

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

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()

a_num = 0
bc_num = 0
ans = 0
i = 0
while i < len(s):
    if s[i] == 'A':
        a_num += 1
        i += 1
    elif s[i:i+2] == 'BC':
        ans += a_num
        i += 2
    else:
        a_num = 0
        b_num = 0
        i += 1

print(ans)

Submission status: Accepted

Solution code:
s = input()
s2 = s.replace("BC", 'D')
ans = 0
na = 0
for c in s2:
  if na:
    if c == "D":
      ans += na
    elif c != "A":
      na = 0
  if c == 'A':
    na += 1
print(ans)

Submission status: Accepted

Solution code:
a=b=0
for s in input().replace('BC','?'):b=s<'B'and(s>'?')+b;a+=b*(s<'A')
print(a)

Submission status: Accepted

Solution code:
a=b=0
for s in input().replace('BC','?'):b=(b+(s>'?'))*(s<'B');a+=b*(s<'A')
print(a)

Submission status: Accepted

Solution code:
import re

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

count = 0
count_a = 0
for string in s:
    if string == 'A':
        count_a += 1
    elif string == 'D':
        count += count_a
    else:
        count_a = 0

print(count)

Submission status: Accepted

Solution code:
s = list(input())

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

print(cnt)

Submission status: Accepted

Solution code:
s =  list(input())

ret = 0
i = 0
a = 0
bc = 0

while True :
    while i < len(s) and s[i] == 'A' :
        a += 1
        i += 1

    while i < len(s) - 1 and s[i:i+2] == ['B', 'C']  :
        bc += 1
        i += 2

    ret += a * bc

    bc = 0

    if i < len(s) and s[i] != 'A' :
        a = 0
        
    while i < len(s) and s[i] != 'A' :
        i += 1

    if i >= len(s) - 1 :
        break
    

print(ret)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()

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

point = 0
charge = 0
for i in range(len(s) - 1, -1, -1):
    if s[i] == "*":
        charge += 1
    elif s[i] == "A":
        point += charge
    else:
        charge = 0

print(point)

Submission status: Accepted

Solution code:
s=input()
s=s.replace('BC','D')
s=list(s)
count=0
 
def change():
  global count
  flg=False
  acount=0
  for i in range(len(s)-1):
    if s[i]=='A' and s[i+1]=='D':
      s[i]='D'
      s[i+1]='A'
      count+=1
      flg=True
      count+=acount
    elif s[i]=='A':
      acount+=1
    else:
      acount=0
      
  return flg
      
change()
 
print(count)

Submission status: Accepted

Solution code:
def solve2():
    s = raw_input()
    s = s.replace('BC', 'X')
    ans = 0
    acc_A = 0
    for ele in s:
        if ele == 'A':
            acc_A += 1
        elif ele == 'X':
            ans += acc_A
        else:
            acc_A = 0
    print ans

if __name__ == '__main__':
    solve2()

Submission status: Accepted

Solution code:
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
  
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
  
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
 
def minus(n):
  return 128-n

s = input()
total = 0
new_s = s.replace("BC", "D")
new_s_list = list(new_s)
new_s_list.append("G")
tmp_start = 0
for i in range(len(new_s_list)):
  if new_s_list[i] in {'B', 'C', 'G'}:
    ppp = list(map(minus, map(ord, new_s_list[tmp_start:i])))
    bit = Bit(128)
    ans = 0
    for j, p in enumerate(ppp):
      bit.add(p, 1)
      ans += j + 1 - bit.sum(p)
    total += ans
    tmp_start = i+1

print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()
def main():
	
	s=f4()
	n1=0
	ans=0
	i=0
	while i<len(s)-1:
		if s[i]=="A":
			n1+=1
		elif s[i:i+2]=="BC":
			ans+=n1
			i+=1
		else:
			n1=0
		i+=1
	print ans

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
S = input().replace('BC', 'X')
N = len(S)

ans = 0
tmp = 0
for i in range(N-1, -1, -1):
    if S[i] == 'A':
        ans += tmp
    elif S[i] == 'X':
        tmp += 1
    else:
        tmp = 0
print(ans)

Submission status: Accepted

Solution code:
s = input()
s = s.replace('BC', 'D')
lst = list(s)

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)

def intinput(): return map(int,input().split())

s=input()
s=s.replace("BC","D")
acnt=0
dcnt=0
ans=0
for i in s[::-1]:
    if i=="D":
        ans+=acnt*dcnt
        acnt=0
        dcnt+=1
    elif i=="A":
        acnt+=1
    else:
        ans += acnt * dcnt
        acnt=0
        dcnt=0
ans+=acnt*dcnt
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import re

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

ans = 0
num_a = 0
for char in s:
    if char == 'A':
        num_a += 1
    elif char == 'D':
        ans += num_a
    else:
        num_a = 0

print(ans)

Submission status: Accepted

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

a = 0 # 連続するaの数
ans = 0 # 答え

for i in t:
    if i == 'A':
        a += 1
    elif i == 'D':
        ans += a
    else:
        a = 0

print(ans)

Submission status: Accepted

Solution code:
S = input().replace("BC", "D")
acc = 0
ans = 0
for s in S:
  if s == "B" or s == "C":
    acc = 0
    continue
  if s == "A":
    acc += 1
  if s == "D":
    ans += acc
print(ans)

Submission status: Accepted

Solution code:
S = input()
S = S.replace('BC', 'D').replace('B', 'E').replace('C', 'E')
S = S.split('E')

ans = 0
for s in S:
    temp = 0
    for c in s:
        if c == 'A':
            temp += 1
        else:
            ans += temp
print(ans)

Submission status: Accepted

Solution code:

from re import sub

raw_s = input()

s = sub("BC","D",raw_s)

#print(s)

ac = 0
ans = 0
for c in s:
    if c == 'A':
        ac += 1

    elif c == 'D':
        ans += ac
        
    else:
        ac = 0
        
print(ans)

Submission status: Accepted

Solution code:
s = input()
t = s.replace('BC', 'D') 
answer = 0
A = 0
for i in range(len(t)):
    if t[i] == 'A':
        A+=1
    elif t[i] == 'D':
        answer+=A
    else:
        A = 0

print(answer)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )

Submission status: Accepted

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

def solve(s):
    ans = 0 ; tmp = 0
    for char in s:
        if char == "A": tmp += 1
        else: ans += tmp
    return ans

ans = 0
for ele in s: ans += solve(ele)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

s = stdin.readline().rstrip()

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def conv2num(c):
    if c == "A":
        return 1
    elif c == "D":
        return 2
    elif c == "B":
        return 3
    else:
        return 4

def solve(s: str):

    res = 0
    s = s.replace("BC","D")
    s = [ conv2num(c) for c in s ]

    acnt = 0
    for i,c in enumerate(s):
        if c == 1:
            acnt += 1
        elif c == 2:
            res += acnt
        else:
            acnt = 0


    print(res)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
s=input()
numA = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'A':
        numA += 1
    elif i < len(s)-1 and s[i] == 'B' and s[i + 1] == 'C':
        ans += numA
    elif i > 0 and s[i] == 'C' and s[i - 1] == 'B':
        pass
    else:
        numA = 0
print(ans)

Submission status: Accepted

Solution code:
S=input().replace("BC","D").replace("B","C").split("C")
ans = 0
for s in S:
    a = 0
    for c in s:
        if c == "A":a+=1
        else:ans += a
print(ans)

Submission status: Accepted

Solution code:
s = input()

if len(s)<3:
    print(0)
    exit()

i = 0
while True:
    if s[i:i+2]=='BC':
        s=s[:i]+'D'+s[i+2:]
    i+=1
    if i>=len(s):
        break


s1 = s.split('B')
s2 = []
for i in range(len(s1)):
    s2+= s1[i].split('C')


ans = 0

for i in range(len(s2)):
    d = s2[i].count('D')
    for j in range(len(s2[i])):
        if s2[i][j]=='A':
            ans += d
        else:
            d-=1


print(ans)

Submission status: Accepted

Solution code:
s = input()

if len(s)<3:
    print(0)
    exit()

i = 0
while True:
    if s[i:i+2]=='BC':
        s=s[:i]+'D'+s[i+2:]
    i+=1
    if i>=len(s):
        break


s1 = s.split('B')
s2 = []
for i in range(len(s1)):
    s2+= s1[i].split('C')


ans = 0

for i in range(len(s2)):
    d = s2[i].count('D')
    for j in range(len(s2[i])):
        if s2[i][j]=='A':
            ans += d
        else:
            d-=1
            if d == 0:
                break

print(ans)

Submission status: Accepted

Solution code:
S = input()
T = S.replace("BC", "D")
a = 0
ans = 0
for i in T:
    if i == "A":
        a += 1
    elif i == "D":
        ans += a
    else:
        a = 0

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()
s = s.replace("BC", "X")
count = 0
a_stack = 0
for idx, char in enumerate(s):
    if char == "A":
        a_stack += 1
    elif char == "X":
        count += a_stack
    else:
        a_stack = 0
print(count)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
"""
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): # それぞれの[AX]+のパターンについてloop
    count = 0 #
    # 時間なくて、index間違えたくないので、逆順にした
    # (AXAXAXA -> XXXAAAA にするんじゃなくて AXAXAXA -> AAAAXXXにする回数)
    t = ''.join(list(reversed(str(m.groups()[0]))))
    for i in range(len(t)):
        if t[i] == "A":
            # Aが見つかったらXの一番右端に持ってくるのに必要な回数をカウント。例えば、
            # AAAXXXAA で4つめのAを探索しているのなら
            # AAAAXXXA にするのに必要な回数は3 と数えたい
            # Aのindex 6 - (既に見つかっているAの数) 3 = 3 で求められる
            res += (i - count)
            count += 1
print(res)

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

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



counta = 0
i = 0
while(i<len(li)-2):
  # print(i, counta, count)
  if(li[i]=='A'):
    counta += 1
    # print(i)
    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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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)
    # ans = 0
    # flag = True
    # s = [i for i in s]
    # while flag:
    #     flag = False
    #     for i in range(n-2):
    #         if s[i:i+3] == ['A', 'B', 'C']:
    #             s[i] = 'B'
    #             s[i+1] = 'C'
    #             s[i+2] = 'A'
    #             flag = True
    #             ans += 1
    # 
    # print(ans)

s=input()
main(s)
# import random
# for i in range(1000):
#     s = [''] * random.randint(1, 100)
#     for j in range(len(s)):
#         s[j] = 'ABC'[random.randint(0, 2)]
#         a,b=main(''.join(s))
#         if a != b:
#             print(''.join(s))
#             print(a, b)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

a_series = 0

for i in range(len(s)):
  #print(i, abc)
  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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#
#　　　　　　　　　　　　　　,. -─────────‐- .、
#　　　　　　　　　　　　 ／／￣￣＼　　　　　　／￣￣＼＼
#　　　　　　　　　　　／　　　　　　　　　　　　　　　　　　　　 ＼
#　　　　　　　　　 ／　　　　　　　　::::::::::::::::::::::::::::::::　　　　　　　＼
#　　　　　　　　／ 　　／ ／￣＼＼::::;;;;;;;;;;;;;;;;;:::::／／￣＼＼　　＼
#　　　　　　 ／　　　　|　 |.　┃　.|　| ::::;;;;;;;;;;;;;::::｜ |.　┃　.|　|　　　 ＼　　　　　　　　　　　　　　　　　　　　　
#　　　　　／ 　　　　　＼ ＼＿／／　::::::::::::::::::　＼＼＿／／　　　　　 ＼　　　　
#　　　 ／　　　　　..／￣￣＼　／　　　::｜::　　　＼　／￣￣＼..　　　　　＼
#　　 / 　 　 　 　 :::::　　　　　　|　　　　　 |　　　　　 |　　　　　　 :::::　　　　　 ヽ.　　　
#　　| 　　　　　　　　　　　　　　|　　　　　 |　　　　　 |　　　　　　　　　　　　　　|.
#　　| 　　　　　　　　　　　　　　＼＿＿／＼＿＿／ 　　　　　　　　　　　　　　|　　　　　　　　　　　　　　　　　
#　　| 　　　　　　　　　　　　　　　｜　　　　　　　｜　　　　　　　　　　　　　　　|
#　　| 　　　　　　　　　　　　　　　｜r─‐┬──､|　　　　　　　　　　　　　　　 |
#　　ヽ　　　　　　　　　　　　　　　 |/　　　|　　 　｜　　　　　　　　　　　　　　/
#　　　 ＼　　　　　　　　　　　　　 ＼　　　　　　／　　　　　　　　　　　　　／
#　　　　　＼ 　　　　　　　　　　　　　￣￣￣￣　　　　　　　　　　　　　／
#
#
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()