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 change_value(ans, a, abc, bc):
    abc_after_change = a + abc + bc - 1
    ans += abc
    if 0 < a and 0 < bc:
        ans += abc_after_change * (abc_after_change + 1) // 2
    else:
        ans += abc - 1
    return ans, 0, 0, 0


def main():
    word = input() + " "
    length = len(word)
    answer = 0
    count_a = 0
    count_abc = 0
    count_bc = 0
    i = 0
    while i < length:
        if i < length - 3 and word[i:i + 3] == "ABC":
            count_abc += 1
            i += 2
        elif word[i] == "A":
            if count_abc:
                answer, count_a, count_abc, count_bc = change_value(answer, count_a, count_abc, count_bc)
            count_a += 1
        elif i < length - 2 and word[i:i + 2] == "BC" and 0 < count_abc:
            count_bc += 1
            i += 1
        elif 0 < count_abc:
            answer, count_a, count_abc, count_bc = change_value(answer, count_a, count_abc, count_bc)
        i += 1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=list(input())
if len(s)<=2:
    print(0)
    exit()

ans=0

for i in range(len(s)):
    if s[i:i+3]==list("ABC"):
        s[i:i+3]=list("BCA")
        ans+=1
        for g in range(i)[::-1]:
            if s[g]=="A":
                s[g+2]="A"
                ans+=1
            else:
                break
print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
if len(s)<=2:
    print(0)
    exit()

ans=0

for i in range(len(s)):
    if s[i:i+3]==list("ABC"):
        s[i:i+3]=list("BCA")
        ans+=1
        for g in range(i)[::-1]:
            if s[g]=="A":
                ans+=1
            else:
                break
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

ans = 0
cnt_ABC, cnt_A, cnt_BC = 0, 0, 0
flag = False
i = 0

while i < len(s):
    if not flag and s[i:i+2] == 'AA':
        cnt_A += 1
        i += 1
        continue
    elif s[i:i+3] == 'ABC':
        flag = True
        cnt_ABC += 1
        i += 3
        continue
    elif flag and s[i:i+2] == 'BC':
        cnt_BC += 1
        i += 2
    else:
        ans += cnt_ABC*(2*cnt_A+cnt_ABC+1)//2 + (cnt_A+cnt_ABC)*cnt_BC
        if s[i] == 'A':
            cnt_A += cnt_ABC + 1
        else:
            cnt_A = 0
        cnt_ABC, cnt_BC = 0, 0
        flag = False
        i += 1

if flag:
    ans += cnt_ABC*(2*cnt_A+cnt_ABC+1)//2 + (cnt_A+cnt_ABC)*cnt_BC

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
ans = 0
cnta = 0
flagb = 0
flabc= 0
for i in range(len(s)):
  if s[i]=="A":
    cnta+=1
  elif cnta and flagb==0 and s[i]=="B":
    flagb=1
  elif cnta and flagb and s[i]=="C":
    ans+=cnta
    flagb=0
  elif cnta and flagb==0 and s[i]=="C":
    cnta = 0
  elif flagb and s[i]=="B":
    flagb=0
    cnta=0
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
ans = 0
cnta = 0
flagb = 0
flabc= 0
for i in range(len(s)):
  if s[i]=="A":
    cnta+=1
  elif cnta and s[i]=="B":
    flagb=1
  elif cnta and flagb and s[i]=="C":
    ans+=cnta
    flagb=0
  elif cnta and flagb==0 and s[i]=="C":
    cnta = 0
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
ans = 0
cnta = 0
flagb = 1
flabc= 0
for i in range(len(s)):
  if s[i]=="A":
    cnta+=1
  elif cnta and s[i]=="B":
    flagb=1
  elif cnta and flagb and s[i]=="C":
    ans+=cnta
    flagb=0
  elif cnta and flagb==0 and s[i]=="C":
    cnta = 0
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a=str(input())
ans=0
i=0
prev=0
while i+3<=len(a):
  flag=1
  if a[i:i+3]=='ABC':
    ans+=1
    if prev:
      ans+=1
    if i-1>=0 and a[i-1]=='A':
      ans+=1
      if prev:
        ans+=1
      else:
        if i-6>=0 and a[i-6:i-1]=='ABCBC':
          ans+=1
    elif i+4<=len(a)-1 and a[i+3]=='B' and a[i+4]=='C':
      ans+=1
      flag=0
      if prev:
        ans+=1
    if flag:
      i+=3
    else:
      i+=5
    prev=1
  else:
    prev=0
    i+=1
print(ans)

Submission status: Wrong Answer

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

ret = 0
a_frq = 0
for s in S:
    if s == 'A':
        a_frq += 1

    elif s == 'X':
        ret += a_frq
        a_frq = 1
    else:
        a_frq = 0

print(ret)

Submission status: Wrong Answer

Solution code:
s = input()
l = len(s)
ans = 0
a_cnt = 0
b_flg = False
for i in range(l):
    if s[i] == "A":
        a_cnt += 1
        b_flg = False
    elif s[i] == "B" and not b_flg:
        b_flg = True
    elif s[i] == "C" and b_flg:
        ans += a_cnt
        b_flg = False
    else:
        a_cnt, b_flg = 0, False

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    s = list(input().rstrip())
    ans = 0
    cnt = 0
    for i in range(len(s)-2):
        if s[i] == "A":
            cnt += 1
        else:
            cnt = 0
        if s[i] == "A" and s[i+1] == "B" and s[i+2] == "C":
            ans += cnt
            cnt = 0
            s[i+2] = "A"
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = list(input().rstrip())
    ans = 0
    cnt = 0
    for i in range(len(s)-2):
        if s[i] == "A":
            cnt += 1
        else:
            cnt = 0
        if s[i] == "A" and s[i+1] == "B" and s[i+2] == "C":
            ans += cnt
            cnt = 1
            s[i+2] = "A"
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

s=input()
n=len(s)
lst=[0]*n
for i in range(n):
    lst[i]=s[i]


ans=0
cnt=0
for i in range(n-2):
    if lst[i]=="A":
        cnt+=1
    else:
        cnt=0
    if lst[i]=="A" and lst[i+1]=="B" and lst[i+2]=="C":
        ans+=cnt
        cnt=0
        lst[i+2]="A"
    # print(lst)
    # print(i,ans,cnt)
print(ans)
# print(lst)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
s = list(s.replace('BC','D'))
a_count = 0
ans = 0
i = 0
while i < len(s) - 1 :
    if s[i:i+2] == ['A','D']:
        ans += a_count + 1
        s[i+1] = 'A'
        s[i] = 'D'
        a_count = 0
    else:
        if s[i] == 'A':
            a_count += 1
        else:
            a_count = 0
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
s = list(s.replace('BC','D'))
a_count = 0
ans = 0
i = 0
while i < len(s) - 1 :
    if s[i:i+2] == ['A','D']:
        ans += a_count + 1
        s[i+1] = 'A'
        s[i] = 'D'
    else:
        if s[i] == 'A':
            a_count += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
a_count = 0
ans = 0
i = 0
while i < n-2:
    #print("".join(s))
    if s[i:i+3] == ['A','B','C']:
        ans += a_count + 1
        s[i+2] = 'A'
        s[i+1] = 'C'
        s[i] = 'B'
        i += 1
        a_count = 0
    else:
        if s[i] == 'A':
            a_count += 1
        else:
            a_count = 0
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
a_count = 0
ans = 0
i = 0
while i < n-2:
    if s[i:i+3] == ['A','B','C']:
        ans += a_count + 1
        s[i+2] = 'A'
        i += 1
        a_count = 0
    else:
        if s[i] == 'A':
            a_count += 1
        else:
            a_count = 0
    i += 1
print(ans)

Submission status: Wrong Answer

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

A=[]
word=""
for i in s:
    if i=="A" or i=="D":
        word +=i
    else:
        A.append(word)
        word=""

cnt=0
for i in A:
    a=0
    for j in i:
        if j=="A":a +=1
        else:cnt +=a

print(cnt)

Submission status: Wrong Answer

Solution code:
import re
s=input()
ans=0
match_s = re.search(r'A*(ABC)+(BC)*',s)

while(True):
    if(match_s):
        abc=match_s.group()
        while(abc.find('ABC')!=-1):
            abc=abc.replace('ABC','BCA')
            ans+=1
        s=abc+s[match_s.end():]
        match_s = re.search(r'A*(ABC)+(BC)*',s)
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
ans=0
a=0
for i in range(len(S)):
    if S[i]=="A":
        a+=1
    elif S[i]=="B":
        if i>0 and S[i-1]=="B":
            a=0        
    elif S[i]=="C":
        if i>0 and S[i-1]=="B":
            ans+=a
        else:
            a=0
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
s = s.replace("BC", "D")
N = len(s)
# print(s)
cnt = 0
for i in range(N-1):
    if s[i] == "A" and s[i+1] == "D":
        cnt += 1
print(sum([x for x in range(1, cnt+1)]))

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import math
import heapq
from collections import defaultdict, Counter, deque
MOD = 10**9 + 7
INF = 10**15


def main():
    ans = 0
    s = input().strip()
    # print(s)
    pre = ""
    state = ""
    cnta = 0
    for c in s:
        if c == "A":
            state ="A"
            cnta += 1
        if c == "B":
            if state == "A":
                state = "AB"
            else:
                state = ""
                cnta = 0
        if c == "C":
            if state == "AB":
                ans += cnta
                state = "A"
            else:
                state = ""
                cnta = 0

    print(ans)

if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    S = S.replace("BC", "D")
    N = len(S)
    ans = 0
    a_cnt = 0
    for i in range(N-1):
        if S[i] == "A":
            a_cnt += 1
        elif S[i] == "D":
            ans += a_cnt
        else:
            a_cnt = 0
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import math
import heapq
from collections import defaultdict, Counter, deque
MOD = 10**9 + 7
INF = 10**15


def main():
    ans = 0
    s = input().strip()
    # print(s)
    pre = ""
    state = ""
    cnta = 0
    for c in s:
        if c == "A":
            state ="A"
            cnta += 1
        if c == "B":
            if state == "A":
                state = "AB"
            else:
                cnta = 0
        if c == "C":
            if state == "AB":
                ans += cnta
                state = "A"
            else:
                state = ""
                cnta = 0

    print(ans)

if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
S = list(input())[: -1]
table = [0] * len(S)
res = 0
for i in range(1, len(S)):
  table[i] += table[i - 1]
  if S[i - 1] == "B" and (S[i] == "C"):
    table[i] += 1
#print(table)
dp = [0] * (len(S) + 1)
for i in range(len(S)):
  if S[i] == "A": dp[i] = 1
for i in range(len(S) - 2):
  if S[i] == "A":
    if table[i + 2] > table[i]:
      dp[i + 3] += dp[i]
    res += dp[i]
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
Acnt = 0
cnt = 0
for c in s:
    if cnt <= 1 and c == "A":
        Acnt += 1
        cnt = 1
    elif c == "A":
        Acnt = 1
        cnt = 1
    elif cnt == 1 and c == "B":
        cnt = 2
    elif cnt == 2 and c == "C":
        ans += Acnt
        Acnt = 1
        cnt = 1
    else:
        Acnt = 0
        cnt = 0
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
Acnt = 0
cnt = 0
for c in s:
    if c == "A":
        Acnt += 1
        cnt = 1
    elif cnt == 1 and c == "B":
        cnt += 1
    elif cnt == 2 and c == "C":
        ans += Acnt
        Acnt = 1
        cnt = 1
    else:
        Acnt = 0
        cnt = 0
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = stdin.readline().rstrip()
s = s.replace("BC","D")
point = 0
count = 0
for i in s:
    if i == "A":
        count += 1
    elif i == "D":
        point += count
    else:
        count = 0
    print(count)
print(point)

Submission status: Wrong Answer

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

if S[0] == 'A':
    ac = 1
else:
    ac = 0

ans = 0
for i in range(1,N):
    if S[i] == 'A':
        ac += 1
    elif S[i] == 'B':
        if S[i-1] != 'A':
            ac = 0
    else:
        if S[i-1] == 'B':
            ans += ac
            S[i] = 'A'
        else:
            ac = 0

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
i = S.find("ABC")
if i < 0:
    print(0)
    exit()
c = 0
p = -1
while 0 <= i < len(S):
    c += 1
    j = i-1
    while j >= 0 and S[j] == "A":
        c += 1
        j -= 1
    if p > 0 and p == j:
        c += 1
    j = i+3
    while j < len(S) and S[j:j+2] == "BC":
        c += 1
        j += 2
    p = j-1
    i = S.find("ABC", j)
print(c)

Submission status: Wrong Answer

Solution code:
S = input()
i = S.find("ABC")
if i < 0:
    print(0)
    exit()
c = 1
while 0 <= i < len(S):
    j = i-1
    while j >= 0 and S[j] == "A":
        c += 1
        j -= 1
    j = i+3
    while j < len(S) and S[j:j+2] == "BC":
        c += 1
        j += 2
    i = S.find("ABC", j)
    c += 1
print(c)

Submission status: Wrong Answer

Solution code:
S=input()

i=0
T=""
while i<len(S):

    if i+1<len(S) and S[i]+S[i+1]=="BC":
        T+="D"
        i+=2
    else:
        T+=S[i]
        i+=1
T=list(T)
count=0
m=0
Acount=0
print(T)
for i in range(len(T)):
    if "A"==T[i]:
        Acount+=1
    elif "D"==T[i]:
        count+=Acount
    else:
        Acount=0



#print(T)
print(count)

Submission status: Wrong Answer

Solution code:
S=input()

i=0
T=""
while i<len(S):

    if i+1<len(S) and S[i]+S[i+1]=="BC":
        T+="D"
        i+=2
    else:
        T+=S[i]
        i+=1
T=list(T)
count=0
m=0
Acount=0
for i in range(len(T)-1):
    if "A"==T[i]:
        Acount+=1
    elif "D"==T[i]:
        count+=Acount
    else:
        Acount=0



#print(T)
print(count)

Submission status: Wrong Answer

Solution code:
def mas(a):
    count=0
    while a:
        count+=a
        a-=1
    return(count)
S=input()

lst=[]
ABCidxlst=set()
BCidxlst=set()
Aidxlst=set()
i=0
flag=0
count=0
count2=0
while i<len(S)-1:
    if i<len(S)-2 and "ABC"==S[i]+S[i+1]+S[i+2]:
        if flag:
            count+=1
        else:
            flag=1
            count=1
        ABCidxlst.add(i)
        i+=3

        continue

    else:
        if count:
            lst.append(count)
            count=0
        flag=0
        i+=1
if count:
    lst.append(count)

for idx in ABCidxlst:
    count3=0
    i=-1
    m=3
    while idx+i>=0 and idx+m+1<len(S):
        if S[idx+i]=="A" and S[idx+m]+S[idx+m+1]=="BC":
            i-=1
            m+=2
            count2+=count3
            if not count3:
                count3+=1
            count3+=1

        else:
            break
    if count3:
        lst.append(count3)


    while idx+m+1<len(S) and "BC"==S[idx+m]+S[idx+m+1]:
        count2+=1

        BCidxlst.add(idx+m+1)
        m+=2

    i=-1
    while idx+i>=0 and "A"==S[idx+i]:
        count2+=1

        Aidxlst.add(idx+i)
        i-=1

#print(count2)
for BC in BCidxlst:
    count2+=((BC+1) in Aidxlst)




"""
print(ABCidxlst)
print(BCidxlst)
print(Aidxlst)
print(lst)
print(count2)
"""
print(sum([mas(i) for i in lst])+count2)

Submission status: Wrong Answer

Solution code:
def mas(a):
    count=0
    while a:
        count+=a
        a-=1
    return(count)
S=input()

lst=[]
ABCidxlst=set()
BCidxlst=set()
Aidxlst=set()
i=0
flag=0
count=0
count2=0
while i<len(S)-1:
    if i<len(S)-2 and "ABC"==S[i]+S[i+1]+S[i+2]:
        if flag:
            count+=1
        else:
            flag=1
            count=1
        ABCidxlst.add(i)
        i+=3

        continue

    else:
        if count:
            lst.append(count)
            count=0
        flag=0
        i+=1
if count:
    lst.append(count)

for idx in ABCidxlst:
    count3=0
    i=-1
    m=3
    while idx+i>=0 and idx+m+1<len(S):
        if S[idx+i]=="A" and S[idx+m]+S[idx+m+1]=="BC":
            i-=1
            m+=2
            if not count3:
                count3+=1
            count3+=1
        else:
            break
    if count3:
        lst.append(count3)


    while idx+m+1<len(S) and "BC"==S[idx+m]+S[idx+m+1]:
        count2+=1

        BCidxlst.add(idx+m+1)
        m+=2

    i=-1
    while idx+i>=0 and "A"==S[idx+i]:
        count2+=1

        Aidxlst.add(idx+i)
        i-=1

#print(count2)
for BC in BCidxlst:
    count2+=((BC+1) in Aidxlst)




"""
print(ABCidxlst)
print(BCidxlst)
print(Aidxlst)
print(lst)
print(count2)
"""
print(sum([mas(i) for i in lst])+count2)

Submission status: Wrong Answer

Solution code:
def mas(a):
    count=0
    while a:
        count+=a
        a-=1
    return(count)
S=input()

lst=[]
ABCidxlst=set()
BCidxlst=set()
Aidxlst=set()
i=0
flag=0
count=0
count2=0
while i<len(S)-1:
    if i<len(S)-2 and "ABC"==S[i]+S[i+1]+S[i+2]:
        if flag:
            count+=1
        else:
            flag=1
            count=1
        ABCidxlst.add(i)
        i+=3

        continue

    else:
        if count:
            lst.append(count)
            count=0
        flag=0
        i+=1
if count:
    lst.append(count)

for idx in ABCidxlst:
    count3=0
    i=-1
    m=3
    while idx+i>=0 and idx+m+1<len(S):
        if S[idx+i]=="A" and S[idx+m]+S[idx+m+1]=="BC":
            i-=1
            m+=2
            count3+=1
        else:
            break
    if count3:
        lst.append(count3)


    while idx+m+1<len(S) and "BC"==S[idx+m]+S[idx+m+1]:
        count2+=1

        BCidxlst.add(idx+m+1)
        m+=2

    i=-1
    while idx+i>=0 and "A"==S[idx+i]:
        count2+=1

        Aidxlst.add(idx+i)
        i-=1

#print(count2)
for BC in BCidxlst:
    count2+=((BC+1) in Aidxlst)




"""
print(ABCidxlst)
print(BCidxlst)
print(Aidxlst)
print(lst)
print(count2)
"""
print(sum([mas(i) for i in lst])+count2)

Submission status: Wrong Answer

Solution code:
def mas(a):
    count=0
    while a:
        count+=a
        a-=1
    return(count)
S=input()

lst=[]
ABCidxlst=set()
BCidxlst=set()
Aidxlst=set()
i=0
flag=0
count=0
count2=0
while i<len(S)-1:
    if i<len(S)-2 and "ABC"==S[i]+S[i+1]+S[i+2]:
        if flag:
            count+=1
        else:
            flag=1
            count=1
        ABCidxlst.add(i)
        i+=3

        continue

    else:
        if count:
            lst.append(count)
            count=0
        flag=0
        i+=1
if count:
    lst.append(count)

for idx in ABCidxlst:
    if idx-1>=0 and idx+4<len(S):
        if S[idx-1]=="A" and S[idx+3]+S[idx+4]=="BC":
            count2+=1

    i=3
    while idx+i+1<len(S) and "BC"==S[idx+i]+S[idx+i+1]:
        count2+=1

        BCidxlst.add(idx+i+1)
        i+=2

    i=-1
    while idx+i>=0 and "A"==S[idx+i]:
        count2+=1

        Aidxlst.add(idx+i)
        i-=1

#print(count2)
for BC in BCidxlst:
    count2+=((BC+1) in Aidxlst)




"""
print(ABCidxlst)
print(BCidxlst)
print(Aidxlst)
print(lst)
print(count2)
"""
print(sum([mas(i) for i in lst])+count2)

Submission status: Wrong Answer

Solution code:
def mas(a):
    count=0
    while a:
        count+=a
        a-=1
    return(count)
S=input()

lst=[]
ABCidxlst=set()
BCidxlst=set()
Aidxlst=set()
i=0
flag=0
count=0
count2=0
while i<len(S)-1:
    if i<len(S)-2 and "ABC"==S[i]+S[i+1]+S[i+2]:
        if flag:
            count+=1
        else:
            flag=1
            count=1
        ABCidxlst.add(i)
        i+=3

        continue

    else:
        if count:
            lst.append(count)
            count=0
        flag=0
        i+=1
if count:
    lst.append(count)

for idx in ABCidxlst:
    i=3
    while idx+i+1<len(S) and "BC"==S[idx+i]+S[idx+i+1]:
        count2+=1

        BCidxlst.add(idx+i+1)
        i+=2

    i=-1
    while idx+i>=0 and "A"==S[idx+i]:
        count2+=1

        Aidxlst.add(idx+i)
        i-=1

#print(count2)
for BC in BCidxlst:
    count2+=((BC+1) in Aidxlst)




"""
print(ABCidxlst)
print(BCidxlst)
print(Aidxlst)
print(lst)
print(count2)
"""
print(sum([mas(i) for i in lst])+count2)

Submission status: Wrong Answer

Solution code:
def mas(a):
    count=0
    while a:
        count+=a
        a-=1
    return(count)
S=input()

lst=[]
ABCidxlst=set()
BCidxlst=set()
Aidxlst=set()
i=0
flag=0
count=0
count2=0
while i<len(S)-1:
    if i<len(S)-2 and "ABC"==S[i]+S[i+1]+S[i+2]:
        if flag:
            count+=1
        else:
            flag=1
            count=1
        ABCidxlst.add(i)
        i+=3

        continue

    else:
        if count:
            lst.append(count)
            count=0
        flag=0
        i+=1
if count:
    lst.append(count)

for idx in ABCidxlst:
    i=3
    while idx+i+1<len(S) and "BC"==S[idx+i]+S[idx+i+1]:
        count2+=1

        BCidxlst.add(idx+i+1)
        i+=2

    i=-1
    while idx+i>=0 and "A"==S[idx+i]:
        count2+=1

        Aidxlst.add(idx+i)
        i-=1

print(count2)
for BC in BCidxlst:
    count2+=((BC+1) in Aidxlst)




"""
print(ABCidxlst)
print(BCidxlst)
print(Aidxlst)
print(lst)
print(count2)
"""
print(sum([mas(i) for i in lst])+count2)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	S = input().rstrip()
	S = [s for s in S]

	ans = 0
	i = 0
	tmp = 0
	bonus = 0
	now = 0
	while i<=len(S)-1:
		s = S[i]
		if now == 0 and s=='A':
			tmp = 0
			bonus = 0
			now = 1
			i += 1
			continue
		elif i <= len(S)-2 and now == 1 and (s == 'B' and S[i+1] == 'C'):
			tmp += 1
			tmp += bonus
			bonus = 0
			i += 2
			continue
		elif now == 1 and s == 'A':
			bonus += 1
			i += 1
			continue
		elif now == 1:
			ans += tmp
			tmp = 0
			now = 0
			i += 1
		else:
			i += 1
	else:
		ans += tmp

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
S = input()
N = len(S)
a = 0
ans = 0
f = 0
for i in range(N-1):
    if S[i] == 'A':
        a += 1
    elif S[i] == 'B':
        if S[i+1] == 'C':
            ans += a
            f = 1
        else:
            a = 0
    else:
        if f != 1:
            a = 0
print(ans)

Submission status: Wrong Answer

Solution code:
def sol(S):
  N = len(S)
  nums = [0 for _ in range(N+1)]
  i = N-1
  while i >= 0:
    if S[i-1:i+1] == 'BC':
      nums[i-1] = nums[i+1] + 1
      nums[i] = nums[i+1] + 1
      i -= 2
    else:
      nums[i] = nums[i+1]
      i -= 1
  ans = 0
  for i, s in enumerate(S):
    if s == 'A':
      ans += nums[i]
  return ans

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

Submission status: Wrong Answer

Solution code:
def sol(S):
  N = len(S)
  nums = [0 for _ in range(N+1)]
  i = N-1
  while i >= 0:
    if S[i-1:i+1] == 'BC':
      nums[i-1] = nums[i+1] + 1
      nums[i] = nums[i+1] + 1
      i -= 2
    else:
      nums[i] = nums[i+1]
      i -= 1
  ans = 0
  for i, s in enumerate(S):
    if s == 'A':
      ans += nums[i]
  return ans

S = input()
ans = 0
for s1 in S.split('BB'):
  for s2 in s1.split('CC'):
    for s3 in s2.split('CB'):
      ans += sol(s2)
print(ans)

Submission status: Wrong Answer

Solution code:
def sol(S):
  N = len(S)
  nums = [0 for _ in range(N+1)]
  i = N-1
  while i >= 0:
    if S[i-1:i+1] == 'BC':
      nums[i-1] = nums[i+1] + 1
      nums[i] = nums[i+1] + 1
      i -= 2
    else:
      nums[i] = nums[i+1]
      i -= 1
  ans = 0
  for i, s in enumerate(S):
    if s == 'A':
      ans += nums[i]
  return ans

S = input()
ans = 0
for s1 in S.split('BB'):
  for s2 in s1.split('CC'):
    ans += sol(s2)
print(ans)

Submission status: Wrong Answer

Solution code:
S=list(input())
s=[]
f=0
for i in range(len(S)):
    if S[i] == "A":
        s.append("A")
        f=0
    elif(S[i] == "B"):
        if(f==0):
            f=1
        else:
            s.append("B")
    else:
        if(f==1):
            s.append("D")
            f=0
        else:
            s.append("C")
            f=0
s=s+[""]
c=0
i=0
while(len(s)>i):
    if s[i]=="A" and s[i+1]=="D":
        s[i]="D"
        s[i+1]="A"
        i-=2
        c+=1
    i+=1

print(c)

Submission status: Wrong Answer

Solution code:
S=list(input())+[""]
s=[]
for i in range(len(S)):
    if S[i] == "A":
        s.append("A")
    elif(S[i] == "B" and S[i+1] == "C"):
        s.append("D")
s=s+[""]
c=0
i=0
while(len(s)>i):
    if s[i]=="A" and s[i+1]=="D":
        s[i]="D"
        s[i+1]="A"
        i-=2
        c+=1
    i+=1

print(c)

Submission status: Wrong Answer

Solution code:
s = list(input())

ans = 0
i = 0
a = 0
abc = 0
found = False
while i+1 < len(s):
    if found and s[i] == 'B' and s[i+1] == 'C':
        ans += 1
        i += 2
        abc = 0
        continue
    if i+2 < len(s) and s[i] == 'A':
        a += 1
        if s[i+1] == 'B' and s[i+2] == 'C':
            ans += a
            a = 0
            ans += abc
            abc += 1
            found = True
            i += 3
            continue
    found = False
    abc = 0
    i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())

ans = 0
i = 0
a = 0
found = False
while i+1 < len(s):
    if found and s[i] == 'B' and s[i+1] == 'C':
        ans += 1
        i += 2
        continue
    if i+2 < len(s) and s[i] == 'A':
        a += 1
        if s[i+1] == 'B' and s[i+2] == 'C':
            ans += a
            a = 0
            ans += 1 if found else 0
            found = True
            i += 3
            continue
    found = False
    i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
s = (stdin.readline().rstrip())
s = s.replace("BC","D")
ans = 0
acc = 0

for i in s:
    if i == "B" or i == "C":
        acc = 0
        continue
        
    if i == "A":
        acc += 1
    
    if i == "D":
        ans += acc

print(acc)

Submission status: Wrong Answer

Solution code:
s = input()
pos = 0
str_len = len(s)
count = 0
seq_a = 0
seq_bc = 0

while pos < str_len:
    if s[pos] == "A":
        while pos < str_len and s[pos] == "A":
            seq_a += 1
            pos += 1
        if pos+1 < str_len and s[pos] == "B" and s[pos + 1] == "C":
            while pos < str_len and s[pos] == "B" and pos + 1 < str_len and s[pos + 1] == "C":
                seq_bc += 1
                pos += 1
            count += seq_a * seq_bc
            if pos < str_len and s[pos] != "A":
                seq_a = 0
            seq_bc = 0
        else:
            pos += 1
            seq_a = 0
    else:
        pos += 1
        seq_a = 0

print(count)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

S = list(rs())
cur = 'D'
answer = 0
A_cnt = 0
for s in S:
    if cur == 'A':
        if s == 'A':
            A_cnt += 1
        elif s == 'B':
            cur = s
        else:
            cur = 'D'
    elif cur == 'B':
        if s == 'A':
            A_cnt = 1
            cur = s
        elif s == 'B':
            A_cnt = 0
            cur = 'D'
        elif s == 'C':
            answer += A_cnt
            cur = s
        else:
            cur = 'D'
            A_cnt = 0
    elif cur == 'C':
        if s == 'A':
            A_cnt += 1
            cur = s
        elif s == 'B':
            cur = s
        else:
            cur = 'D'
            A_cnt = 0
    else:
        if s == 'A':
            A_cnt += 1
            cur = s
        else:
            A_cnt = 0
            cur = 'D'

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

S = list(rs())
cur = 'D'
answer = 0
A_cnt = 0
for s in S:
    if cur == 'A':
        if s == 'A':
            A_cnt += 1
        elif s == 'B':
            cur = s
        else:
            cur = 'D'
    elif cur == 'B':
        if s == 'A':
            A_cnt = 1
            cur = s
        elif s == 'B':
            A_cnt = 0
            cur = s
        elif s == 'C':
            answer += A_cnt
            cur = s
        else:
            cur = 'D'
    elif cur == 'C':
        if s == 'A':
            A_cnt += 1
            cur = s
        elif s == 'B':
            cur = s
    else:
        if s == 'A':
            A_cnt += 1
            cur = s
        else:
            A_cnt = 0

print(answer)