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: Time Limit Exceeded

Solution code:
import re
S = str(input())

ans = 0
while True:
    m = re.findall('ABC', S)
    if(len(m)==0):
        break
    else:
        ans += len(m)
        S = re.sub('ABC', 'BCA', S)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0
while 'ABC' in s:
    s = s.replace('ABC', 'BCA', 1)
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
while "ABC" in s:
	ans += s.count("ABC")
	s = s.replace("ABC", "BCA")
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
while True:
  ss = str(s)
  s = s.replace('ABC', 'BCA', 1)
  if ss == s:
    break
  cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
list = list(S)
cnt=0
while 'ABC' in S:
    n=S.index('ABC')
    list[n] = 'B'
    list[n+1] = 'C'
    list[n+2] = 'A'
    S=''.join(list)
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
s = input()
ans = 0
tmp = 0
i = 0
while i < len(s)-1:
    if s[i] == "A":
        tmp += 1
    elif s[i] == "B":
        if s[i+1] == "C":
            ans += tmp
            i += 1
        else:
            tmp = 0
    else:
        tmp = 0
    i += 1
print(ans)

Submission status: Accepted

Solution code:
S = input()

cnt = 0
ans = 0
n = 0
len_S = len(S)

while n < len_S:
  if S[n]== "A":
    cnt += 1
  elif S[n] == "B":
    if n < len_S-1:
      if S[n+1] == "C":
        ans += cnt
        n += 1
      else:
        cnt = 0
  else:
    cnt = 0
  n += 1
print(ans)

Submission status: Accepted

Solution code:

def resolve():
    S = input()

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

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import sys, math, re
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

s = input()
s = s.replace('BC', 'D')
matches = re.findall(r'[AD]+', s)
ans = 0
for w in matches:
    n = len(w)
    p = n-1
    for i in range(n-1, -1, -1):
        if w[i] == 'A':
            ans += p-i
            p -= 1

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


s = input()


# In[6]:


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)


# In[ ]:

Submission status: Accepted

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

for i in s:
    if i == 'A':
        A_count += 1
    elif i == 'B' or i == 'C':
        A_count = 0
    else:
        ans += A_count
print(ans)

Submission status: Accepted

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    ans = 0
    stack = []
    for c in input():
        if len(stack) >= 2 and stack[-2:] == ["A", "B"] and c == 'C':
            ans += len(stack) - 1
            stack.pop()
        elif len(stack) >= 1 and stack[-1] == 'A' and c in ('A', 'B'):
            stack.append(c)
        elif c == 'A':
            stack = ['A']
        else:
            stack = []
    print(ans)
resolve()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    s = input()
    s = s.replace('BC', 'D')
    a = 0; d = 0
    ans = 0
    for t in s:
        if a > 0:
            if t == 'D':
                d += 1
            elif t == 'A':
                if d > 0:
                    ans += a*d
                    d = 0
            elif t != 'D' and d > 0:
                ans += a*d
                a = 0
                d = 0
            else:
                a = 0
                d = 0
        a += (t == 'A')
    if a > 0 and d > 0:
        ans += a*d
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))

######################################################

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

Submission status: Accepted

Solution code:
s = input()
d = []
i = 0
while i < len(s)-1:
    if s[i] == 'B' and s[i+1] == 'C':
        d.append('D')
        i += 2
    else:
        d.append(s[i])
        i += 1
ans = 0
x = 0
for i in range(len(d)):
    if d[i] == 'A':
        x += 1
    elif d[i] == 'D':
        ans += x
    else:
        x = 0
print(ans)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	#n = int(input())
	#b , c = tin()
	#s = input()
	s=input()
	pre='C'
	ret = 0
	a_num=0
	for c in s:
		d = pre + c
		if d == 'AA':
			a_num += 1
		elif d == 'BA':
			a_num=1
		elif d == 'CA':
			a_num += 1
		elif d == 'AB':
			pass
		elif d== 'BB':
			a_num = 0
		elif d == 'CB':
			pass
		elif d == 'AC':
			a_num = 0
		elif d == 'BC':
			ret += a_num
		else:
			a_num = 0
		
		pre = c
	return ret
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
S = input()

A = []

ok = 1
ans = 0
cnt = 0
for i in range(len(S)):
    if ok == 1 and i < len(S)-1 and S[i] == 'B' and S[i+1] == 'C':
        A.append('B')
        ans += cnt
    elif S[i] == 'A':
        A.append('A')
        cnt += 1
        ok = 1
    elif ok == 1 and i > 0 and S[i-1] == 'B' and S[i] == 'C':
        continue
    else:
        A = []
        cnt = 0
        ok = 0

print(ans)

Submission status: Accepted

Solution code:
s = str(input())

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

Submission status: Accepted

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

s = input()
n = len(s)
ans = 0
p = 0
cnt_a = 0
while True:
    while p<n and s[p] == 'A':
        cnt_a += 1
        p += 1
    
    while p<n-1 and s[p] == 'B' and s[p+1] == 'C':
        ans += cnt_a
        p+=2
    while p<n and s[p] == 'C':
        p+=1
        cnt_a = 0
    while p<n and s[p] == 'B':
        p+=1
        cnt_a=0
    
    if p>=n:
        break
print(ans)

Submission status: Accepted

Solution code:
a = list(input())
ans = 0
i = 0

a_count = 0  # 連続ででたAの個数
# BC_count = 0  # BC出てきたらそれまでのAの数分

while i < len(a)-1:
    if a[i] == "A":
        count = "A"
        a_count += 1
    elif a[i]+a[i+1] == "BC":
        count = "BC"
        ans += a_count
        i += 1
    else:
        count = "VBNM"
        a_count = 0
    # print(a_count, ans, i, count)
    i += 1

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def run():
    S = input()
    SS = []
    dic = {'A': -1, 'B' : -2, 'C' : -3}
    chain = False
    skip = False
    for i in range(len(S)):
        if not chain:
            if S[i:i+2] == 'BC':
                chain = True
                skip = True
                SS.append(1)
            else:
                SS.append(dic[S[i]])
        else:
            if skip:
                skip = False
                continue
            else:
                if S[i:i+2] == 'BC':
                    skip = True
                    SS[-1] += 1
                else:
                    SS.append(dic[S[i]])
                    chain = skip = False
    #print(SS)
    ans = 0
    BCbox = 0
    for x in SS[::-1]:
        if x > 0:
            BCbox += x
        elif x == -1:
            ans += BCbox
        else:
            BCbox = 0
    print(ans)



if __name__ == "__main__":
    run()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = list(input())[::-1]+["L"]
n = len(s)

ans = 0
count = 0
now = 0
while now < n-1:
    if s[now] == "C" and s[now+1] == "B":
        count += 1
        now += 2
    elif s[now] == "A":
        if count > 0:
            ans += count
        else:
            count = 0
        now += 1
    else:
        count = 0
        now += 1
print(ans)

Submission status: Accepted

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	s=RD()[::-1]+"DDDDD"

	i=0
	cnt=0
	ans=0

	#print(s)

	while i<=len(s)-3:
		if s[i:i+2]=="CB":
			cnt+=1
			i+=2
		elif s[i]=="A":
			ans+=cnt
			i+=1
		else:
			cnt=0
			i+=1
	print(ans)





if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = input()
n = len(s)
ind = n-1
cnt = 0
res = 0
f = False
while ind >= 0:
    if ind-1 >= 0 and (s[ind-1],s[ind]) == ('B','C'):
        f = True
        cnt += 1
        ind -= 2
    elif f and s[ind] == 'A':
        res += cnt
        ind -= 1
    else:
        f = False
        cnt = 0
        ind -= 1
print(res)

Submission status: Accepted

Solution code:
s = input().replace('BC', 'D')
s = s.replace('B', 'C')
l = [x for x in s.split(sep='C') if x != '']
ans = 0
for subs in l:
    numA = 0
    for c in subs:
        if c == 'A':
            numA += 1
        elif c == 'D':
            ans += numA

print(ans)

Submission status: Accepted

Solution code:
s = input()
s = s.replace('BC','D')
a_cnt = 0
before_s = 'A'
ans = 0
for i in range(len(s)):

    if s[i] == 'A':
        a_cnt += 1
        before_s = s[i]
    elif (s[i]=='D'):
        ans +=a_cnt
        before_s = 'A'
    else:
        a_cnt = 0
        before_s = s[i]

print(ans)

Submission status: Accepted

Solution code:
s = input()

tmp = ""
cnt = 0
ans = 0
flag = False  # skip用
for i in range(0, len(s) - 1):
    if flag:
        flag = False
        continue
    if s[i] == "A":
        cnt += 1
    elif s[i] + s[i + 1] == "BC":
        ans += cnt
        flag = True
    else:
        cnt = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class FenwickTree:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        self.k_init = 2 ** (self.n - 1).bit_length()

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.n:
            self.data[i] += x
            i += i & -i


def get_invnum(li):
    n = len(li)
    t = FenwickTree(2)
    res = 0
    for i in range(n):
        if li[i] == "A":
            t.add(2, 1)
        else:
            res += t.sum(2)
    return res


S = input()
S = list(S.replace("BC", "D"))
N = len(S)
li = []
ans = 0
for c in S + ["B"]:
    if c not in ("A", "D"):
        ans += get_invnum(li)
        li = []
    else:
        li.append(c)
print(ans)

Submission status: Accepted

Solution code:
s = input()

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

Submission status: Accepted

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

a_flag = False
ans = 0
last_a = 0
for si in s:
    if si == 'A':
        a_flag = True
        last_a += 1
    elif si == 'x' and a_flag:
        ans += last_a
    else:
        a_flag = False
        last_a = 0

print(ans)

Submission status: Accepted

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

a_flag = False
ans = 0
last_a = 0
for si in s:
    if si == 'A':
        a_flag = True
        last_a += 1
    elif si == 'x' and a_flag:
        # ans += 1
        ans += last_a
    else:
        a_flag = False
        last_a = 0

print(ans)


# while 
# cnt = s.count('ABC')
# s = s.replace('ABC','BCA')
# cnt += s.count('ABC')
# print(cnt)

# ab_flag = False
# a_flag = False
# a_cnt = 0
# ans = 0
# for si in s:
#     if si == 'A': 
#         a_cnt += 1
#         ab_flag = False
#     elif si == 'B':
#         if a_flag: 
#             ab_flag = True
#         else: 
#             a_flag = False
#             ab_flag = False
#     elif si == 'C':
#         if ab_flag:
#             ab_flag = False
#             a_flag = True
#             ans += 1
#         else:
#             ab_flag = False
#             a_flag = False
#     else:
#         ab_flag = False
#         a_flag = False
# print(ans)

Submission status: Accepted

Solution code:
s = str(input())
s = s.replace('BC', 'D')
#print(s)
S = []
temp = []
for c in s:
    if c == 'A' or c == 'D':
        temp.append(c)
    else:
        if temp:
            S.append(temp)
            temp = []
else:
    if temp:
        S.append(temp)
#print(S)
ans = 0
for s in S:
    cnt = 0
    for j in range(len(s)):
        if s[j] == 'A':
            cnt += 1
        else:
            ans += cnt
print(ans)

Submission status: Accepted

Solution code:
s = input()
s = s.replace("BC", "X")
#"AX" -> "XA"

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

Submission status: Accepted

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

Submission status: Accepted

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

print(sum(ans))

Submission status: Accepted

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

s = input()[:-1]
A = 0
ans = 0
i = 0

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

print(ans)

Submission status: Accepted

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

ans, cnt = 0, 0
for s in news:
    if s == 'A':
        cnt += 1
    elif s == 'D':
        ans += cnt
    else:
        cnt = 0

print(ans)

Submission status: Accepted

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

def count_inv(s):
  res = num = 0
  for si in s:
    if si == 'A':
      num += 1
    else:
      res += num
  return res


for s in S:
  ans += count_inv(s)
  
print(ans)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
ans = num_a = 0

pre_is_a = True
i = 0
while i < N:
    s = S[i]
    if s == 'A':
        pre_is_a = True
        num_a += 1
    elif s == 'B' and pre_is_a:
        num_bc = 0
        flag = True
        for j in range(i, N-1, 2):
            if S[j:j+2] == 'BC':
                num_bc += 1
            else:
                flag = False
                break
        if num_bc:
            ans += num_a * num_bc
            if flag:
                i = N
            else:
                i = j - 1
        else:
            pre_a = False
            num_a = 0
    else:
        pre_a = False
        num_a = 0

    i += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# move all A's to the back whenever you see ABC i.e it becomes BCA
# Example ABCBCBC after operation it becomes BCBCBCA
# Count the consecutive no of BC and whenever u see a A add the count to answer that many moves required to move A to last
# if it's not A or any other string make cnt as 0
def main():
	s = input()
	cnt = 0
	prev =0
	ans = 0
	i = len(s)-1
	while i>=0:
		if s[i]=='C' and s[i-1]=='B':
			cnt = cnt+1
			i = i-2
		else:
			if s[i]=='A':
				ans = ans+cnt
			else:
				cnt = 0
			i = i-1
	print(ans)
if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:

def main():
	s = input()
	cnt = 0
	prev =0
	ans = 0
	i = len(s)-1
	while i>=0:
		if s[i]=='C' and s[i-1]=='B':
			cnt = cnt+1
			i = i-2
		else:
			if s[i]=='A':
				ans = ans+cnt
			else:
				cnt = 0
			i = i-1
	print(ans)
if __name__=="__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=input()
#ABCBCBC
N=len(S)
if N<3:
    print(0)
    exit()
S=list(S)
#abc前にaが続く数
As=0
#abcごにbc続く数
BCs=0
ans=0
flag=False
safe=-1
for i in range(N-2):
    if "".join(S[i:i+3])=="ABC":
        ans+=1
        ans+=As


        S[i]="B"
        S[i+1]="C"
        S[i+2]="A"
        safe=i+1

    elif S[i]=="A":
        As+=1
    else:
        if i!=safe:
            As=0
    #print(As)

print(ans)

Submission status: Accepted

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

ca = 0

T = []

tt = ""
for i in range(len(S)):
    if S[i] == 'A':
        tt += S[i]

    elif S[i] == 'B' or S[i] == 'C':
        # clear
        if tt != "":
            T.append(tt)
            tt = ""
    
    elif S[i] == 'D':
        tt += S[i]

if tt != "":
    T.append(tt)
    tt = ""

# solve
ans = 0
for seq in T:
    ca = 0
    for c in seq:
        if c == 'A':
            ca += 1
        else:
            ans += ca

print(ans)

Submission status: Accepted

Solution code:
s = input()
# ABC -> BCA
# "BC"を纏めて考える
s = s.replace('BC', 'D')
# AとDだけからなる部分列において、各Dについて、左にあるAの数を数える
# 部分列は最終的に D...DA...Aとなる
ans = 0
cntA = 0
for i in range(len(s)):
    if s[i] == 'A':
        cntA += 1
    elif s[i] == 'D':
        ans += cntA
    # AとD以外(= B or C)の場合は一旦途切れる
    else:
        cntA = 0

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

s = input()
s = s.replace('BC', 'D')
s_list = s.split("B")
x = []
for e in s_list:
    x += e.split("C")

ans = 0
for e in x:
    cnt_d = 0
    now = 0
    for c in e[::-1]:
        if c == 'D':
            cnt_d += 1
        if c == 'A':
            now += cnt_d
    ans += now
print(ans)

Submission status: Accepted

Solution code:
def agc034_b():
    s = str(input())
    n = len(s)
    if n < 3: return print(0)

    r = s[::-1]
    cb = 0
    ans = 0
    for i in range(1, n):
        if r[i] == 'A':
            if r[i-1] == 'C': cb = 0
            else: ans += cb
        elif r[i] == 'B':
            if r[i-1] == 'C': cb += 1
            else: cb = 0
        else:
            if r[i-1] == 'C': cb = 0
            else: continue
    print(ans)

if __name__ == '__main__':
    agc034_b()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    lS = list(S)
    acnt = 0
    ans = 0
    for i in range(len(S) - 2):
        if lS[i] == "A":
            acnt += 1
        else:
            acnt = 0

        if lS[i] == "A" and lS[i + 1] == "B" and lS[i + 2] == "C":
            ans += acnt
            lS[i + 2] = "A"
            if acnt > 1:
                lS[i + 1] = "A"
                acnt -= 2

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    # BC -> D に変換
    #     ABCACCBABCBCAABCB -> ADACCBADDAADB
    # 後ろから順にみてDからスタートしてB/Cが出てきたら区切る
    #     AD (ACCB) ADDAAD (B)
    # 各セクションでAの右側にあるDの数を足し合わせる
    #     1 + 3 + 1 + 1 = 6
    T = list()
    for ch in S:
        if len(T) > 0 and T[-1] == 'B' and ch == 'C':
            T[-1] = 'D'
            continue
        T.append(ch)
    T.reverse()
    ans, cnt_d = 0, 0
    for ch in T:
        if ch == 'A':
            ans += cnt_d
        elif ch in ['B', 'C']:
            cnt_d = 0
        else:  # ch == 'D'
            cnt_d += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
s=s.replace('BC','.')
s=s.replace('B','/')
s=s.replace('C','/')
sl = s.split('/')
cnt =0
for si in sl:
    incre = 0
    for ci in si:
        if ci == 'A':
            incre +=1
        if ci == '.':
            cnt += incre
print(cnt)

Submission status: Accepted

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    S=input()
    S=S.replace("BC","D")
    S+="Z"
    N=len(S)
    ans=0
    cnta=0

    
    for i in range(N):
        if S[i]=="D":
            ans+=cnta
        elif S[i]=="A":
            cnta+=1
        else:
            cnta=0
                
    print(ans)
            

main()

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
S=[]
t=0
while t<n-1:
    if s[t]=='B' and s[t+1]=='C':
        S.append('R')
        t+=1
    else:
        S.append(s[t])
    t+=1
if t==n-1:
    S.append(s[t])
m=len(S)
amount=0
bc=0
a=0
for i in range(m):
    if S[i]=='A':
        a += 1
    elif S[i]=='R':
        amount += a
        bc=1
    else:
        a=0
        bc=0
print(amount)

Submission status: Accepted

Solution code:
s = input()
a = 0
ans = 0
n = len(s)
i = 0
while(i<n):
  if s[i] == "A":
    a += 1
  elif s[i] == "B" and i+1<n and s[i+1] == "C":
    ans += a
    i += 1
  else:
    a = 0
  i += 1
print(ans)

Submission status: Accepted

Solution code:

        
def main():
    import sys

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

    s = input()
    s = s.replace('BC', 'D')
    n = len(s)
    i = 0
    ans = 0
    while i < n:
        while i < n and (s[i] == 'B' or s[i]=='C'):
            i += 1
        st = i
        cnt = 0
        while i < n and (s[i] == 'A' or s[i]=='D'):
            if s[i] == 'A':
                cnt += 1
            i += 1
        ed = i
        for j in range(st, ed):
            if s[j] == 'A':
                cnt -= 1
                ans +=ed-j-cnt-1
    print(ans)

    
    


                
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = list(input())
n = len(s)
ans = 0
c = 0
i = 0
while i < n-1:
    if s[i] == "B" and s[i+1] == "C":
        ans += c
        i += 1
    elif s[i] == "A":
        c += 1
    else:
        c = 0
    i += 1
print(ans)

Submission status: Accepted

Solution code:

s = input()
i, ans, total = 0, 0, 0

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

print(ans)