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:
import re

s=input()

#print(re.split('BB|CC|CB',s))

#print(s)

s=s.replace("A","0")
s=s.replace("BC","1")
s=re.split('B|C|CB',s)
print(s)

cnt=0
for i in s:
    
    start=0
    start=i.find("0")
    while start!=-1:
        
        
        if start<=len(i):
            cnt+=i[start:].count("1")
           
        start=i.find("0",start+1)
        
print(cnt)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder B
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    S = list(input()) # abc -> ['a','b','c']
    out=0
    a=0
    b=False
    for s in S:
        if s=="A":
            a+=1
            b=False
        elif s=="B":
            if b:
                b=False
                a=0
            else:
                b=True
        else:
            if b:
                out+=a
            else:
                a=0
            b=False
    print(out)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)

count = 0
for l in S_li:
    a = l.find('0')
    if a<0:
        break
    bc = l.rfind('1')
    if a < bc:
        count += (bc-a)
        while True:
            i=a+1
            j=bc
            a = l[i:j].find('0')
            if a<0:
                break
            bc = l[i:j].rfind('1')
            if a < bc:
                count += (bc-a)
            else:
                break
    else:
        break

print(str(count))

Submission status: Wrong Answer

Solution code:
import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)

count = 0
for l in S_li:
    a = l.find('0')
    if a<0:
        break
    bc = l.rfind('1')
    if a < bc:
        count += (bc-a)
        while True:
            i=a+1
            j=bc
            a = l[i:j].find('0')
            if a<0:
                break
            bc = l[i:j].rfind('1')
            if a < bc:
                count += (bc-a)
            else:
                break

print(str(count))

Submission status: Wrong Answer

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

a_series = 0

for i in range(len(s)):
  #print(i, abc)
  if s[i] == 'A':
    abc = 1
    if abc != 1:
      a_series = 0
    a_series += 1
  elif s[i] == 'B':
    if abc == 1:
      abc = 2
    else:
      abc = 0
      a_series = 0
  elif s[i] == 'C':
    if abc == 2:
      abc = 1
      count += a_series
    else:
      abc = 0
      a_series = 0
      
print(count)

Submission status: Wrong Answer

Solution code:
s = str(input())
cnt = 0
while "ABC" in s:
    for i in range(len(s)-2):
        if s[i:i+3] == "ABC":
            a_cnt = 0
            bc_cnt = 0
            j = i-1
            while j >= 0 and s[j] == "A":
                a_cnt += 1
                j -= 1
            k = i + 3
            while k <= len(s)-2 and s[k:k+2] == "BC":
                bc_cnt += 1
                k += 2
            if a_cnt > 0 and bc_cnt > 0:
                cnt += (a_cnt + bc_cnt)*2
                s = s[:max(0,j+1)] + "BC"*bc_cnt + "A"*a_cnt + s[min(k,len(s)-2):]
            else:
                cnt += 1 + a_cnt + bc_cnt
                if i == 0:
                    s = "BC"*bc_cnt +"BCA" + "A"*a_cnt + s[min(k,len(s)-2):]
                elif i >= len(s)-3:
                    s = s[:max(1,j+1)] + "BC"*bc_cnt +"BCA" + "A"*a_cnt
                else:
                    s = s[:max(1,j+1)] + "BC"*bc_cnt + "A"*a_cnt + s[min(k,len(s)-2):]
            
            break
print(cnt)

Submission status: Wrong Answer

Solution code:
S=input()
count=0
for i in range(2,len(S)-1):
    if S[i-2:i+1]=="ABC":
        count_A=1
        count_B=1
        k=i+2
        while S[k-1:k+1]=="BC" and k<=len(S)-1:
            k=k+2
            if S[k-1:k+1]=="BC":
                count_B+=1
        j=i-2
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A+count_B-1
        S=S[0:i-count_A-1]+"BC"*count_B+"A"*count_A+S[i+count_B*2-1:len(S)]
i=len(S)-1
if S[i-2:i+1]=="ABC":
        count_A=1
        j=i-2
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A
print(count)

Submission status: Wrong Answer

Solution code:
S = list(input())
c = 0
# flag = True
ABC = []
for i in range(len(S) - 2):
    if (S[i] == 'A') and (S[i+1] == 'B') and (S[i+2] == 'C'):
        ABC.append([i, i+1, i+2])
        c += 1
        try:
            if ABC[-2] == [i-3, i-2, i-1]:
                c += 1
        except:
            continue

for A, B, C in ABC:
    try:
        if S[A-1] == 'A':
            c += 1
    except:
        continue
    
    try:
        if (S[C+1] == 'B') and (S[C+2] == 'C'):
            c += 1
    except:
        continue

for i in range(len(ABC) - 1):
    try:
        if ABC[i] == [ABC[i+1][0] - 6, ABC[i+1][1] - 6, ABC[i+1][2] - 6]:
            if (S[ABC[i][2] + 1] == 'B') and (S[ABC[i][2] + 2] == 'C') and (S[ABC[i+1][0]] == 'A'):
                c += 1
    except:
        continue
    
print(c)

Submission status: Wrong Answer

Solution code:

a = input()+"f"
s  =0
A = 0
F = 0

BC = 0
ff = 0
res = 0
for i , j , k in zip(a,a[1:],a[2:]):
    # print(i,j,k)
    if F == 0 and ff ==0:
        # print("A ",i,j,k)
        if i+j+k =="ABC":
            res += A+1
            A +=1
            F = 1
            ff +=2
        elif i == "A" and j == "A" :A+=1
        else: A = 0
    elif ff == 0 and F!=0:
        if i+j+k =="ABC":
            res += 1+BC+A
            A+=1
            F += 1
            ff +=2
        elif i + j == "BC" :

            res +=A
            A +=1
            ff+=1
        elif  i == "A" and j == "A" :
            A+=1+BC
            BC = 0
            F = 0
        else:
            A =0
            F = 0
            BC = 0
    else:
        if ff>0:ff-=1
        # print("C ",i,j,k)

print(res)

Submission status: Wrong Answer

Solution code:
def main(s):
    n = len(s)
    cum = [0] * (n+1)
    sr = s[::-1]
    for i in range(n-1):
        if sr[i] == 'C' and sr[i+1] == 'B':
            cum[i+1] = cum[i] + 1
        elif (0 < i and sr[i-1:i+2] == 'BCB'):
            cum[i+1] = 0
        elif sr[i] == 'A' or sr[i:i+2] in ['BC'] or (0 < i and sr[i-1:i+2] == 'CBA'):
            cum[i+1] = cum[i]
        else:
            cum[i+1] = 0
    cum[n] = cum[n-1]
    ans = 0
    for i in range(n):
        if s[i] != 'A':
            continue
        ans += cum[n-i-1]

    print(ans)
    # ans1 = ans
    # flag = True
    # s = [i for i in s]
    # ans = 0
    # while flag:
    #     flag = False
    #     for i in range(n-2):
    #         if s[i:i+3] == ['A', 'B', 'C']:
    #             s[i] = 'B'
    #             s[i+1] = 'C'
    #             s[i+2] = 'A'
    #             flag = True
    #             ans += 1
    #
    # print(ans)
    # return ans, ans1

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder B
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    S = list(input()) # abc -> ['a','b','c']
    out=0
    a=0
    b=False
    while S:
        print(S,a,out)
        s=S.pop(0)

        if s=="A":
            a+=1
            b=False
        elif s=="B":
            if b:
                b=False
                a=0
            else:
                b=True
        else:
            if b:
                out+=a
                # for _ in range(a):
                #     S.insert(0, "A")
            else:
                a=0
            # a=0
            b=False
                

        # if S[0]=="A":
        #     a+=1
        #     del S[0]
        # elif S[0:2]==["B","C"]:
        #     del S[0:2]
        #     out+=a

        #     for _ in range(a):
        #         S.insert(0, "A")
        #     a=0
        # else:
        #     del S[0]
        #     a=0
    print(out)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main(s):
    n = len(s)
    cum = [0] * (n+1)
    sr = s[::-1]
    for i in range(n-1):
        if sr[i] == 'C' and sr[i+1] == 'B':
            cum[i+1] = cum[i] + 1
        elif (0 < i and sr[i-1:i+2] == 'BCB'):
            cum[i+1] == 0
        elif sr[i] == 'A' or sr[i:i+2] in ['BC'] or (0 < i and sr[i-1:i+2] == 'CBA'):
            cum[i+1] = cum[i]
        else:
            cum[i+1] = 0
    cum[n] = cum[n-1]
    ans = 0
    for i in range(n):
        if s[i] != 'A':
            continue
        ans += cum[n-i-1]

    print(ans)
    # ans1 = ans
    # flag = True
    # s = [i for i in s]
    # ans = 0
    # while flag:
    #     flag = False
    #     for i in range(n-2):
    #         if s[i:i+3] == ['A', 'B', 'C']:
    #             s[i] = 'B'
    #             s[i+1] = 'C'
    #             s[i+2] = 'A'
    #             flag = True
    #             ans += 1
    # 
    # print(ans)
    # return ans, ans1

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

Submission status: Wrong Answer

Solution code:
import re
s = input()
ptns = re.findall(r'A*ABC(?:A|ABC|BC)+', s)
c = 0
for ptn in ptns:
    ais  = [i for i in range(len(ptn)) if ptn[i] == "A"]
    for j, ai in enumerate(reversed(ais)):
        c += (len(ptn) -1 - ai - j) // 2
print(c)

Submission status: Wrong Answer

Solution code:
s=list(input())

i=0
j=0
ans=0
z=[]
while i!=len(s)-2:
    try:
        if s[i]=="A" and s[i+1]=="B" and s[i+2]=="C":
           z.append(1)
           i=i+3

        elif s[i]=="B" and s[i+1]=="C":
            z.append(2)
            i=i+2

        else:
            if s[i]=="A":
                z.append(3)
                i=i+1

            else:
                z.append(0)
                i=i+1

    except:
       break

ans=0
i=0
r1=0
r2=0
r3=0
print(z)

while i!=len(z):
    if z[i]==1:


        r1=r1+1
        ans = ans + r1
        ans=ans+r3
        if r2!=0:
            r3=0
        r2=0
    elif z[i]==2:
        if r1>=1:
            r2=r2
            ans=ans+r1+r3


    elif z[i]==3:
        if r2!=0:
            r3=0
        r3=r3+1+r2
        if r1!=0:
            r1=1
        else:
            r1=0
        r2=0

    else:
        r1 = 0
        r2 = 0
        r3 = 0
    i=i+1
    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
pre_chance = 0
pos_chance = 0

for i in range(len(s)-1):
    if s[i] == "A":
        if s[i:i+2] == "AA":
            pre_chance += 1
            pos_chance = 0
        elif s[i:i+3] == "ABC":
            ans += pre_chance + 1
            pre_chance = 0
        else:
            pre_chance = 0
            pos_chance = 0
    elif s[i] == "B":
        if s[i:i+2] == "BC" and pos_chance == 1:
            ans += 1
            pre_chance = 0
        else:
            pre_chance = 0
            pos_chance = 0
    elif s[i] == "C":
        if s[i-1:i+2] == "BCA" and pos_chance == 1:
            pre_chance += 1
            pos_chance = 1
        elif s[i-1:i+1] == "BC" and pos_chance == 1:
            pos_chance = 1
        elif s[i-2:i+2] == "ABCB":
            pos_chance = 1
        else:
            pre_chance = 0
            pos_chance = 0

print(ans)

Submission status: Wrong Answer

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time

#N, A, B, C, D = [ int(x) for x in input().split() ]

S = input()

ans = 0

pointer = 0
while True:
    abc_p = S[pointer:].find("ABC")
    if abc_p == -1:
        break
    abc_p = pointer + abc_p
    left_a_num = 0
    _s = S[abc_p:pointer - 1:-1] if pointer >= 1 else S[abc_p::-1]
    for s in _s:
        if s == 'A':
            left_a_num += 1
        else:
            break
    left_a_num -= 1
    pointer = abc_p + 3
    ABC_cnt = 1
    BC_cnt = 0
    BC_incr = 0
    A_cnt = 0
    while True:
        if pointer + 3 <= len(S) and S[pointer:pointer + 3] == 'ABC':
            ABC_cnt += 1
            pointer += 3
            if A_cnt:
                ans += A_cnt
                ans += BC_incr
        elif pointer + 2 <= len(S) and S[pointer:pointer + 2] == 'BC':
            BC_cnt += 1
            BC_incr += ABC_cnt
            pointer += 2
        elif pointer + 1 < len(S) and S[pointer + 1] == 'A':
            A_cnt += 1
            pointer += 1
        else:
            break
    ans += BC_incr
    _n = min(BC_cnt,left_a_num)
    ans += int((_n * (_n + 1)) / 2)
    ABC_cnt += _n
    ans += int((ABC_cnt * (ABC_cnt + 1)) / 2)

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder B
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    S = list(input()) # abc -> ['a','b','c']
    out=0
    a=0
    b=False
    while S:
        # print(S,a,out)
        s=S.pop(0)

        if s=="A":
            a+=1
            b=False
        elif s=="B":
            if b:
                b=False
                a=0
            else:
                b=True
        else:
            if b:
                out+=a
                # for _ in range(a):
                #     S.insert(0, "A")
            # a=0
            b=False
                

        # if S[0]=="A":
        #     a+=1
        #     del S[0]
        # elif S[0:2]==["B","C"]:
        #     del S[0:2]
        #     out+=a

        #     for _ in range(a):
        #         S.insert(0, "A")
        #     a=0
        # else:
        #     del S[0]
        #     a=0
    print(out)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()
tot = 0
cnt_prev = 0
cnt_A = 0
while(len(s) > 0):
    cnt_A = 0
    try:
        ind = s.index("BC")
    except ValueError as e:
        break
    i = ind
    while(True):
        i -= 1
        if s[i] == "A":
            cnt_A += 1
        else:
            if i == -1:
                cnt_A += cnt_prev
            break
    tot += cnt_A
    cnt_prev = cnt_A
    s = s[ind+2:]
print(tot)

Submission status: Wrong Answer

Solution code:
import sys

s =  [0] + [ord(i) for i in sys.stdin.readline()] + [0, 0]

ret = 0
i = 0

while i < len(s) :
    if s[i] == 65 and s[i+1] == 66 and s[i+2] == 67 :
        j = 0
        while s[i-j-1] == 65 :
            s[i-j+1] = 65
            j += 1
        s[i-j-1] = 66

        s[i+2] = 65
        i += 2
        ret += 1 + j
        
    else :
        i += 1

print(ret)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder B
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    S = list(input()) # abc -> ['a','b','c']
    out=0
    a=0
    b=False
    while S:
        # print(S,a,out)
        s=S.pop(0)

        if s=="A":
            a+=1
            b=False
        elif s=="B":
            if b:
                b=False
                a=0
            else:
                b=True
        else:
            if b:
                out+=a
                # for _ in range(a):
                #     S.insert(0, "A")
            a=0
            b=False
                

        # if S[0]=="A":
        #     a+=1
        #     del S[0]
        # elif S[0:2]==["B","C"]:
        #     del S[0:2]
        #     out+=a

        #     for _ in range(a):
        #         S.insert(0, "A")
        #     a=0
        # else:
        #     del S[0]
        #     a=0
    print(out)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time

#N, A, B, C, D = [ int(x) for x in input().split() ]

S = input()

ans = 0

pointer = 0
while True:
    abc_p = S[pointer:].find("ABC")
    if abc_p == -1:
        break
    abc_p = pointer + abc_p
    left_a_num = 0
    _s = S[abc_p:pointer - 1:-1] if pointer >= 1 else S[abc_p::-1]
    for s in _s:
        if s == 'A':
            left_a_num += 1
        else:
            break
    left_a_num -= 1
    pointer = abc_p + 3
    ABC_cnt = 1
    BC_cnt = 0
    BC_incr = 0
    A_cnt = 0
    while True:
        if pointer + 3 <= len(S) and S[pointer:pointer + 3] == 'ABC':
            ABC_cnt += 1
            pointer += 3
            if A_cnt:
                ans += A_cnt
                ans += BC_incr
        elif pointer + 2 <= len(S) and S[pointer:pointer + 2] == 'BC':
            BC_cnt += 1
            BC_incr += ABC_cnt
            pointer += 2
        elif pointer + 1 <= len(S) and S[pointer] == 'A':
            A_cnt += 1
            pointer += 1
        else:
            break
    ans += BC_incr
    _n = min(BC_cnt,left_a_num)
    ans += int((_n * (_n + 1)) / 2)
    ABC_cnt += _n
    ans += int((ABC_cnt * (ABC_cnt + 1)) / 2)

print(ans)

Submission status: Wrong Answer

Solution code:
a = input()+"f"
s  =0
A = 0
F = 0

BC = 0
ff = 0
res = 0
for i , j , k in zip(a,a[1:],a[2:]):
    # print(i,j,k)
    if F == 0 and ff ==0:
        # print("A ",i,j,k)
        if i+j+k =="ABC":
            res += A+1
            A +=1
            F = 1
            ff +=2
        elif i == "A" and j == "A" :A+=1
        else: A = 0
    elif ff == 0 and F!=0:
        if i+j+k =="ABC":
            res += 1+BC+A
            A+=1
            BC = 0
            F += 1
            ff +=2
        elif i + j == "BC" :

            res +=A+BC
            A = 0
            BC +=1
            ff+=1
        elif  i == "A" and j == "A" :
            A+=1+BC
            BC = 0
            F = 0
        else:
            A =0
            F = 0
            BC = 0
    else:
        if ff>0:ff-=1
        # print("C ",i,j,k)

print(res)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    s = input()
    maximum = len(s) - 1

    index = 0
    count = 0

    while True:
        index = s.find('ABC', index)
        if index == -1:
            print(str(count))
            exit()
        # BC
        bc_add = 4
        bc_count = 0
        while True:
            if index + bc_add <= maximum:
                if s[index + bc_add - 1:index + bc_add + 1] == 'BC':
                    bc_count += 1
                    bc_add += 2
                else:
                    break
            else:
                break
        # A
        a_hikizan = 1
        a_count = 0
        while True:
            if index - a_hikizan >= 0:
                if s[index - a_hikizan] == 'A':
                    a_count += 1
                    a_hikizan -= 1
                else:
                    break
            else:
                break
        # ABC
        abc_add = 5
        abc_count = 1
        while True:
            if index + abc_add <= maximum:
                if s[index + abc_add - 2:index + abc_add + 1] == 'ABC':
                    abc_count += 1
                    abc_add += 3
                else:
                    break
            else:
                break
        if abc_count > 1:
            bc_add2 = abc_add - 1
            while True:
                if index + bc_add2 <= maximum:
                    if s[index + bc_add2 - 1:index + bc_add2 + 1] == 'BC':
                        bc_count += 1
                        bc_add2 += 2
                    else:
                        break
                else:
                    break
        count += (abc_count + (abc_count if not a_count * bc_count == 0 else 0) + a_count + bc_count)
        index += 3

Submission status: Wrong Answer

Solution code:
s = input()


ans = 0

ans += s.count("ABC")
# ans += s.count("ABCBC")
# ans += s.count("AABC")
ans += s.count("ABCBCAABC")
# ans += s.count("ABCABC")



count = 0
count1 = 0
count2 = 0
i = 0
abccount = []
abcbccount = []
aabccount = []
while(i<len(s)-2):
    if s[i:i+3] == "ABC":
        count += 1
        i += 3 
    else:
        if count > 1:
            abccount.append(count)
        count = 0
        i += 1

while(i<len(s)-4):
    if s[i:i+5] == "ABCBC":
        count1 += 1
        if i+5 < len(s)-1:
            i += 5
            j = 0
            while(1):
                if i+j < len(s)-1:
                    if s[i+5:i+7]=="BC":
                        count1 += 1
                        j += 2
                    else:
                        abcbccount.append(count1)
                        count1 = 0
                        break
                else:
                    break
        else:
            break
    else:
        count1 = 0
        i += 1

while(i<len(s)-3):
    if s[i:i+4] == "AABC":
        count2 += 1
        j = 1
        while(1):
            if i-j >=0:
                if s[i]=="A":
                    count2 += 1
                    j += 1
                else:
                    aabccount.append(count2)
                    count2 = 0
            else:
                break

    i += 1




if count > 1:
    abccount.append(count)

abcbccount.append(count1)
aabccount.append(count2)

# print(abccount)

for num in abccount:
    ans += num*(num-1)//2

for num in abcbccount:
    ans += num

for num in aabccount:
    ans += num





# ans2 = 0

# f = True
# while(f):
#     count = s.count("ABC")
#     ans2 += count
#     if count==0:
#         f = False
#     s = s.replace("ABC","BCA")


print(ans)
# print(ans2)

Submission status: Wrong Answer

Solution code:
import sys

s =  [0] + [ord(i) for i in sys.stdin.readline()] + [0, 0]

ret = 0
i = 0

while i < len(s) :
    if s[i] == 65 and s[i+1] == 66 and s[i+2] == 67 :
        j = 0
        while s[i-j-1] == 65 :
            s[i-j+1] = 65
            j += 1
        s[i-j-1] = 66

        s[i+2] = 65
        i += 2
        ret += 1 + j
        print(s)
        
    else :
        i += 1

print(ret)

Submission status: Wrong Answer

Solution code:

S = input()

count = 0

for i in range(100):
 count += S.count('ABC')
 S = S.replace('ABC','BCA')

print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def Find_and_Replace(S,f_i):
    i = f_i
    count = 0
    new_f_i = 0
    while i < len(S):
        if S[i] == 'C':
            if S[i-2] == 'A' and S[i-1] == 'B':
                S[i-2] = 'B'
                S[i-1] = 'C'
                S[i] = 'A'

                if count == 0:
                    new_f_i = max(2,i-2)
                i += 2
                count += 1

            else:
                i += 3
                if count == 0:
                    new_f_i = i

        else:
            if S[i] == 'B':
                i += 1
            elif S[i] == 'A':
                i += 2
            else:
                i += 3
        #print(i,S)
    return count,new_f_i

S = list(input())
total_count = 0
f_i = 2
while True:
    count,new_f_i = Find_and_Replace(S,f_i)
    print(count,f_i,new_f_i)
    f_i = new_f_i
    if count <= 0:
        break
    else:
        total_count += count

print(total_count)

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
pre_chance = 0
pos_chance = 0

for i in range(len(s)-1):
    if s[i] == "A":
        if s[i:i+2] == "AA":
            pre_chance += 1
            pos_chance = 0
        elif s[i:i+3] == "ABC":
            ans += pre_chance + 1
            pre_chance = 0
        else:
            pre_chance = 0
            pos_chance = 0
    elif s[i] == "B":
        if s[i:i+2] == "BC" and pos_chance == 1:
            ans += 1
            pre_chance = 0
        else:
            pre_chance = 0
            pos_chance = 0
    elif s[i] == "C":
        if s[i-1:i+2] == "BCA" and pos_chance == 1:
            pre_chance += 1
        elif s[i-1:i+1] == "BC" and pos_chance == 1:
            pos_chance = 1
        elif s[i-2:i+2] == "ABCB":
            pos_chance = 1
        else:
            pre_chance = 0
            pos_chance = 0

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
S = list(s)
 
abc = ['A','B','C']
bca = ['B','C','A']
bc = ['B','C']
ans = 0
i = 0
A = [0]*len(s)
while i < len(s)-2:
    if S[i] == 'A':
        if S[i+1:i+3] == bc:
            A[i] = 0
            ans += 1
            j = i-1
            while j >= 0 and A[j]:
                A[j] = 0
                ans += 1
                j -= 1
            i += 2
        else: i += 1
    else:
        A[i] = 1
        i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

s = stdin.readline().rstrip()
l = []

for i in range(len(s)-2):
    if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
        l.append(i)

#print(l)

ll = []
count = 0

def souwa(n):
    sum = 0
    while 0<n:
        sum+=n
        n -= 1
    return sum

for i in range(len(l)):
    # 前にaがあればcount
    if l[i] > 0:
        j = l[i]-1
        while 0 <= j:
            if s[j] != 'A':
                break
            count += 1
            j -= 1

    # うしろにbcがあればcount
    j = l[i]+3
    while j < len(s)-1:
      if s[j] != 'B' or s[j+1] != 'C':
        break
      count += 1
      j += 2


#print(l, count)

for i in range(len(l)):
    if l[i] < 0:
        continue
    join = 0
    j = i+1
    while j < len(l):
        #print(l[j-1], l[j])
        if l[j-1] == (l[j]-3):
            join += 1
            j += 1
            continue
        k = l[j-1]+3
        #print (k, s[k], s[k+1])
        while k < len(s)-1:
          if s[k] != 'B' or s[k+1] != 'C':
            break
          k += 2
        #print ("BC CHECK", k, l[j])
        if k == l[j]:
            #print("BC JOIN")
            join += 1
            j += 1
            continue
        while k < l[j]:
          if s[k] != 'A':
            break
          k += 1
        #print ("A CHECK", k, l[j])
        if k == l[j]:
            #print("A JOIN")
            join += 1
            j += 1
            continue
        break
    
    count += souwa(join+1)
    #print("COUNT", count, join+1)
    while 0 < join:
        l[i+join] = -1
        join -= 1

print(count)

Submission status: Wrong Answer

Solution code:
S = input()

count = 0

for i in range(1000):
 count += S.count('ABC')
 S = S.replace('ABC','BCA')

print(count)

Submission status: Wrong Answer

Solution code:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
while True:
    if tmp==len(s)-2: break
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        if tmp>0:
            i=tmp
            while i>0:
                i-=1
                if s[i]=="A":
                    ans+=1
                else: break

        ans+=1
    
    else: tmp+=1

print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, a, b, c, d = LI_()
    s = S()
    flg = False
    for i in range(a, c-1):
        if s[i] == "#" and s[i + 1] == "#":
            print("No")
            return
    for i in range(b, d-1):
        if s[i] == "#" and s[i + 1] == "#":
            print("No")
            return
    if c > d:
        for i in range(b,d+1):
            if s[i] == "." and s[i + 1] == "." and s[i - 1] == ".":
                flg = True
                break
        if not flg:
            print("No")
            return
    print("Yes")
    return

#B
def B():
    s = S()
    i = len(s) - 1
    state = 0
    ans = 0
    bc = 2
    while i >= 0:
        if state == 0:
            if s[i] == "C":
                state += 1
        elif state == 1:
            if s[i] == "B":
                state += 1
            else:
                state = 0
        elif state == 2:
            if s[i] == "A":
                ans += 1
            elif s[i] == "B":
                state = 0
            else:
                state += 1
        elif state == 3:
            if s[i] == "B":
                state += 1
            elif s[i] == "C":
                bc = 2
                state = 1
            else:
                bc = 2
                state = 0
        else:
            if s[i] == "A":
                ans += bc
            elif s[i] == "B":
                state = 0
                bc = 2
            else:
                bc += 1
                state = 3
        #print(state,s[i])
        i -= 1
    print(ans)

    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    B()