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: Wrong Answer

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
    i += 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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']
    print(ans)
resolve()

Submission status: Wrong Answer

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 != 'D' and d > 0:
                ans += a*d
                d = 0
            elif t == 'A':
                pass
            else:
                a = 0
        a += (t == 'A')
    if a > 0 and d > 0:
        ans += a*d
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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
      cnt = 1
    else:
      cnt = 0
      i += 1
print(ans)

Submission status: Wrong Answer

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 == 1:
            BCbox += 1
        elif x == -1:
            ans += BCbox
        else:
            BCbox = 0
    print(ans)



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
s = input()

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 (before_s == 'B') & (s[i]=='C'):
        ans +=a_cnt
        before_s = 'A'
    elif (before_s == 'A') & (s[i] == 'B'):
        before_s = s[i]
    else:
        a_cnt = 0
        before_s = s[i]

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

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

Submission status: Wrong Answer

Solution code:
S = list(input().strip())
N = len(S)
cnt = 0
for i in range(N-3+1):
    if S[i]=="A" and S[i+1]=="B" and S[i+2]=="C":
        cur = i
        S[i]="B"
        S[i+1]="C"
        S[i+2]="A"
        cnt += 1
        for j in range(cur-1,-1,-1):
            if S[j]=="A":
                cnt += 1
            else:
                break
print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
n_A = 0
i = 0
def process(ind):
    '''
    ABCを形成したら、[左端ind、右端ind、それ単体での操作回数]を返す
    aを発見したら渡す
    '''
    #n_AはABCのまえのAであり、ABCのAは含まない
    n_A = 0
    nex = ind + 1
    while True:
        if nex+1 == n:
            return []
        if s[nex] == 'A':
            n_A += 1
        elif not(s[nex] == 'B' and s[nex+1] == 'C'):
            return []
        else:
            break
        nex += 1
    nex = nex + 2
    # nex+1まででA..ABCを形成することは保証されてる
    # n_CはABCの後のCであり、ABCのCは含まない
    if n_A > 0:
        nex -= 1
    n_BC = 0
    next_is_B = True
    while True:
        if nex >= n:
            break
        if next_is_B and s[nex] == 'B':
            nex += 1
            next_is_B = False
        if s[nex] == 'C' and not next_is_B:
            n_BC += 1
            nex += 1
            next_is_B = True
        else:
            break
    return [ind,nex-1,1+n_A+n_BC]
i = 0
ABC_ls = []
ans = 0
while i < n:
    if s[i] == 'A':
        try:
            start, end, times = process(i)
            ABC_ls.append([start,end])
            ans += times
            i = end +1
        except:
            i += 1
    else:
        i += 1
start_previous = -2
end_previous = -2
on = 0
for start,end in ABC_ls:
    if end_previous +1 == start:
        on += 1
    else:
        if on > 0:
            ans += on - 1
    start_previous = start
    end_previous = end
if on > 0:
    ans += on
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import*
s=input()
n=len(s)
a=[0]
ai=[]
i=0
flag=0
while i<n:
  if flag==0 and s[i]=='A':
    flag=1
    ai+=[i]
    i+=1
    continue
  elif flag :
    if s[i]=='A':
      ai+=[i]
      i+=1
    elif s[i:i+2]!='BC':
      flag=0
      a+=[i]
      i+=2
    else:i+=2
  else:i+=1
if flag:a+=[n]
ans=0
for i,na in enumerate(a[1:]):
  ba=a[i]
  j=ai[bisect_left(ai,ba)]
  ca=s[j:na].count('A')
  for i,sa in enumerate(s[j:na]):
    if sa=='A':
      ans+=(na-(j+i)-ca)//2
      ca-=1
print(ans)

Submission status: Wrong Answer

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
for i in range(N-2):
    if "".join(S[i:i+3])=="ABC":
        ans+=1
        ans+=As
        BCs=0

        S[i]="B"
        S[i+1]="C"
        S[i+2]="A"
        if As>=1:
            As-=1
    elif S[i]=="A":
        As+=1
    else:
        As=0

print(ans)

Submission status: Wrong Answer

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
for i in range(N-2):
    if "".join(S[i:i+3])=="ABC":
        ans+=1
        ans+=As
        BCs=0

        S[i]="B"
        S[i+1]="C"
        S[i+2]="A"
    elif S[i]=="A":
        As+=1
    else:
        As=0

print(ans)

Submission status: Wrong Answer

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
for i in range(N-2):
    if "".join(S[i:i+3])=="ABC":
        ans+=1
        ans+=BCs
        ans+=As
        BCs=0
        As=0
        S[i]="B"
        S[i+1]="C"
        S[i+2]="A"
    elif S[i]=="A":
        As+=1
    else:
        As=0

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
str_list = []
for i in range(len(s)):
    str_list.append(s[i])


cnt = 0
continue_a = 0
for i in range(len(str_list) - 2):
    if str_list[i] == "A":
        continue_a += 1
    else:
        continue_a = 0
    if str_list[i] == "A" and str_list[i + 1] == "B" and str_list[i + 2] == "C":
        str_list[i] = "B"
        str_list[i + 1] = "C"
        str_list[i + 2] = "A"
        cnt += 1
        cnt += (continue_a - 1)
        # continue_a = 0
        # i += 1

# print(str_list)
print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
str_list = []
for i in range(len(s)):
    str_list.append(s[i])

cnt = 0
continue_a = 0
for i in range(len(str_list) - 2):
    if str_list[i] == "A":
        continue_a += 1
    else:
        continue_a = 0
    if str_list[i] == "A" and str_list[i + 1] == "B" and str_list[i + 2] == "C":
        str_list[i] = "B"
        str_list[i + 1] = "C"
        str_list[i + 2] = "A"
        cnt += 1
        cnt += continue_a - 1
        continue_a = 0
        i += 1

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
str_list = []
for i in range(len(s)):
    str_list.append(s[i])

# cnt = 0
# tf = True
# while tf == True:
#     tf = False
#     for i in range(len(str_list) - 2):
#         if str_list[i] == "A" and str_list[i + 1] == "B" and str_list[i + 2] == "C":
#             str_list[i] = "B"
#             str_list[i + 1] = "C"
#             str_list[i + 2] = "A"
#             cnt += 1
#             tf = True

cnt = 0
continue_a = 0
for i in range(len(str_list) - 2):
    if str_list[i] == "A":
        continue_a += 1
    else:
        continue_a = 0
    if str_list[i] == "A" and str_list[i + 1] == "B" and str_list[i + 2] == "C":
        str_list[i] = "B"
        str_list[i + 1] = "C"
        str_list[i + 2] = "A"
        cnt += 1
        cnt += continue_a - 1
        # print(continue_a-1,"!!!!")
        continue_a = 0
        # print(i)
        i += 1

# print(str_list)
print(cnt)

Submission status: Wrong Answer

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':
            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: Wrong Answer

Solution code:
#abcabc
#bcaabc
#bcabca
#bcbcaa

s=list(input())

cnt=0
kouho=[]

for i in range(1,len(s)-1):
    if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
        cnt+=1
        s[i-1]="B"
        s[i]="C"
        s[i+1]="A"
        kouho.append(i-1)
        kouho.append(i+2)
print(kouho)
while 1:
    flag=0
    while len(kouho)>0:
        i=kouho.pop()
        if 0<=i-1 and i+1<=len(s)-1:
            if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
                flag=1
                cnt+=1
                s[i-1]="B"
                s[i]="C"
                s[i+1]="A"
                kouho.append(i-1)
                kouho.append(i+2)
    #print(s)
    if flag==0:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
#abcabc
#bcaabc
#bcabca
#bcbcaa

s=list(input())

cnt=0

ll=1
rr=len(s)-1

while 1:
    flag=0
    if ll>=rr:
        break
    startll=ll
    startrr=rr

    for i in range(ll,min(rr,len(s)-1)):
        if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
            if ll==startll:
                ll=i-1

            flag=1
            cnt+=1
            s[i-1]="B"
            s[i]="C"
            s[i+1]="A"
            rr=i+3
    #print(s)
    if flag==0:
        break
print(cnt)

Submission status: Wrong Answer

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":
            lS[i + 2] = "A"
            ans += acnt
            acnt = 0

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000000)
s=list(input())

def saiki(i):
  global c
  if 0<=i <len(s)-2:
    if s[i]=="A" and s[i+1]=="B" and s[i+2]=="C":
      j=i
      while j>=0 and s[j]=="A":
        j-=1
        c+=1

      s[i+2]="A"
      i+=1
    saiki(i+1)
c=0
saiki(0)

print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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()
    latest_is_b = False
    for ch in S:
        if ch == 'B':
            latest_is_b = True
        else:
            if latest_is_b:
                if ch == 'C':
                    T.append('D')
                else:
                    T.extend(['B', ch])
            else:
                T.append(ch)
            latest_is_b = False
    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: Wrong Answer

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("ABC","D")
    S=S.replace("BC","E")
    S+="Z"
    N=len(S)
    ans=0
    cnta=0
    cntd=0
    cnte=0
    cnta2=0#末尾にaが何個つくか
    cnte2=0#先頭にaが何個つくか
    
    def calc(x):
        return ((1+x)*x)//2
    
    for i in range(N):
        if S[i]=="D":
            cntd+=1
            cnta2=0
        elif S[i]=="E":
            cnte+=1
            cnta2=0
            if cntd==0 and cnta==0:
                cnte2+=1
        elif S[i]=="A":
            cnta+=1
            cnta2+=1
        else:
            if cntd>0:
                ans+=cnte+cnta+calc(cntd)-cnta2-cnte2
            cntd=0
            cnta=0
            cnte=0
            cnta2=0
            cnte2=0
                
    print(ans)
            

main()

Submission status: Wrong Answer

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("ABC","D")
    S=S.replace("BC","E")
    S+="Z"
    N=len(S)
    ans=0
    cnta=0
    cntd=0
    cnte=0
    cnta2=0#末尾にaが何個つくか
    
    def calc(x):
        return ((1+x)*x)//2
    
    for i in range(N):
        if S[i]=="D":
            cntd+=1
            cnta2=0
        elif S[i]=="E":
            cnte+=1
            cnta2=0
        elif S[i]=="A":
            cnta+=1
            cnta2+=1
        else:
            if cntd>0:
                ans+=cnte+cnta+calc(cntd)-cnta2
            cntd=0
            cnta=0
            cnte=0
            cnta2=0
                
    print(ans)
            

main()

Submission status: Wrong Answer

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("ABC","D")
    S=S.replace("BC","E")
    S+="Z"
    N=len(S)
    ans=0
    cnta=0
    cntd=0
    cnte=0
    
    def calc(x):
        return ((1+x)*x)//2
    
    for i in range(N):
        if S[i]=="D":
            cntd+=1
        elif S[i]=="E":
            cnte+=1
        else:
            ans+=cnte+cnta+calc(cntd)
            cntd=0
            cnta=0
            cnte=0
            if S[i]=="A":
                cnta+=1
                
    print(ans)
            

main()

Submission status: Wrong Answer

Solution code:
s=list(input())
n=len(s)
amount=0
t=0
b=0
bc=0
for i in range(n):
    if s[i]=='A':
        t += 1
        b=0
    elif s[i]=='B':
        if b==1:
            t=0
        b=1
    else:
        if b==1:
            amount += t
            bc=1
            b=0
        else:
            t=0
print(amount)

Submission status: Wrong Answer

Solution code:
s=list(input())
n=len(s)
amount=0
t=0
b=0
for i in range(n):
    if s[i]=='A':
        t += 1
        b=0
    elif s[i]=='B':
        if b==1:
            t=0
        b=1
    else:
        if b==1:
            amount += t
            b=0
        else:
            t=0
print(amount)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    s = SS()

    # AがBCを乗り越えて右に進むイメージ
    # それぞれのAに対して右側に連続したBCがいくつあるか
    ans = 0
    len_s = len(s)
    for i in range(len_s):
        cnt = 0
        if s[i] == 'A':
            cnt += 1
        elif i + 1 < len_s and s[i] == 'B' and s[i+1] == 'C':
            ans += cnt
        else:
            cnt = 0
            
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
s=input()

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

print(ans)

Submission status: Wrong Answer

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    aAdd = 0
    bcAdd = 0
    j = 0
    isAbc = False
    while j <= (len(s) - 2):
        if s[j] == "A" and s[j - 1] == "A" and j > 0:
            aAdd = aAdd + 1
        elif isAbc == False:
            aAdd = 0
        else:
           pass

        if isAbc and s[j:(j+2)] == "BC":
            bcAdd = bcAdd + 1
            j = j + 2
            continue
        else:
            isAbc = False
            n = n + bcAdd * aAdd

        str = s[j:(j + 3)]
        if str == "ABC":
            aAdd = aAdd + 1
            isAbc = True
        j = j + 1

    if bcAdd != 0 and aAdd != 0:
        n = n + bcAdd * aAdd
    return n
print mul(s)

Submission status: Wrong Answer

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    aAdd = 0
    j = 0
    while j <= (len(s) - 2):
        if s[j] == "A" and s[j - 1] == "A" and j > 1:
            aAdd = aAdd + 1
        else:
            aAdd = 0

        str = s[j:(j + 3)]
        if str == "ABC":
            n = n + 1 + aAdd
            s = s[:j] + "BCA" + s[j + 3:]
        j = j + 1
    return n
print mul(s)

Submission status: Wrong Answer

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    _res = 0
    for i in xrange(len(s) - 1):
        j = i + _res
        if j < 0:
            j = i
        _res = 0
        str = s[j:(j + 3)]
        if str == "ABC":
            n = n + 1
            _res = -2
            s = s[:j] + "BCA" + s[j + 3:]
    return n
print mul(s)

Submission status: Wrong Answer

Solution code:
s = list(input())
# s = list('ABCABC')
# s = list('ABCACCBABCBCAABCB')

count = 0

if len(s) < 3:
	print(0)
ccc = 0
while count + 3 <= len(s):
	if s[count: count + 3] == ['A', 'B', 'C']:
		s[count: count + 3] = ['B', 'C', 'A']
		ccc += 1
		if count - 2 >= 0:
			count -= 2
	count += 1

print(ccc)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

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


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

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


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import re

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


cnt = 0

for i in range(1,len(new_s)):
    if new_s[i-1] == 'A' and  new_s[i] == 'D':
        cnt += 1
        new_s[i-1] = 'D'
        new_s[i] = 'A'
        i -= 2
    else:
        i += 1


print(cnt)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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
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:
        U+=1
        I+=2
    else:
        U=0
        I+=1

print(T)

Submission status: Wrong Answer

Solution code:
s = input()

match = ["ABC", "ABCBC", "AABC", "ABCBCAABC", "AABCABCBC"]

ans = 0
for x in match:
    ans += s.count(x)

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
ans = 0
r = len(s)
tmp = 0
ans = 0
a = 0
while tmp < len(s):
    if s[tmp:tmp + 3] != ["A", "B", "C"]:
        tmp += 1
        a = 0
    else:
        l = tmp - 1
        r = tmp + 3
        bc = 0
        abc = 1
        while s[l] == 'A' and l >= 0:
            a += 1
            l -= 1
        while r < len(s):
            if s[r:r + 2] == ['B','C']:
                bc += 1
                r += 2
            elif s[r:r + 3] == ['A', 'B', 'C']:
                abc += 1
                r += 3
            else:
                break
        ans += a + bc + ((abc + 1) * abc) // 2
        for i in range(abc):
            s[r-1-i] = 'A'
        tmp = r
print(ans)

Submission status: Wrong Answer

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())
    Sint = [1 if c == 'A' else 2 if c=='B' else 3 for c in S]
    N = len(S)

    ans = 0
    for i in range(N-2):
        if Sint[i] == 1 and Sint[i+1] == 2 and Sint[i+2] == 3:
            ans += 1
            Sint[i],Sint[i+1],Sint[i+2] = 2,3,1
            for j in range(i-1,-1,-1):
                if Sint[j] == 1:
                    ans += 1
                else:
                    break
    print(ans)



    
main()

Submission status: Wrong Answer

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

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

            a_cnt = 1
        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: Wrong Answer

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 += 2
                    idx += 3
                else:
                    break

            while len(s) - 2 >= idx:
                if [s[idx], s[idx + 1]] == next:
                    ans += 1
                    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()