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:
# %%time
s = input()

k = 0
kk = 0
ans = 0
while True:
    
    if k >= len(s) - 1:
        break
    if s[k : k + 3] == "ABC":
        n = 0
        while True:
            if s[k + 3 + n  *2 : k + 5 + n * 2] == "BC":
                if s[k + 3 + n * 2 > len(s)]:
                    break
                n += 1
            else:
                break
                
        m = 0
        while True:
            ind = k-m-1
            if ind  < 0:
                break
            if s[ind] == "A":
                m+=1
            else:
                break
        
        if m + n == 0:                
            j = 0
            while True:
                if s[k + 3 + j * 3 > len(s)]:
                    break
                if s[k + 3 + j  *3 : k + 6 + j * 3] == "ABC":
                    j += 1
                else:
                    break
            s = s[:k] + "BC" * (j + 1) + "A" * (j + 1) + s[k+3 + j * 3:]
            kk = k
            k -= 1
            ans += (j + 1) * j // 2 + 1
            
        else:
            s = s[:k-m] + "BC" * (n + 1) + "A" * (m + 1) + s[k+3 + n * 2:]        
            kk = k
            k -= 1
            ans += (n + 1) * (m + 1)
        

    else:
        k += 1
        if kk > k:
            k = kk

print(ans)

Submission status: Time Limit Exceeded

Solution code:
ST = input()

def changeABC2BCA(ST):
    S = [s for s in ST]
    search_char = 2
    count = 0
    
    while(search_char != len(ST) and search_char <= len(ST)):
        if S[search_char - 2] + S[search_char - 1] + S[search_char] == "ABC":
            S[search_char-2] , S[search_char - 1] , S[search_char] ="B", "C", "A"
            search_char = max(search_char - 1, 2)
            count += 1
        else:
            search_char += 1
    
    return count
  
print(changeABC2BCA(ST))

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input().replace('BC', 'X')
    count = 0
    N = len(s)
    for i in range(N - 1):
        if s[i] == 'A':
            n = 1
            while i + n < N:
                if s[i + n] == 'A':
                    n += 1
                    continue
                elif s[i + n] == 'X':
                    n += 1
                    count += 1
                else:
                    break
    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = 0
while True:
    T = S.replace('ABC', 'BCA', 1)
    if T == S:
        break
    else:
        S = T
        n = n + 1
print(n)

Submission status: Time Limit Exceeded

Solution code:
s = input()
L = len(s)
s = s + "hh"
count = 0
new_count = 1
LIST = [L-1-i for i in range(L-2)]
while(new_count):
    new_list = []
    new_count = 0
    append = new_list.append
    for i in LIST:
        if s[i] == "C":
            if s[i-1] == "B":
                if s[i-2] == "A":
                    append(i+2)
                    append(i-1)
                    s = s[:i-2] + "BCA" + s[i+1:]
                    new_count += 1
    count += new_count
    LIST = [i for i in new_list]
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()

count = 0

# for i in range(1000000):
#     c = S.count('ABC')
#     if c == 0:
#         print(count)
#         quit()
#     count = count + c
#     S = S.replace('ABC', 'BCA', c)

for i in range(1000000):
    if 'ABC' not in S:
        print(count)
        quit()
    S = S.replace('ABC', 'BCA', 1)
    count = count + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()

pat = re.compile('ABC')
count = 0

while True:
    st = pat.search(s)
    if st:
        s = s.replace('ABC', 'BCA', 1)
        count += 1
    else:
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
from pprint import pprint

def solve(s):
    count = 0
    s_tmp = []
    for i in xrange(len(s)):
        s_tmp.append(s[i])
    i = 0
    while i < len(s) - 2:
        # print "i = ", i
        if s_tmp[i] == 'A' and s_tmp[i+1] == 'B' and s_tmp[i+2] == 'C':
            s_tmp[i] = 'B'
            s_tmp[i+1] = 'C'
            s_tmp[i+2] = 'A'
            # print s_tmp
            if i >= 1:
                i -= 2
            count += 1
        i += 1

    return count

if __name__ == '__main__':

    s = sys.stdin.readline().strip()
    print solve(s)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
base_str = input()
exp = "(A+)((BC)+)"
flg = True
res = 0
while(flg):
  m = re.search(exp, base_str)
  if m is None:
    break
  match_str = m.group(0)
  trans_num = len(m.group(1)) * len(m.group(2)) / 2
  res += int(trans_num)
  replace_str = "A" * len(m.group(1))
  base_str = replace_str + base_str[m.end(0):]
print(res)

Submission status: Time Limit Exceeded

Solution code:
s = input()

result = 0

result += s.count('AABCBC')

s = s.replace('AABCBC', 'ABCABC')

while 1 == 1:
    if 'AABC' in s:
        result += s.count('AABC')
        s = s.replace('AABC', 'ABCA')
    elif 'ABCBC' in s:
        result += s.count('ABCBC')
        s = s.replace('ABCBC', 'BCABC')
    else:
        break

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

print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
S = S[::-1]

ans = 0
bc_score = 0
flag = 1

if len(S) <= 1:
    print(0)
    exit()

while len(S) != 0:
    if S[0]=="C" and S[1] == "B":
        bc_score = 0
        bc_score += 1
        S.pop(0)
        S.pop(0)
        flag = 1
        if len(S) == 1:
            if S[0] == "A":
                ans += 1
            S.pop(0)
        while flag == 1 and len(S) != 0:
            flag = 0
            if S[0]=="C" and S[1] == "B":
                bc_score += 1
                S.pop(0)
                S.pop(0)
                flag = 1
            if len(S) != 0:
                if S[0] == "A":
                    ans += bc_score
                    S.pop(0)
                    flag = 1
    else:
        S.pop(0)

print(ans)

Submission status: Time Limit Exceeded

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


def solve(s: str):

    res = 0

    s = list(s)

    i = 0
    endi = len(s) - 2
    while i < endi:
        if s[i] == "A" and s[i+1] =="B" and s[i+2] =="C":
            ai = -1
            for j in range(i-1,-1,-1):
                if j >=0 and s[j] == "A":
                    ai = j 
                else:
                    break
            if ai >= 0:
                s[ai]   = "B"
                s[ai+1] = "C"
                s[ai+2] = "A"

                res += 1
                for k in range(ai+3,i+3):
                    s[k] = "A"
                    res += 1
            else:
                s[i]   = "B"
                s[i+1] = "C"
                s[i+2] = "A"
                res += 1
            i += 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:
# -*- coding: utf-8 -*-



S = list("Z"+str(input()))
idx = 1
l = len(S)

ans = 0
while(idx <= l-3):
  if S[idx]=="A" and S[idx+1]=="B" and S[idx+2] == "C":
    ans += 1
    S[idx],S[idx+1],S[idx+2] = ["B","C","A"]
    idx -= 1
  else:
    idx += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())

s = list()
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):

        count += 1
        s[i] = 1
        s[i+1] = 2
        s[i+2] = 0

        if(i == 1):
            i -= 2
        if(i >2):
            i -= 2
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit
setrecursionlimit(1000000000)
s = input()
S = list(s)

abc = ['A','B','C']
bca = ['B','C','A']
bc = ['B','C']
ans = 0
if S[0:3] == abc:
    ans += 1
    S[0:3] = bca

def check(i:int):
    global S
    global ans
    # print(S)
    if S[i] == 'A':
        if S[i+1:i+3] == bc:
            ans += 1
            S[i:i+3] = bca
            check(i-1)

for i in range(len(s)-2):
    check(i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

s = input()
s = s[::-1]
ans = 0
if s.count('CBA') != 0:
    for i in range(s.count('A')):
        if s.find('CBA') < s.find('A'):
            a = re.search('(CB)+A', s)
            ans += (a.end()-a.start()-1)//2
        s = s.replace('A', '', 1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt  = 0
while "ABC" in s:
  s = s[::-1].replace("CBA", "ACB", 1)[::-1]
  cnt = cnt + 1

print(cnt)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:

s=input()


target = 'ABC'

count=0
debug=0
while True:
    n=s.count("ABCABC")
    s=s.replace("ABCABC","BCBCAA",n)
    count+=3*n
    n=s.count("ABCBC")
    s=s.replace("ABCBC","BCBCA",n)
    count+=2*n
    n=s.count("ABC")
    s=s.replace("ABC","BCA",n)
    count+=n
    if n==0:
        break
    #debug+=1
    #if debug>10:
    #    break
print(count)

Submission status: Time Limit Exceeded

Solution code:
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    s = input()
    ope_num = 0
    index = 0
    a_stock = 0
    b_stock = 0
    s = list(s)
    while index < len(s):
        si = s[index]
        # print(index)
        # print(si)
        # print(a_stock)
        # print(b_stock)
        # print(ope_num)
        # print()
        if si == "A":
            if b_stock > 0:
                b_stock = 0
                a_stock = 1
            else:
                a_stock = 1
        elif si == "B" and a_stock > 0 and b_stock == 0:
            b_stock = 1
        elif si == "C" and a_stock > 0 and b_stock > 0:
            ope_num = ope_num + 1
            s[index - 2] = "B"
            s[index - 1] = "C"
            s[index] = "A"
            a_stock = 0
            b_stock = 0
            if index - 4 >= -1:
                index = index - 4
            else:
                index = index - 1
        else:
            a_stock = 0
            b_stock = 0
        index = index + 1
    print(ope_num)

# ABCの数
# ABCの前にあるAの分だけ追加で操作できる
# ABCの後にある(BC)の分だけ追加で操作できる

# Aがあれば覚えておく
# ABCと来たら、それより前側に隣接しているAの回数だけ追加で操作できる
# 見つかったABCをBCAに置き換える
# BCAのAから走査を継続する

Submission status: Time Limit Exceeded

Solution code:
A = input() 
found = False
count = 0
while not found:
    n = A.count("ABC")
    if n == 0:
        found = True
    else:
        count += n
        A = A.replace("ABC","BCA")
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()

count = 0

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
total_count = 0
while True:
    i = 2
    count = 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'
                count += 1
                i += 2
            else:
                i += 3
        else:
            if S[i] == 'B':
                i += 1
            elif S[i] == 'A':
                i += 2
            else:
                i += 3

    if count <= 0:
        break
    else:
        total_count += count
print(total_count)

Submission status: Time Limit Exceeded

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

while True:
    i = 0
    flag = True
    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 += 1
            flag = False
    if(flag):
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
S = S[::-1]

ans = 0
bc_score = 0
flag = 1

if len(S) <= 1:
    print(0)
    exit()

while len(S) != 0:
    if S[0]=="C" and S[1] == "B":
        bc_score = 0
        bc_score += 1
        S.pop(0)
        S.pop(0)
        flag = 1
        if len(S) <= 1:
            if S[0] == "A":
                ans += 1
            S.pop(0)
        while flag == 1 and len(S) != 0:
            flag = 0
            if S[0]=="C" and S[1] == "B":
                bc_score += 1
                S.pop(0)
                S.pop(0)
                flag = 1
            if S[0] == "A":
                ans += bc_score
                S.pop(0)
                flag = 1
    else:
        S.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# %%time
s = input()

k = 0
kk = 0
ans = 0
while True:
    
    if k >= len(s) - 1:
        break
    if s[k : k + 3] == "ABC":
        n = 0
        while True:
            if s[k + 3 + n  *2 : k + 5 + n * 2] == "BC":
                if s[k + 3 + n * 2 > len(s)]:
                    break
                n += 1
            else:
                break
                
        m = 0
        while True:
            ind = k-m-1
            if ind  < 0:
                break
            if s[ind] == "A":
                m+=1
            else:
                break
        
#         if m + n > 0:                
        s = s[:k-m] + "BC" * (n + 1) + "A" * (m + 1) + s[k+3 + n * 2:]        
        kk = k
        k -= 1
        ans += (n + 1) * (m + 1)
#         else:
#             j = 0
#             while True:
#                 if s[k + 3 + j  *3 : k + 5 + n * 2] == "BC":
#                     if s[k + 3 + n * 2 > len(s)]:
#                         break
#                     n += 1
#                 else:
#                     break
        

    else:
        k += 1
        if kk > k:
            k = kk

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()

cnt=0
for j in range(len(s)):
  i=(len(s)-1)-j
  if s[i]=='A':
    flg=True
    k=1
    while flg:
      if s[i+2*(k-1)+1:i+2*k+1]=='BC':
        k+=1
      else:
        k+=-1
        flg=False
    if k>0:
      s=s[:i] + 'BC'*k + 'A' + s[i+2*k+1:]
      cnt+=k  
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
while True:
    if tmp==len(s)-2: break
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        if tmp>0: tmp-=1
        ans+=1
    
    else: tmp+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit
setrecursionlimit(1000000000)
s = input()
S = list(s)

abc = ['A','B','C']
bca = ['B','C','A']
ans = 0
if S[0:3] == abc:
    ans += 1
    S[0:3] = bca

def check(i:int):
    global S
    global ans
    # print(S)
    if S[i] == 'A':
        if S[i:i+3] == abc:
            ans += 1
            S[i:i+3] = bca
            check(i-1)

for i in range(len(s)-2):
    check(i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

#TLE版

ans = 0
tmp = 1
while 1:
    if tmp != 1:
        break
    tmp = 0
    for i in range(len(s)):
        if i >= len(s) - 2:
            continue
        if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
            if i - 1 > 0 and s[i-1] == 'A' and i+4 < len(s) and s[i+3] == 'B' and s[i+4] == 'C':
                s[i-1] = 'B'
                s[i] = 'C'
                s[i+1] = 'B'
                s[i+2] = 'C'
                s[i+3] = 'A'
                s[i+4] = 'A'
                tmp = 1
                ans += 4
            elif i+4 < len(s) and s[i+3] == 'B' and s[i+4] == 'C':
                s[i] = 'B'
                s[i+1] = 'C'
                s[i+2] = 'B'
                s[i+3] = 'C'
                s[i+4] = 'A'
                tmp = 1
                ans += 2
            elif i - 1 > 0 and s[i-1] == 'A':
                s[i-1] = 'B'
                s[i] = 'C'
                s[i+1] = 'A'
                s[i+2] = 'A'
                tmp = 1
                ans += 2
            else:
                s[i] = 'B'
                s[i+1] = 'C'
                s[i+2] = 'A'
                tmp = 1
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

S = str(input())
k=int(0)

for i in range(20000):
#while S.find('ABC')>=0:
#    S = S[:S.rfind('ABC')] + 'BCA' + S[(S.rfind('ABC')+3):]
#    k +=1
    matchedList = re.findall("ABC",S)
    k += len(matchedList)
    S = S.replace('ABC', 'BCA')

ans = (int(k))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

count = 0

while(True):
    for s in range(len(S)):
        if S[s] == 'A':
            if s+2 < len(S) and S[s+1] == 'B' and S[s+2] == 'C':
                S[s] = 'B'
                S[s+1] = 'C'
                S[s+2] = 'A'
                count += 1
                #print(S)

    #print('終了確認')
    count2 = 0
    for s in range(len(S)):
        if S[s] == 'A':
            if s+2 < len(S) and S[s+1] == 'B' and S[s+2] == 'C':
                count2 += 1
    #print(count2)
    if count2 == 0:
        break
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = [ord(c) for c in list(input())]
length = len(S)

count = 0
i = 0
if length > 2:
    # i == 0
    s = S[i] + S[i + 1] + S[i + 2]
    if S[i] == 65 and S[i + 1] == 66 and S[i + 2] == 67:
        S[i] = 66
        S[i + 1] = 67
        S[i + 2] = 65
        count += 1
    # i > 0
    while i < length - 2:
        # ABC順不同
        s = S[i] + S[i + 1] + S[i + 2]
        if s == 198:
            # A, B
            if S[i] == 65 and S[i + 1] == 66:
                S[i] = 66
                S[i + 1] = 67
                S[i + 2] = 65
                count += 1
                if S[i - 1] == 65:
                    i -= 1
                else:
                    i += 2
            else:
                i += 1
        else:
            i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

def main():
    s = input()
    c = 0

    pat = re.compile('A*ABC')
    while True:
        m = pat.search(s)
        if not m:
            break
        n = len(m.group()) - 2
        t = 'BC' + 'A' * n
        s = s[:m.start()] + t + s[m.end():]
        c += n

    print(c)
        


main()

Submission status: Time Limit Exceeded

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

ans = 0
while(True):
    cnt = s.count("ABC")
    if cnt == 0:
        break
    ans += cnt
    s = s.replace("ABC", "BCA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ans=0
while s.count('AABCBC')>0:
    ans+=s.count('AABCBC')*4
    s=s.replace('AABCBC','BCBCAA')
while s.count('ABCBC')>0:
    ans+=s.count('ABCBC')*2
    s=s.replace('ABCBC','BCBCA')
while s.count('AABC')>0:
    ans+=s.count('AABC')*2
    s=s.replace('AABC','BCAA')
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(input())
l = len(S)
i = 0
j = -1
ans = 0
while i<l:
    if j==-1 and S[i] == "A":
        j=i
        i+=1
    elif j>=0 and S[i] == "A":
        i+=1
    elif j>=0:
        #"BC"が続く回数を数える
        bc = 0
        k = 0
        for c in S[i:]:
            if bc == 0 and c=="B":
                bc=1
            elif bc == 1 and c=="C":
                bc=0
                k+=1
            else:break
        if k > 0 :
            S[j+2*k-1] = "C"
            S[j+2*k:i+2*k] = ["A"]*(i-j)
            ans += (i-j)*k
            i = j+2*k-1
            j = -1
        else:
            j = -1
            i+=1
    else:
        i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#
#　　　　　　　　　　　　　　,. -─────────‐- .、
#　　　　　　　　　　　　 ／／￣￣＼　　　　　　／￣￣＼＼
#　　　　　　　　　　　／　　　　　　　　　　　　　　　　　　　　 ＼
#　　　　　　　　　 ／　　　　　　　　::::::::::::::::::::::::::::::::　　　　　　　＼
#　　　　　　　　／ 　　／ ／￣＼＼::::;;;;;;;;;;;;;;;;;:::::／／￣＼＼　　＼
#　　　　　　 ／　　　　|　 |.　┃　.|　| ::::;;;;;;;;;;;;;::::｜ |.　┃　.|　|　　　 ＼　　　　　　　　　　　　　　　　　　　　　
#　　　　　／ 　　　　　＼ ＼＿／／　::::::::::::::::::　＼＼＿／／　　　　　 ＼　　　　
#　　　 ／　　　　　..／￣￣＼　／　　　::｜::　　　＼　／￣￣＼..　　　　　＼
#　　 / 　 　 　 　 :::::　　　　　　|　　　　　 |　　　　　 |　　　　　　 :::::　　　　　 ヽ.　　　
#　　| 　　　　　　　　　　　　　　|　　　　　 |　　　　　 |　　　　　　　　　　　　　　|.
#　　| 　　　　　　　　　　　　　　＼＿＿／＼＿＿／ 　　　　　　　　　　　　　　|　　　　　　　　　　　　　　　　　
#　　| 　　　　　　　　　　　　　　　｜　　　　　　　｜　　　　　　　　　　　　　　　|
#　　| 　　　　　　　　　　　　　　　｜r─‐┬──､|　　　　　　　　　　　　　　　 |
#　　ヽ　　　　　　　　　　　　　　　 |/　　　|　　 　｜　　　　　　　　　　　　　　/
#　　　 ＼　　　　　　　　　　　　　 ＼　　　　　　／　　　　　　　　　　　　　／
#　　　　　＼ 　　　　　　　　　　　　　￣￣￣￣　　　　　　　　　　　　　／
#
#
import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    s = ST()
    S=list(s)
    #愚直解
    frag = True
    cnt=0
    while frag:
        frag = False
        for i in range(len(S) - 2)[::-1]:
            if S[i:i + 3] == list("ABC"):
                frag = True
                cnt+=1
                S[i:i + 3] = "BCA"
    print(cnt) 
    """
    ans=0
    while 1:
        c = s.count("ABC")
        if c == 0:
            break
        ans += c
        s=s.replace("ABC", "BCA")
    print(ans)
    
    ans=0
    while index < len(S):
        if S[index] == A:
            bcindex =index+1
            while bcindex < len(S)-1:
                if S[bcindex:bcindex + 2] == list("BC"):
                    ans += 1
                    bcindex += 2
                    frag = True
                else:
                    break
            if frag:
                index = bcindex - 1
                S[index]="A"
            else:
                index = bcindex         
        index +=1
"""

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
while True:
    if "ABC" in s:
        i = s.find("ABC")
        ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
        s = "".join(ss)
        ss = [si for si in s]
        cnt += 1
    else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().rstrip()
cnt = 0
while 1:
    a = s.replace('ABC', 'BCA', 1)
    if a == s:
        break
    else:
        s = a
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

count = 0
tmp_list = list(range(len(S)-2))

while tmp_list != []:
    new_list = []
    for i in set(tmp_list):
        if "".join(S[i:i+3]) == "ABC":
            count += 1            
            S[i:i+3] = ["B", "C", "A"]
            
            if i-1 >= 0 and S[i-1] == "A":
                new_list.extend([i-1, i+2])
            else:
                new_list.extend([i+2])
    tmp_list = new_list

print(count)

Submission status: Time Limit Exceeded

Solution code:
moji = input()
a = [moji[i:i+1] for i in range(len(moji))]
b = a[1:]
c = a[2:]

cnt = 0
for i in range(a.count('A')):
	for a1,a2,a3,n in zip(a,b,c,range(len(c))):
		if a1 == 'A' and a2 == 'B' and a3 == 'C':
			cnt += 1
			a[n]   = 'B'
			a[n+1] = 'C'
			a[n+2] = 'A'
			b[n]   = 'C'
			b[n+1] = 'A'
			c[n]   = 'A'	
			if n >= 1:
				b[n-1] = 'B'
				c[n-1] = 'C'
			if n >= 2:
				c[n-2] = 'A'
                
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter as D, defaultdict as dd
from math import factorial as ft, ceil as aron
from fractions import gcd as pg 
from bisect import insort as ins, bisect_left as bl, bisect_right as br
import sys

#sys.stdin=open("./testfile1.py","r")
raw_input=sys.stdin.readline

f1=lambda : map(int,raw_input().split())
f2=lambda : map(int,list(raw_input()))
f3=lambda : int(raw_input())
f4=lambda : raw_input().rstrip()

st=[]
s=f4()
n1=0
ans=0
for i in s:
	n1+=1
	st.append(i)
	if n1>=3:
		s1=""
		for i in xrange(3):
			s1+=st.pop()
		s1=s1[::-1]
		if s1=="ABC":
			ans+=1
			s1="BCA"
			while st and st[-1]=="A":
				s1+="A"
				ans+=1
				st.pop()
			st.extend(list(s1))

		else:
			st.extend(list(s1))

print ans

Submission status: Time Limit Exceeded

Solution code:
s=input()

cnt=0
for j in range(len(s)):
  i=(len(s)-1)-j
  if s[i]=='A':
    flg=True
    k=1
    while flg:
      if s[i+1:i+2*k+1]=='BC'*k:
        k+=1
      else:
        k+=-1
        flg=False
    if k>0:
      s=s[:i] + 'BC'*k + 'A' + s[i+2*k+1:]
      cnt+=k  
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#
#　　　　　　　　　　　　　　,. -─────────‐- .、
#　　　　　　　　　　　　 ／／￣￣＼　　　　　　／￣￣＼＼
#　　　　　　　　　　　／　　　　　　　　　　　　　　　　　　　　 ＼
#　　　　　　　　　 ／　　　　　　　　::::::::::::::::::::::::::::::::　　　　　　　＼
#　　　　　　　　／ 　　／ ／￣＼＼::::;;;;;;;;;;;;;;;;;:::::／／￣＼＼　　＼
#　　　　　　 ／　　　　|　 |.　┃　.|　| ::::;;;;;;;;;;;;;::::｜ |.　┃　.|　|　　　 ＼　　　　　　　　　　　　　　　　　　　　　
#　　　　　／ 　　　　　＼ ＼＿／／　::::::::::::::::::　＼＼＿／／　　　　　 ＼　　　　
#　　　 ／　　　　　..／￣￣＼　／　　　::｜::　　　＼　／￣￣＼..　　　　　＼
#　　 / 　 　 　 　 :::::　　　　　　|　　　　　 |　　　　　 |　　　　　　 :::::　　　　　 ヽ.　　　
#　　| 　　　　　　　　　　　　　　|　　　　　 |　　　　　 |　　　　　　　　　　　　　　|.
#　　| 　　　　　　　　　　　　　　＼＿＿／＼＿＿／ 　　　　　　　　　　　　　　|　　　　　　　　　　　　　　　　　
#　　| 　　　　　　　　　　　　　　　｜　　　　　　　｜　　　　　　　　　　　　　　　|
#　　| 　　　　　　　　　　　　　　　｜r─‐┬──､|　　　　　　　　　　　　　　　 |
#　　ヽ　　　　　　　　　　　　　　　 |/　　　|　　 　｜　　　　　　　　　　　　　　/
#　　　 ＼　　　　　　　　　　　　　 ＼　　　　　　／　　　　　　　　　　　　　／
#　　　　　＼ 　　　　　　　　　　　　　￣￣￣￣　　　　　　　　　　　　　／
#
#
import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    s = ST()
    """S=list(s)
    #愚直解
    frag = True
    cnt=0
    while frag:
        frag = False
        for i in range(len(S) - 2)[::-1]:
            if S[i:i + 3] == list("ABC"):
                frag = True
                cnt+=1
                S[i:i + 3] = "BCA"
    print(cnt)"""
    ans=0
    while 1:
        c = s.count("ABC")
        if c == 0:
            break
        ans += c
        s=s.replace("ABC", "BCA")
    print(ans)        
    """
    ans=0
    while index < len(S):
        if S[index] == A:
            bcindex =index+1
            while bcindex < len(S)-1:
                if S[bcindex:bcindex + 2] == list("BC"):
                    ans += 1
                    bcindex += 2
                else:
                    break
            checked = bcindex
        elif                     
        index +=1
    pass"""


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    s = input()
    ope_num = 0
    index = 0
    a_stock = []
    b_stock = []
    s = list(s)
    while index < len(s):
        si = s[index]
        # print(index)
        # print(si)
        # print(a_stock)
        # print(b_stock)
        # print(ope_num)
        # print()
        if si == "A":
            if len(b_stock) > 0:
                b_stock = []
                a_stock = [si]
            else:
                a_stock.append(si)
        elif si == "B" and len(a_stock) > 0 and len(b_stock) == 0:
            b_stock.append(si)
        elif si == "C" and len(a_stock) > 0 and len(b_stock) > 0:
            ope_num = ope_num + 1
            s[index - 2] = "B"
            s[index - 1] = "C"
            s[index] = "A"
            a_stock = []
            b_stock = []
            if index - 4 >= -1:
                index = index - 4
            else:
                index = index - 1
        else:
            a_stock = []
            b_stock = []
        index = index + 1
    print(ope_num)

# ABCの数
# ABCの前にあるAの分だけ追加で操作できる
# ABCの後にある(BC)の分だけ追加で操作できる

# Aがあれば覚えておく
# ABCと来たら、それより前側に隣接しているAの回数だけ追加で操作できる
# 見つかったABCをBCAに置き換える
# BCAのAから走査を継続する

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()
ct = 0

while True:
  D = re.findall('A+BC',S)
  if len(D)==0:
    break
  for d in D:
    num = len(d)-2
    R=re.subn(d, 'BC'+'A'*num, S)
    S=R[0]
    ct+=R[1]*num
print(ct)

Submission status: Time Limit Exceeded

Solution code:
s=input()
i=-1
t=""
while s!=t:
  i+=1
  t=s
  s=s.replace("ABC","BCA",1)
print(i)

Submission status: Time Limit Exceeded

Solution code:
s = input()
arr = list(s)

num = 0
while True:
  r = s.find("ABC")
  
  if r >= 0:
    num += 1
    arr[r:r+3] = ["B", "C", "A"]
    s = "".join(arr)
  else:
    break

print(num)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

cnt = 0
# print(s, cnt)

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

print(cnt)