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

s = s.replace("BC","D")
count=0

while ('AD' in s)==True:
    s=s.replace("AD","DA",1)
    count+=1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()

count=0

while ('ABC' in s)==True:
    s=s.replace("ABC","BCA",1)
    count+=1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
def search(a):
  x = 0
  while a.count("ABC") >= 1:
    x += a.count("ABC")
    a.replace("ABC","BCA")
  if a.count("ABC") == 0:
    return x
print(search(input()))

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = len(s)
s = s+['z','z']
mem = []
c = 0
run = 0
i=0
while(i<n):
    if s[i]=='A' and s[i+1]=='B' and s[i+2]=='C':
        c+=1
        s[i] = 'B'
        s[i + 1] = 'C'
        s[i + 2] = 'A'
        mem.append(i+2)
        run=1
        i+=3
    else:
        i+=1
        if s[i]=='A':
            mem.append(i)

while(run):
    run = 0
    m = -1
    for i in range(len(mem)):
        idex = mem[i]
        if s[idex] == 'A' and s[idex + 1] == 'B' and s[idex + 2] == 'C' and i>m:
            c += 1
            s[idex] = 'B'
            s[idex + 1] = 'C'
            s[idex + 2] = 'A'
            mem.append(i + 2)
            run = 1
            mem[i]= idex+2
            m=idex+2
print(c)

Submission status: Time Limit Exceeded

Solution code:
S = input()

cnt = 0
while True:
    if "ABC" in S:
        cnt += 1
        s_list = list(S)
        index = S.find("ABC")
        b_list = ["B", "C", "A"]
        for i in range(index, index+3):
            if i == index:
                s_list[i] = b_list[0]
            elif i == index + 1:
                s_list[i] = b_list[1]
            else:
                s_list[i] = b_list[2]
        S = ''.join(s_list) 
    else:
        break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import re

s = list(re.sub(r'[BC]+', 'B', input().strip().replace('BC', 'D')))
n = len(s)
#print(s)

end = n - 1

count = 0

if n >= 3:
  i = 0
  while i < end:
    if s[i] == 'A' and s[i + 1] == 'D':
      s[i] = 'D'
      s[i + 1] = 'A'
      count = count + 1

      j = i - 1
      while j >= 0 and s[j] == 'A':
        s[j] = 'D'
        s[j + 1] = 'A'
        count = count + 1
        j = j - 1

    i = i + 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().strip().replace('BC', 'D'))
n = len(s)
end = n - 1

count = 0

if n >= 3:
  i = 0
  while i < end:
    if s[i] == 'A' and s[i + 1] == 'D':
      s[i] = 'D'
      s[i + 1] = 'A'
      count = count + 1

      j = i - 1
      while j >= 0 and s[j] == 'A':
        s[j] = 'D'
        s[j + 1] = 'A'
        count = count + 1
        j = j - 1

    i = i + 1

print(count)

Submission status: Time Limit Exceeded

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

ans = 0

while True:
  if s.count('AD') == 0:
    break
  else:
    ans += s.count('AD')
    s = s.replace('AD', 'DA')

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().strip())
n = len(s)
end = n - 2

count = 0

if n >= 3:
  i = 0
  while i < end:
    if s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C':
      s[i] = 'B'
      s[i + 2] = 'A'
      count = count + 1

      j = i - 1
      while j >= 0 and s[j] == 'A':
        s[j] = 'B'
        s[j + 2] = 'A'
        count = count + 1
        j = j - 1

      i = i + 2
    else:
      i = i + 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().strip())
n = len(s)
end = n - 2

count = 0

if n >= 3:
  i = 0
  while i < end:
    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'
      count = count + 1

      j = i - 1
      while j >= 0 and s[j] == 'A':
        s[j] = 'B'
        s[j + 1] = 'C'
        s[j + 2] = 'A'
        count = count + 1
        j = j - 1

      i = i + 2
    else:
      i = i + 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().strip())
n = len(s)
end = n - 2

count = 0

if n >= 3:
  i = 0
  while i < end:
    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'
      count = count + 1

      j = i - 1
      while j >= 0:
        if s[j] == 'A':
          s[j] = 'B'
          s[j + 1] = 'C'
          s[j + 2] = 'A'
          j = j - 1
          count = count + 1
        else:
          break

      i = i + 2
    else:
      i = i + 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().strip())
n = len(s)
end = n - 2

count = 0

if n >= 3:
  while True:
    changed = False
    i = 0
    while i < end:
      if s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C':
        changed = True
        if i >= 1 and s[i - 1] == 'A':
          s[i - 1] = 'B'
          s[i] = 'C'
          s[i + 1] = 'A'
          s[i + 2] = 'A'
          count = count + 2
          i = i + 2
        else:
          s[i] = 'B'
          s[i + 1] = 'C'
          s[i + 2] = 'A'
          count = count + 1
          i = i + 2
      else:
        i = i + 1

    if not changed:
      break

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

count = 0
ans = 0

ss = s.replace('BC','X')

ans_list = [0]*len(ss)

for i in range(len(ss)):
    if ss[i] == 'A':

        for j in range(i+1, len(ss)):
            if ss[j] == 'X':
                ans_list[i] += 1

            elif ss[j] == 'A':
                continue
            else:
                break

print(sum(ans_list))

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
import re


def inputs(): return [int(s) for s in input().split()]


def main():
    s = input()
    cnt = 0
    s = s.replace('A', '1').replace('BC', '0')
    arr = re.split('[A-C]', s)
    arr = [list(x) for x in arr]

    flag = True
    while flag:
        flag = False
        for a in arr:

            for i in range(len(a)-1):
                if a[i] > a[i + 1]:
                    a[i], a[i + 1] = a[i+1], a[i]
                    cnt += 1
                    flag = True
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
pre = -1
while cnt != pre:
    pre = cnt
    for i in range(len(s)-1):
        if s[i:i+2] == list('AD'):
            cnt += 1
            s[i] = 'D'
            s[i+1] = 'A'
            
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()

BC_str = ""
A_index_lst = []
for i in range(len(S)):
    if S[i] == "A":
        A_index_lst.append(len(BC_str))
    else:
        BC_str += S[i]



ans = 0
i = 0
running_A_count = 0
while i + 1 < len(BC_str):
    if i in A_index_lst:
        running_A_count += A_index_lst.count(i)
    if BC_str[i] == "B" and BC_str[i+1] == "C":
        ans += running_A_count
        i += 1
    else:
        running_A_count = 0
    
    i += 1






print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

str=""
i=0
cnt=0
ans=0
while i<l:
	x=s[i]
	if x=="A":
		str+="A"
		i+=1
		cnt+=1
	elif i+1<l and s[i]=="B" and s[i+1]=="C":
		str+="D"
		i+=2
		ans+=cnt
	else:
		str+="X"
		i+=1
		cnt=0


print ans

Submission status: Time Limit Exceeded

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

str=""
i=0
LSUM=[0]
cnt=0
ans=0
while i<l:
	x=s[i]
	if x=="A":
		str+="A"
		i+=1
		cnt=LSUM[-1]+1
	elif i+1<l and s[i]=="B" and s[i+1]=="C":
		str+="D"
		i+=2
		cnt=LSUM[-1]
		ans+=cnt
	else:
		str+="X"
		i+=1
		cnt=0
	LSUM.append( cnt )


print ans

Submission status: Time Limit Exceeded

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

str=""
i=0
LSUM=[0]
cnt=0
ans=0
while i<l:
	x=s[i]
	if x=="A":
		str+="A"
		i+=1
		cnt=LSUM[-1]+1
	elif i+1<l and s[i]=="B" and s[i+1]=="C":
		str+="D"
		i+=2
		cnt=LSUM[-1]
		ans+=cnt
	else:
		str+="X"
		i+=1
		cnt=0
	LSUM.append( cnt )

LSUM=LSUM[1:]

ans=0
for i,x in enumerate(str):
	if x=="D":
		ans+=LSUM[i]

print ans

Submission status: Time Limit Exceeded

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

str=""
i=0
LSUM=[0]
cnt=0
while i<l:
	x=s[i]
	if x=="A":
		str+="A"
		i+=1
		cnt=LSUM[-1]+1
	elif i+1<l and s[i]=="B" and s[i+1]=="C":
		str+="D"
		i+=2
		cnt=LSUM[-1]
	else:
		str+="X"
		i+=1
		cnt=0
	LSUM.append( cnt )

LSUM=LSUM[1:]

ans=0
for i,x in enumerate(str):
	if x=="D":
		ans+=LSUM[i]

print ans

Submission status: Time Limit Exceeded

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

count = 0

if n >= 3:
  while True:
    i = n - 3
    changed = False
    while i >= 0:
      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'
        count = count + 1
        changed = True
      i = i - 1

    if not changed:
      break

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- Coding: utf-8 -*-

S = input()
length = len(S)
ans = 0

l=0
while l+2 < length:
  s = S[l:l+3]
  if s == "ABC":
    ans += 1
    S = S[:l] + "BCA" + S[l+3:]
    l -= 1
  else:
    if s[2] == "A":
      l += 2
    elif s[2] == "B":
      l += 1
    else:
      l += 3

  l = max(0, l)

print(ans)

Submission status: Time Limit Exceeded

Solution code:

S = input()
length = len(S)
ans = 0
l=0
while l+2 < length:
  s = S[l:l+3]
  if s == "ABC":
    ans += 1
    S = S[:l] + "BCA" + S[l+3:]
    l -= 2
  else:
    l += 1

  l = max(0, l)

print(ans)

Submission status: Time Limit Exceeded

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

count = 0

if n >= 3:
  while True:
    for i in range(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'
        count = count + 1
        break
    else:
      break

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- Coding: utf-8 -*-

originalS = raw_input()

i = 0
while originalS.find('ABC')!=-1:
  originalS = originalS.replace('ABC', 'BCA', 1)
  i += 1

print i

Submission status: Time Limit Exceeded

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


def solve(s: str):
    
    s = s.replace('BC', 'X')

    ans = 0
    while('AX' in s):
        ans += s.count('AX')
        s = s.replace('AX', 'XA', len(s))
        

    print(ans)
    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def solve(s: str):
    
    s = s.replace('BC', 'X', len(s))

    ans = 0
    while('AX' in s):
        ans += s.count('AX')
        s = s.replace('AX', 'XA', len(s))
        

    print(ans)
    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
ss = list(input())
ans = 0
tmp = 0
s = []

for i in range(len(ss) - 1):
    if ss[i] == "B" and ss[i+1] == "C":
        s.append("D")
        ss[i+1] = "M"
    else:
        if ss[i] != "M":
            s.append(ss[i])
        else:
            pass

while (True):
    tmp = 0
    for i in range(len(s) - 1):
        if s[i] ==  "A" and s[i+1] == "D":
            s[i] = "D"
            s[i+1] = "A"
            tmp += 1
    if tmp != 0:
        ans += tmp
    else:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy
S = input()
#print(S)
S2 = S.replace("BC", "D")
#print(S2)
flag = 0
countnum = 0
start = -1
count = 0
for i in range(len(S2)):
    #print(S2[i])
    if S2[i] == "A" or S2[i] == "D":
        if flag == 0:
            start = i
        countnum += 1
        flag = 1
    elif flag == 1:
        for j in range(start + countnum - 1, start - 1, -1):
            #print(S2[j], j)
            if S2[j] == "D":
                for k in range(j, start - 1, -1):
                    if S2[k] == "A":
                        count += 1
        flag = 0
        countnum = 0
    #print(S3)
    #print(s4)
if countnum != 0:
    for i in range(start + countnum - 1, start - 1, -1):
        #print(S2[i], i)
        if S2[i] == "D":
            for j in range(i, start - 1, -1):
                if S2[j] == "A":
                    count += 1
#print(S3)

print(count)

Submission status: Time Limit Exceeded

Solution code:
import copy
S = input()
#print(S)
S2 = S.replace("BC", "D")
#print(S2)
S3 = []
s4 = ""
flag = 0
for i in range(len(S2)):
    #print(S2[i])
    if S2[i] == "A" or S2[i] == "D":
        s4 = s4 + S2[i]
        flag = 1
    elif flag == 1:
        S3.append(s4)
        s4 = ""
        flag = 0
    #print(S3)
    #print(s4)
S3.append(s4)
#print(S3)
count  = 0
for i in S3:
    for j in range(len(i) - 1, -1, -1):
        #print(i[j], j, count)
        if i[j] == "D":
            for k in range(j, -1, -1):
                if i[k] == "A":
                    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8
# sample code

import re

s = input()

num = 0
i = 0
t = s.replace('BC', 'D')
while True:
	l = len(re.findall('AD', t))
	if l == 0:
		break
	num += l
	u = t.replace('AD', 'DA')
	t = u
	i += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.replace('A', 'X').replace('BC', 'Y')
score = 0
while 'XY' in s:
	i = s.count('XY')
	s = s.replace('XY','YX')
	score += i
print(score)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.replace('A', 'X').replace('BC', 'Y')
score = 0
while 'XY' in s:
	s = s.replace('XY','YX',1)
	score += 1
print(score)

Submission status: Time Limit Exceeded

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

s = input()
s = s.replace('BC', 'D')
s = re.split('[BC]', s)
res = 0

for kk in s:
  if not kk:
    continue
  while 'AD' in kk:
    res += kk.count('AD')
    kk = kk.replace('AD', 'DA')
    
print(res)

Submission status: Time Limit Exceeded

Solution code:
import re

s = input()
s = s.replace('BC', 'D')
s = re.split('[BC]', s)
re = 0

for kk in s:
  if not kk:
    continue
  while 'AD' in kk:
    re += kk.count('AD')
    kk = kk.replace('AD', 'DA')
    
print(re)

Submission status: Time Limit Exceeded

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

s = s.replace("BC","D")
t = re.split("[BC]",s)
t = [x for x in t if x]
#print(s)
#print(t)

for e in t:
    ind=[i for i, x in enumerate(e) if x == 'A']
    #print(ind)
    for j in ind:
        srt = e[j+1:]
        ans+=srt.count("D")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
text = list(input())

count = 0

for j in range(100):
    i = 0
    non = 0
    while len(text) > i+2:
        if text[i] == "A" and text[i+1] == "B" and text[i+2] == "C":
            text[i],text[i+1],text[i+2] = text[i+1],text[i+2],text[i]
            count += 1
        else:
            non += 1
        i += 1
    if non == i:
        break
    
print(count)

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

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
def getScore(arr):
    total = 0
    minus = 0
    start = 0
    while 0 in arr[start:]:
        total += arr.index(0, start)
        total -= minus
        minus += 1
        start = arr.index(0, start) + 1

    return total

total = 0
data = []
toInt = {'A':1, 'B':2, 'C':2}
i = 0
while i < len(s)-1:
    # BC=>0
    if s[i] == 'B' and s[i+1] == 'C':
        data.append(0)
        i += 1
    elif s[i] == 'A':
        data.append(toInt[s[i]])
    else:
        if len(data) > 0:
            total += getScore(data)
            data = []
    i+=1

if len(data) > 0:
    total += getScore(data)
    data = []
    
print(total)

Submission status: Time Limit Exceeded

Solution code:
s = input()

data = []
toInt = {'A':1, 'B':2, 'C':2}
i = 0
while i < len(s)-1:
    # BC=>0
    if s[i] == 'B' and s[i+1] == 'C':
        data.append(0)
        i += 1
    else:
        data.append(toInt[s[i]])
    i+=1

if i < len(s):
    data.append(toInt[s[i]])


def getScore(s):
    total = 0
    minus = 0
    start = 0
    while 0 in arr[start:]:
        total += arr.index(0, start)
        total -= minus
        minus += 1
        start = arr.index(0, start) + 1

    return total


# Calculate score
total = 0
start = 0 
while start < len(data):
    end = start
    while end < len(data) and not data[end] == 2:
        end += 1
    arr= data[start:end]
    total += getScore(arr)
    start = end + 1
    
print(total)

Submission status: Time Limit Exceeded

Solution code:
s = input()
sl = list(s)
ls = range(len(s) - 1)
loop = 1
c = 0
while (loop):
    loop = 0
    for i in ls:
        if sl[i:i + 3] == ['A', 'B', 'C']:
            sl[i] = 'B'
            sl[i + 1] = 'C'
            sl[i + 2] = 'A'
            c += 1
            loop = 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = input()

s = s.replace('BC','D')

c = 0
while s.count('AD') != 0:
  s = s.replace('AD','DA',1)
  c += 1
  
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

retn = 0

loop = True
while(loop):

    x = s.find("BC")
    if (x == -1):
        break

    if (x != 0):
        bb = s.rfind("B",0,x)
        cc = s.rfind("C",0,x)

        if (cc > bb):
            bb = cc
        retn += x - bb - 1
        s = "A" * (x - bb - 1) + s[x+2:]
    else:
        s = s[x+2:]

print(retn)

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = len(S)
A = ""
k = 0
while k < l-1:
    if S[k:k+2] == "BC":
        A += "D"
        k += 2
    else:
        A += S[k]
        k += 1
S = A
S = S.replace("B","C")
L = list(S.split("C"))

def keisan(s):
    if len(s) == 0:
        return 0
    l = len(s)
    t = 1
    ret = 0
    for k in range(len(s)):
        if s[k] == "A":
            ret += l-k-t
            t += 1
    return ret

ans = 0
for s in L:
    ans += keisan(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = len(S)
for k in range(1,l):
    if S[k-1:k+1] == "BC":
        S = S[:k-1] + "D" + S[k+1:]
        l -= 1
S = "".join(S)
S = S.replace("B","C")
L = list(S.split("C"))

def keisan(s):
    if len(s) == 0:
        return 0
    l = len(s)
    t = 1
    ret = 0
    for k in range(len(s)):
        if s[k] == "A":
            ret += l-k-t
            t += 1
    return ret

ans = 0
for s in L:
    ans += keisan(s)
print(ans)

Submission status: Time Limit Exceeded

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

def change():
  global count
  flg=False
  for i in range(len(s)-2):
    if s[i]=='A' and s[i+1]=='B' and s[i+2]=='C':
      s[i]='B'
      s[i+1]='C'
      s[i+2]='A'
      count+=1
      flg=True
      
  return flg
      
while(change()):
  pass

print(count)

Submission status: Time Limit Exceeded

Solution code:
def BC2D(s):
    n=len(s)
    index=[]
    for i in range(n-1):
        if s[i:i+2]=='BC':
            index.append(i)
    new_s=''
    for i in range(n):
        if i in index:
            new_s+='D'
        elif i-1 in index:
            new_s+=''
        else:
            new_s+=s[i]
    return new_s

def partition(s):
    s='B'+s+'C'
    group=[]
    n=len(s)
    left=0
    right=0
    for i in range(n-1):
        block=s[i:i+2]
        if block[0] in ['B','C'] and block[1] in ['A','D']:
            left=i
        if block[0] in ['A','D'] and block[1] in ['B','C']:
            right=i
            group.append(s[left:right+1])
    return group

def turningNo(s):
    totalCount=0
    count=0
    n=len(s)
    s=s[::-1]
    for i in range(n):
        if s[i]=='A':
            totalCount+=count
        if s[i]=='D':
            count+=1
    return totalCount

s=input()
s=BC2D(s)
G=partition(s)
answer=0
for element in G:
    answer+=turningNo(element)
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def bubble_sort(A):
    cnt = 0
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if A[j] == 'D' and A[j-1] == 'A':
                A[j], A[j-1] = A[j-1], A[j]
                cnt += 1
    return cnt

s = input()
total = 0
new_s = s.replace("BC", "D")
new_s_list = list(new_s)
new_s_list.append("G")
tmp_start = 0
for i in range(len(new_s_list)):
  if new_s_list[i] in {'B', 'C', 'G'}:
    total += bubble_sort(new_s_list[tmp_start:i])
    tmp_start = i

print(total)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
s = input()

# search A
listA = []
for i in range(len(s) - 1, -1, -1):
    if s[i] == "A":
        listA.append(i)

cnt = 0
for a in listA:
    while s[a : a + 3] == "ABC":
        s = s[: a] + "BCA" + s[a + 3 :]
        a += 2
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import re

s = input()
s = s.replace('BC', 'D')
s = re.split('[BC]', s)

ans = 0
for subs in s:
    while(True):
        cnt = subs.count("AD")
        if cnt == 0:
            break
        ans += cnt
        subs = subs.replace("AD", "DA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.replace("BC","D")
c = 0
while True:
    c_tmp = c
    c += s.count('AD')
    s = s.replace("AD","DA")
    if c == c_tmp:
        break
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

sl = s.replace('BC', 'D').replace('B', 'C')
while 'AC' in sl or  'CD' in sl or  'CC' in sl:
    sl = sl.replace('AC', 'C')
    sl = sl.replace('CD', 'C')
    sl = sl.replace('CC', 'C')
sl = sl.split('C')

def func(l):
    ind = [i for i, x in enumerate(l) if x == 'D']
    cl =  sum([l[:i].count('A') for i in ind])
    return cl

c = 0
for l in sl:
    l = list(l)
    c += func(l)
print(c)

Submission status: Time Limit Exceeded

Solution code:
S = input().replace("BC", "D")
in_ABC = S.count("AD")
count = 0
while in_ABC != 0:
  S = S.replace("AD", "DA", 1)
  count += 1
  in_ABC = S.count("AD")
print(count)

Submission status: Time Limit Exceeded

Solution code:
def looper(s):
    fd = s.find('ABC')
    fd1 = fd
    fd2 = fd
    ans = 0
    s = s[:fd] + 'BCA' + s[fd+3:]

    while True:
        if fd + 5 > len(s):
            break
        if s[fd+3:fd+5] == 'BC':
            ans = ans + 1

            s = s[:fd+2] + 'BCA' + s[fd+5:]
            fd1 = fd1 + 2
        else:
            break

    while True:
        if fd == 0:
            break
        if s[fd2-1] == 'A':
            ans = ans + 1
            if fd == 1:
                s=s[:fd2-1] + 'BCA' + s[fd2+1:]

            else:
                s = s[:fd2-2] + 'BCA' + s[fd2+1:]
                fd2 = fd2 - 1
        else:
            break
    return ans, s

s = input()
fd = s.find('ABC')
ans = 0

if fd == -1:
    print(ans)
    exit()
else:
    ans = ans + 1

while s.find('ABC') != -1:
    ans = ans + looper(s)[0]
    #s = looper(s)[1]

#print(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
c = 0
while True:
    c_tmp = c
    c += s.count('ABC')
    s = s.replace("ABC","BCA")
    if c == c_tmp:
        break
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

sl = s.replace('BC', 'D').replace('B', 'C').replace('AC', 'C').replace('CD', 'C').split('C')
sl = [x for x in sl if (len(x)>1 and 'A' in x and 'D' in x)]

c = 0
for l in sl:
    l = list(l)
    ind = [i for i, x in enumerate(l) if x == 'D']
    cl =  [l[:i].count('A') for i in ind]
    c += sum(cl)
print(c)

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=S.count('A')
s=S.replace('BC','D')
l=len(s)
ans=0
for i in range(n):
  k=s.rfind('A')
  b=s[k:].find('B')
  c=s[k:].find('C')
  if b==-1 and c==-1:
    ans+=l-k-1
  elif b==-1:
    ans+=c-1
  elif c==-1:
    ans+=b-1
  else:
    ans+=min(b,c)-1
  
  s=s[:k]+s[k+1:]
  l-=1
print(ans)

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 = str(input())

s = list()
s.append(2)
s.append(2)
s.append(2)


for i in S:
    if(i=="A"):
        s.append(0)
    elif(i=="B"):
        s.append(1)
    else:
        s.append(2)
count= 0
i = 0


while(i < len(s) -2):
    if(s[i]==0 and s[i+1]==1 and s[i+2] == 2):
        j = i

        while (s[j] == 0):
            j -= 1
        count += i - j

        s[j+1:i+3] = [0] * (i+3-(j+1))
        s[j+2] = 2

    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=S.count('A')
s=S.replace('BC','D')
l=len(s)
ans=0
for i in range(n):
  k=s.rfind('A')
  b=s[k:].find('B')
  c=s[k:].find('C')
  if b==-1:
    b=1000000
  if c==-1:
    c=1000000
  if b==1000000 and c==1000000:
    ans+=l-k-1
  else:
    ans+=min(b,c)-1
  s=s[:k]+s[k+1:]
  l-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
S=S.replace("BC", "D")
S=S.replace("C" or "B", "_")
S=S.replace("__" or "___", "_")
count=0
for i in range(1,len(S)-1):
    if S[i-1:i+1]=="AD":
        count_A=1
        count_B=1
        k=i+1
        while S[k]=="D" and k<len(S)-1:
            k=k+1
            if S[k]=="D":
                count_B+=1
        j=i-1
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A*count_B
        S=S[0:i-count_A]+"D"*count_B+"A"*count_A+S[i+count_B:len(S)]
        i+=count_B-1
i=len(S)-1
if S[i-1:i+1]=="AD":
        count_A=1
        j=i-1
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input ()
cnt = 0

while True:
    if 'ABC' in s:
        s = s.replace ('ABC', 'BCA', 1)
        cnt += 1
    else:
        break

print (cnt)

Submission status: Time Limit Exceeded

Solution code:
S=input()
S=S.replace("BC", "D")
S=S.replace("C", "_")
S=S.replace("B", "_")
S=S.replace("__", "_")
S=S.replace("__", "_")
S=S.replace("__", "_")
count=0
for i in range(1,len(S)-1):
    if S[i-1:i+1]=="AD":
        count_A=1
        count_B=1
        k=i+1
        while S[k]=="D" and k<len(S)-1:
            k=k+1
            if S[k]=="D":
                count_B+=1
        j=i-1
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A*count_B
        S=S[0:i-count_A]+"D"*count_B+"A"*count_A+S[i+count_B:len(S)]
        i+=count_B-1
i=len(S)-1
if S[i-1:i+1]=="AD":
        count_A=1
        j=i-1
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

sl = s.replace('BC', 'D').replace('B', 'C').replace('AC', 'C').replace('CD', 'C').split('C')
sl = [x for x in sl if (len(x)>1 and 'A' in x)]

c = 0
for l in sl:
    if 'D' not in l or 'A' not in l :
        continue
    else:
        l = list(l)
        ind = [i for i, x in enumerate(l) if x == 'D']
        cl =  [l[:i].count('A') for i in ind]
        c += sum(cl)
print(c)

Submission status: Time Limit Exceeded

Solution code:
import copy

s="Z"
s+=input()
len_s=len(s)

def work(s,totalnum):
  new_s=["Z"]
  num=0
  skipnum=0
  for i in range(1,len_s):
    #print(i,s,new_s)
    if skipnum>0:
      skipnum-=1
      continue

    if i<=len_s-3:
      if s[i]=="A" and s[i+1]=="B" and s[i+2]=="C":
        new_s.append("B")
        new_s.append("C")
        new_s.append("A")
        num+=1
        skipnum+=2
      else:
        new_s.append(s[i])
    else:
        new_s.append(s[i])

  totalnum+=num
  #print("d")
  #print(s,new_s)
  if num==0:
    return totalnum
  else:
    return work(new_s,totalnum)

print(work(s,0))

Submission status: Time Limit Exceeded

Solution code:
S=input()
S=S.replace("BC", "D")
count=0
for i in range(1,len(S)-1):
    if S[i-1:i+1]=="AD":
        count_A=1
        count_B=1
        k=i+1
        while S[k]=="D" and k<len(S)-1:
            k=k+1
            if S[k]=="D":
                count_B+=1
        j=i-1
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A*count_B
        S=S[0:i-count_A]+"D"*count_B+"A"*count_A+S[i+count_B:len(S)]
        i+=count_B-1
i=len(S)-1
if S[i-1:i+1]=="AD":
        count_A=1
        j=i-1
        while S[j]=="A" and j-1>=0:
            j=j-1
            if S[j]=="A":
                count_A+=1
        count+=count_A
print(count)