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: Time Limit Exceeded

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 +=  1
        s[i+2] = 'A'
        s[i+1] = 'C'
        s[i] = 'B'
        i -= 2
        a_count = 0
    else:
        if s[i] == 'A':
            a_count += 1
        else:
            a_count = 0
    i += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = 0
while(s.count("ABC")!=0):
    index = s.index("ABC")
    s = s[:index] + "BCA" + s[index+3:]
    t += 1
print(t)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# ABC
s=input()
s=s.replace('BC','D')
is_ABC=True
ans=0
while is_ABC:
    t=s.count('AD')
    ans+=t
    s=s.replace('AD','DA')
    if t==0:
        is_ABC=False
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = stdin.readline().rstrip()
point = 0
while True:
    if "ABC" not in s:
        break
    else:
        point += s.count("ABC")
        s = s.replace("ABC","BCA")
print(point)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
ans = 0
while True:
    i = S.find('ABC')
    if i == -1:
        break
    elif i == 0:
        S = 'BCA' + S[i+3:]
        ans += 1
    elif i == N-3:
        S = S[:i]+'BCA'
        ans += 1
    else:
        S = S[:i]+'BCA'+S[i+3:]
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
b = True
ans = 0
while b:
    b = False
    for i in range(N-2):
        if S[i] == 'A' and S[i+1] == 'B' and S[i+2] == 'C':
            b = True
            S[i] = 'B'
            S[i+1] = 'C'
            S[i+2] = 'A'
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S):
    S = [c for c in S]

    ans = 0
    N = len(S)
    i = 0
    while i < N - 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 += 1
            i = max(0, i - 4)
        else:
            i += 1
    return ans


def main():
    S = read_str()
    print(slv(S))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
i = S.find("ABC")
c = 0
while i >= 0:
    S = "".join([S[:i], "BCA", S[i+3:]])
    i = S.find("ABC")
    c += 1
print(c)

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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
count2=-1
m=0
#print(T)
while count!=count2:

    count2=count
    flag=0
    for i in range(m,len(T)-1):
        if "A"==T[i] and "D" == T[i+1]:
            T[i]="D"
            T[i+1]="A"
            count+=1
            """if flag==0:
                m=i
                flag=1"""
        #print(T)
print(count)

Submission status: Time Limit Exceeded

Solution code:
S=input()

i=0
T=""
while i<len(S):
    if S[i]=="A":
        T+=S[i]
    elif i+1<len(S) and S[i]+S[i+1]=="BC":
        T+="D"

count=0
count2=-1
while count!=count2:
    count2=count
    for i in range(len(S)-1):
        if "AD"==S[i]+S[i+1]:
            S[i],S[i+1]="D","A"
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
while 'ABC' in S:
    count += S.count('ABC')
    S = S.replace('ABC', 'BCA')
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
while 'ABC' in S:
    count += 1
    S = S.replace('ABC', 'BCA', 1)
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0
s = s.replace('BC','X')
s = s.replace('B','Z').replace('C','Z')
s = s.split('Z')
for i in s:
    for j in range(len(i)):
        if i[j] == 'X':
            count += i[:j].count('A')
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0
s = s.replace('BC','X')
for i in range(len(s)):
    if s[i] == 'X':
        for j in range(i,-1,-1):
            if s[j] == 'B' or s[j] == 'C':
                break
            if s[j] == 'A':
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input().strip()
x = s[0]
for i in range(1,len(s)):
    if s[i]=="C" and s[i-1]=="B":
        x = x[:-1]+"T"
    else:
        x += s[i]
cnt = 0
ca = 0
for i in range(len(x)):
    if x[i]=="A":
        ca += 1
    elif x[i]=="T":
        cnt += ca
    elif x[i]=="B" or x[i]=="C":
        ca = 0
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
res = []
ind = 0
len_s = len(s)
string = ""
while ind < len_s:
    _s = s[ind]
    if _s == "A":
        string += "A"
        ind += 1
    elif _s == "B" and ind + 1 < len_s and s[ind + 1] == "C":
        string += "BC"
        ind += 2
    else:
        if len(string) > 0:
            res.append(string)
            string = ""
        ind += 1
if len(string) > 0:
    res.append(string)


def count(string):
    cnt = 0
    ind = len(string) - 1
    res = 0
    while ind >= 0:
        s = string[ind]
        if s == "C":
            ind -= 2
            cnt += 1
        else:
            ind -= 1
            res += cnt
    return res

ans = 0
for r in res:
    ans += count(r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
res = re.finditer(r"(A*(BC)+)+", s)

def count(string):
    cnt = 0
    ind = len(string) - 1
    res = 0
    while ind >= 0:
        s = string[ind]
        if s == "C":
            ind -= 2
            cnt += 1
        else:
            ind -= 1
            res += cnt
    return res

ans = 0
for r in res:
    ans += count(r.group())
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
t=["","",""]
s=t+s+t

c=0
i=0
while(len(s)>i):
    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"
        i-=2
        c+=1
    i+=1

print(c)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

ans = 0
i = 0
while i+2 < len(s):
    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'
        i = max(0,i-1)
    else:
        i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

ans = 0
while True:
    i = 0
    found = False
    while i+2 < len(s):
        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'
            i += 2
            found = True
        else:
            i += 1
    if not found:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ans=0
while(1):
    for i in range(1,len(s)-1):
        if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
            ans+=1
            s=s[:i-1]+"BCA"+s[i+2:]
    if not "ABC" in s:
        print(ans)
        exit()

Submission status: Time Limit Exceeded

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

    if not "ABC" in s:
        print(ans)
        exit()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = (stdin.readline().rstrip())
ans = 0
while s.count("ABC") != 0:
    ans += s.count("ABC")
    s = s.replace("ABC","BCA")
print(ans)

Submission status: Time Limit Exceeded

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 = rs()
answer = 0
while True:
    x = S.count('ABC')
    if x > 0:
        answer += x
        S = S.replace('ABC', 'BCA')
    else:
        break
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = list(s.replace("BC", "D"))
pos = 0
str_len = len(s)
count = 0
state = 0
seq_a = 0
seq_d = 0


while pos < str_len:
    if state == 0:
        if s[pos] == "A":
            state = 1
            seq_a = 1
        pos += 1
    elif state == 1:
        if s[pos] == "A":
            state = 1
            seq_a += 1
            pos += 1
        elif s[pos] == "D":
            while pos < str_len and s[pos] == "D":
                seq_d += 1
                pos += 1
            tmp_pos = pos - seq_a
#            for i in range(tmp_pos-seq_d, tmp_pos):
#                s[i] = "D"
            for i in range(tmp_pos, pos):
                s[i] = "A"
            pos = tmp_pos
            count += seq_a * seq_d
            state = 0
            seq_a = 0
            seq_d = 0
        else:
            state = 0
            pos += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = list(s.replace("BC", "D"))
pos = 0
str_len = len(s)
count = 0
state = 0
seq_a = 0
seq_d = 0


while pos < str_len:
    if state == 0:
        if s[pos] == "A":
            state = 1
            seq_a = 1
        pos += 1
    elif state == 1:
        if s[pos] == "A":
            state = 1
            seq_a += 1
            pos += 1
        elif s[pos] == "D":
            while pos < str_len and s[pos] == "D":
                seq_d += 1
                pos += 1
            tmp_pos = pos - seq_a
            for i in range(tmp_pos-seq_d, tmp_pos):
                s[i] = "D"
            for i in range(tmp_pos, pos):
                s[i] = "A"
            pos = tmp_pos
            count += seq_a * seq_d
            state = 0
            seq_a = 0
            seq_d = 0
#            print(pos, s)
        else:
            state = 0
            pos += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
pos = 0
cnt = 0
acnt = 0
if len(s) < 3:
    print(0)
else:
    while(True):
        if s[pos:pos+3] == "ABC":
            s = s[:pos-acnt] + "BCA"+ s[pos-acnt:pos] + s[pos+3:]
            pos += 2
            cnt += acnt+1
            # print(s, cnt)
        else:
            if s[pos] == "A":
                acnt += 1
            else:
                acnt = 0
            pos += 1
        if pos >= len(s)-2:
            break
    print(cnt)

Submission status: Time Limit Exceeded

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

while pos < str_len:
    if state == 0:
        if s[pos] == "A":
            state = 1
            seq_a = 1
        pos += 1
    elif state == 1:
        if s[pos] == "B":
            state = 2
        elif s[pos] == "A":
            state = 1
            seq_a += 1
        else:
            state = 0
        pos += 1
    elif state == 2:
        if s[pos] == "C":
            for i in range(pos - seq_a + 1, pos + 1):
                s[i] = "A"
            s[pos - seq_a - 1] = "B"
            s[pos - seq_a] = "C"
            pos = pos - seq_a + 1
            if pos < 0:
                pos = 0
            count += seq_a
            state = 0
            seq_a = 0
#            print(pos, s)
        elif s[pos] == "A":
            state = 1
            seq_a = 1
            pos += 1
        else:
            state = 0
            pos += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
pos = 0
cnt = 0
acnt = 0
if len(s) < 3:
    print(0)
else:
    while(True):
        if s[pos:pos+3] == "ABC":
            s = s[:pos-acnt] + "BCA"+ s[pos-acnt:pos] + s[pos+3:]
            if pos > 0:
                pos -= acnt+1
            cnt += acnt+1
            acnt = 0
            # print(s, cnt)
        else:
            if s[pos] == "A":
                acnt += 1
            else:
                acnt = 0
            pos += 1
        if pos >= len(s)-2:
            break
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
pos = 0
str_len = len(s)
count = 0
state = 0

while pos < str_len:
    if state == 0:
        if s[pos] == "A":
            state = 1
        pos += 1
    elif state == 1:
        if s[pos] == "B":
            state = 2
        elif s[pos] == "A":
            state = 1
        else:
            state = 0
        pos += 1
    elif state == 2:
        if s[pos] == "C":
            s[pos-2:pos+1] = "BCA"
            pos -= 3
            if pos < 0:
                pos = 0
            count += 1
            state = 0
        elif s[pos] == "A":
            state = 1
            pos += 1
        else:
            state = 0
            pos += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
pos = 0
cnt = 0
if len(s) < 3:
    print(0)
else:
    while(True):
        if s[pos:pos+3] == "ABC":
            s = s[:pos] + "BCA" + s[pos+3:]
            if pos > 0:
                pos -= 1
            cnt += 1
        else:
            if s[pos+1:pos+3] == "AB":
                pos += 1
            elif s[pos+2:pos+3] == "A":
                pos += 2
            else:
                pos += 3
        if pos >= len(s)-2:
            break
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
pos = 0
cnt = 0
if len(s) < 3:
    print(0)
else:
    while(True):
        if s[pos:pos+3] == "ABC":
            s = s[:pos] + "BCA" + s[pos+3:]
            if pos > 0:
                pos -= 1
            cnt += 1
        else:
            pos += 1
        if pos >= len(s)-2:
            break
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
pos = 0
cnt = 0
if len(s) < 3:
    print(0)
else:
    # print(type(s))
    # print(type(s[0:3]))
    # print(type("ABC"))
    while(True):
        if s[pos:pos+3] == "ABC":
            s = s[:pos] + "BCA" + s[pos+3:]
            pos = 0
            cnt += 1
            # print(s)
        else:
            pos += 1
        if pos >= len(s)-2:
            break
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()

ans = 0
while "ABC" in s:
    ans += len(s.split("ABC"))-1
    s = s.replace("ABC", "BCA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
w=list(input())
now=0
num=len(w)
w.append(" ")
count=0
while now<num:
  if w[now]!="A":
    now+=1
  else:
    if w[now+1]!="B":
      now+=1
    else:
      if w[now+2]!="C":
        now+=1
      else:
        w[now]="B"
        w[now+1]="C"
        w[now+2]="A"
        count+=1
        back=now-1
        b=True
        while back>0 and b==True:
          if w[back]!="A":
            b=False
          else:
            w[back]="B"
            w[back+1]="C"
            w[back+2]="A"
            count+=1
            back-=1
        now+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
w=list(input())
now=0
num=len(w)
w.append(" ")
count=0
while now<num:
  if w[now]!="A":
    now+=1
  else:
    if w[now+1]!="B":
      now+=1
    else:
      if w[now+2]!="C":
        now+=1
      else:
        w[now]="B"
        w[now+1]="C"
        w[now+2]="A"
        count+=1
        back=now-1
        b=True
        while back>0 and b==True:
          if w[back]!="A":
            b=False
          else:
            w[back]="B"
            w[back+1]="C"
            w[back+2]="A"
            count+=1
            back-=1
        now+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
w=list(input())
now=0
num=len(w)
w.append(" ")
count=0
while now<num:
  if w[now]!="A":
    now+=1
  else:
    if w[now+1]!="B":
      now+=1
    else:
      if w[now+2]!="C":
        now+=1
      else:
        w[now]="B"
        w[now+1]="C"
        w[now+2]="A"
        count+=1
        back=now-1
        b=True
        while back>0 and b==True:
          if w[back]!="A":
            b=False
          else:
            w[back]="B"
            w[back+1]="C"
            w[back+2]="A"
            count+=1
            back-=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    s=input()
    s2=list(s.replace('BC','D'))
    N=len(s2)
    i=0
    ans=0
    if N>=2:
        if s2[i] == 'A' and s2[i+1] == 'D':
            s2[i],s2[i+1]='D','A'
            ans+=1
        while True:
            while True:
                i+=1
                if i >= N-1:
                    print(ans)
                    exit()
                if s2[i] == 'A' and s2[i+1] == 'D':
                    break
            s2[i],s2[i+1]='D','A'
            if i!=0 and s2[i-1]=='A':
                i-=2
            ans+=1
    else:
        print(ans)
        
        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():    
    s=list(input())
    N=len(s)
    i=0
    ans=0
    if N>=3:
        if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
            s[i],s[i+1],s[i+2]='B','C','A'
            i+=1
            ans+=1
        while True:
            while True:
                i+=1
                if i >= N-2:
                    print(ans)
                    exit()
                if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
                    break
            s[i],s[i+1],s[i+2]='B','C','A'
            if i!=0 and s[i-1]=='A':
                i-=2
            else:
                i+=1
            ans+=1
    else:
        print(ans)
        
        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    def checkABC(x):
        if s[x]=='A' and s[x+1]=='B' and s[x+2]=='C':
            s[x],s[x+1],s[x+2]='B','C','A'
            return True
        else:
            return False
    
    s=list(input())
    N=len(s)
    i=0
    ans=0
    if N>=3:
        while True:
            if checkABC(i) == True:
                if i!=0 and s[i-1]=='A':
                    i-=2
                else:
                    i+=1
                ans+=1
            while True:
                i+=1
                if i >= N-2:
                    print(ans)
                    exit()
                if s[i] == 'A':
                    break
    else:
        print(ans)
        
        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    def checkABC(x):
        if s[x]=='A' and s[x+1]=='B' and s[x+2]=='C':
            s[x],s[x+1],s[x+2]='B','C','A'
            return True
        else:
            return False
    
    s=list(input())
    N=len(s)
    i=0
    ans=0
    while True:
        if i >= N-2:
            break
        if checkABC(i) == True:
            if i!=0 and s[i-1]=='A':
                i-=2
            else:
                i+=1
            ans+=1
        i+=1
    print(ans)
        
        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def checkABC(x):
    if s[x]=='A' and s[x+1]=='B' and s[x+2]=='C':
        s[x],s[x+1],s[x+2]='B','C','A'
        return True
    else:
        return False

def main():
    global s
    s=list(input())
    N=len(s)
    i=0
    ans=0
    while True:
        if i >= N-2:
            break
        if checkABC(i) == True:
            i=min(i-2,-1)
            ans+=1
        i+=1
    print(ans)
        
        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

i = 0
count = 0
if len(s) >=3:
  while i <= len(s)-2:
    if s[i:i+3] == list("ABC"):
      s[i] = "B"
      s[i+1] = "C"
      s[i+2] = "A"
      count += 1
      if i != 0:
        i -= 1
    else:
      i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
while "ABC" in s:
  ans += s.count("ABC")
  s = s.replace("ABC", "BCA")
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
lenS = len(S)
ans = 0
flag = True

while flag is True:
    i = S.find('ABC')
    if i == -1:
      falg = False
      break
      
    num_l_A = 0
    if i>=1:
      for ia in range(i-1,-1,-1):
        if S[ia] == 'A':
          num_l_A += 1
        else:break
    num_r_BC = 0
    if i+3 < lenS:
      for ibc in range(i+3,lenS):
        if S[ibc:ibc+1] == 'BC': num_r_BC += 1
        else:break
    ans += 1 + num_l_A + num_r_BC
    S = S[:i-num_l_A] + 'BC'*(1+num_r_BC) + 'A'*(1+num_l_A) + S[i+num_r_BC*2+3:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
pos = s.find("ABCBC")
while(pos != -1):
    s = s.replace("ABCBC", "A", 1)
    ans += 2
    pos = s.find("ABCBC")

pos = s.find("ABC")
while(pos != -1):
    s = s.replace("ABC", "A", 1)
    cur, acnt = pos, 0
    while(cur > 0 and s[cur-1] == "A"):
        acnt += 1
        cur -= 1
    ans += 1+acnt
    pos = s.find("ABC")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import heapq

s = input()
s += " "

S = ""

for i in range(len(s)-1):
    if s[i] == "B" and s[i+1] == "C":
        S += "Z"

    elif i > 0 and s[i] == "C" and s[i-1] == "B":
        continue
    else:
        S += s[i]

#print (S)
la = deque([])
ans = 0
for i in range(len(S)):

    if S[i] == "A":
        la.append(i)

    elif S[i] == "Z" and len(la) > 0:
        lastA = la.popleft()
        ans += i - lastA
        la.append(i)

    else:
        la = deque()

print (ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import heapq

s = input()
s += " "

S = ""

for i in range(len(s)-1):
    if s[i] == "B" and s[i+1] == "C":
        S += "Z"

    elif i > 0 and s[i] == "C" and s[i-1] == "B":
        continue
    else:
        S += s[i]

#print (S)
la = []
ans = 0
for i in range(len(S)):

    if S[i] == "A":
        heapq.heappush(la,i)

    elif S[i] == "Z" and len(la) > 0:
        lastA = heapq.heappop(la)
        ans += i - lastA
        heapq.heappush(la,i)

    else:
        la = []

print (ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

N=len(s)

f=True
cnt=0
while f:
    f=False
    for i in range(2,N):
        if "".join(s[i-2:i+1])=="ABC":
            s[i-2]="B"
            s[i-1]="C"
            s[i]="A"
            cnt+=1
            f=True
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

N=len(s)

f=True
cnt=0
while f:
    f=False
    for i in range(2,N):
        if "".join(s[i-2:i+1])=="ABC":
            s[i-2]="B"
            s[i-1]="C"
            s[i]="A"
            cnt+=1
            f=True
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
count=0
for i in range(len(s)-2):
    if s[i:i+3]==['A','B','C']:
        count+=1
        s[i]='B'
        s[i+1]='C'
        s[i+2]='A'
        for j in range(1,len(s)):
            if i-j<0:
                break
            if s[i-j]=='A':
                count+=1
                s[i-j]='B'
                s[i-j+1]='C'
                s[i-j+2]='A'
                
            
            else:
                break
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
t = ""

i = 0
while i < n-1:
    if s[i:i+2] == "BC":
        t += "D"
        i += 2
    else:
        t += s[i]
        i += 1

# print(t)

m = len(t)
i = 0
p = 0
ans = 0
while i < m:
    while i < m and (t[i] == "A" or t[i] == "D"):
        if t[i] == "A":
            p += 1
        else:
            ans += p
        i += 1
    p = 0
    i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())

i=0
while i<len(S)-1:
  if S[i]=="B" and S[i+1]=="C":
    S[i]="D"
    S.pop(i+1)
  i+=1
    
t=[]
nl=[]
for i in S:
  if i=="A" or i=="D":
    nl.append(i)
  else:
    t.append(nl)
    nl=[]
t.append(nl)

T=list(filter(lambda x:len(x)>0,t))

ans=0
for i in T:
  dn=0
  for j in range(len(i)):
    if i[j]=="D":
      ans+=j-dn
      dn+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()

ans=0
while "ABC" in S:
  ind=S.index("ABC")
  S=list(S)
  S[ind]="B"
  S[ind+1]="C"
  S[ind+2]="A"
  S="".join(S)
  ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
i = 0

for i in range(len(s)-1):
    if s[i] == 'B' and s[i+1] == 'C':
        s[i] = 'D'
        s.pop(i+1)
    if i >= len(s) - 2:
        break

a = 0
ans = 0
for i in range(len(s)):
    if s[i] == 'A':
        a += 1
    elif s[i] == 'D':
        ans += a
    else:
        a = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#%%
s = list(input())
ans = 0
i = 0

while True:
    if i < 0 :
        i += 1
    elif len(s)-2 <= i:
        break
    else:
        if s[i] == 'A':
            if s[i+1] == 'B' and s[i+2] == 'C':
                ans += 1
                s[i], s[i+1], s[i+2] = 'B', 'C', 'A'
                i -= 1
            else:
                i += 1
        else:
            i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#%%
s = list(input())
ans = 0
i = 0

while True:
    if i < 0 :
        i += 1
    elif len(s)-2 <= i:
        break
    else:
        if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
            ans += 1
            s[i], s[i+1], s[i+2] = 'B', 'C', 'A'
            i -= 1
        else:
            i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
s=list(input().replace("BC","Z"))
ans=0

def seisan(list):
    count=0
    N=len(list)-1
    for index,content in enumerate(list):
        if content=="A":
            count+=(N-index)
            N-=1
    return count

temp=[]
while s:
    this=s.pop(0)
    if this=="B" or this=="C":
        ans+=seisan(temp)
        temp.clear()
    else:
        temp.append(this)

print(ans+seisan(temp))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
s=input()
s=list(s.replace('BC','Z'))
ans=0
 
def seisan(list):
    count=0
    N=len(list)-1
    for index,content in enumerate(list):
        if content=="A":
            count+=(N-index)
            N-=1
    return count

temp=[]
while s:
    this=s.pop(0)
    if this=="B" or this=="C":
        ans+=seisan(temp)
        temp.clear()
    else:
        temp.append(this)

print(ans+seisan(temp))

Submission status: Time Limit Exceeded

Solution code:
S = list(input().replace("BC", "D"))
l = []
ans = 0
for i in range(len(S)-1):
    if(S[i] == "A" and S[i+1] == "D"):
        l.append(i+1)
        l.append(i-1)
        S[i] = "D"
        S[i+1] = "A"
        ans += 1

while(len(l)):
    target = l[0]
    if(target < len(S) - 1 and target >= 0):
        if(S[target] == "A" and S[target+1] == "D"):
            ans += 1
            S[target+1] = "A"
            S[target] = "D"
            l.append(target+1)
            l.append(target-1)
    l.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
# Your code here!
s=list(input())

temp=0
ans=0
next="Z"
while s:
    this=s.pop(0)#正直一個前の値をthatに入れてもよかったと思う
    if this == "A":
        temp+=1
    elif this == "B":
        if len(s)==0:#s.popできるか調べる
            break
        next=s.pop(0)
        if next=="C":
            ans+=temp
            s=["A"]*temp+s
        elif next=="A":#ここ繋ぎがうまく作れんかった
            temp=1
            continue
        temp=0
    elif this == "C":#虚無
        temp=0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    ans = 0
    while 1:
        i = S.find("ABC")
        if i < 0:
            break
        li = 0
        for l in range(i-1,-1,-1):
            if S[l] == "A":
                li += 1
            else:
                break
        ri = 0
        for r in range(i+3,len(S)-1,2):
            if S[r] == "B" and S[r+1] == "C":
                ri += 1
            else:
                break
        x = min(li, ri)
        S = S[:i-x] + "BC"*(x+1) + "A"*(x+1) + S[i+3 + x*2:]
        ans += 1 if x == 0 else (x+1)**2
        
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline

def main():
    S = input().strip()
    ans = 0
    while 1:
        c = S.count("ABC")
        if c == 0:
            break
        else:
            ans += c
            S = S.replace("ABC", "BCA")
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
x,n,ans = [],0,0
for i in input():
    if i=="A": x.append(0)
    elif i=="B": x.append(1)
    else: x.append(2)
while n<len(x)-2:
    if x[n]==0 and x[n+1]==1 and x[n+2]==2:
        x[n],x[n+1],x[n+2] = 1,2,0
        n = max(0,n-1)
        ans+=1
    else: n+=1
print(ans)