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:
def main():
    s=input()
    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 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]

    # if n < 1000:
    #     print(ans)
    #     return
    print(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)
    
main()

Submission status: Wrong Answer

Solution code:
print('No')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=input()
cnt=0
abc=s.count("ABC")

while abc!=0:
    cnt+=abc
    s=s.replace("ABC","BCA")
    s=s.replace("BB","")
    s=s.replace("ABC","AC")
    abc=s.count("ABC")

print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np
# import math
# import random
# import fractions
# import sys
# sys.setrecursionlimit(100000)
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = stdin.readline().rstrip().split()[0]
S = stdin.readline().rstrip().split()[0]
N = len(S)
newS=[]
i=0
while i<N:
  if i>N-3:
    newS.append(S[i])
    i+=1
  elif S[i:i+3]=="ABC":
    newS.append("D")
    i+=3
  elif S[i:i+2]=="BC":
    newS.append("E")
    i+=2
  else:
    newS.append(S[i])
    i+=1
# print(newS)

lns = len(newS)
ans = 0
for i in range(lns-1):
  if newS[i]=="A" and newS[i+1]=="D":
    newS[i]="D"
    newS[i+1]="A"
    ans+=1
for i in range(lns-1):
  if newS[lns-1-i]=="E" and newS[lns-2-i]=="D":
    newS[lns-1-i]="D"
    newS[lns-2-i]="E"
    ans+=1
# print(ans,newS)

newS.append("Z")
Dcount = 0
Acount = 0


for i in range(lns+1):
  if newS[i]=="D":
    Dcount+=1
  else:
    if Dcount!=0:
      ans+=(Dcount*(Dcount+1))//2
      # Dcountはまだ初期化しない
      ans+=Acount
      Acount=0
    
    if newS[i]=="A":
      Acount+=1
      Dcount=0
    elif newS[i]=="E":
      if Dcount!=0:
        ans+=1
      Acount=0
    else:
    
      Dcount=0
      Acount=0
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
counter = 0
answer = 0
aCounter = 0
i = 0
while i < len(s):
  if s[i] == "A":
    counter = 1
    aCounter += 1
  elif s[i] == "B":
    if counter == 1:
      counter = 2
    else:
      counter = 0
      aCounter = 0
  else: # i == C
    if counter == 2:
      answer += aCounter
      counter = 1
    else:
      counter = 0
      aCounter = 0
  i += 1
print(answer)

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


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

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

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

    else:
        r1 = 0
        r2 = 0
        r3 = 0
    i=i+1
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 = 0
            F = 1
            ff +=2
        elif i == "A" and j == "A" :A+=1
        else: A = 0
    elif ff == 0 and F!=0:
        # print("B ",i,j,k)
        if i+j+k =="ABC":
            res += 1+F+BC
            A = 1
            BC = 0
            F += 1
            ff +=2
        elif i + j == "BC" :

            res += F
            A = 0
            BC +=1
            ff+=1
        elif i == "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:
s = list(str(input()))

ans = 0

for i in range(len(s)-2):
    if s[i] == "A" and s[i+1] == "B" and s[i+2] == "C":
        s[i] = "B"
        s[i+1] = "C"
        s[i+2] = "A"
        ans = ans+1
        j=i-1
        while(j > 0):
            if s[j] == "A":
                s[j] = "B"
                s[j+1] = "C"
                s[j+2] = "A"
                ans = ans+1
            j=j-1
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
NN= S.count("ABCABC")
C=0
if NN>=1:
  for i in range(NN):
    S=S.replace('ABCABC','BCABCA')
    C=C+2
N=S.count("ABC")
while N>0:
  S=S.replace('ABC','BCA')
  C=C+1
  N=S.count("ABC")
  if S in ('ABCABC'):
    S=S.replace('ABCABC','BCABCA')
    C=C+2
print(C)

Submission status: Wrong Answer

Solution code:
def snum(S):
    # bcの数
    origS = S[:]
    bccnt = 0
    i = 0
    bcIndex = []
    base = 0
    while "BC" in S[i:]:
        S = S[i:]
        bcIndex.append( S.find("BC")  + base)
        i = S.find("BC") + 2
        base += i


    aIndex = []
    i = 0
    base = 0
    S = origS[:]
    while "A" in S[i:]:    
        S = S[i:]
        aIndex.append(S.find("A") + base)
        i = S.find("A") + 1
        base += i


    ans = 0
    ai = 0
    bc = 0
    nokoriBC = len(bcIndex)
    nowA = 0

    subS = [0] * len(origS)
    for a in aIndex:
        subS[a] = 1
    for b in bcIndex:
        subS[b] = 2

    for i in range(len(origS)):
        if subS[i]  == 1:
            nowA += 1
        elif subS[i] == 2:
            ans += nowA * nokoriBC
            nowA = 0
            nokoriBC -= 1
            if (nokoriBC == 0): break
    return ans
    return (aIndex, bcIndex)

S = input()
pos = []
i = 0
while "CC"  in S[i:] or "BB" in S[i:]:
    S = S[i:]
    # a = min( S.find("CC"), S.find("BB"))
    c = (S.find("CC"))
    b = S.find("BB")
    a = 0
    if ( c != -1 and b != -1) :
        a = min(b,c)
    elif b == -1:
        a = c
    else :
        a = b

    pos.append( S[i:a+1]  )
    i = a+2
pos.append(S[i:])
# print(kpos)
ans = 0
for p in pos:
    tmp = snum(p)
    ans += tmp
print(ans)

Submission status: Wrong Answer

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

def conv2num(c):
    if c == "A":
        return 1
    elif c == "B":
        return 2
    else:
        return 3

def solve(s: str):

    res = 0

    s = [ conv2num(c) for c in s ]
    i = 0
    endi = len(s) - 2
    while i < endi:
        if s[i] == 1 and s[i+1] == 2 and s[i+2] == 3:
            ai = i
            for j in range(i-1,-1,-1):
                if s[j] == 1 and j >=0 :
                    ai = j 
                else:
                    break
            #s[ai]   = 2
            #s[ai+1] = 3
            s[ai+2] = 1
            res += 1

            st = ai+3
            ed = i +3
            for k in range(st,ed):
                s[k] = 1
                res += 1
            i += 1
        i += 1

    print(res)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np
# import math
# import random
# import fractions
# import sys
# sys.setrecursionlimit(100000)
# N = int(stdin.readline().rstrip().split()[0])
# A = [int(x) for x in stdin.readline().rstrip().split()]
# N, X = [int(x) for x in stdin.readline().rstrip().split()]
# S = stdin.readline().rstrip().split()[0]
S = stdin.readline().rstrip().split()[0]
N = len(S)
newS=[]
i=0
while i<N:
  if i>N-3:
    newS.append(S[i])
    i+=1
  elif S[i:i+3]=="ABC":
    newS.append("D")
    i+=3
  elif S[i:i+2]=="BC":
    newS.append("E")
    i+=2
  else:
    newS.append(S[i])
    i+=1
# print(newS)

lns = len(newS)
ans = 0
for i in range(lns-1):
  if newS[i]=="A" and newS[i+1]=="D":
    newS[i]="D"
    newS[i+1]="A"
    ans+=1
for i in range(lns-1):
  if newS[lns-1-i]=="E" and newS[lns-2-i]=="D":
    newS[lns-1-i]="D"
    newS[lns-2-i]="E"
    ans+=1
# print(ans,newS)

newS.append("Z")
Dcount = 0
Acount = 0


for i in range(lns+1):
  if newS[i]=="D":
    Dcount+=1
  else:
    if Dcount!=0:
      ans+=(Dcount*(Dcount+1))//2
      # Dcountはまだ初期化しない
      ans+=Acount
      Acount=0
    """
    if newS[i]=="A":
      Acount+=1
      Dcount=0
    elif newS[i]=="E":
      if Dcount!=0:
        ans+=1
      Acount=0
    """
    
    Dcount=0
    Acount=0
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
counter = 0
answer = 0
aCounter = 0
i = 0
while i < len(s):
  if s[i] == "A":
    counter = 1
    aCounter += 1
  elif s[i] == "B":
    if counter == 1:
      counter = 2
    else:
      counter = 0
      aCounter = 0
  else: # i == C
    if counter == 2:
      answer += aCounter
      counter = 1
    else:
      counter = 0
  i += 1
print(answer)

Submission status: Wrong Answer

Solution code:
s = input()
ss = [ si for si in s ]

cnt = 0
cnti = 0
i = 0
while True:
    while True:
        if i < len(s)-2 and ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i += 1
            if i < len(s)-2:
                break
        elif i >= len(s)-2:
            i = 0
            cnti += 1
            break
        else:
            i += 1
    if cnti > 2:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
s=input()
s=list(s)
s.append('D')
s.append('D')
n=[]
i=0
ct=0
while True:
    if s[i]=='A' and s[i+1]=='B' and s[i+2]=='C':
        ct+=1
        n.append(i+2)
        i+=3
        while True:
            if s[i]=='B' and s[i+1]=='C':
                ct+=1
                n.append(i+1)
                i+=2
            if i==len(s)-2:break
            else:break
        if i>=4:
            while True:
                if s[i-4]=='A':
                    ct+=1
                    i-=1
                if i>=6 and s[i-4]=='C':
                    if i-4 in n:
                        ct+=1
                        break
                    else:break
                if i-4==0:
                    if s[0]=='A':
                        ct+=1
                    break
                elif s[i-4]!='A':break
    else:i+=1
    if i>len(s)-1:break
print(ct)

Submission status: Wrong Answer

Solution code:
s = input()
ss = [ si for si in s ]

cnt = 0
cnti = 0
i = 0
while True:
    while True:
        if i < len(s)-2 and ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i += 1
            if i < len(s)-2:
                break
        elif i >= len(s)-2:
            i = 0
            cnti += 1
            break
        else:
            i += 1
    if cnti > 1:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    s=input()
    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 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]
    
    if n < 1000:
        print(ans)
        return 
    
    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)

Submission status: Wrong Answer

Solution code:
# %%time
s = input()
# s = "ABC" * 10000


k = 0
kk = 0
ans = 0
while True:
    
    if k >= len(s) - 1:
        break
    if s[k : k + 3] == "ABC":
        n = 0
        while True:
            if s[k + 3 + n  *2 : k + 5 + n * 2] == "BC":
#                 aaa
                if s[k + 3 + n * 2 > len(s)]:
                    break
                n += 1
            else:
                break
                
        m = 0
        while True:
            ind = k-m-1
            if ind  < 0:
                break
            if s[ind] == "A":
                m+=1
            else:
                break

        if n == 0:       
            j = 0
            while True:
                if k + 3 + j * 3 > len(s):
                    break
#                 print(s[k + 3 + j  *3 : k + 6 + j * 3])
                if s[k + 3 + j  *3 : k + 6 + j * 3] == "ABC":
                    j += 1
                else:
                    break
            s = s[:k] + "BC" * (j + 1) + "A" * (j + 1) + s[k+3 + j * 3:]
            kk = k + j * 3
            kk = k
            k -= 1
            print(j)
            ans += (j + 2) * (j + 1) // 2
            
        else:
            s = s[:k-m] + "BC" * (n + 1) + "A" * (m + 1) + s[k+3 + n * 2:]        
            kk = k + m  + n * 2
            kk = k
            k -= 1
            ans += (n + 1) * (m + 1)
        
#         print(s)
#         aaaa
    else:
#         print(k)
        k += 1
        if kk > k:
            k = kk

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
if N < 3:
    print(0)
    exit()
dp = [0 for _ in range(N+1)]
last_3 = [None]*(N+1)
cnt_A = [0]*(N+1)
if S[0] == "A" and S[1] == "B" and S[2] == "C":
    dp[3] = 1
    last_3[3] = ["B", "C", "A"]
else:
    last_3[3] = [S[0], S[1], S[2]]

if last_3[3][0] == "A":
    cnt_A[3] = cnt_A[2] + 1

for i in range(4, N+1):
    dp[i] = dp[i-1]
    if S[i-1] == "C" and last_3[i-1][1] == "A" and last_3[i-1][2] == "B":
        dp[i] += 1
        last_3[i] = ["B", "C", "A"]

        if last_3[i-1][0] == "A":
            dp[i] += cnt_A[i-1]
            if cnt_A[i] == 1:
                last_3[i] = ["C", "A", "A"]
            elif cnt_A[i] >= 2:
                last_3[i] = ["A", "A", "A"]

    else:
        last_3[i] = [last_3[i-1][1], last_3[i-1][2], S[i-1]]

    if last_3[i][0] == "A":
        cnt_A[i] = cnt_A[i-1] + 1
print(dp[N])

Submission status: Wrong Answer

Solution code:
s = input()

count = 0
counta = 0
for i in range(len(s)):
    if s[i] == "A":
        counta += 1
    elif s[i] == "B":
        if s[i-1] != "B":
            pass
        else:
            counta = 0
    else:
        if s[i-1] == "B":
            count += counta
            counta = 1
        else:
            counta = 0
print(count)

Submission status: Wrong Answer

Solution code:
s = input()
ss = [ si for si in s ]

cnt = 0
cnti = 0
i = len(s)
while True:
    while True:
        if i < len(s)-2 and ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i -= 1
            if i < 0:
                break
        elif i < 0:
            i = len(s)
            cnti += 1
            break
        else:
            i -= 1
    if cnti > 2:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
import re

S = input()
ct = 0

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

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
a=[0 for i in range(len(s))]
bc=[0 for i in range(len(s))]
if len(s)<3:
  print(0)
else:
  for i in range(len(s)):
    if s[i]=="A":
      a[i]=1
  for i in range(len(s)-1):
    if s[i]=="B" and s[i+1]=="C":
      bc[i]=1
  for i in range(len(s)-2,-1,-1):
    if bc[i-2]>0:
      bc[i-2]+=bc[i]
      bc[i]=0
  for i in range(len(s)-2):
    if a[i]>0 and bc[i+1]>0:
      a[i+2]=1
  for i in range(len(s)-1):
    if a[i+1]>0:
      a[i+1]+=a[i]
      a[i]=0
  
  for i in range(len(s)-2):
    if a[i]>0 and bc[i+1]>0:
      ans+=a[i]*bc[i+1]
  print(ans)

Submission status: Wrong Answer

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

ans = 0
keep = 0

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

if S[0] == 'A' and N > 1 and S[0:2] != "AC":
    ans += keep
print(ans)

Submission status: Wrong Answer

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

p1 = 0
p2 = 0
j = 0
frag = True
foundA = False
foundBC = False

while j+1 < l:
    if s[j:j+2] == 'BB' or s[j:j+2] == 'CC' or s[j:j+2] == 'AC' or s[j:j+2] == 'CB' or s[j:j+2] == 'BA':
        p2 = 0
        j += 1
    elif s[j] == 'A':
        p2 += 1
        j += 1
        foundA = True
        foundBC = False

    elif s[j:j+2] == 'BC' and foundA:
        p1 += p2
        j += 2
        foundA = False
        foundBC = True

    elif s[j:j+2] == 'BC' and foundBC:
        p1 += 1
        j += 2

    else:
        p2 = 0
        foundA = False
        foundBC = False
        
print(p1)

Submission status: Wrong Answer

Solution code:
s = input().strip()
ret = 0
for i in range(100):
    ret += s.count('ABC')
    s = s.replace('ABC', 'BCA')
print(ret)

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:
        # print("A ",i,j,k)
        if i+j+k =="ABC":
            res += A+1
            A = 0
            F = 1
            ff +=2
        elif i == "A" and j == "A" :A+=1
        else: A = 0
    elif ff == 0 and F!=0:
        # print("B ",i,j,k)
        if i+j+k =="ABC":
            res += 1+F+BC
            A = 1
            BC = 0
            F += 1
            ff +=2
        elif i + j == "BC" :

            res += 1
            A = 0
            BC +=1
            ff+=1
        elif i == "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:
s=input()
s=list(s)
s.append('D')
s.append('D')
n=[]
i=0
ct=0
while True:
    if s[i]=='A' and s[i+1]=='B' and s[i+2]=='C':
        ct+=1
        n.append(i+2)
        i+=3
        while True:
            if s[i]=='B' and s[i+1]=='C':
                ct+=1
                n.append(i+1)
                i+=2
                if i+1==len(s):
                    break
            else:break
        if i>=4:
            while True:
                if s[i-4]=='A':
                    ct+=1
                    i-=1
                if i>=6 and s[i-4]=='C':
                    if i-4 in n:
                        ct+=1
                        break
                    else:break
                if i-4==0:
                    if s[0]=='A':
                        ct+=1
                    break
                elif s[i-4]!='A':break
    else:i+=1
    if i>len(s)-1:break
print(ct)

Submission status: Wrong Answer

Solution code:
s = input()
ss = [ si for si in s ]

cnt = 0
cnti = 0
i = 0
while True:
    while True:
        if i < len(s)-2 and ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i += 1
            if i >= len(s)-2:
                break
        elif i >= len(s)-2:
            i = 0
            cnti += 1
            break
        else:
            i += 1
    if cnti > 2:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
a=[0 for i in range(len(s))]
bc=[0 for i in range(len(s))]
if len(s)<3:
  print(0)
else:
  for i in range(len(s)):
    if s[i]=="A":
      a[i]=1
  for i in range(len(s)-1):
    if s[i]=="B" and s[i+1]=="C":
      bc[i]=1
  for i in range(len(s)-2):
    if a[i]>0 and bc[i+1]>0:
      a[i+2]=1
  for i in range(len(s)-1):
    if a[i+1]>0:
      a[i+1]+=a[i]
      a[i]=0
  for i in range(len(s)-2,-1,-1):
    if bc[i-2]>0:
      bc[i-2]+=bc[i]
      bc[i]=0
  for i in range(len(s)-2):
    if a[i]>0 and bc[i+1]>0:
      ans+=a[i]*bc[i+1]
  print(ans)

Submission status: Wrong Answer

Solution code:
import re
s = input()
ans = 0
for v in re.finditer('(A(BC)+A?)+', s):
    sub = s[v.start():v.end()]
    total = sub.count('BC')
    abc_num = sub.count('ABC')
    ans += total * (total - 1) // 2 + abc_num
    
print(ans)

Submission status: Wrong Answer

Solution code:
import re

S = str(input())
k=int(0)

#for i in range(2000):
while () :
#while S.find('ABC')>=0:
#    S = S[:S.rfind('ABC')] + 'BCA' + S[(S.rfind('ABC')+3):]
#    k +=1
    matchedList = re.findall("ABC",S)
    a = len(matchedList)
    k += a
    S = S.replace('ABC', 'BCA')
    if a==0 : break

ans = (int(k))

print(ans)

Submission status: Wrong Answer

Solution code:
s = input().strip()
ret = 0
for i in range(50):
    ret += s.count('ABC')
    s = s.replace('ABC', 'BCA')
print(ret)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10000)
mod = 1000000007


def mul(a, b):
    return ((a % mod) * (b % mod)) % mod


def power(x, y):
    if y == 0:
        return 1
    elif y == 1:
        return x % mod
    elif y % 2 == 0:
        return power(x, y / 2) ** 2 % mod
    else:
        return power(x, y / 2) ** 2 * x % mod


def div(a, b):
    return mul(a, power(b, mod - 2))


# def gcd(a_gcd, b_gcd):
#     while b_gcd != 0:
#         a_gcd, b_gcd = b_gcd, a_gcd % b_gcd
#     return a_gcd
#
#
# def lcm(a_lcm, b_lcm):
#     return a_lcm * b // gcd(a_lcm, b_lcm)
#
#
# def max_sum(N_max_sum, a_max_sum):
#     dp = [0] * (N_max_sum + 1)
#     for i in range(N_max_sum):
#         dp[i + 1] = max(dp[i], dp[i] + a_max_sum[i])
#     return dp[N]
#
#
# def knapsack(N_knapsack, W, weight, value):
#     inf = float("inf")
#     dp = [[-inf for _ in range(W+1)] for j in range(N_knapsack + 1)]
#     for i in range(W+1):
#         dp[0][i] = 0
#
#     for i in range(N_knapsack):
#         for w in range(W+1):
#             if weight[i] <= w:
#                 dp[i+1][w] = max(dp[i][w-weight[i]]+value[i], dp[i][w])
#             else:
#                 dp[i+1][w] = dp[i][w]
#     return dp[N_knapsack][W]
#
#
# class UnionFind:
#     def __init__(self, n):
#         self.par = [i for i in range(n+1)]
#         self.rank = [0] * (n+1)
#
#     # 検索
#     def find(self, x):
#         if self.par[x] == x:
#             return x
#         else:
#             self.par[x] = self.find(self.par[x])
#             return self.par[x]
#
#     # 併合
#     def union(self, x, y):
#         x = self.find(x)
#         y = self.find(y)
#         if self.rank[x] < self.rank[y]:
#             self.par[x] = y
#         else:
#             self.par[y] = x
#             if self.rank[x] == self.rank[y]:
#                 self.rank[x] += 1
#
#     # 同じ集合に属するか判定
#     def same_check(self, x, y):
#         return self.find(x) == self.find(y)


S = input()
# N = int(input())
# A = list(map(int, input().split()))
# X = [int(input()) for _ in range(N)]

ans = 0
a_count = 0
bc_count = 0
bc_frag = False
i = 0
while i < len(S):
    # print(S[i])
    if bc_frag:
        if i < len(S) - 1:
            if S[i:i + 2] == "BC":
                ans += 1
                # print(1)
                i += 2
                a_count = 1
            else:
                bc_frag = False
                if S[i] == 'A':
                    a_count += 1
                    # print(2)
                else:
                    a_count = 0
                    # print(3)

                if S[i:i + 3] == "ABC":
                    ans += 1
                    ans += a_count - 1
                    # print(4)
                    bc_frag = True
                    i += 3
                    a_count = 0
                else:
                    i += 1
                    # print(78)
        else:
            break
    else:
        if S[i] == 'A':
            a_count += 1
            # print(5)
        else:
            a_count = 0
            # print(6)
        if i < len(S) - 2:
            if S[i:i + 3] == "ABC":
                ans += 1
                ans += a_count - 1
                # print(7)
                bc_frag = True
                i += 3
                a_count = 1
            else:
                i += 1
                # print(8)
        else:
            break




print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0

ids = []
for i in range(len(S)-2):
    if S[i:i+3] == 'ABC':
        ids.append(i)

if not ids:
    print(0)
    exit()

seq = 1
pi = -999
ss = []
for i in ids:
    if i - pi == 3:
        seq += 1
    elif pi >= 0:
        ans += seq*(seq+1)//2
        ss.append((i,i+3*seq))
        seq = 1
    pi = i

ans += seq*(seq+1)//2

a_start = set()
bc_end = set()

for a,b in ss:
    while a > 0 and S[a-1] == 'A':
        a -= 1
        ans += 1
    a_start.add(a)
    while b < len(S)-1 and S[b:b+2] == 'BC':
        b += 2
        ans += 1
    bc_end.add(b)

ans += len(a_start & bc_end)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    now = 0
    aa = 0
    while(1):
        now = S.find("ABC")
        if now == -1:
            break

        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            i = 1
            aa = 0
            while(S[now-i] == 'A' and now-i >= 0):
                i += 1
                aa += 1

        aa += 1
        bc = 1
        now += 3
        while(S[now:now+2] == "BC"):
            now += 2
            bc += 1

        ans += aa*bc
        S = S[now:]
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
s = input().strip()
ret = 0
for i in range(20):
    ret += s.count('ABC')
    s = s.replace('ABC', 'BCA')
print(ret)

Submission status: Wrong Answer

Solution code:
s = input()

result = 0

result += s.count('AABCBC')

s = s.replace('AABCBC', 'ABCABC')


while 'AABC' in s or 'ABCBC' in s:
    result += s.count('AABC')
    s = s.replace('AABC', 'ABCA')
    result += s.count('ABCBC')
    s = s.replace('ABCBC', 'BCABC')

result += s.count('ABC')
s = s.replace('ABC', 'BCA')

print(result)

Submission status: Wrong Answer

Solution code:
s=input()
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 sr[i] == 'A' or sr[i:i+2] in ['BC'] or (1 < 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)

Submission status: Wrong Answer

Solution code:
import re

S = str(input())
k=int(0)

#for i in range(2000):
while () :
#while S.find('ABC')>=0:
#    S = S[:S.rfind('ABC')] + 'BCA' + S[(S.rfind('ABC')+3):]
#    k +=1
    matchedList = re.findall("ABC",S)
    a = len(matchedList)
    k += a
    if a==0 : break
    S = S.replace('ABC', 'BCA')

ans = (int(k))

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()

ABC_dict = {'A':1, 'B':2, 'C':3}
cnt = 0
abc_lst = [ABC_dict[S[i]] for i in range(len(S))]
for i in range(len(S)-2):
    abc = abc_lst[i:i+3]
    if abc == [1,2,3]:
        cnt += 1
        abc_lst[i] = 2
        abc_lst[i+1] = 3
        abc_lst[i+2] = 1

for i in range(len(S)-2)[::-1]:
    abc = abc_lst[i:i+3]
    if abc == [1,2,3]:
        cnt += 1
        abc_lst[i] = 2
        abc_lst[i+1] = 3
        abc_lst[i+2] = 1
for i in range(len(S)-2):
    abc = abc_lst[i:i+3]
    if abc == [1,2,3]:
        cnt += 1
        abc_lst[i] = 2
        abc_lst[i+1] = 3
        abc_lst[i+2] = 1
for i in range(len(S)-2)[::-1]:
    abc = abc_lst[i:i+3]
    if abc == [1,2,3]:
        cnt += 1
        abc_lst[i] = 2
        abc_lst[i+1] = 3
        abc_lst[i+2] = 1

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()

cnt = 0
x = 0
while x <=len(s):
    if s[x:x+3] == "ABC":
        s = s[:x]+"BCA"+s[x+3:]
        cnt += 1
        for y in range(0,len(s),2):
            if s[x+3+y:x+5+y] == "BC":
                s = s[:x+5+y-3]+"BCA"+s[x+5+y:]
                cnt += 1
            else:
                break
        if x>0:
            for z in range(1,len(s)):
                if s[x-z] == "A":
                    s = s[:x-z]+"BCA"+s[x-z+3:]
                    cnt += 1
                else:
                    break
        x += 3
    else:
        x += 1
print(cnt)

Submission status: Wrong Answer

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