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

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    S = in_s()
    N = len(S)

    def abc_count():

        count = 0
        fa, fb = False, False
        for i in range(N):
            if S[i] == 'A':
                fa = True
            elif fa and S[i] == 'B':
                fb = True
            elif fb and S[i] == 'C':
                count += 1
                fa, fb = False, False
            else:
                fa, fb = False, False

        return count

    ans = 0
    while True:
        t = abc_count()
        if t == 0:
            break
        else:
            ans += t
            S = S.replace('ABC', 'BCA')

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
a = list(input())
if len(a) <= 2:
    print(0)
else:
    count = 0
    c = 0
    i = 0
    while True:
        if a[i:i+3] == ["A","B","C"]:
            a[i:i+3] = ["B","C","A"]
            count += 1
            c += 1
            i += 3
        else:
            i += 1
        if i > len(a) - 3:
            if c == 0:
                break
            else:
                i =0
                c = 0
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

S = 'S' + input()
new_s = list(S.replace('BC', 'D'))


cnt = 0
i = 1
while i < len(new_s):
    if new_s[i-1] == 'A' and  new_s[i] == 'D':
        cnt += 1
        new_s[i-1] = 'D'
        new_s[i] = 'A'
        i -= 2
    else:
        i += 1


print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import re

S = 'S' + input()
new_s = list(S.replace('BC', 'D'))


cnt = 0
i = 1
while i < len(new_s):
    if new_s[i-1] == 'A' and  new_s[i] == 'D':
        cnt += 1
        new_s[i-1] = 'D'
        new_s[i] = 'A'
        i -= 2
    else:
        i += 1
    new_s = list(''.join(new_s).replace('BC', 'D'))

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = list('S'+input())
#print(S)

cnt = 0
i = 2
while i < len(S):
    if S[i-2] == 'A' and S[i-1] == 'B' and  S[i] == 'C':
        cnt += 1
        S[i-2] = 'B'
        S[i-1] = 'C'
        S[i] = 'A'
        i -= 3
    else:
        i += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))+[3,3,3]
N=len(S)


if N<=2:
    print(0)
    sys.exit()

T=0
F=True
while F:
    j=0
    F=False
    while j<=N-3:
        k=j
        while (S[k]==0) and (S[k+1]==1) and (S[k+2]==2):
            k+=3

        for i in range(j,k):
            S[i]=(S[i]+1)%3
            F=True
            
        T+=(k-j)//3
        j=k+1
print(T)

Submission status: Time Limit Exceeded

Solution code:
import sys

def f(s):
    if s=="A":
        return 0
    elif s=="B":
        return 1
    else:
        return 2

S=list(map(f,input()))
N=len(S)

if N<=2:
    print(0)
    sys.exit()

T=0
F=True
while F:
    F=False
    for i in range(N-2):
        if (S[i]==0) and (S[i+1]==1) and (S[i+2]==2):
            T+=1
            S[i],S[i+1],S[i+2]=1,2,0
            F=F or True

print(T)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=''
skip=False
for i in range(len(s)-1):
    if skip:
        skip=False
        continue
    if s[i:i+2]=='BC':
        t+='D'
        skip=True
    else:
        t+=s[i]

ans,cnt=0,0
for i in range(len(t)):
    if t[i]=='A':
        cnt+=1
    if t[i] in ['B','C']:
        cnt=0
    if t[i]=='D':
        ans+=cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    S = list(input())
    Sint = [1 if c == 'A' else 2 if c=='B' else 3 for c in S]
    N = len(S)

    ans = 0
    i = 0
    while i < N-2:
        if Sint[i] == 1 and Sint[i+1] == 2 and Sint[i+2] == 3:
            ans += 1
            Sint[i],Sint[i+1],Sint[i+2] = 2,3,1
            for j in range(i-1,-1,-1):
                if Sint[j] == 1:
                    ans += 1
                    Sint[j],Sint[j+1],Sint[j+2] = 2,3,1
                else:
                    break
            i += 2
        else:
            i += 1
    print(ans)



    
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    S = list(input())
    Sint = [1 if c == 'A' else 2 if c=='B' else 3 for c in S]
    N = len(S)

    ans = 0
    for i in range(N-2):
        if Sint[i] == 1 and Sint[i+1] == 2 and Sint[i+2] == 3:
            ans += 1
            Sint[i],Sint[i+1],Sint[i+2] = 2,3,1
            for j in range(i-1,-1,-1):
                if Sint[j] == 1:
                    ans += 1
                    Sint[j],Sint[j+1],Sint[j+2] = 2,3,1
                else:
                    break
    print(ans)



    
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    S = list(input())
    Sint = [1 if c == 'A' else 2 if c=='B' else 3 for c in S]
    N = len(S)

    ans = 0
    for i in range(N-2):
        if Sint[i] == 1 and Sint[i+1] == 2 and Sint[i+2] == 3:
            ans += 1
            Sint[i],Sint[i+1],Sint[i+2] = 2,3,1
            for j in range(i-1,-1,-1):
                if Sint[j] == 1 and Sint[j+1] == 2 and Sint[j+2] == 3:
                    ans += 1
                    Sint[j],Sint[j+1],Sint[j+2] = 2,3,1
                else:
                    break
    print(ans)



    
main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
stat = 0
a = 0
bc = 0
abc = 0
ans = 0
def count():
    global ans, a, bc, abc
    newa = a+abc
    cnt = abc
    while cnt>0:
        ans += cnt
        cnt -= 1
        if a>0:
            a -= 1
            cnt += 1
        if bc>0:
            bc -= 1
            cnt += 1
    a = newa
    bc = 0
    abc = 0
i = 0
while i<n:
    if stat==0:
        if s[i:i+3]=='ABC':
            a = 0
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a = 1
            stat = 1
            i += 1
        else:
            i += 1
    elif stat==1:
        if s[i:i+3]=='ABC':
            abc = 1
            stat = 2
            i += 3
        elif s[i]=='A':
            a += 1
            i += 1
        else:
            stat = 0
    elif stat==2:
        if s[i:i+3]=='ABC':
            abc += 1
            i += 3
        elif s[i:i+2]=='BC':
            bc = 1
            stat = 3
            i += 2
        else:
            count()
            stat = 1
    else:
        if s[i:i+2]=='BC':
            bc += 1
            i += 2
        else:
            count()
            stat = 1
if stat>=2:
    count()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
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:
s=list(map(ord,list(input())))

c=0
for i in range(len(s)):
    if s[i:i+3]==[65,66,67]:
        j=1
        ci=1
        while s[i-j]==65:
            ci+=1
            j+=1
            if i-j<0:
                break
        s[i-j+1]=66
        s[i-j+2]=67
        s[i-j+3:i+3]=[65 for i in range(j)]
        c+=ci        

print(c)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

c=0
for i in range(len(s)):
    if s[i:i+3]==["A","B","C"]:
        j=1
        ci=1
        while s[i-j]=="A":
            ci+=1
            j+=1
            if i-j<0:
                break
        s[i-j+1]="B"
        s[i-j+2]="C"
        s[i-j+3:i+3]=["A" for i in range(j)]
        c+=ci        

print(c)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

c=0
for i in range(len(s)):
    if s[i:i+3]==["A","B","C"]:
        j=1
        ci=1
        while s[i-j]=="A":
            ci+=1
            j+=1
            if i-j<0:
                break
        s[i-j+1]="B"
        s[i-j+2]="C"
        s[i-j+3:i+3]=["A" for i in range(j)]
        c+=ci        

print(c)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

c=0
for i in range(len(s)):
    j=i
    while s[j:j+3]==["A","B","C"]:
        s[j:j+3]=["B","C","A"]
        c+=1
        j-=1
        if j==-1:
            break

print(c)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

c=0
for i in range(len(s)):
    j=i
    while s[j:j+3]==["A","B","C"]:
        s[j:j+3]=["B","C","A"]
        c+=1
        j-=1
        if j==-1:
            break

print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
# import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())
    target = ["A", "B", "C"]
    next = ["B", "C", "A"]

    idx = 0
    ans = 0
    while len(s) - 3 >= idx:
        flg = True
        for i in range(3):
            if s[idx + i] != target[i]:
                flg = False
                break

        if flg:
            for i in range(3):
                s[idx + i] = next[i]
            idx = max(0, idx - 1)
            ans += 1
            continue
        else:
            idx += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
while True:
    if "ABC" in s:
        cnt += s.count("ABC")
        s = s.replace("ABC", "BCA")
    else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=input()
s=s.replace("BC","D")
s=deque(list( s ))
t=deque([]) #
cnt=0

#print(t,s)

while s!=deque([]):
  x=s.popleft()
  if t==deque([]):
    t.append(x)
  else:
    if t[-1]=="A" and x=="D":
      tmp=t.pop()
      s.appendleft(tmp)
      s.appendleft(x)
      cnt += 1
    else:
      t.append(x)
      
#print(t,s)        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=deque(list( input() ))
t=deque([]) #

cnt=0

while s!=deque([]):
  x=s.popleft()
  if len(t)>=2 and x=="C":
    q=t.pop()
    p=t.pop()
    if p=="A" and q=="B":
      cnt += 1
      t.append("B")
      s.appendleft("A")
      s.appendleft("C")
    else:
      t.append(p)
      t.append(q)
      t.append(x)
      
  else:
    t.append(x)
        
#print(t,s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=deque(list( input() ))
t=deque([]) #

cnt=0
while s!=deque([]):
  x=s.popleft()
  if len(t)>=2 and x=="C":
    q=t.pop()
    p=t.pop()
    if p=="A" and q=="B":
      cnt += 1
      s.appendleft("A")
      s.appendleft("C")
      s.appendleft("B")
    else:
      t.append(p)
      t.append(q)
      t.append(x)
      
  else:
    t.append(x)
        
#print(t,s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=deque(list( input() ))
t=deque([]) #

cnt=0
while s!=deque([]):
  x=s.popleft()
  if len(t)>=2 and x=="C":
    if t[-2]=="A" and t[-1]=="B":
      cnt += 1
      t.pop()
      t.pop()
      s.appendleft("A")
      s.appendleft("C")
      s.appendleft("B")
    else:
      t.append(x)
  else:
    t.append(x)
        
#print(t,s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=deque(list( input() ))
t=deque([]) #

cnt=0
while len(s)>0:
  x=s.popleft()
  if len(t)>=2 and x=="C":
    if t[-2]=="A" and t[-1]=="B":
      cnt += 1
      t.pop()
      t.pop()
      s.appendleft("A")
      s.appendleft("C")
      s.appendleft("B")
    else:
      t.append(x)
  else:
    t.append(x)
        
#print(t,s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s=deque(list( input() ))
t=deque([]) #

#print(t,s)

cnt=0
while len(s)>0:
  x=s.popleft()
  if len(t)>=2 and x=="C":
    if t[-2]=="A" and t[-1]=="B":
      cnt += 1
      t.pop()
      t.pop()
      s.appendleft("A")
      s.appendleft("C")
      s.appendleft("B")
    else:
      t.append(x)
  else:
    t.append(x)
        
#print(t,s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
cnt = 0
rslt = S[::-1]

while rslt.find('CBA')!=-1:
    rslt = rslt.replace('CBA', 'ACB', 1)
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# 解説
# ABC -> CBA
# BCは増えない&離れない
# AD -> DAを考える

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
  
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
  
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

S = input()
S = S.replace("BC", "D")
# print(S)

# ADからなる連続領域に分ける
ranges = []
i = 0
while i < len(S):
    while i < len(S) and S[i] != 'A' and S[i] != 'D':
        i += 1
    j = i
    while j < len(S) and (S[j] == 'A' or S[j] == 'D'):
        j += 1
    ranges.append((i, j - 1))
    i = j + 1

# 各AD列の点灯数を求める
total = 0
for (i, j) in ranges:
    # print(i, j, S[i:j+1])
    ans = 0
    for i in range(i, j + 1):
        for j in range(i + 1, j + 1):
            if S[i] == 'A' and S[j] == 'D':
                ans += 1
            # print(i, j, S[i], S[j])
    total += ans
print(total)

Submission status: Time Limit Exceeded

Solution code:
# https://atcoder.jp/contests/agc034/tasks/agc034_b

S = input()
count = 0
while "ABC" in S:
    # print(S)
    count += S.count("ABC")
    S = S.replace("ABC", "BCA")
print(count)

Submission status: Time Limit Exceeded

Solution code:
def f(c):
    return ord(c) - ord("A")


def main():
    s = list(map(f, input()))
    ln = len(s)

    abc = [0, 1, 2]
    bca = [1, 2, 0]

    ans = 0
    for i in range(ln - 2):
        if s[i:i+3] == abc:
            s[i:i+3] = bca
            ans += 1
            j = i - 1
            while j >= 0 and s[j:j+3] == abc:
                s[j:j+3] = bca
                ans += 1
                j -= 1
            i += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(input())
    ln = len(s)

    abc = ["A", "B", "C"]
    bca = ["B", "C", "A"]

    ans = 0
    for i in range(ln - 2):
        if s[i:i+3] == abc:
            s[i:i+3] = bca
            ans += 1
            j = i - 1
            while j >= 0 and s[j:j+3] == abc:
                s[j:j+3] = bca
                ans += 1
                j -= 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
ln = len(s)

abc = ["A", "B", "C"]
bca = ["B", "C", "A"]

ans = 0
for i in range(ln - 2):
    if s[i:i+3] == abc:
        s[i:i+3] = bca
        ans += 1
        j = i - 1
        while j >= 0 and s[j:j+3] == abc:
            s[j:j+3] = bca
            ans += 1
            j -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=input()
count=0

while n.count('ABC')>0:
    count+=n.count('ABC')
    n=n.replace('ABC','BCA')
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
cnt = 0
flg = 1

while flg == 1:
    flg = 0
    for i in range(N-1,1,-1):
        if S[i-2] == 'A' and S[i-1] == 'B' and S[i] == 'C':
            cnt += 1
            S[i-2] = 'B'
            S[i-1] = 'C'
            S[i] = 'A'
            flg = 1
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
cnt = 0
flg = 1
A = []
for i in range(N):
    if S[i] == 'A':
        A.append(i)


while flg == 1:
    flg = 0
    B = []
    for i in A:
        if i < N-2:
            if S[i] == 'A' and S[i+1] == 'B' and S[i+2] == 'C':
                cnt += 1
                S[i] = 'B'
                S[i+1] = 'C'
                S[i+2] = 'A'
                B.append(i+2)
                flg = 1
            else:
                B.append(i)
    A = B
    # print(S)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
cnt = 0
flg = 1
for i in range(N):
    if S[i] == 'A':
        S[i] = 0
    elif S[i] == 'B':
        S[i] = 1
    else:
        S[i] = 2

while flg == 1:
    flg = 0
    for i in range(N-2):
        if S[i] == 0 and S[i+1] == 1 and S[i+2] == 2:
            cnt += 1
            S[i] = 1
            S[i+1] = 2
            S[i+2] = 0
            flg = 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

while s.count("ABC")!=0:
    s=s.replace("ABC", "BCA")
    count2+=s.count("ABC")
print(count+count2)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
n=len(s)
ans=0
if 'ABC' not in s:
    print(0)
elif 'ABC' in s:
        p=s.count('ABC')
        while 'ABC' in s:
            s=s.replace('ABC','BCA')
            q=s.count('ABC')
             
            ans=ans+q
            
        print(p+ans)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
n=len(s)
ans=0
if 'ABC' not in s:
    print(0)
elif 'ABC' in s:
        p=s.count('ABC')
        while n>0:
            s=s.replace('ABC','BCA')
            q=s.count('ABC')
            n=n-1 
            ans=ans+q
            
        print(p+ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
n=len(s)

cnt=0
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"
        cnt+=1
        if i!=0:
            i-=1
    else:
        i+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
# ABCACCBABCBCAABCB
n=len(s)
ans=0
na,nb,nc=0,0,0

i=0
while i<n:
  if s[i]=='A':
    na+=1
    i+=1
  elif s[i]=='B' and i<n-1 and s[i+1]=='C' and na>0:
    ans+=na
    s[i-na]='B'
    s[i+1-na]='c'
    for j in range(i+2-na,i+2):
      s[j]='A'
    i+=2
  else:
    na=0
    i+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
s=s.replace("BC","D")
ans=0
x=s.count("AD")
while x>0:
    ans+=x
    s=s.replace("AD","DA")
    x=s.count("AD")
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
BCidxs = []
i = 0
while i < N:
  if "".join(S[i:i+2]) == "BC":
    BCidxs.append(i)
  i += 1
ans = 0
L = len(BCidxs)
i = 0
while i < L:
  idx = BCidxs[i]
  if "".join(S[idx-1:idx+2]) == "ABC":
    ans += 1
    S[idx-1] = "B"
    S[idx] = "C"
    S[idx+1] = "A"
    BCidxs[i] -= 1
  else:
    i += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
Aidxs = []
i = 0
while i < N:
  if S[i] == "A":
    Aidxs.append(i)
  i += 1
ans = 0
L = len(Aidxs)
i = 0
while i < L:
  idx = Aidxs[i]
  if "".join(S[idx:idx+3]) == "ABC":
    ans += 1
    S[idx] = "B"
    S[idx+1] = "C"
    S[idx+2] = "A"
    Aidxs[i] += 2
    i = max(0, i-2)
  else:
    i += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import re
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    S = input()

    ans = 0
    res = re.findall(r'A*ABC(?:A|BC)*', S)
    for r in res:
        r = re.sub(r'(BC)', 'X', r)
        cnt = 0
        for c in r[::-1]:
            if c == 'X':
                cnt += 1
            else:
                ans += cnt

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
ans=0

re_abc=re.compile(r'ABC')
while(True):

    abc_num=len(re.findall(re_abc,s))
    if(abc_num==0):
        break
    else:
        ans+=abc_num
        s=re.sub(re_abc,'BCA',s)
#    print(s)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
while True:
    a = s.replace("ABC", "BCA", 1)
    if s == a:
        break
    else:
        s = a
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
cnt = 0
while 'ABC' in s:
    s = re.sub(r'ABC', 'BCA', s, count=1)
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

ans = 0
while True:
    t = s.count('ABC')
    if t == 0:
        break
    ans += t
    s = s.replace('ABC', 'BCA')
print(ans)

Submission status: Time Limit Exceeded

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

s_tmp = list(s)
ans = 0
for i in range(N - 2):
    if s_tmp[i] == 'A' and s_tmp[i + 1] == 'B' and s_tmp[i + 2] == 'C':
        for i_back in range(i):
            if s_tmp[i - 1 - i_back] == 'A':
                continue
            else:
                break
        else:
            i_back = i
        #
        for j in range(i - i_back, i + 3):
            if j == i - i_back:
                s_tmp[j] = 'B'
            elif j == i - i_back + 1:
                s_tmp[j] = 'C'
            else:
                s_tmp[j] = 'A'
        #
        ans += (i_back + 1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

s = STR()
a = []
i = 0
while i < len(s):
    if s[i] == 'A':
        a.append(0)
        i += 1
    elif i < len(s) - 1 and s[i:i + 2] == 'BC':
        a.append(1)
        i += 2
    else:
        a.append(2)
        i += 1
debug(a)
ans = 0
flag = True
while flag:
    flag = False
    for i in reversed(range(1, len(a))):
        if a[i] == 1 and a[i - 1] == 0:
            ans += 1
            a[i], a[i - 1] = a[i  - 1], a[i]
            flag = True
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
S = S.replace("BC","D")
ans = 0
while S.count("AD") > 0:
  ans += S.count("AD")
  S = S.replace("AD","DA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()

s2list=list(s.replace("BC","D"))
#print(s2list)

block_list=[]
bl=[]
for s2 in s2list:
  if s2=="A" or s2=="D":
    bl.append(s2)
  else:
    if len(bl)>0:
      block_list.append("".join(bl))
      bl=[]
else:
  if len(bl)>0:
    block_list.append("".join(bl))
#print(block_list)

answer=0
for bl in block_list:
  while(bl.find("AD")!=-1):
    answer+=bl.count("AD")
    bl=bl.replace("AD","DA")
    
print(answer)

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = list(S.replace("BC", "X"))
N = len(S)
pos = 1
ans = 0

c = True
while c:
    c = False
    pos = 1
    while pos<N:
        if S[pos-1]=="A" and S[pos]=="X":
            S[pos-1], S[pos] = S[pos], S[pos-1]
            c = True
            ans += 1
        pos += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.replace("BC", "D")
result = 0
for i in range(len(s)-1):
    if s[i]=="A":
        x = 0
        for j in range(i, len(s)):
            if s[j]=="A":
                continue
            elif s[j]=="D":
                x += 1
            else:
                break
        result += x
print(result)

Submission status: Time Limit Exceeded

Solution code:
s = input()

count = 0
while s.count("ABC"):
    # print(s)
    count += s.count("ABC")
    s = s.replace("ABC", "BCA")
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = list("x"+input()+"xx")
N = len(S)

ans = 0
b = 0
while b<N:
    if S[b]=="A" and S[b+1]=="B" and S[b+2]=="C":
        S[b]="B"
        S[b+1]="C"
        S[b+2]="A"
        b-=2
        ans+=1
    b+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list("xxx"+input()+"xxx")
N = len(S)

ans = 0
b = 0
while b<N:
    if S[b]=="A" and "".join(S[b:b+3])=="ABC":
        S[b]="B"
        S[b+1]="C"
        S[b+2]="A"
        b-=3
        ans+=1
    b+=1
print(ans)

Submission status: Time Limit Exceeded

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]="B"
      s[l+1]="C"
      s[l+2]="A"
      ans+=1
      l-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
cnt=0
while s.count('ABC')!=0:
  n = s.count('ABC')
  cnt += n
  s = re.sub('ABC', 'BCA',s)
print(cnt)

Submission status: Time Limit Exceeded

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]

i=0
ans=0
while i<n-2:
    # print(i)
    # print(lst[i:i+3])
    if lst[i]=="A" and lst[i+1]=="B" and lst[i+2]=="C":
        lst[i],lst[i+1],lst[i+2]="B","C","A"
        ans+=1
        i=max(0,i-1)
        continue
    i+=1
print(ans)
# print(lst)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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