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:
s = list(input())
ans = 0
ls = len(s)
i = 0
ca = 0
while i < ls-2:
    if s[i:i+3] == ['A', 'B', 'C']:
        ans += 1 + ca
        # print(i, ans, ca)
        s[i+2] = 'A'
        if ca >= 1:
            ca -= 2
            s[i+1] = 'A'
        else:
            ca = 0
        continue
    elif s[i] == 'A':
        ca += 1
    else:
        ca = 0
    i += 1

print(ans)

Submission status: Accepted

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


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

# ABCの数
# ABCの前にあるAの分だけ追加で操作できる
# ABCの後にある(BC)の分だけ追加で操作できる

# Aがあれば覚えておく
# ABCと来たら、それより前側に隣接しているAの回数だけ追加で操作できる
# 見つかったABCをBCAに置き換える
# BCAのAから走査を継続する

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

Solution code:
s = input()
count = 0

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

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

    length = len(s)

print(count)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(500000)
s=list(input())
ans=0
cA=1
L=[]
def dfs(v):#vはインデックス
    if 0<=v+1<=len(s)-1 and s[v+1]=='A':
        s[v+1]='#'
        global cA
        cA+=1
        dfs(v+1)
    elif 0<=v+2<=len(s)-1 and s[v+1]=='B' and s[v+2]=='C':
        global ans
        ans+=cA
        dfs(v+2)
    else:
        cA=1
        return

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

Submission status: Accepted

Solution code:
s = input()

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

cnt = 0

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

Submission status: Accepted

Solution code:
S = raw_input()

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

print count

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
s=input()


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

print(c)

Submission status: Accepted

Solution code:
s = input()

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

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

print(sum1)

Submission status: Accepted

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

ans = 0
keep = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

    print(c)
        


main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

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


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


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

# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

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

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

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

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


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


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


def read():
    return input()


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


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


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

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()
ans=0
a=[0 for i in range(len(s))]
bc=[0 for i in range(len(s))]
if len(s)<3:
  print(0)
else:
  for i in range(len(s)-2):
    if s[i]=="A":
      a[i]+=1
    if s[i+1]=="B" and s[i+2]=="C":
      a[i+2]+=a[i]
    if s[i+1]=="A":
      a[i+1]+=a[i]
      
  for i in range(len(s)-1):
    if s[i]=="B" and s[i+1]=="C":
      bc[i]=1
  #for i in range(len(s)-1):
   # if a[i+1]>0:
    #  a[i+1]+=a[i]
     # a[i]=0
  #for i in range(len(s)-2):
   # if a[i]>0 and bc[i+1]>0:
    #  a[i+2]=a[i]
  #for i in range(len(s)-2,-1,-1):
   # if bc[i-2]>0:
    #  bc[i-2]+=bc[i]
     # bc[i]=0
  #for i in range(len(s)-1):
   # if a[i+1]>0:
    #  a[i+1]+=a[i]
     # a[i]=0
  
  for i in range(len(s)-2):
    if a[i]>0 and bc[i+1]>0:
      ans+=a[i]*bc[i+1]
  print(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("A", "0")
    S = S.replace("BC", "1")
    a = 0
    b = 0
    for s in S:
        if s == "0":
            a += 1
        elif s == "1":
            ans += a
            b = 1
        else:
            a = 0
    print(ans)


main()

Submission status: Accepted

Solution code:
from collections import deque


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

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


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

Submission status: Accepted

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

import sys

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
flag = 0
ABCflag = 0

i = 0
while i < N-2:

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

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

    else:
        flag = 0

    i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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


path.reverse()

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

print(ans)

Submission status: Accepted

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

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())
S = S[::-1]

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import re

S = input()
ct = 0

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

Submission status: Accepted

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

def cal_subop(l):
    cum_a = [0 for i in range(len(l))] #index iより左のAの数
    tmp = 0
    for i in range(len(l)):
        tmp += (1 if l[i]=='A' else 0)
        cum_a[i] = tmp

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

cursor = 0
ans = 0
tmp  = []

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

ans += cal_subop(tmp)
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

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


s = ns()

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
s = list(input())
a,b = 0,0
bc = 0
ans = 0
for i in s:
    if i == "A":
        if b == 1:
            a, b = 1, 0
        else:
            a += 1
            a_now = 1
    elif i == "B":
        if b == 1 or a == 0:
            a, b = 0, 0
        else:
            b = 1
    else:
        if b == 1:
            ans += a
            b = 0
        else:
            a, b = 0, 0
print(ans)

Submission status: Accepted

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

ans = 0
acc_A = 0
for s in S:
    if s=='A':
        acc_A += 1
    elif s=='X':
        ans += acc_A
    else:
        acc_A = 0

print(ans)

Submission status: Accepted

Solution code:
s = str(input())
_s = []
s.count('BC')
p = 0
for i in range(len(s)-1):
    if p == 1:
        p = 0
    elif s[i:i+2]=='BC':
        _s+=['D']
        p = 1
    else:
        _s+=[s[i]]

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

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

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

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

def main():
  s=S()

  # c=0
  # while True:
  #   if s.count('ABC')>0:
  #     c+=s.count('ABC')
  #     s=s.replace('ABC','BCA')
  #   else:
  #     break

  # print(s)
  # return c

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

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

  return sm

print(main())

Submission status: Accepted

Solution code:
S = input()

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

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

Submission status: Accepted

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


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


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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()

Amemo = 0
result = 0
A = False
B = False
C = False
for c in s:
    if c == 'A':
        if B:
            Amemo = 0
        A = True
        B = False
        C = False
        Amemo += 1
    elif c == 'B':
        if A or C:
            A = False
            B = True
            C = False
        else:
            Amemo = 0
            A = False
            B = False
            C = False
    elif c == 'C':
        if B:
            result += Amemo
            A = False
            B = False
            C = True
        else:
            Amemo = 0
            A = False
            B = False
            C = False
print(result)

Submission status: Accepted

Solution code:
s = input()
K = []
for i, l in enumerate(s):
    if l == "A":
        K.append(1)
    elif i+1 < len(s) and l+s[i+1] == "BC":
        K.append(2)
    elif i > 0 and s[i-1]+l == "BC":
        continue
    else:
        K.append(0)
#print(K)
ans = 0
ct_1 = 0
for k in K:
    if k == 1:
        ct_1 += 1
    elif k == 2:
        ans += ct_1
    else:
        ct_1 = 0

print(ans)

Submission status: Accepted

Solution code:
S = input()
SS = S.replace('BC','X')
#SS
import re
SSS = re.split('[BC]', SS)
#SSS
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:
import numpy as np
from collections import defaultdict
from functools import reduce

s = input()
N = len(s)
old_idx = 0
res = 0

lis = []

i = 0
while i < N:
    if s[i] == 'A':
        cnt = 1
        while i + cnt < N and s[i+cnt] == 'A':
            cnt += 1
        lis.append((s[i], cnt))
        i += cnt
    else:
        lis.append((s[i], 1))
        i += 1

lisN = len(lis)

i = 0
while i < lisN-2:
    if lis[i][0] == 'A' and lis[i+1][0] == 'A':
        lis[i+1] = ('A', lis[i][1]+lis[i+1][1])
        # print(lis)
    if lis[i][0] == 'A' and lis[i+1][0] == 'B' and lis[i+2][0] == 'C':
        res += lis[i][1]
        lis[i+2] = ('A', lis[i][1])
        lis[i] = ('B', 1)
        lis[i+1] = ('C', 1)
        # print(lis)

    i += 1

print(res)

Submission status: Accepted

Solution code:
S=input()
lst=[]
i=0
while i<len(S):
    if S[i]=='A':lst+=[1]
    elif S[i]=='B' and i<len(S)-1 and S[i+1]=='C':lst+=[2];i+=1
    else:lst+=[0]
    i+=1
lll=[]
i=0
while i<len(lst):
    c=i
    while i<len(lst) and lst[c]==lst[i]:
        i+=1
    lll+=[[lst[c],i-c]]
r=0
i=0
while i<len(lll)-1:
    if lll[i][0]==1 and lll[i+1][0]==2:
        r+=lll[i][1]*lll[i+1][1]
        lll[i],lll[i+1]=lll[i+1],lll[i]
        if i<len(lll)-2 and lll[i+2][0]==1:
            lll[i+2][1]+=lll[i+1][1]
    i+=1
print(r)

Submission status: Accepted

Solution code:
S = list(input())
LI = [[]]
for i in range(len(S)):
    if S[i] == 'A':
        LI[-1].append(0)
    elif S[i] == 'B':
        if i != len(S)-1:
            if S[i+1] == 'C':
                LI[-1].append(1)
            else:
                LI.append([])
    elif S[i] == 'C':
        if i != 0:
            if S[i-1] != 'B':
                LI.append([])
ansl = []
for i in range(len(LI)):
    cnt = 0
    for j in range(len(LI[i])):
        if LI[i][j] == 0:
            ansl.append(0)
            cnt += 1
        else:
            ansl.append(cnt)
ans = sum(ansl)
print(ans)

Submission status: Accepted

Solution code:
s=input()
t=s.replace("BC","D")
"""print(t)"""
u=""
cnt=0
dcnt=0
for i in range(1,len(t)+1):
  num=(-1)*i
  """print("t",t[num])"""
  if t[num]=="B" or t[num]=="C":
    dcnt=0
  if t[num]=="D":
    dcnt+=1
  if t[num]=="A":
    cnt+=dcnt
  
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np
words = lambda t : list(map(t, input().split()))

str = input()
s = [c for c in str]
total = 0
i = 0
preva = 0
while i < len(s):
    #print("i:", i ,s[i:], preva)
    cnta = preva
    preva = 0
    cntbc = 0
    if s[i] == 'A':

        while i < len(s):
            #print(j, s[j])
            if s[i] == 'A':
                cnta += 1
                i+=1
            else:
                break
        while i < len(s) - 1:
            #print(k, s[k], s[k+1])
            if s[i] == 'B' and s[i+1] == 'C':
                cntbc += 1
                i += 2
            else:
                break
        if cnta > 0 and cntbc > 0:
            total += cnta * cntbc
            preva = cnta
        else:
            preva = 0
    else:
        i += 1
print(total)

Submission status: Accepted

Solution code:
s=list(input())
s_new=[]
ans=0
flag=False
for i in range(len(s)-1):
  if flag==True:
    flag=False
    continue
  if s[i]=="B" and s[i+1]=="C":
    s_new.append("P")
    flag=True
  else:
    s_new.append(s[i])
s_new="".join(s_new)
import re
s_new=re.split("[BC]",s_new)
s_new=[list(i) for i in s_new if i]
for i in range(len(s_new)):
  count=0
  for j in range(len(s_new[i])):
    if s_new[i][-j-1]=="P":
      count+=1
    else:
      ans+=count
print(ans)

Submission status: Accepted

Solution code:
import bisect
from operator import itemgetter
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
N = len(s)

cntA = 0
ans = 0
i = 0
while i < N:
  if s[i] == "A":
    cntA += 1
  elif s[i:i+2] == "BC":
    ans += cntA
    i += 1
  else:
    cntA = 0
  i += 1

print(ans)

Submission status: Accepted

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


def solve(S: str):
    tmp = []
    i = 0
    while i < len(S):
        j = i+1
        if S[i: i+3] == 'ABC':
            j = i
            while True:
                if S[j:j+3] == 'ABC':
                    j += 3
                elif S[j:j+2] == 'BC':
                    j += 2
                else:
                    break
            jl = i
            while jl >= 0:
                if S[jl] != 'A':
                    break
                jl -= 1
            jl += 1


            if len(tmp)>0 and tmp[-1][1] == jl:
                tmp[-1][0] += S[jl: j]
                tmp[-1][1] = j
            else:
                tmp.append([S[jl: j], j])

        i = j
    ans = 0
    for elem, _ in tmp:
        cnt_a = Counter(elem)['A']
        for i in range(len(elem)):
            if elem[i] == 'A':
                cnt_a -= 1
                ans += (len(elem)-1-i-cnt_a)//2
                assert (len(elem)-1-i-cnt_a)%2==0

    print(ans)
    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()
line = 0
Acount = 0
ans = 0
leng = len(S)
while line<leng-1:
    if S[line] == "A":
        Acount += 1
        line += 1
    elif S[line]== "B" and S[line+1]=="C":
        ans += Acount
        line += 2
    else:
        Acount = 0
        line += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()

aa = 0 #連続するAカウント
i = 0
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import math

def main():
    s = list(raw_input())
    substr = ''
    total = []
    cnt = 0
    m = False
    for i in s:
        if i == 'A' and m == False:
            cnt += 1
        elif i == 'A':
            cnt = 1
            m = False
        elif i == 'B' and m == False:
            m = True
        elif i == 'C' and m == True:
            total.append(cnt)
            m = False
        else:
            m = False
            cnt = 0
    result = 0
    for i in total:
        result += i
    print result

if __name__ == "__main__":
    main()