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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    s = input()
    s = s.replace("BC", "D")
    #print(s)
    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()

Submission status: Accepted

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

cnt = 0
ans = 0

# AとXが連続している限り、Xより左のAは右にスキップしていける
for x in s:
    if x == 'A':
        cnt += 1
    elif x == 'X':
        ans += cnt
    else:
        cnt = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()

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

count=0

c=0
while c<len(s):
    if s[c]=="A" or s[c]=="D":
        d=c
        while s[d]=="A" or s[d]=="D":
            d+=1
            if d==len(s):
                break
        an=0
        for i in range(c,d):
            if s[i]=="A":
                an+=1
            else:
                count+=an
        c=d
    else:
        c+=1

print(count)

Submission status: Accepted

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

cnt = 0
ans = 0
for s in S[::-1]:
    if s == 'X':
        cnt += 1
    elif s == 'A':
        ans += cnt
    else:
        cnt = 0

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    s = [c for c in gete()]
    ans = 0
    a = 0
    cur = 0
    while cur < len(s) - 2:

        if "".join(s[cur:cur + 3]) == "ABC":
            ans += a + 1
            cur += 2
            s[cur] = "A"
        else:
            if s[cur] == "A":
                a += 1
            else:
                a = 0
            cur += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=list(input())
S=[]
n=0
while n+1<len(s):
    if s[n:n+2]==["B","C"]:
        S.append("D")
        n+=1
    else:
        S.append(s[n])
    n+=1
a=0
ans=0
for i in range(len(S)):
    if S[i]=="D":
        ans+=a
    elif S[i]=="A":
        a+=1
    else:
        a=0
print(ans)

Submission status: Accepted

Solution code:
def main():
    import re
    s = input()
    s = s.replace('BC', 'D')

    obj = re.findall('[AD]+', s)
    ans = 0

    for x in obj:
        cnt = 0
        for i in x:
            if i == 'A':
                cnt += 1
            if i == 'D':
                ans += cnt

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10**7)

ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

s = ns()

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

cnt = 0
ans = 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:
# 解説
# ABC -> CBA
# BCは増えない&離れない
# AD -> DAを考える
# Aの右にBCが何個あるか

s = input()
n = len(s)
i = 0
ans = 0
count_a = 0

while i < n:
    if s[i] == 'A':
        count_a += 1
    elif i < n - 1 and s[i:i+2] == 'BC' and count_a > 0:
        ans += count_a
        i += 1
    else:
        count_a = 0
    i += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
ans = 0

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

Submission status: Accepted

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

S = readline().rstrip()
S = S.replace("BC","X")
S = S.replace("B",".").replace("C",".")
arr = S.split(".")
ans = 0
for a in arr:
  cnt = 0
  for i in range(len(a)):
    if a[i] == "X":
      ans += (i - cnt)
      cnt += 1
  
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

cnt=0
cntA=0
cntD=0

for i in range(N):
  if s[i]=="A":
    cntA += 1
  elif s[i]=="D":
    cnt += cntA
  else:
    cntA=0
    cntD=0
    
print(cnt)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
ans = 0
cntA = 0
for i in range(n-1):
    if s[i] == 'A':
        cntA += 1
    elif s[i: i+2] == 'BC':
        ans += cntA
    elif i > 0 and s[i-1: i+1] == 'BC':
        continue
    else:
        cntA = 0

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
p = []
flag = False
for i in range(n-1):
    if flag:
        flag = False
        continue 
    if s[i] == "B" and s[i+1] == "C":
        flag = True
        p.append("#")
    elif s[i] == "A":
        p.append(s[i])
    else:
        p.append("!")

nowa = 0
ans = 0
for i in range(len(p)):
    if p[i] == "A":
        nowa += 1
    elif p[i] == "#":
        ans += nowa
    elif p[i] == "!":
        nowa = 0
print(ans)

Submission status: Accepted

Solution code:
def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    s = input()
    i = 0
    cc = output = 0
    while i < len(s)-1:
        if s[i] == 'A':
            cc += 1
            i += 1
        elif s[i] == 'B' and s[i+1] == 'C':
            output += cc
            i += 2
        else:
            cc = 0
            i += 1
    print(output)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())
    target = ["A", "B", "C"]
    next = ["B", "C"]

    idx = 0
    ans = 0
    a_cnt = 0

    while len(s) - 3 >= idx:
        if [s[idx], s[idx + 1], s[idx + 2]] == target:
            ans += a_cnt
            ans += 1
            a_cnt += 1

            idx += 3
            while len(s) - 3 >= idx:
                if [s[idx], s[idx + 1], s[idx + 2]] == target:
                    ans += a_cnt + 1
                    idx += 3
                    a_cnt += 1
                else:
                    break

            while len(s) - 2 >= idx:
                if [s[idx], s[idx + 1]] == next:
                    ans += a_cnt
                    idx += 2
                else:
                    break

        elif s[idx] != "A":
            a_cnt = 0
            idx += 1
        elif s[idx] == "A":
            a_cnt += 1
            idx += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
l = len(s)
t = [0]*l
c=0
ch=0
for i in range(l):
    if s[l-1-i]=='C' and c == 0:
        c=1
    elif s[l-i-1]=='B' and c == 1:
        c=0
        ch+=1
    elif s[l-i-1]=='A' and c == 1:
        c=0
        ch = 0
    elif s[l-i-1]=='A':
        c=0
    elif s[l-i-1]=='C' and c==1:
        ch=0
        c=1
    else:
        ch=0
        c=0
    t[l-i-1]=ch
ans = 0
for i in range(l):
    if s[i]=='A':
        ans += t[i]
print(ans)

Submission status: Accepted

Solution code:
s = input()

ss = s.replace('BC', '0').replace('A','1')

cnt = 0
ans = 0

for i in range(len(ss)-1, -1, -1):
    if ss[i] == 'B' or ss[i] == 'C':
        cnt = 0
    elif ss[i] == '1': ans += cnt
    else : cnt += 1
        
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

s = list(input())
chain = {}
abcs = []
kugiri = 0
countA = 0
ans = 0

for i in range(1,len(s)):
    if not((s[i] == "B" and s[i-1] == "C") or (s[i] == "B" and s[i-1] == "A") or (s[i] == "C" and s[i-1] == "B") or (s[i] == "A" and s[i-1] == "C") or (s[i] == "A" and s[i-1] == "A")):
        kugiri = i
        countA = 0
    elif s[i-1] == "A":
        countA += 1
    if s[i] == "C" and s[i-1] == "B":
        ans += countA

print(ans)

Submission status: Accepted

Solution code:
s = input().replace('BC','D')
d = 0
ans = 0
for c in s[-1::-1]:
    if c == 'D':
        d += 1
    elif c == 'A':
        ans += d
    else:
        d = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
n = len(s)

i = 0
numA = 0
res = 0
while i < n-1:
    if s[i] == 'A':
        numA += 1
    elif s[i] == 'B' and s[i+1] == 'C':
        res += numA
        i += 1
    else:
        numA = 0
    i += 1
print(res)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
s += "ZZ"
def make(s,i):
    cnt = 0
    a,bc = [],[]
    while True:
        if s[i] == "A":
            a.append(cnt)
            i += 1
        elif s[i:(i+2)] == "BC":
            bc.append(cnt)
            i += 2
        else:
            while s[i] != "A":
                if s[i] == "Z":
                    break
                i += 1
            return a,bc,i
        cnt += 1

def cnt(bc):
    c = 0
    for i in range(len(bc)):
        c += bc[i]-i
    return c

i = 0
ans = 0
while i < n:
    a,bc,i = make(s,i)
    ans += cnt(bc)
print(ans)

Submission status: Accepted

Solution code:
s = input()

S = []
N = len(s)
i = 0
while i < N:
    if i < N - 1 and s[i:i + 2] == 'BC':
        S.append('D')
        i += 2
    else:
        S.append(s[i])
        i += 1

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/agc034/tasks/agc034_b

s = input()
s = s.replace('BC', 'X')
ans = 0
a = 0
for i in range(len(s)):
    if s[i] == 'A':
        a += 1
    elif s[i] == 'X':
        ans += a
    else:
        a = 0
print(ans)

Submission status: Accepted

Solution code:
s = input()
s = s.replace('BC', 'T')
a_count = 0
ans = 0
for c in s:
    if c == 'A':
        a_count += 1
    elif c == 'T':
        ans += a_count
    else:
        a_count = 0
print(ans)

Submission status: Accepted

Solution code:
def main():

    s = list(input())
    ans = 0
    h = 0
    i = 0
    while i < len(s):
        if s[i] == "A":
            h += 1
            # print(i, h, s)
            if s[i:i+3] == ["A", "B", "C"]:
                ans += h
                h -= 1
                i += 2
                s[i] = "A"
            else:
                i += 1
        else:
            h = 0
            i += 1
    return ans

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

Submission status: Accepted

Solution code:
def main():
    S = input()
    S = S.replace("BC", "D")

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x,'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x,'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x,'x') # rev => int(res, 16)
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    s = IS()
    n = len(s)
    s = s[::-1]
    cb_num = 0
    a_num = 0
    cnt = 0
    i = 0
    while i < n:
        if cb_num == 0:
            if s[i:i+2] == 'CB':
                cb_num += 1
                i += 2
                continue
        if s[i:i+2] == 'CB':
            cb_num += 1
            i += 2
        elif s[i] == 'A':
            cnt += cb_num
            i += 1
        else:
            cb_num = 0
            i += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = list(input()) #sys.stdin.readlineは最後が改行

def mergecount(A):
    cnt = 0
    n = len(A)
    if n>1:
        A1 = A[:n>>1]
        A2 = A[n>>1:]
        cnt += mergecount(A1)
        cnt += mergecount(A2)
        i1=0
        i2=0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt

ans=0
ss = []
i=0
finish_calc = 0
while i<len(s):
    if s[i]=='A':
        ss.append(1)
        i+=1
        finish_calc=0
    elif i+1<len(s):
        if s[i]=='B' and s[i+1]=='C':
            ss.append(0)
            finish_calc=0
            i+=2
            continue
        if finish_calc==0:
            ans += mergecount(ss)
            ss=[]
            i+=1
            finish_calc=1
        else:
            i+=1
    elif finish_calc==0:
        ans += mergecount(ss)
        ss=[]
        i+=1
        finish_calc=1
    else:
        i+=1

if finish_calc==0:
    ans += mergecount(ss)

print(ans)

Submission status: Accepted

Solution code:
s = input()

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

ans = 0
a_cnt = 0
for e in s:
    if e == "A":
        a_cnt += 1

    elif e == "D":
        ans += a_cnt

    else:
        a_cnt = 0

print(ans)

Submission status: Accepted

Solution code:
S = input()
newS = S.replace("BC","D")
A_count = 0
ans = 0
for i in range(len(newS)):
    if newS[i] == "A":
        A_count += 1
    elif newS[i] == "D":
        ans += A_count
    else:
        A_count = 0
print(ans)

Submission status: Accepted

Solution code:
s = input().replace('BC', 'o').replace('B', 'x').replace('C', 'x')

ans = cnt = 0
for c in s[::-1]:
    if c == 'x':
        cnt = 0
        continue
    elif c == 'o':
        cnt += 1
    else:
        ans += cnt
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    len_s=len(s)
    i=0
    cunt_a=0
    ans=0
    while i<len_s:
        if s[i]=='A':  
            cunt_a+=1
        elif i<len_s-1 and s[i:i+2]=="BC" and cunt_a>0:
            ans+=cunt_a
            i+=1
        else:
            cunt_a=0
        i+=1
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    s=input()
    len_s=len(s)
    i=0
    cunt_a=0
    ans=0
    while i<len_s:
        if s[i]=='A':  
            cunt_a+=1
        elif i<len_s-1 and s[i:i+2]=="BC" and cunt_a>0:
            ans+=cunt_a
            i+=1
        else:
            cunt_a=0
        i+=1
    print(ans)
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
ans=0
bc=0
i=len(s)-1
while i>=0:
  if s[i]=="A":
    ans+=bc
    i-=1
  elif i>0 and s[i-1:i+1]=="BC":
    bc+=1
    i-=2
  else:
    bc=0
    i-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = '?' + read().rstrip().decode()[::-1]
ans = 0
cnt = 0
index = 0
for _ in range(len(s) + 1):
    if s[index] == 'C' and s[index + 1] == 'B':
        cnt += 1
        index += 1
    elif s[index] == 'A':
        ans += cnt
    else:
        cnt = 0
    index += 1
    if index >= len(s) - 1:
        break
if s[-1] == 'A':
    ans += cnt
print(ans)

Submission status: Accepted

Solution code:
S = input()
ans = 0
idx = 0
a_count = 0
while idx < len(S):
    if S[idx] == "A":
        a_count += 1
    elif S[idx:idx+2] == "BC":
        ans += a_count
        idx += 1
    else:
        a_count = 0
    idx += 1
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)

res = 0
ac = 0
i = 0
while i<n:
    if s[i]=="A":
        ac += 1
    elif s[i]=="B" and i+1<n and s[i+1]=="C":
        res += ac
        i += 1
    else:
        ac = 0
    i += 1
print(res)

Submission status: Accepted

Solution code:
s = input()

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

a = 0
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
s=input()
n=len(s)

cnt=0
ans=0

for i in range(n-1):
    if s[i]=="A":
        cnt+=1
    elif s[i:i+2]=="BC":
        ans+=cnt
    elif i>0 and s[i-1:i+1]=="BC":
        pass
    else:
        cnt=0

print(ans)

Submission status: Accepted

Solution code:
S=input().replace("BC","#")

cnt,point=0,0
for i in range(len(S)):
    if S[i]=="A":point +=1
    elif S[i]=="#":cnt +=point
    else:point=0
print(cnt)

Submission status: Accepted

Solution code:
def main(s):
  n=len(s)
  ans=0
  na=0
  i=0
  while i<n:
    if s[i]=='A':
      na+=1
      i+=1
    elif s[i]=='B' and i<n-1 and s[i+1]=='C' and na>0:
      ans+=na
      i+=2
    else:
      na=0
      i+=1
  print(ans)
if __name__=='__main__':
  s=list(input())
  main(s)

Submission status: Accepted

Solution code:
from collections import deque
s=list(input())
d=deque()
n=len(s)
acount=0
cnt=0
for i in range(n):
    d.append(s[i])
i=0
while i<n:
    if s[i]=="A":
        acount=acount+1
    elif (i<n-1)and(acount>0)and(s[i]=="B")and(s[i+1]=="C"):
        cnt=cnt+acount
        i=i+1
    else:
        acount=0
    i=i+1
    #print(acount,cnt)
    #input()
print(cnt)

Submission status: Accepted

Solution code:
s=input() 
n=len(s)
cnt=0
temp=[0]*n
JinA=[] 
for i in range(n-1):
    if s[i]=="A":
        if i>0:
            temp[i]+=temp[i-1]+1 
        else:
            temp[i]+=1
    if s[i]=="B" and s[i+1]=="C" and i>0:
        temp[i+1]+=temp[i-1]
        cnt+=temp[i-1] 

print(cnt)

Submission status: Accepted

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

ans = 0
a_count = 0

for i in range(len(s)):
    if s[i] == "A":
        a_count += 1
    elif s[i] == "X":
        ans += a_count
    else:
        a_count = 0

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)

s = s.replace("BC","X")
s += "DD"
nn = len(s)

alen = 0
ans = 0
for i in range(nn):
    if s[i] == "A":
        alen += 1
    elif s[i] == "X":
        ans += alen
    else:
        alen = 0
        
print(ans)

Submission status: Accepted

Solution code:
s=input()
s=s.replace("BC","D")
n=len(s)
ans=0
cnt=0
for i in range(n-1,-1,-1):
    if s[i]=="B" or s[i]=="C":
        cnt=0
    elif s[i]=="D":
        cnt+=1
    else:
        ans+=cnt
print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
ans = 0
cnt = 0
i = 0
while i < N:
  if S[i] == "A":
    cnt += 1
    i += 1
  elif "".join(S[i:i+2]) == "BC":
    ans += cnt
    i += 2
  else:
    cnt = 0
    i += 1
print(ans)

Submission status: Accepted

Solution code:
s=input()
n=0
c=0
i=0
while i<len(s):
  if s[i]=="A":
    c+=1
  elif s[i:i+2]=="BC":
    n+=c
    i+=1
  else:
    c=0
  i+=1
print(n)

Submission status: Accepted

Solution code:
sl = list(input())
dl = []

n = len(sl)
i = 0
while i < n:

    if i < n-1 and sl[i] == "B" and sl[i+1] == "C":
        dl.append("D")
        i += 2
    else:
        dl.append(sl[i])
        i += 1

ans = 0
c = 0
for d in dl:
    if d == "A":
        c += 1
    elif d == "D":
        ans += c
    else:
        c = 0


print(ans)

Submission status: Accepted

Solution code:
s = list(input())
t = []
ls = len(s)
for i in range(ls):
    if s[i] == "A":
        t.append("A")
    elif s[i] == "B":
        if not i == ls - 1:
            if s[i + 1] == "C":
                t.append("D")
            else:
                t.append("B")
    else:
        if not i == 0:
            if not s[i - 1] == "B":
                t.append("C")
t = "".join(t).split("B")
for u in range(len(t)):
    t[u] = t[u].split("C")
ans = 0
for u in t:
    for v in u:
        dcnt = 0
        v = list(v)
        for i in range(len(v)):
            if v[i] == "D":
                ans += (i - dcnt)
                dcnt += 1
print(ans)

Submission status: Accepted

Solution code:
#AGC034-B ABC
"""
ABC→BCA
ABCBC→BCABC→BCBCA
AABC→ABCA→BCAA

"BC"の文字列は絶対に不変で、これが"A"を左から右にキャリーする役割を持つ。
それ以外の文字列、Bの後にCが無い状態もしくは、Cの前にBが無い時、キャリーが終わる。
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

s = readline().rstrip().decode('utf-8')
ans = 0 
a_ct = 0
bc_ct = 0
flag = 0
for i in range(len(s)-1):
    if flag:
        flag = 0
        continue
    if s[i] == "A":
        a_ct += 1
    elif s[i] == "B":
        if s[i+1] == "C":
            bc_ct += 1
            ans += a_ct
            flag = 1
        else:
            a_ct = 0
            bc_ct = 0
    else:
        a_ct = 0
        bc_ct = 0

print(ans)

Submission status: Accepted

Solution code:
# map(int, input().split())
# list(map(int, input().split()))
# for _ in range(int(input())):
# print(' '.join([str(x) for x in ans]))

s = input()

numBC = 0
total = 0
i = len(s)-1
while i >= 0:    
    if s[i] == 'A':
        total += numBC
    elif s[i] == 'C' and i > 0 and s[i-1] == 'B':
            numBC += 1
            i -= 1
    else:
        numBC = 0
    i-=1

print(total)

Submission status: Accepted

Solution code:
import sys
import re
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


# def main():
#     S = input()

#     ans = 0
#     res = re.findall(r'A*ABC(?:A|BC)*', S)
#     for r in res:
#         r = re.sub(r'(BC)', 'X', r)
#         cnt = 0
#         for c in r[::-1]:
#             if c == 'X':
#                 cnt += 1
#             else:
#                 ans += cnt

#     print(ans)


def main():
    S = input()

    S = re.sub(r'(BC)', 'X', S)

    ans = 0
    cnt = 0
    for s in S[::-1]:
        if s == 'X':
            cnt += 1
        elif s == 'A':
            ans += cnt
        else:
            cnt = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for i in range(len(l)-1,-1,-1):
    if l[i]!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Submission status: Accepted

Solution code:
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for c in l[::-1]:
    if c!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Submission status: Accepted

Solution code:
s = input()
if 'BC' not in s:
  print(0)
  import sys
  sys.exit()
t = s.split('BC')
m = 0
ans = 0
for l in t[:-1]:
  cnt = 0
  for i in range(len(l)-1,-1,-1):
    if l[i]!='A':
      ans += cnt
      m = cnt
      break
    cnt += 1
  else:
    m += cnt
    ans += m
print(ans)

Submission status: Accepted

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

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

print(ans)