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:
def main():
    S = str(input())
    S += "B"
    N = len(S)
    for i in range(N-1):
        if S[i] == "B" and S[i+1] == "C":
            S = S[:i]+"DE"+S[(i+2):]
    S = S.replace("E","")
    left = 0
    count = 0
    rep = 0
    ar = 0
    N = len(S)
    for i in range(N):
        if S[i] == "D":
            count += 1
            rep += i-left
        elif S[i] == "B" or S[i] == "C":
            ar += rep-(count*(count-1)//2)
            rep = 0
            count = 0
            left = i+1

    print(ar)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = str(input())
    S += "Z"
    N = len(S)
    for i in range(N-1):
        if S[i] == "B" and S[i+1] == "C":
            S = S[:i]+"DE"+S[(i+2):]
    S = S.replace("E","")
    left = 0
    right = 0
    count = 0
    rep = 0
    ar = 0
    N = len(S)
    while right <= N-1:
        if S[right] == "A":
            right += 1
        elif S[right] == "D":
            count += 1
            rep += right-left
            right += 1
        else:
            ar += rep-(count*(count-1)//2)
            rep = 0
            count = 0
            right += 1
            left = right

    print(ar)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
#AGC34 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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
m = len(t)
ans = 0
a,d = 0,0
for i in range(m):
    if t[i] == 'A':
        a += 1
    elif t[i] == 'D':
        ans += a
        d += 1
    else:
        a,d = 0,0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    li = []
    li_append = li.append
    is_A = False
    now_state = 0
    for ind,s in enumerate(S):
        if(is_A):
            if(now_state==1):
                now_state = 0
                continue
            else:
                if(s=='A'):
                    li[-1] += 'A'
                elif(S[ind:ind+2]=='BC'):
                    li[-1] += 'B'
                    now_state = 1
                else:
                    is_A = False
        elif(s=='A'):
            is_A = True
            now_state = 0
            li_append('A')
    cnt = 0
    for i in li:
        cntb = i.count('B')
        for ind,s in enumerate(i):
            if(s=='A'):
                cnt += cntb
            elif(s=='B'):
                cntb -= 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
li = []
is_A = False
now_state = 0
for ind,s in enumerate(S):
    if(is_A):
        if(now_state==1):
            now_state = 0
            continue
        else:
            if(s=='A'):
                li[-1] += 'A'
            elif(S[ind:ind+2]=='BC'):
                li[-1] += 'B'
                now_state = 1
            else:
                is_A = False
    elif(s=='A'):
        is_A = True
        now_state = 0
        li.append('A')
cnt = 0
for i in li:
    for ind,s in enumerate(i):
        if(s=='A'):
            cnt += i[ind+1:].count('B')

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
cnt = 0
len_s = len(S)
i = 2
while i <= len_s-1:
  if S[i-2:i+1] == 'ABC':
    S = S[:i-2] + 'BCA' + S[i+1:]
    cnt += 1
    i = max(0, i-3)
  else:
    i += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
c = 0
ln = len(s)
while True:
	f = []
	n = ""
	m = s
	for i in range(ln):
		if len(n) == 3:
			f.append(n[0])
			n = n[1:]
		n += s[i]
		if n == "ABC":
			n = "BCA"
			c += 1
			f.append(n)
			f.append(s[i+1:])
			n = ""
			break
			
	s = "".join(f)
	if n:
		s += n
	if m == s:
		break	
print(c)

Submission status: Time Limit Exceeded

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

c = 0
ln = len(s)
while "ABC" in s:
	f = []
	for i in s:
		if len(n) == 3:
			f.append(n[0])
			n = n[1:]
		n += i
		if n == "ABC":
			n = "BCA"
			c += 1
			f.append(n)
			n = ""
	s = "".join(f)
	if n:
		s += n		
print(c)

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:
import copy
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
b = input()

a = [b[i] for i in range(len(b))]
place = len(a)-3
s = 0
if len(a) < 3:
    print(0)
    exit(0)
length = len(a)
while place != -1:
    if place >= length-2:
        place = length - 3
    i = place
    #print(i,len(a))
    if a[i] =='A' and a[i+1] == 'B' and a[i+2] == 'C':
        a[i]='B'
        a[i+1]='C'
        a[i+2]='A'
        place+=3
        if place >= length-2:
            place = length-3
        s+=1
    else:
        place-=1
print(s)

Submission status: Time Limit Exceeded

Solution code:
import copy
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
b = input()

a = [b[i] for i in range(len(b))]
place = len(a)-3
s = 0
if len(a) < 3:
    print(0)
    exit(0)
test = "ABC"
while place != -1:
    if place >= len(a)-2:
        place = len(a) - 3
    i = place
    #print(i,len(a))
    if ''.join(a[i:i+3]) ==test:
        a[i]='B'
        a[i+1]='C'
        a[i+2]='A'
        place+=3
        if place >= len(a)-2:
            place = len(a)-3
        s+=1
    else:
        place-=1
print(s)

Submission status: Time Limit Exceeded

Solution code:
import copy
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
b = input()

a = [b[i] for i in range(len(b))]
place = len(a)-3
s = 0
if len(a) < 3:
    print(0)
    exit(0)
while place != -1:
    if place >= len(a)-2:
        place = len(a) - 3
    i = place
    #print(i,len(a))
    if a[i] =='A' and a[i+1] == 'B' and a[i+2] == 'C':
        a[i]='B'
        a[i+1]='C'
        a[i+2]='A'
        place+=3
        if place >= len(a)-2:
            place = len(a)-3
        s+=1
    else:
        place-=1
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
i = 0
T = ""
while i <= N-2:
    if S[i] == 'B' and S[i+1] == 'C':
        T += 'D'
        i += 1
    else:
        T += S[i]
    i += 1


N = len(T)
ans = 0
l = 0
while l < N:

    if T[l] == 'B' or T[l] == 'C':
        l += 1
        continue
    
    r = l
    tmp = ''
    while r < N and (T[r] == 'A' or T[r] == 'D'):
        tmp += T[r]
        r += 1
    
    M = len(tmp)
    D_count = [0]*(M+1)
    for i in range(M-1, -1, -1):
        D_count[i] = D_count[i+1]
        if tmp[i] == 'D':
            D_count[i] += 1
    
    for i in range(M):
        if tmp[i] == 'A':
            ans += D_count[i]
    
    if r == N:
        break
    else:
        l = r+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
i = 0
T = ""
while i <= N-2:
    if S[i] == 'B' and S[i+1] == 'C':
        T += 'D'
        i += 1
    else:
        T += S[i]
    i += 1


N = len(T)
substring = []
l = 0
while l < N:

    if T[l] == 'B' or T[l] == 'C':
        l += 1
        continue
    
    r = l
    tmp = ''
    while r < N and (T[r] == 'A' or T[r] == 'D'):
        tmp += T[r]
        r += 1
    substring.append(tmp)
    if r == N:
        break
    else:
        l = r+1

ans = 0
for s in substring:
    M = len(s)
    D_count = [0]*(M+1)
    for i in range(M-1, -1, -1):
        D_count[i] = D_count[i+1]
        if s[i] == 'D':
            D_count[i] += 1
    
    for i in range(M):
        if tmp[i] == 'A':
            ans += D_count[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
i = 0
T = ""
while i <= N-2:
    if S[i] == 'B' and S[i+1] == 'C':
        T += 'D'
        i += 1
    else:
        T += S[i]
    i += 1


N = len(T)
ans = 0
l = 0
while l < N:

    if T[l] == 'B' or T[l] == 'C':
        l += 1
        continue
    
    r = l
    tmp = ''
    while r < N and (T[r] == 'A' or T[r] == 'D'):
        tmp += T[r]
        r += 1
    
    M = len(tmp)
    D_count = [0]*(M+1)
    for i in range(M-1, -1, -1):
        D_count[i] = D_count[i+1]
        if tmp[i] == 'D':
            D_count[i] += 1
    
    for i in range(M):
        if T[i] == 'A':
            ans += D_count[i]
    
    if r == N:
        break
    else:
        l = r+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
i = 0
T = ""
while i <= N-2:
    if S[i] == 'B' and S[i+1] == 'C':
        T += 'D'
        i += 1
    else:
        T += S[i]
    i += 1


N = len(T)
substring = []
l = 0
while l < N:

    if T[l] == 'B' or T[l] == 'C':
        l += 1
        continue
    
    r = l
    tmp = ''
    while r < N and (T[r] == 'A' or T[r] == 'D'):
        tmp += T[r]
        r += 1
    substring.append(tmp)
    if r == N:
        break
    else:
        l = r+1

ans = 0
for s in substring:
    M = len(s)
    D_count = [0]*(M+1)
    for i in range(M-1, -1, -1):
        D_count[i] = D_count[i+1]
        if s[i] == 'D':
            D_count[i] += 1
    
    for i in range(M):
        if T[i] == 'A':
            ans += D_count[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())

i=0
count=0

def exist(st):
    h = 0
    for i in range(len(st)-2):
        if st[i]=='A' and st[i+1]=='B' and st[i+2]=='C':
            h=1
    return h


while exist(s):
    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
    if i==len(s)-3:
        i=0
    i+=1
print(count)

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:
import re
 
count = 0
 
def count_inversion(sequence):
    return sum(sum(m>n for m in sequence[i+1:]) for i,n in enumerate(sequence))
 
s = input()
s = s.replace("BC", "D")
s = re.split(r"[B-C]+", s)
 
while "" in s:
    s.remove("")
for x in s:
    count += count_inversion(list(x))
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
s = re.sub("BC", "D", s)

i = 0
while re.findall("AD", s):
    num_abc = len(re.findall("AD", s))
    i = i + num_abc
    s = re.sub("AD", "DA", s)

print(i)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()

i = 0
while re.findall("ABC", s):
    num_abc = len(re.findall("ABC", s))
    i = i + num_abc
    s = re.sub("ABC", "BCA", s)

print(i)

Submission status: Time Limit Exceeded

Solution code:
s = input()

a = 0
b = 1
while b>0:
    b = 0
    s = s.replace("ABC",".")
    a += s.count(".")
    b = s.count(".")
    s = s.replace(".","BCA")

print(a)

Submission status: Time Limit Exceeded

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

count = 0
for i,c in enumerate(d):
    if c == "A":
        for j, h in enumerate(d[i+1:]):
            if h == "D":
                count += 1
            elif h == "A":
                continue
            else:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = str(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:
import sys
INPUT = sys.stdin.readline
 
def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

s = SINGLE_STRING()
s = list(s)

i = 0
ans=0

while(i!=len(s)-1):
    if 'A' == s[i]:
        if 'B' == s[i+1] and 'C' == s[i+2]:
            s[i] = "B"
            s[i+1]="C"
            s[i+2]="A"
            i=0
            ans = ans + 1
        else:
            i=i+1                
    else:
        i=i+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
sentence = input()
def recABC(s, c):
  temp = 0
  for i in range(0, len(s) - 2):
    if s[i] == "A":
      if s[i + 1] == "B":
        if s[i + 2] == "C":
            temp += 1
            if len(s) <= i + 3:
              s = s[: i] + "BCA"
            else:
              s = s[: i] + "BCA" + s[i + 3:]
            #print(s)
  
  if temp == 0:
    return c
  else:
    return recABC(s, c + temp)
print(recABC(sentence, 0))

Submission status: Time Limit Exceeded

Solution code:
s=input()

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

count=-1
total=0
while count!=0:
    count=s.count('AD')
    total += count
    s = s.replace("AD","DA")
    #print(s)

print(total)

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()
S =re.sub("BC","D",S)

cnt = 0

if S.count("AD")==0:
  print(cnt)
else:
  while True:
    fd = S.count("AD")
    if fd == 0:
      break
    cnt += fd
    S = re.sub("AD", "DA", S)
  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
counts = 10 * s.count("ABCABCABCABC")
s = s.replace("ABCABCABCABC","BCBCBCBCAAAA")
counts += 6 * s.count("ABCABCABC")
s = s.replace("ABCABCABC","BCBCBCAAA")
counts += 3 * s.count("ABCABC")
s = s.replace("ABCABC","BCBCAA")
counts +=  s.count("ABC")
s = s.replace("ABC","BCA")
tmp = s.count("ABC")
counts += tmp 
while tmp != 0:
  s = s.replace("ABC","BCA")
  tmp = s.count("ABC")
  counts += tmp
  
print(counts)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re

count = 0

def count_inversion(sequence):
    return sum(sum(m>n for m in sequence[i+1:]) for i,n in enumerate(sequence))

s = input()
s = s.replace("BC", "D")
s = re.split(r"[B-C]+", s)

while "" in s:
    s.remove("")
for x in s:
    count += count_inversion(list(x))
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

count = 0

class Count:
    def __init__(self):
        self.count = 0
    def bSort(self, a):
        for i in range(len(a)):
            for j in range(len(a)-1, i, -1):
                if a[j] > a[j-1]:
                    a[j], a[j-1] = a[j-1], a[j]
                    self.count += 1

s = input()
s = s.replace("BC", "D")
s = re.split(r"[B-C]+", s)

while "" in s:
    s.remove("")
for x in s:
    a = Count()
    a.bSort(list(x))
    count += a.count
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = [ s for s in input()]

cnt = 0
now_cnt = -1

while cnt != now_cnt:
  now_cnt = cnt
  for i in range (len(S)-2):
    if S[i] is "A" and S[i+1] is "B" and S[i+2] is "C":
      cnt += 1
      S[i], S[i+1], S[i+2] = "B", "C", "A"
      
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()

count = 0

while s != s.replace('ABC', 'BCA'):
  t = s
  s = s.replace('ABC', 'BCA')
  for i in range(len(s)):
    if s[i] != t[i]:
      count += 1
      
print(count//3)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
c = 0
N = len(s)
if "BC" in s:
  ss = s[0:s.index("BC")]
  ib = 0
  for i in range(N-1):
    if s[i] == "B" and s[i+1] == "C":
      ss = ss + s[ib+2:i] + "D"
      ib = i

  #print(ss)
  
  if ss[0] == "A" or ss[0] == "D":
    t = [-1]
  else:
    t = []
  for i in range(len(ss)):
    if ss[i] == "B" or ss[i] == "C":
      t.append(i)
  if ss[-1] == "A" or ss[-1] == "D":
    t.append(N-1)
  #print(t)

  S = []
  for i in range(len(t)-1):
    S.append(ss[t[i]+1:t[i+1]])
  #print(S)

  for i in S:
    #print(i,c)
    for j in range(len(i)):
      if i[j] == "D":
        #print(j)
        c += j
    n = i.count("D")
    c -= (n-1)*n//2

  print(c)
  
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
s = input()
length = len(s)
count = 0
s_sub = ""

index = 0
while index < length:
  if index+1 < length and s[index:index+2] == 'BC':
    s_sub += 'D'
    index += 2
  else:
    s_sub += s[index]
    index += 1
#print (s_sub)

index = 0
count = 0
while True:
  flag = 0
  while index < length-1:
    if s_sub[index:index+2] == 'AD':
      count += 1
      s_sub = s_sub[0:index] + 'DA' + s_sub[index+2:length]
      #print (s_sub)
      index += 1
      flag = 1
    else:
      index += 1
  if flag == 0:
    break
  index = 0
print (count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
ar= list(input())
count = 0
while True:
    t = 0
    for i in range(len(ar)-2):
        if ar[i:i+3] == ["A","B","C"]:
            ar[i:i+3] = ["B","C","A"]
            count += 1
            t += 1
    if t == 0:
        break
print(count)

Submission status: Time Limit Exceeded

Solution code:
#import random
S = input()
#S = ''.join([random.choice(['A', 'B', 'C']) for i in range(200000)])
S += 'B'
S = S.replace('BC', 'D')
S = S.replace('C', 'B')
#print(S)
L = []
flag = 0 # flag = 0: 左端探索中 flag = 1: 右端探索中
l = 0
for i in range(len(S)):
    if flag == 0 and S[i] != 'B':
        flag = 1
        l = i
    elif flag == 1 and S[i] == 'B':
        flag = 0
        L.append(S[l:i])
counter = 0
for i in L:
    for j in range(len(i) - 1):
        if i[j] == 'A':
            counter += i[j + 1:].count('D')
print(counter)

Submission status: Time Limit Exceeded

Solution code:
S = input()
s = []
for i in range(len(S)):
    if S[i] == 'A':
        s.append(0)
    elif S[i] == 'B':
        s.append(1)
    else:
        s.append(2)
flag = 0
counter = 0
while flag == 0:
    flag = 1
    for i in range(len(s) - 2):
        if s[i : i + 3] == [0, 1, 2]:
            s = s[:i] + [1, 2, 0] + s[i+3:]
            flag = 0
            counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
S = input()
flag = 0
counter = 0
while flag == 0:
    flag = 1
    for i in range(len(S) - 2):
        if S[i : i + 3] == 'ABC':
            S = S[:i] + 'BCA' + S[i+3:]
            flag = 0
            counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import bisect
from collections import defaultdict
import math
import re

AABC = re.compile(r"A+X")
ABCBC = re.compile(r"AX+")

def repl(S):
    cnt = 0
    cnt = S.count("AX")
    new_S = S.replace("AX", "XA")
    return cnt, new_S

def main():
    S = input().strip()
    S = S.replace("BC", "X")

    cnt = 0
    while True:
        c, S = repl(S)
        if c == 0:
            break
        cnt += c
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import bisect
from collections import defaultdict
import math

def repl(S):
    cnt = S.count("ABC")
    new_S = S.replace("ABC", "BCA")
    return cnt, new_S

def main():
    S = input().strip()

    cnt = 0
    while True:
        c, S = repl(S)
        if c == 0:
            break
        cnt += c
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
pattern = r"(A+(BC)*A*(BC)+)+"
pattern2 = r"(A|BC)"
match_iter = re.finditer(pattern, S)
# print([i.group() for i in match_iter])
count = 0
for match in match_iter: ## AABCBCAABCBC
    string = match.group()
    match2 = re.findall(pattern2, string)
    cntA = 0
    for string2 in match2:
        if string2 == "A":
            cntA += 1
        else:
            count += cntA
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:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    s=input().replace("BC","X")
#    print(s)
    c=0
    while "AX" in s:
        c+=s.count("AX")
        s=s.replace("AX","XA")
    print(c)    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
def change(a):
	global turn
	if [s[a], s[a+1], s[a+2]] == ["A", "B", "C"]:
		s[a], s[a+1], s[a+2] = s[a+1], s[a+2], s[a]
		turn += 1
		if a > 0:
			return a - 1
		else:
			return a + 2 
	else:
		return a + 1

pointer = 0
turn = 0
while pointer < len(s)-2:
	pointer = change(pointer)
print(turn)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import  re
s = input().replace("BC", "X")
cnt = 0
while "AX" in s:
  if "AXAX" in s:
    tmp = re.subn("AXAX", "XXAA", s)
    s = tmp[0]
    cnt += tmp[1] * 3

  tmp = re.subn("AX", "XA", s)
  s = tmp[0]
  cnt += tmp[1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import  re
s = input().replace("BC", "X")
cnt = 0
while "AX" in s:
  tmp = re.subn("AX", "XA", s)
  s = tmp[0]
  cnt += tmp[1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input().replace("BC", "X")
cnt = 0
while True:
  pos = s.find("AX")
  if pos == -1:
    break
  s = s[:pos] + "XA" + s[pos + 2:]
  cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
while True:
  pos = s.find("ABC")
  if pos == -1:
    break
  s = s[:pos] + "BCA" + s[pos + 3:]
  cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
a=[-1]*l
stack=[]
for i in range(l):
    if s[i]=='A':
        a[i]=1
        stack.append(i)
    elif s[i]=='B':
        a[i]=2
    else:
        a[i]=3
count=0
while stack:
    i=stack.pop(0)
    if i<l-2:
        if a[i]==1 and a[i+1]==2 and a[i+2]==3:
            count+=1
            a[i]=2
            a[i+1]=3
            a[i+2]=1
            stack.append(i+2)
            if (i-1>=0):
                if(a[i-1]==1):
                    stack.append(i-1)
            #print(a)
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
c = 0
def conv(s):
    if 'ABC' in s:
        global c
        c += s.count('ABC')
        return s.replace('ABC', 'BCA')
    else:
        return None
while True:
    s = conv(s)
    if s is None:
        break
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
l = len(s)
count = 0
while "ABC" in "".join(s):
	for i in range(l-2):
		if "".join(s[i:i+3]) == "ABC":
			s[i:i+3] = "BCA"
			count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
s=re.sub("AC|BB|[^ABC][^ABC]|AX|BX|XB|XC","X",s)
s=re.sub("XX*","X",s)
#print(s)
c=0
while "ABC" in s:
    t=re.subn("ABC","BCA",s)
    s=t[0]
    c+=t[1]
    lastA=s.rfind("A")
    topA=s.find("A")
    topC=s.find("C")
    #print(s,lastA)
    #print(s[lastA:(lastA + 3)])
    if not s[lastA:(lastA + 3)] == "ABC":
        s=s[:lastA]
    s=s[topA:]
    
    s=re.sub("AC|BB|[^ABC][^ABC]|AX|BX|XB|XC","X",s)
    s=re.sub("XX*","X",s)
    #print(s)
print(c)

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
c=0
while "ABC" in s:
    t=re.subn("ABC","BCA",s)
    s=t[0]
    c+=t[1]
    lastA=s.rfind("A")
    #print(s,lastA)
    #print(s[lastA:(lastA + 3)])
    if not s[lastA:(lastA + 3)] == "ABC":
        s=s[:lastA]
    #print(s)
print(c)

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
c=0
while "ABC" in s:
    t=re.subn("ABC","BCA",s)
    s=t[0]
    c+=t[1]
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
a=0
b=0
c=0
s=s.replace('BC','0')
if(s.find('A0')==-1):
    print('{0}'.format(b))
    a=1
while (a==0):
    s=s.replace('A0','0A',1)
    b+=1
    if(s.find('A0')==-1):
        print('{0}'.format(b))
        a=1

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
a=0
b=0
c=0
if(s.find('ABC')==-1):
    print('{0}'.format(b))
    a=1
while (a==0):
    s=s.replace('ABC','BCA',1)
    b+=1
    if(s.find('ABC')==-1):
        print('{0}'.format(b))
        a=1

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def merge(num_array, left, mid, right):
    '''
    配列num_arrayとその配列の部分配列を表す変数left, mid, rightを引数とし、部分配列をソートする。
    leftは部分配列の先頭要素であるがrightは部分配列の末尾+1である。
    '''
    L = deepcopy(num_array[left:mid])
    R = deepcopy(num_array[mid:right])
    inversionCnt = 0

    remainL = len(L)

    L.append(2)
    R.append(2)

    i = 0
    j = 0

    for k in range(left, right):
        if L[i] <= R[j]:
            num_array[k] = L[i]
            i += 1
            remainL -= 1
        else:
            num_array[k] = R[j]
            j += 1
            inversionCnt += remainL
    
    return inversionCnt

def SetnumberInversion(num_array, left, right, inversion_list):
    if left == right - 1:
        return
    
    mid = (left + right) // 2
    SetnumberInversion(num_array, left, mid, inversion_list)
    SetnumberInversion(num_array, mid, right, inversion_list)
    inversion_list.append(merge(num_array, left, mid, right))

def numberInversion(num_array):
    left = 0
    right = len(num_array)
    inversion_list = []

    SetnumberInversion(num_array, left, right, inversion_list)
    return (sum(inversion_list))
  
S = input()
S = S.replace("BC", "D")
S_list = S.split("B")
SS_list = []
for SS in S_list:
    SS_list.append(SS.split("C"))

allinversion = 0

for ss in SS_list:
    for s in ss:
        s = s.replace("D", "0")
        s = s.replace("A", "1")
        num_array = [int(num) for num in s]
        if s:
            allinversion += numberInversion(num_array)

print(allinversion)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
str1=re.sub('BC','D',s)
ans=0
while 'AD' in str1:
    n=str1.count('AD')
    ans+=n
    str1=str1.replace('AD','DA',n)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
str1=re.sub('BC','D',s)
ans=0
while 'AD' in str1:
    ans+=str1.count('AD')
    str1=re.sub('AD','DA',str1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
str1=re.sub('BC','D',s)
ans=0
while re.search('AD',str1):
    ans+=str1.count('AD')
    str1=re.sub('AD','DA',str1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
for n in range(len(s)):
    if s[n:n+2] == 'BC':
        s = s[:n]+'D'+s[n+2:]
#print(s)
ans = 0
cnta = 0
for n in range(len(s)):
    if s[n] == 'A':
        cnta += 1
    elif s[n] == 'D':
        ans += cnta
    elif s[n] == 'B' or s[n] == 'C':
        cnta = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
for n in range(len(s)):
    if s[n:n+2] == 'BC':
        s = s[:n]+'D'+s[n+2:]
#print(s)
ans = 0
cnta = []
for n in range(len(s)):
    if s[n] == 'A':
        cnta.append(0)
    elif s[n] == 'D':
        cnta = list(map(lambda x: x+1, cnta))
    elif s[n] == 'B' or s[n] == 'C':
        ans += sum(cnta)
        cnta = []

ans += sum(cnta)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.split('BC')
count = 0

for i in range(0,len(s)-1):
    j = 1
    while(j<=len(s[i]) and s[i][-j]=='A'):
        count = count + 1
        s[i+1] = 'A' + s[i+1]
        j = j + 1
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
cntA = 0
for i in range(0, len(s) - 1):
    if s[i] == 'A':
        cntA += 1
    elif s[i: i + 2] == "BC":
        cntANext = 0
        for j in range(0, cntA):
            ans += 1
            cntANext += 1
        cntA = cntANext
    elif i > 0 and s[i - 1: i + 1] == "BC":
        continue
    else:
        cntA = 0
print(ans)
# print(*data, sep='\n')