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
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=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:
#!/usr/bin/env python3
import sys

def conv2num(c):
    if c == "A":
        return 1
    elif c == "D":
        return 2
    elif c == "B":
        return 3
    else:
        return 4

def solve(s: str):

    res = 0

    s = s.replace("BC","D")
    s = [ conv2num(c) for c in s ]
    i = 0
    endi = len(s) - 1
    while i < endi:
        if s[i] == 1 and s[i+1] == 2:
            ai = i
            for j in range(i-1,-1,-1):
                if s[j] == 1 and j >=0 :
                    ai = j 
                else:
                    break
            s[ai]   = 2
            s[ai+1] = 1
            res += 1

            st = ai+2
            ed = i +2
            if st != ed:
                for k in range(st,ed):
                    s[k] = 1
                    res += 1
        i += 1

    print(res)

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

s=input()

#print(re.split('BB|CC|CB',s))

#print(s)

#s=s.replace("A","0")
s=s.replace("BC","1")
s=re.split('B|C|CB',s)
s= [x for x in s if x]

cnt=0
for i in s:
    
    start=0
    start=i.find("A")
    while start!=-1:
        
        
        if start<=len(i):
            cnt+=i[start:].count("1")
           
        start=i.find("A",start+1)
        
print(cnt)

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):
            s[j+2] = 0
            j -= 1
        
        s[j:i] = [0] * (i-j)
        s[j+2] = 2
        count += i - j

    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)

count = 0
for l in S_li:
    a = l.find('0')
    bc = l.rfind('1')
    if 0 <= a < bc:
        count += (bc-a)
        while True:
            i = a+1
            a = l[:bc].find('0',i)
            bc = l[:bc].rfind('1',i)
            if 0 <= a < bc:
                count += (bc-a)
            else:
                break
print(str(count))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python

import sys

s= sys.stdin.readline().strip()
n= len(s)
#
c= 0
#
while True:
    i = s.find("ABC")
    if i<0: break
    a = 1
    for j in range(i-1,-1,-1):
        if s[j]=="A": a+=1
        else: break
    #
    bc= 1
    for j in range(i+3,n,2):
        if s[j:j+2]=="BC": bc+=1
        else: break
    #
    j= i+2*bc
    s= ("A"*a) + s[j+1:]
    n-= j - a
    c+= a*bc
print( c )

Submission status: Time Limit Exceeded

Solution code:
import re

s=input()

#print(re.split('BB|CC|CB',s))

#print(s)

#s=s.replace("A","0")
s=s.replace("BC","1")
s=re.split('B|C|CB',s)


cnt=0
for i in s:
    
    start=0
    start=i.find("A")
    while start!=-1:
        
        
        if start<=len(i):
            cnt+=i[start:].count("1")
           
        start=i.find("A",start+1)
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()

if len(s)<3:
    print(0)
    exit()

i = 0
while True:
    if s[i:i+2]=='BC':
        s=s[:i]+'D'+s[i+2:]
    i+=1
    if i>=len(s):
        break


s1 = s.split('B')
s2 = []
for i in range(len(s1)):
    s2+= s1[i].split('C')


ans = 0

for i in range(len(s2)):
    d = s2[i].count('D')
    for j in range(len(s2[i])):
        if s2[i][j]=='A':
            ans += d
        else:
            d-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

def solve():
    s = [_ for _ in raw_input()]
    ans = 0
    chlist = []
    for i in range(len(s) - 2):
        if s[i] == 'A':
            chlist.append(i)
    found = True
    while found:
        found = False
        tmp_cl = deepcopy(chlist)
        for it in range(len(tmp_cl)):
            i = chlist[it]
            if s[i:(i+3)] == ['A', 'B', 'C']:
                s[i], s[i+1], s[i+2] = 'B', 'C', 'A'
                chlist[it] = i + 2
                ans += 1
                found = True
    print ans

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def Find_and_Replace(S,f_i):
    i = f_i
    count = 0
    new_f_i = f_i
    while i < len(S):
        if S[i] == 'D':
            if S[i-1] == 'A':
                S[i-1] = 'D'
                S[i] = 'A'

                if count == 0:
                    new_f_i = max(2,i-1)
                i += 1
                count += 1

            else:
                i += 2
        else:
                i += 1
        #print(i,S)
    return count,new_f_i

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

total_count = 0
f_i = 1
while True:

    count,new_f_i = Find_and_Replace(S,f_i)
    #print(count,f_i,new_f_i)
    f_i = new_f_i
    if count <= 0:
        break
    else:
        total_count += count

print(total_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):
            s[j+2] = 0
            j -= 1
            
        s[j+2] = 2
        count += i - j

    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

def dfs(st):
  q = st.count('AD')
  if q <= 0:
    return 0
  else:
    st = st.replace('AD', 'DA')
    return dfs(st) + q
    
print(dfs(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.replace("BC","D").replace("B","E").replace("C","E")
s = s.split("E")

def solve(s):
    ans = 0
    while "AD" in s:
        ans += s.count("AD")
        s = s.replace("AD","DA")
    return ans

ans = 0
for ele in s: ans += solve(ele)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

sl = s.replace('BC', 'D').replace('B', 'C').replace('AC', 'C').split('C')
sl = [x for x in sl if 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 = 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):
            count+= 1
            s[j+2] = 0
            j -= 1
            
        s[j+2] = 2

    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)

count = 0
for l in S_li:
    while True:
        a = l.find('0')
        bc = l.rfind('1')
        if 0 <= a < bc:
            count += (bc-a)
            l = l[a+1:bc]
        else:
            break

print(str(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

for i in range(len(s) -2):
        
    if(s[i]==0 and s[i+1]==1 and s[i+2] == 2):
        j = i
        while (s[j] == 0):
            count+= 1
            s[j+2] = 0
            j -= 1
            
        s[j+2] = 2

print(count)

Submission status: Time Limit Exceeded

Solution code:
S=input()
count=0
for i in range(2,len(S)-1):
    if S[i-2:i+1]=="ABC":
        count_A=1
        count_B=1
        k=i+2
        while S[k-1:k+1]=="BC" and k<=len(S)-1:
            k=k+2
            if S[k-1:k+1]=="BC":
                count_B+=1
        j=i-2
        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-1]+"BC"*count_B+"A"*count_A+S[i+count_B*2-1:len(S)]
        i=i+(count_B-1)*2
i=len(S)-1
if S[i-2:i+1]=="ABC":
        count_A=1
        j=i-2
        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()
S=""
i=0
leng=len(s)
while(i<leng):
    if s[i]=="A":
        S+="A"
        i+=1
    elif i+1<leng and s[i]=="B" and s[i+1]=="C":
        S+="B"
        i+=2
    else:
        S+="C"
        i+=1
T=S[::-1]
l=[0]*len(T)
ans=0
t=len(T)
for i in range(len(T)-1):
    if T[i]=="B":
        l[i+1]=l[i]+1
    elif T[i]=="A":
        l[i+1]=l[i]
        ans+=l[i]
if T[-1]=="A":
    ans+=l[-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

s=input()

#print(re.split('BB|CC|CB',s))

#print(s)

s=s.replace("A","0")
s=s.replace("BC","1")
s=re.split('B|C|CB',s)


cnt=0
for i in s:
    
    start=0
    start=i.find("0")
    while start!=-1:
        
        
        if start<=len(i):
            cnt+=i[start:].count("1")
           
        start=i.find("0",start+1)
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import re
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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

def main():
    S = input()

    count_num = 0
    # 再帰関数で切っていく
    while "ABC" in S:
        while True:
            if S[0] != "A":
                S = S[1:]
            elif S[:2] == "AC":
                S = S[2:]
            elif S[:3] != "ABC" and S[1:3] != "AB" and S[2] != "A":
                S = S[3:]
            else:
                break
        # print(S)
        count_num += S.count("ABC")
        S = S.replace("ABC", "BCA")
        # print("rep", S)
    print(count_num)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)
 
count = 0
for l in S_li:
    if len(l):
        while True:
            a = l.find('0')
            if a<0:
                break
            bc = l.rfind('1')
            if a < bc:
                l = l[a+1:bc]
                count += (bc-a)
            else:
                break
 
print(str(count))

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
ST = input().replace('BC', 'D')
S = list(ST)
search_char = 1
count = 0

while(search_char != len(S) and search_char <= len(S)):
    if S[search_char - 1] + S[search_char] == "AD":
        S[search_char - 1] , S[search_char] ="D", "A"
        search_char = max(search_char - 1, 1)
        count += 1
    else:
        search_char += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder B
"""

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column

# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']

def main():
    S = list(input()) # abc -> ['a','b','c']
    out=0
    a=0
    b=False
    while S:
        # print(S,a,out)
        s=S.pop(0)

        if s=="A":
            a+=1
            b=False
        elif s=="B":
            if b:
                b=False
                a=0
            else:
                b=True
        else:
            if b:
                out+=a
                # for _ in range(a):
                #     S.insert(0, "A")
            else:
                a=0
            # a=0
            b=False
                

        # if S[0]=="A":
        #     a+=1
        #     del S[0]
        # elif S[0:2]==["B","C"]:
        #     del S[0:2]
        #     out+=a

        #     for _ in range(a):
        #         S.insert(0, "A")
        #     a=0
        # else:
        #     del S[0]
        #     a=0
    print(out)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

ans = 0
while "AD" in s:
    ans += s.count("AD")
    s = s.replace("AD","DA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
cnt = 0
pat = re.compile('ABC')
while 1:
    c = pat.subn('BCA', s)
    if c[1] == 0:
        break
    s = c[0]
    cnt += c[1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=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:
# -*- coding: utf-8 -*-
s = input()
count = s.count("ABC")
tmp_count = -1
if count != 0: 
    while tmp_count != 0:
        s = s.replace("ABC", "BCA")
        tmp_count = s.count("ABC")
        count += tmp_count
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys

s =  [0] + [ord(i) for i in sys.stdin.readline()] + [0, 0]

ret = 0
i = 0

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

        s[i+2] = 65
        i += 2
        ret += 1 + j
        
    else :
        i += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
import re

s = input()
cnt = 0
while True:
    t = re.subn('ABC', 'BCA', s)
    s = t[0]
    if t[1] != 0:
        cnt += t[1]
    else:
        break
print(cnt)

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":
		s[i], s[i + 1], s[i + 2] = "B", "C", "A"
		# print("".join(s))
		ans += 1
		j = 1
		if i > 0:
			while s[i - j] == "A":
				s[i - j], s[i - j + 1], s[i - j + 2] = "B", "C", "A"
				ans += 1
				# print("".join(s))
				if i - j == 0:
					break
				j += 1
		k = 3
		if i < len(s) - 4:
			while s[i + k] == "B" and s[i + k + 1] == "C":
				s[i + k - 1], s[i + k], s[i + k + 1] = "B", "C", "A"
				ans += 1
				# print("".join(s))
				if i + k + 2 > len(s) - 1:
					break
				k += 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

s = input()
if not ('ABC' in s):
  print(0)
  exit()

from collections import deque
S = deque(maxlen=200000)
for ss in s:
  S.append(ss)

ans = 0
ihavea = 0
ihaveab = 0
#保持してる文字列
tmpS = ''
while 1:
  try:
    tmp = S.popleft()
  except:
    print(ans)
    exit()
  if ihavea == 0 and ihaveab==0 and (tmp=='B' or tmp=='C'):
    continue
  if ihavea == 0 and ihaveab==0 and tmp=='A':
    ihavea = 1
    tmpS += 'A'
    continue
  if ihavea == 1 and tmp=='A':
    ihavea = 1
    tmpS += 'A'
    continue
  if ihavea == 1 and tmp=='C': #ノーチャン
    ihavea = 0
    tmpS = ''
    continue
  #AABとかになるパターンをまだ考慮してない
  if ihavea == 1 and tmp=='B':
    ihaveab = 1
    ihavea = 0
    tmpS += 'B'
    continue
  if ihaveab == 1 and tmp=='A': #惜しいけどabは無駄になる
    ihaveab = 0
    tmpS = ''
    S.appendleft('A')
    continue
  if ihaveab == 1 and tmp=='B':
    ihaveab = 0
    tmpS = ''
    continue
  if ihaveab == 1 and tmp=='C':
    ihaveab = 0
    ans += len(tmpS)-1
    for _ in range(len(tmpS)-1):
      S.appendleft('A')
    tmpS = ''
#    if tmpS == 'AB': tmpS = ''
#    else:
#      tmpS = tmpS[0:-2] #末尾のABを取り除くだけ
#      if tmpS[-1]=='A': ihavea = 1
#      elif tmpS[-2:]=='AB': ihaveab = 1
    continue

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
first = True

for i in range(len(s) -2):
        
    if(s[i]==0 and s[i+1]==1 and s[i+2] == 2):
        j = i
        while (s[j] == 0):
            count+= 1
            s[j+2] = 0
            j -= 1
            
        s[j+2] = 2

    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import re


scan = sys.stdin.readline


def main():
    s = scan().rstrip()
    count = 0
    aaabcbcbc = re.compile("AAABCBCBC")
    aabcbc = re.compile("AABCBC")
    aabc = re.compile("AABC")
    abcbc = re.compile("ABCBC")
    abc = re.compile("ABC")

    while abc.search(s):
        if aaabcbcbc.search(s):
            s, tmp = aaabcbcbc.subn("BCBCBCAAA", s)
            count += tmp * 9
        elif aabcbc.search(s):
            s, tmp = aabcbc.subn("BCBCAA", s)
            count += tmp * 4
        elif aabc.search(s):
            s, tmp = aabc.subn("BCAA", s)
            count += tmp * 2
        elif abcbc.search(s):
            s, tmp = abcbc.subn("BCBCA", s)
            count += tmp * 2
        elif abc.search(s):
            s, tmp = abc.subn("BCA", s)
            count += tmp

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

def main():
    data = input()
    ans = 0
    while 'ABC' in data:
        ans += data.count('ABC')
        data = data.replace('ABC', 'BCA')

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()


def main():
    s = list(input())
    a = 0

    while True:
        t = s[:]
        c = 0

        p1 = p2 = ''
        for i, j in enumerate(s):
            if p1 == 'A' and p2 == 'B' and j == 'C':
                c += 1
                a += 1

                t[i - 2] = 'B'
                t[i - 1] = 'C'
                t[i] = 'A'

                p1, p2 = 'CA'
            else:
                p1, p2 = p2, j

        if not c:
            break

        s = t

    print(a)


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        if self.buffer.tell():
            return self.buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
s = input()
count = 0
count += s.count('ABC')
while count != 0:
    s = s.replace("ABC", "BCA")
    count += s.count('ABC')
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
length = len(s)
test = False
count = 0


def check():
    for i in range(length - 2):
        if s[i] == "A" and s[i + 1] == "B" and s[i + 2] == "C":
            return False
        else:
            pass
    return True


def change():
    for i in range(length - 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'
            global count
            count += 1


while not check():
    if len(s) < 3:
        count = 0
        break
    change()

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def Find_and_Replace(S,f_i):
    i = f_i
    count = 0
    new_f_i = 0
    while i < len(S):
        if S[i] == 'C':
            if S[i-2] == 'A' and S[i-1] == 'B':
                S[i-2] = 'B'
                S[i-1] = 'C'
                S[i] = 'A'

                if count == 0:
                    new_f_i = max(2,i-2)
                i += 2
                count += 1

            else:
                i += 3
                if count == 0:
                    new_f_i = i

        else:
            if S[i] == 'B':
                i += 1
            elif S[i] == 'A':
                i += 2
            else:
                i += 3
        #print(i,S)
    return count,new_f_i

S = list(input())
total_count = 0
f_i = 2
while True:
    count,new_f_i = Find_and_Replace(S,f_i)
    #print(count,f_i,new_f_i)
    f_i = new_f_i
    if count <= 0:
        break
    else:
        total_count += count

print(total_count)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
cnt = 0
#re.compile('ABC')

while 1:
    c = re.subn('ABC', 'BCA', s)
    if c[1] == 0:
        break
    s = c[0]
    cnt += c[1]
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys

fin = sys.stdin.readline
s_abc = fin().split()[0]

end_flag = True
count = 0
while end_flag:
    if 'ABC' not in s_abc:
        end_flag = False
    for i in range(len(s_abc)-2):
        tri_char = s_abc[i: i+3]
        if tri_char == 'ABC':
            count += 1
            s_abc = s_abc[:i] + 'BCA' + s_abc[i+3:]

print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len(s)
A=s.count('A')
ans=0

for i in range(A):
  l=s.rfind('A')
  k=l
  if k+2<n:
    for j in range(100):
      if s[k+1]=='B' and s[k+2]=='C':
        ans+=1
        if k+4<n:
          k+=2
        else:
          break
      else:
        break
  s=s[:l]+s[l+1:]
  n-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
s = 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:
import re
S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = re.split('B|C',N1)

count = 0
for l in S_li:
    if len(l):
        while True:
            a = l.find('0')
            if a<0:
                break
            bc = l.rfind('1')
            if a < bc:
                l = l[a+1:bc]
                count += (bc-a)
            else:
                break

print(str(count))

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 3
ans = 0
while 1:
  #  print(s)
    if len(s) < 3:
        break
    if count > len(s):
        break
    elif s[count-3:count] == "ABC":
        s = s[:count-3] + "BCA" + s[count:]
        count -= 2
        ans += 1
    count += 1
    if count < 3:
        count = 3
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len(s)
A=s.count('A')
ans=0

for i in range(A):
  l=s.rfind('A')
  k=l
  if k+2<n:
    for j in range(1000):
      if s[k+1]=='B' and s[k+2]=='C':
        ans+=1
        if k+4<n:
          k+=2
        else:
          break
      else:
        break
  s=s[:l]+s[l+1:]
  n-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())

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


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

count= 0
i = 0
first = True

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):
            count+= 1
            s[j+2] = 0
            j -= 1
            
        s[j+2] = 2

    i += 1
print(count)

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys

def check(abc_lst, idx, cnt):
    if abc_lst[idx:idx+3] == [1,2,3]:
        cnt += 1
        abc_lst[idx] = 2
        abc_lst[idx+1] = 3
        abc_lst[idx+2] = 1
        if idx > 0 and abc_lst[idx-1] == 1:
            cnt = check(abc_lst, idx-1, cnt)
    while idx < len(abc_lst) - 2:
        if abc_lst[idx+1:idx+4] == [1,2,3]:
            cnt = check(abc_lst, idx+1, cnt)
        idx += 1
    return cnt

sys.setrecursionlimit(10**9)

S = input()
ABC_dict = {'A':1, 'B':2, 'C':3}
abc_lst = [ABC_dict[S[i]] for i in range(len(S))]
print(check(abc_lst, 0, 0))

Submission status: Time Limit Exceeded

Solution code:
s = input()

if s.count('ABC') == 0:
  print(0)
  exit()

c = 0
  
while s.count('ABC') != 0:
  s = s.replace('ABC','BCA',1)
  c += 1

print(c)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = len(s)
c = n-1
ans = 0
while True:
    if c <2:
        break
    if s[c-2]+s[c-1]+s[c]=="ABC":
        ans += 1
        s[c-2]="B"
        s[c-1]="C"
        s[c]="A"
        if c+2 < n:
            c +=2
    else:
        if s[c-1] !="C":
            c -=2
        else:
            c -=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0

while "ABC" in s:
    ans += s.count("ABC")
    s = s.replace("ABC","BCA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
scount=0
i=0
while i < 1:
    if 'ABC' in S:
        scount += S.count('ABC')
        S=S.replace('ABC','BCA')
        i=0
    else:
        print(scount)
        i=2

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline
    S = list(input().strip())
    n = len(S)

    ans = 0
    cnt = 0
    for i in range(n):
        if S[i:i+3] == ['A', 'B', 'C']:
            cnt += 1
            S[i] = 'B'
            S[i+1] = 'C'
            S[i+2] = 'A'

    S = S[::-1]
    for i in range(n):
        if S[i:i+3] == ['C', 'B', 'A']:
            cnt += 1
            S[i] = 'A'
            S[i+1] = 'C'
            S[i+2] = 'B'

    ans += cnt
    while cnt > 0:
        cnt = 0
        S = S[::-1]
        for i in range(n):
            if S[i:i+3] == ['A', 'B', 'C']:
                cnt += 1
                S[i] = 'B'
                S[i+1] = 'C'
                S[i+2] = 'A'

        S = S[::-1]
        for i in range(n):
            if S[i:i+3] == ['C', 'B', 'A']:
                cnt += 1
                S[i] = 'A'
                S[i+1] = 'C'
                S[i+2] = 'B'

        ans += cnt


    return ans


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

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

s = input()
counter = 0
while 'ABC' in s:
        n = s.count('ABC')
        s = s.replace('ABC', 'BCA', n)
        counter += n
print(counter)

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:
li = input()
count = 0
while True:
  if li.find("ABC") == -1:
    break
  else:
    lis = list(li)
    a = li.find("ABC")
    lis[a] = "B"
    lis[a+1] = "C"
    lis[a+2] = "A"
    li = "".join(lis)
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

ans = 0

while "ABC" in s:
    cnt = s.count("ABC")
    s = s.replace("ABC","BCA")
    ans += cnt
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:

S = input()
N1 = S.replace('A','0').replace('BC','1')
S_li = N1.split('B')

count = 0
for bigstr in S_li:
    if len(bigstr):
        li = bigstr.split('C')
        for l in li:
            if len(l):
                while True:
                    a = l.find('0')
                    if a<0:
                        break
                    bc = l.rfind('1')
                    if a < bc:
                        l = l[a+1:bc]
                        count += (bc-a)
                    else:
                        break

print(str(count))