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 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()
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]

print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input())
ans = 0
add = 0
for i in range(len(s)):
#  print(ans, add)
  if s[i] == "A":
    add += 1
  elif s[i] == "B":
    if i == len(s) - 1 or s[i+1] != "C":
      add = 0
    else:
      ans += add

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':
    ans += keep
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==-1 : break
    S = S.replace('ABC', 'BCA')

ans = (int(k))

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
cnt_a = 0
change = False
b1 = ''
for i in s:
  if i == 'A' and b1 != 'B':
    cnt_a += 1
    b1 = 'A'
    change = False
  elif i == 'B' and (b1 == 'A' or change == True):
    b1 = 'B'
    change = False
  elif i == 'C' and b1 == 'B':
    ans += cnt_a
    change = True
    b1 = 'C'
  else:
    change = False
    b1 =  ''
    cnt_a = 0
print(ans)

Submission status: Wrong Answer

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

ans = 0
flag = 0
ABCflag = 0

i = 0
while i < N-2:

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

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

    else:
        flag = 0
        if S[i]+S[i+1] == 'BC':
            if ABCflag:
                ans += ABCflag
                ABCflag = 1
            i += 1
        else:
            ABCflag = 0
    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 = input()
ans = 0
search = re.search('A+(BC)+', s)
while search:
    start, end = search.span()
    abc = s[start:end]
    ca = abc.count('A')
    cbc = abc.count('BC')
    c = ca + cbc - 1
    # print(c, abc)
    ans += c
    if cbc >= 2:
        s = s.replace(abc, 'A', 1)
    else:
        s = s.replace(abc, 'A'*c, 1)
    search = re.search('A+(BC)+', s)
# print(s)
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
ans=s.count("ABC")
for i in s:
    s=s.replace("ABC","BCA")
    continue
print(ans)

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

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

Submission status: Wrong Answer

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

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

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

        i = 1
        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            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"):
            ans += 1
            now += 2

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


main()

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 (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)

Submission status: Wrong Answer

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


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

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

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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()
        count += 1
        # BC
        bc_add = 4
        while True:
            if index + bc_add <= maximum:
                if s[index + bc_add - 1:index + bc_add + 1] == 'BC':
                    count += 1
                    bc_add += 2
                else:
                    break
            else:
                break
        # A
        a_hikizan = 1
        while True:
            if index - a_hikizan >= 0:
                if s[index - a_hikizan] == 'A':
                    count += 1
                    a_hikizan -= 1
                else:
                    break
            else:
                break
        # ABC
        abc_add = 5
        while True:
            if index + abc_add <= maximum:
                if s[index + abc_add - 2:index + abc_add + 1] == 'ABC':
                    count += 2
                    abc_add += 3
                    index += 3
                else:
                    break
            else:
                break
        index += 3

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

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

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

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


main()

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
cnt_a = 0
change = False
b1 = ''
for i in s:
  if i == 'A':
    cnt_a += 1
    b1 = 'A'
    change = False
  elif i == 'B' and (b1 == 'A' or change == True):
    b1 = 'B'
    change = False
  elif i == 'C' and b1 == 'B':
    ans += cnt_a
    change = True
    b1 = 'C'
  else:
    change = False
    b1 =  ''
    cnt_a = 0
print(ans)

Submission status: Wrong Answer

Solution code:
import re

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

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

ans = (int(k))

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
            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
                else:break
    else:i+=1
    if i>len(s)-1:break
print(ct)

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':
    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 = list(input())
N = len(S)
import sys
if N < 3:
    print(0)
    sys.exit()

cnt = 0
BClist = [0] * N
Alist = [0] * N
idx = N-1
while idx >= 0:
    if S[idx-1] == 'B' and S[idx] == 'C':
        cnt += 1
        BClist[idx] = cnt
        BClist[idx-1] = cnt
        idx -= 1
    elif S[idx] != 'A':
        BClist[idx] = -1
        cnt = 0
        
    idx -= 1
    
idx = 0
cnt = 0
while idx < N:
    if S[idx] == 'A':
        cnt += 1
        Alist[idx] = cnt
    else:
        cnt = 0
        
    idx += 1
    
idx = 0
ans = 0
flag = False
while idx < N:
    if Alist[idx] <= 0:
        idx += 1
        continue
    while Alist[idx] > 0:
        idx += 1
        if idx >= N:
            flag = True
            break
    if flag:break
    tmp = Alist[idx-1]
    if BClist[idx] <= 0:
        idx += 1
        continue
    else:
        ans += tmp * BClist[idx]
        idx += 2
    
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

print(cnt)

Submission status: Wrong Answer

Solution code:
S = input()

A = [[] for i in range(len(S)+1)]
ans = 0
j = 0
b = False
for i in range(len(S)):
    if S[i] == "A":
        A[j].append(0)
        b = False
    elif b == False and S[i] == "B":
        b = True
    elif b == True and S[i] == "C":
        A[j].append(1)
        b = False
    else:
        b = False
        j += 1
        
for i in range(j+1):
    li = A[i]
    length = 0
    n = 0
    for k in range(len(li)):
        length += k*li[k]
        n += li[k]
    length -= n*(n-1)//2
    ans += length
    
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
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 = 0
            BC = 0
            F += 1
            ff +=2
        elif i + j == "BC" :
            res += 1
            A = 0
            F = 1
            BC +=1
            ff+=1
        elif i == "A":
            A+=1
        else:
            A =0
            F = 0
            BC = 0
    else:
        ff-=1
        # print("C ",i,j,k)

print(res)

Submission status: Wrong Answer

Solution code:
s = input()
count = 0
k = len(s)//2
for i in range(k):
        if 'ABC' in s:
                count = s.count('ABC')
                s = s.replace('ABC','BCA')
        else:
                break
print(count)

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]
            last_3[i] = ["C", "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 = list(input())
count = 0
for i in range(len(s)):
    if s[-i-1] == 'A':
        tmp = -i-1
        while tmp <= -3 and s[tmp+1] == 'B' and s[tmp+2] == 'C':
            s.pop(tmp)
            s.insert(tmp+2, 'A')
            count += 1
            tmp += 2
print(count)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)

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

s=input()
ans=0
k=len(s)
i=0
tmp=0
while i<k:
    f=0
    if tmp != 0:
        f=1
    tmp = 0
    while i<k and s[i]=="A":
        i+=1
        tmp+=1
    if i+1<k and tmp!=0 and s[i]+s[i+1]=="BC":
        i+=2
        while i+1<k and s[i]+s[i+1]=="BC":
            i+=2
            tmp+=1
        ans += tmp+f
    else:
        i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
S = raw_input()

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

print count

Submission status: Wrong Answer

Solution code:
s = input()

ans = 0
abc = []
for n in range(len(s)):
    if s[n:n+3] == 'ABC':
        top = n
        bot = n+2
        ans += 1
        a,b = 0,0
        while s[top-1] == 'A':
            top -= 1
            ans += 1
            a += 1
        while s[bot+1:bot+3] == 'BC':
            bot += 2
            ans += 1
            b += 1
        ans += (1+min(a,b))*min(a,b)//2
        abc.append((top,bot))

for n in range(len(abc)-1):
    if abc[n][1]+1 == abc[n+1][0]:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
c = 0
state = 0
res = 0
stack = 0
loop = 0
while c < len(s):
    """
    loop = loop + 1
    if loop > 100:
        break
    print("c" + str(c))
    print("cur: " + "".join(s))
    print("state" + str(state))
    """
    if state == 0:
        if s[c] == "A":
            state = 1
            stack = 0
    elif state == 1:
        if s[c] == "B":
            state = 2
        else:
            if s[c] == "A":
                state = 1
                stack += 1
                #print("stack up:" + str(stack))
            else:
                state = 0
                stack = 0
    elif state == 2:
        if s[c] == "C":
            #print("hit")
            #print(" stack:" + str(stack))
            res += 1 + stack
            s[c-2-stack], s[c-1-stack], s[c-stack] = "B", "C", "A"
            for i in range(stack):
                s[c-i] = "A"
            c = c - 3 - stack
            state = 0
            stack = 0
        else:
            state = 0
            stack = 0
    c = c + 1
print(res)

Submission status: Wrong Answer

Solution code:
a=input()
a=[i for i in a]
l=len(a)
count=0
of=0
s=1
while 1:
    if s==1:
        of=0
        s=0
        x="x"
        y="y"
        for i in a:
            if y=="A" and x=="B" and i=="C":
                a[of-2]="C"
                a[of-1]="A"
                a[of]="B"
                s=1
                count+=1
                break
            of+=1
            y=x
            x=i
    else:
        break
print(count)

Submission status: Wrong Answer

Solution code:
A = input() 
found = False
count = 0
while not found:
    n = A.count("AABC")
    if n == 0:
        found = True
    else:
        count += 2 * n
        A = A.replace("AABC","BCAA")
    n = A.count("ABCBC")
    if n == 0:
        found = True
    else:
        count += 2 * n
        A = A.replace("ABCBC","BCBCA")
    n = A.count("ABC")
    if n == 0:
        found = True
    else:
        count += n
        A = A.replace("ABC","BCA")
print(count)

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 sys

def main():
    input = sys.stdin.readline
    S = input().strip()
    n = len(S)
    T = [0] * n

    ans = 0
    right = 2
    for i in range(n):
        if S[i:i+3] == 'ABC':
            ans += 1 
            T[i+2] = 'A'

            if i+3 < n and S[i+3:i+5] == 'BC':
                ans += 1
                right = i+5
                T[i+4] = 'A'
                T[i+2] = 'B'

            j = 0
            if T[i-1] == 'A':
                j = 1
                while i - j >= 0 and T[i-j] == 'A':
                    ans += 1
                    T[i-j] = 'B'
                    T[i-j+2] = 'A'
                    j += 1

            if i - j == right:
                ans += 1

            right = max(right, i+3)

        elif S[i] == 'A':
            T[i] = S[i]

    return ans


if __name__ == '__main__':
    print(main())

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)[::-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

print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()

f = True
ans = 0

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



count = 0
i = 0
abccount = []
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


if count > 1:
    abccount.append(count)

# print(abccount)

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









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


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
    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':
    ans += keep
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
s = input()

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

Submission status: Wrong Answer

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

s = S()
N = len(s)

cntA = 0
ans = 0
for i in range(N-1):
  if s[i] == "A":
    cntA += 1
  elif s[i:i+2] == "BC":
    ans += cntA
  elif s[i:i+2] == "CB" or s[i:i+2] == "CA":
    continue
  else:
    cntA = 0

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
c = 0
state = 0
res = 0
stack = 0
loop = 0
while c < len(s):
    """
    loop = loop + 1
    if loop > 100:
        break
    print("c" + str(c))
    print("cur: " + "".join(s))
    print("state" + str(state))
    """
    if state == 0:
        if s[c] == "A":
            state = 1
            stack = 0
    elif state == 1:
        if s[c] == "B":
            state = 2
        else:
            if s[c] == "A":
                state = 1
                stack += 1
                #print("stack up:" + str(stack))
            else:
                state = 0
                stack = 0
    elif state == 2:
        if s[c] == "C":
            #print("hit")
            #print(" stack:" + str(stack))
            res += 1 + stack
            s[c-2-stack], s[c-1-stack], s[c-stack] = "B", "C", "A"
            for i in range(stack):
                s[c-i] = "A"
            c = c - 3 - stack
            #print("replaced" + "".join(s))
            state = 0
            stack = 0
        else:
            state = 0
        if s[c] == "A":
            state = 1
    c = c + 1
print(res)

Submission status: Wrong Answer

Solution code:
import re
s = input()
ans = 0
search = re.search('A+(BC)+', s)
while search:
    start, end = search.span()
    abc = s[start:end]
    ca = abc.count('A')
    cbc = abc.count('BC')
    c = ca + cbc - 1
    # print(c, abc)
    ans += c
    s = s.replace(abc, 'A'*c, 1)
    search = re.search('A+(BC)+', s)
# print(s)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

count = 0
counta = 0
flagb = False
for i in range(len(s)):
    if s[i] == "A":
        counta += 1
        flagb = False
    elif s[i] == "B":
        if counta > 0:
            flagb = True
        else:
            counta = 0
    else:
        if flagb == True:
            count += counta
            counta = 1
            flagb = False
        else:
            counta = 0
            flagb = False        
print(count)

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    S = input().strip()
    n = len(S)
    T = [0] * n

    ans = 0
    right = 2
    for i in range(n):
        if S[i:i+3] == 'ABC':
            ans += 1 
            T[i+2] = 'A'

            if i+3 < n and S[i+3:i+5] == 'BC':
                ans += 1
                right = i+5
                T[i+4] = 'A'
                T[i+2] = 'B'

            j = 0
            if T[i-1] == 'A':
                j = 1
                while i - j >= 0 and T[i-j] == 'A':
                    ans += 1
                    j += 1

            if i - j == right:
                ans += 1

            right = max(right, i+3)

        elif S[i] == 'A':
            T[i] = S[i]

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
A = str(input())
S = [0]*len(A)
for i in range(len(A)):
    if A[i] == "A":
        S[i] = 0
    elif A[i] == "B":
        S[i] = 1
    else:
        S[i] = 2
        
def henkan(S,idx):
    if idx == 0:
        idx = 1
    for i in range(idx-1,len(S)-2):
        if S[i] == 0 and S[i+1] == 1 and S[i+2] == 2:
            S[i] = 1
            S[i+1] = 2
            S[i+2] = 0
            idx = i
            return True
            break
    return False
ans = 0
idx = 0
while (henkan(S,idx)):
    ans += 1

print(ans)

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
count = 0
for i in range(len(s)):
    if s[-i-1] == 'A':
        tmp = -i-1
        while s[tmp+1] == 'B' and s[tmp+2] == 'C':
            s[tmp] = 'B'
            s[tmp+1] = 'C'
            s[tmp+2] = 'A'
            count += 1
            tmp += 2
print(count)

Submission status: Wrong Answer

Solution code:
# いい感じに切っていく
s = input()

cnt = 0

while True:
    i = s.find("ABC")
    if i == -1:
        break
    else:
        cnt += 1
        # 1つ戻るタイプを計算
        j = i - 1
        while True:
            if (s[j] != "A") | (j < 0):
                s = "A" + s[i+3:]
                break
            else:
                cnt += 1
                j -= 1
                
print(cnt)