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 = input()
    s_long = len(s)
    kaisu = 0
    check = 0
    while 1:
        if check > s_long - 3:
            print(kaisu)
            return
        if s[check:check+3] == 'ABC':
            s = s[:check] + 'BCA' + s[check+3:]
            kaisu += 1
            check += 2
            i = check - 3
            while 1:
                if i < 0:
                    break
                else:
                    if s[i] != 'A':
                        break
                    else:
                        s = s[:i] + 'BCA' + s[i+3:]
                        kaisu += 1
                        i -= 1
        else:
            check += 1

if __name__ == "__main__":
    main()

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 :
        if s[i - 1] == 65 :
            s[i] += 1
            s[i+1] += 1
            s[i+2] -= 2
            i -= 1
        else :
            s[i+2] -= 2
            i += 2
        ret += 1
        
    else :
        i += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
s = list(input().rstrip())
ans = 0
start = 0
while start < len(s) - 2:
    l1, l2, l3 = s[start], s[start + 1], s[start + 2]
    if l1 == 'A' and l2 == 'B' and l3 == 'C':
        s[start] = 'B'
        s[start + 1] = 'C'
        s[start + 2] = 'A'
        if start > 0:
            start = start - 1
        else:
            start = start + 1
        ans += 1
    else:
        start += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

cnt = 0
x = 0
while x <=len(s):
    if s[x:x+3] == "ABC":
        s = s[:x]+"BCA"+s[x+3:]
        cnt += 1
        for y in range(0,len(s),2):
            if s[x+3+y:x+5+y] == "BC":
                s = s[:x+5+y-3]+"BCA"+s[x+5+y:]
                cnt += 1
            else:
                break
        if x>0:
            for z in range(1,len(s)):
                if s[x-z] == "A":
                    s = s[:x-z]+"BCA"+s[x-z+3:]
                    cnt += 1
                else:
                    break
    x += 1
print(cnt)

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:
        tpl = re.subn('ABC', 'BCA', s)
        s = tpl[0]
        count += tpl[1]
    else:
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()

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

s=s.split()


ans=0

for i in range(len(s)):
    while 1:
        s[i],x=re.subn('AD','DA',s[i])
        if x==0:
            break
        ans+=x

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
counter = 0
answer = 0
for i in range(len(s)):
  if s[i] == "A":
    counter = 1
  elif s[i] == "B":
    if counter == 1:
      counter = 2
    else:
      counter = 0
  else: # i == C
    if counter == 2:
      j = i-3
      answer += 1
      while j >= 0 and s[j] == "A":
        s[j+3] = "A"
        answer += 1
        j -= 1
      s[j+1] = "B"
      s[j+2] = "C"
      s[j+3] = "A"
      counter = 1
    else:
      counter = 0
print(answer)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

count = 0
i = 0
while i < len(S) - 2:
    if S[i] == "A" and S[i + 1] == "B" and S[i + 2] == "C":
        S[i] = "B"
        S[i + 1] = "C"
        S[i + 2] = "A"
        count += 1
        i -= 2
    i += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def serch(s, index, li):
    h = []
    x = len(s) - 4
    for i in li:
        if s[i] + s[i+1] + s[i+2] == 'ABC':
            if 0 < i:
                if s[i-1] == 'A':
                    h.append(i-1)
            if i < x:
                h.append(i+2)
            s = s[0:i] + 'BCA' + s[i+3:]
            index += 1
    h = list(set(h))

    return s, index, h

S = input()

li = []

for i in range(len(S) - 2):
    if S[i] == 'A':
        li.append(i)

ans = 0

while len(li):
    S, ans, li = serch(S, ans, li)

print(ans)

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":
            b_stock = 0
            a_stock = 1
        elif a_stock > 0:
            if si == "B" and b_stock == 0:
                b_stock = 1
            elif si == "C" 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
        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:
s = list(input())
def change(i):
    global s
    #print(i)
    if s[i : i + 3] == ["A", "B", "C"]:
        s[i:i + 3] = ["B", "C", "A"]
        #print(s)
        return 1
    return 0

cnt = 0
for i in range(0,  len(s)):
    if change(i):
        cnt += 1
        for j in range(i - 1, -1, -1):
            if not change(j):
                break
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
from pprint import pprint

def solve(s):
    count = 0
    s_tmp = []
    l = len(s)
    for i in xrange(l):
        s_tmp.append(s[i])
    i = 0
    while i < l - 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 = list(input())
counter = 0
answer = 0
i = 0
while i < len(s):
  if s[i] == "A":
    counter = 1
  elif s[i] == "B":
    if counter == 1:
      counter = 2
    else:
      counter = 0
  else: # i == C
    if counter == 2:
      j = i-3
      answer += 1
      while j >= 0 and s[j] == "A":
        s[j+3] = "A"
        answer += 1
        j -= 1
      s[j+1] = "B"
      s[j+2] = "C"
      s[j+3] = "A"
      counter = 1
    else:
      counter = 0
  i += 1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s = input()
res = 0
while 'ABC' in s:
    tmp = s.split('ABC')
    res += len(tmp) - 1
    s = 'BCA'.join(tmp)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import re

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

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

    print(c)
        


main()

Submission status: Time Limit Exceeded

Solution code:

S = [x for x in input()]
N = len(S)

i = N - 1
count = 0
while i >= 0:
    j = i
    while i < N:
        if i >= 2:
            if S[i-2] == 'A' and S[i-1] == 'B' and S[i] == 'C':
                S[i-2] = 'B'
                S[i-1] = 'C'
                S[i] = 'A'
                i += 2
                count += 1
            else:
                break
        else:
            break
            
    i = j - 1
    
print(count)

Submission status: Time Limit Exceeded

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

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

cnt = 0
updated = True
while updated:
    newlist = []
    updated = False
    for i in range(len(listA)):
        if "C" in s[listA[i]:] == False:
            # このAはレンジ外になるのでもう使わない
            pass
        if "B" in s[listA[i]:] == False:
            # このAはレンジ外になるのでもう使わない
            pass
        elif s[listA[i] : listA[i] + 3] == "ABC":
            cnt += 1
            s = s[:listA[i]] + "BCA" + s[listA[i] + 3:]
            #print("i={}, s={}".format(i, s))
            newlist.append(listA[i] + 2)
            updated = True
        else:
            newlist.append(listA[i])
    listA = newlist

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = []
for c in s:
    l.append(c)
count = 0
i = 0
last_abc = []
while i+2 < len(l):
    if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
        last_abc.append(i)
        i += 3
        continue
    i += 1
    if len(last_abc) > 0:
        j = last_abc[0]
        for _ in range(len(last_abc)):
            l[j] = 'B'
            l[j+1] = 'C'
            j += 2
        for _ in range(len(last_abc)):
            l[j] = 'A'
            j += 1
        count += (len(last_abc) + 1) * len(last_abc) // 2
        last_abc = []

if len(last_abc) > 0:
    j = last_abc[0]
    for _ in range(len(last_abc)):
        l[j] = 'B'
        l[j+1] = 'C'
        j += 2
    for _ in range(len(last_abc)):
        l[j] = 'A'
        j += 1
    count += (len(last_abc) + 1) * len(last_abc) // 2

indices = set()
for i in range(len(l) - 2):
    if l[i] == 'A' and l[i+1] == 'B' and l[i+2] == 'C':
        count += 1
        l[i] = 'B'
        l[i+1] = 'C'
        l[i+2] = 'A'
        if i > 0 and l[i-1] == 'A':
            indices.add(i-1)
        if i+4 < len(l):
            indices.add(i+2)

while len(indices) > 0:
    new_indices = set()
    for i in indices:
        if l[i] == 'A' and l[i+1] == 'B' and l[i+2] == 'C':
            count += 1
            l[i] = 'B'
            l[i+1] = 'C'
            l[i+2] = 'A'
            if i > 0 and l[i-1] == 'A':
                new_indices.add(i-1)
            if i+4 < len(l):
                new_indices.add(i+2)
    indices = new_indices

print(count)

Submission status: Time Limit Exceeded

Solution code:
ST = input()

def changeABC2BCA(ST):
    S = list(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:
            if S[search_char - 1] == 'A':
                search_char += 1
            elif S[search_char] == 'A':
                search_char += 2
            else:
                search_char += 3
    
    return count
  
print(changeABC2BCA(ST))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
ans = 0
while 'ABC' in s:
    ans += len(re.findall('ABC',s))
    s = s.replace('ABC','BCA')
print(ans)

Submission status: Time Limit Exceeded

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

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

def solve(s: str):

    res = 0

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

                res += 1
                for k in range(ai+3,i+3):
                    s[k] = 1
                    res += 1
            else:
                s[i]   = 2
                s[i+1] = 3
                s[i+2] = 1
                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:
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 :
        s[i] += 1
        s[i+1] += 1
        s[i+2] -= 2

        ret += 1
        i -= 1
    else :
        i += 1

print(ret)

Submission status: Time Limit Exceeded

Solution code:
import re
base_str = input()
exp = "(A+)((BC)+)"
flg = True
res = 0
tmp_end = 0
tmp_A = 0
for m in re.finditer(exp, base_str):
  A_len = len(m.group(1)) 
  if tmp_end == m.start(0):
    A_len += tmp_A
  match_str = m.group(0)
  trans_num = A_len * len(m.group(2)) / 2
  res += int(trans_num)
  tmp_end = m.end(0)
  tmp_A = A_len
print(res)

Submission status: Time Limit Exceeded

Solution code:
s = input()

result = 0
flag = 1

while flag > 0:
    count_1 = 0
    count_1 += s.count('AABC')
    if count_1 > 0:
        result += count_1*2
        s = s.replace('AABC', 'BCAA')

    count_2 = 0
    count_2 += s.count('ABCBC')
    if count_2 > 0:
        result += count_2*2
        s = s.replace('ABCBC', 'BCBCA')
    
    count_3 = 0
    count_3 += s.count('ABC')
    if count_3 > 0:
        result += count_3
        s = s.replace('ABC', 'BCA')
    if (count_1 + count_2 + count_3) == 0:
        break
    
print(result)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys;
import string;
sys.setrecursionlimit(20000) # 再起呼び出しの最大数をセット

import copy
import itertools

from functools import lru_cache
#@lru_cache(maxsize = None)

# 整数の入力
#n = int(input())

# スペース区切りの整数の入力
#a = [x for x in map(int, input().split())]
#n, a, b, c, d = map(int, input().split())
#print("sp", n, q, file=sys.stderr)

# 改行区切りの整数の入力
#a = []
#for x in range(m):
#  a.append([int(x) for x in input().split()])
#p = [int(x) for x in input().split()]

# 文字列の入力
s = input()

# 出力
#print("{} {}".format(a+b+c, s))
#a.sort()
#print(set(a), file=sys.stderr)
#print(len(set(a)))

#cache = dict()

abc = 'ABC'
bca = 'BCA'

def doit(s):
  res = 0
  while True:
    s2 = s.replace(abc, bca, 1)
    if s == s2:
      break
    s = s2
    res += 1
  return res
    
#print(doit([0] * 4))
#doit()
try:
  print(doit(s))
    
except Exception as e:
  print("e", e, file=sys.stderr)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import re

s = stdin.readline().rstrip()
rex = r"ABC"
cnt = 0
while re.search(rex, s):
    cnt += len(re.findall(rex, s))
    s = s.replace('ABC', 'BCA')

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0

for i in range(100000000):
    cnt += s.count("ABC")
    temp = s.replace("ABC", "BCA")
    if s != temp:
        s = temp
    else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=0
txt=raw_input()
txt_c=txt.replace("ABC","BCA",1)
while txt_c!=txt:
  txt_d=txt_c.replace("ABC","BCA",1)
  n=n+1
  txt = txt_c
  txt_c = txt_d
else:
  print n

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=[]
a=[]
for i in range(len(S)):
    s.append(S[i])
    if s[i]=="A": a.append(i)

tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
while True:
    if a==[] or a[0]>=len(s)-2: break
    tmp=a[0]
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        a.insert(0,tmp+2)
        if tmp>0 and s[tmp-1]=="A": a.insert(0,tmp-1)
        ans+=1
    else: a.pop(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()

count = 0

pattern = re.compile("ABC")

while re.search(pattern, S):
    S, tmp_count = re.subn(pattern, "BCA", S)
    count += tmp_count
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit
setrecursionlimit(1000000000)
import numpy as np
s = input()
S = list(s)
S = np.array(S)

abc = np.array(['A','B','C'])
bca = np.array(['B','C','A'])
bc = np.array(['B','C'])
ans = 0
if (S[0:3] == abc).all():
    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).all():
            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 = input()
li = list(s)

ans = 0
tmp = 0
length = len(li)
n = 0

while "ABC" in s:
    for i in range(n,length+1):
        if tmp == 0:
            if li[i] == "A":
                tmp += 1
                continue
            else:
                continue
        elif tmp == 1:
            if li[i] == "B":
                tmp += 1
                continue
            elif li[i] == "A":
                continue
            else:
                tmp = 0
                continue
        elif tmp == 2:
            if li[i] == "C":
                li[i] = "A"
                li[i-1] = "C"
                li[i-2] = "B"
                ans += 1
                s = "".join(li)
                tmp = 0
                n = i - 5
                break
            else:
                continue
print(ans)

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 = read()
    ans = 0
    while True:
        x = s.count('ABC')
        if x:
            ans += x
            s = s.replace('ABC', 'BCA')
        else:
            break
    print(ans)



# 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")


def read():
    return input()


def read_array(sep=None, maxsplit=-1):
    return read().split(sep, maxsplit)


def read_int():
    return int(read())


def read_int_array(sep=None, maxsplit=-1):
    return [int(a) for a in read_array(sep, maxsplit)]

# endregion

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
argS = input()

countsum = 0
wkcount = argS.count("ABC")

while wkcount > 0:
  argS = argS.replace("ABC","BCA")
  countsum += wkcount
  wkcount = argS.count("ABC")
print(countsum)

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0
k = len(s)//2
for i in range(k):
        if 'ABC' in s:
                count += s.count('ABC')
                s = s.replace('ABC','BCA')
        else:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import re


scan = sys.stdin.readline


def main():
    s = scan().rstrip()
    count = 0

    aabcbc = re.compile("AABCBC")
    aabc = re.compile("AABC")
    abcbc = re.compile("ABCBC")
    abc = re.compile("ABC")

    while abc.search(s):
        if 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:
a=input()
a=[i for i in a]
l=len(a)
count=0
of=0
s=1
while 1:
    if s==1:
        of=0
        s=0
        x="x"
        y="y"
        for i in a:
            if y=="A" and x=="B" and i=="C":
                a[of-2]="B"
                a[of-1]="C"
                a[of]="A"
                s=1
                count+=1
                break
            of+=1
            y=x
            x=i
    else:
        break
print(count)

Submission status: Time Limit Exceeded

Solution code:
m=input()
s_list=list(m)
count=0
while ("ABC" in m):
    m_s=m.find("ABC")
    s_list[m_s:m_s+3]=["B","C","A"]
    count+=1
    m=""
    for x in s_list:
        m+=x         
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
c = 0
state = 0
res = 0
stack = 0
loop = 0
while c < len(s):
    """
    loop = loop + 1
    if loop > 100:
        break
    print("c" + str(c))
    print("cur: " + "".join(s))
    print("state" + str(state))
    """
    if state == 0:
        if s[c] == "A":
            state = 1
            stack = 0
    elif state == 1:
        if s[c] == "B":
            state = 2
        else:
            if s[c] == "A":
                state = 1
                stack += 1
                #print("stack up:" + str(stack))
            else:
                state = 0
                stack = 0
    elif state == 2:
        if s[c] == "C":
            #print("hit")
            #print(" stack:" + str(stack))
            res += 1 + stack
            s[c-2-stack], s[c-1-stack], s[c-stack] = "B", "C", "A"
            for i in range(stack):
                s[c-i] = "A"
            c = c - 3 - stack
            state = 0
            stack = 0
            if s[c] == "A":
                state = 1
                stack = 0
        else:
            state = 0
            stack = 0
            if s[c] == "A":
                state = 1
                stack = 0
    c = c + 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def Find_and_Replace(S):
    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
    return count

S = list(input())
total_count = 0

while True:
    count = Find_and_Replace(S)
    if count <= 0:
        break
    else:
        total_count += count
        
print(total_count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

result = 0

result += s.count('AABCBC')

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


while 'AABC' in s:
    result += s.count('AABC')
    s = s.replace('AABC', 'ABCA')
while 'ABCBC' in s:
    result += s.count('ABCBC')
    s = s.replace('ABCBC', 'BCABC')

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

print(result)

Submission status: Time Limit Exceeded

Solution code:
import math

s = input()
ans = 0
for i in reversed(range(int(math.log2(len(s)))+1)):
    w = 2**i
    while "A" + "BC" * w in s or "A" * w + "BC" in s:
        m = s.count("A" + "BC" * w)
        s = s.replace("A" + "BC" * w, "BC" * w + "A")
        m += s.count("A" * w + "BC")
        s = s.replace("A" * w + "BC", "BC" + "A" * w)
        ans += w * m

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

c = 0
for i in range(len(s)-3):
  s = s[:i]+s[i:].replace('ABC','BCA',1)
  if s.count('ABC') == 0:
    print(c+1)
    exit()
  else:
    c += 1

Submission status: Time Limit Exceeded

Solution code:
import sys 
sys.setrecursionlimit(500000)
s=input()
ans=0
def dfs(v):#vはインデックス
    if 0<=v+1<=len(s)-1 and s[v+1]=='A':
        dfs(v+1)
    elif 0<=v+2<=len(s)-1 and s[v+1]=='B' and s[v+2]=='C':
        global ans
        ans+=1
        dfs(v+2)
    else:
        return
L=[]
for i,I in enumerate(s):
    if I=='A':
        L.append(i)
for l in L:
    dfs(l)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re

S = input()

count = 0
while "ABC" in S:
    S, tmp_count = re.subn("ABC", "BCA", S)
    count += tmp_count
    
print(count)

Submission status: Time Limit Exceeded

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

ans = 0
tmp = 0

while "ABC" in s:
    for i in range(len(li)):
        if tmp == 0:
            if li[i] == "A":
                tmp += 1
                continue
            else:
                continue
        elif tmp == 1:
            if li[i] == "B":
                tmp += 1
                continue
            elif li[i] == "A":
                continue
            else:
                tmp = 0
                continue
        elif tmp == 2:
            if li[i] == "C":
                li[i] = "A"
                li[i-1] = "C"
                li[i-2] = "B"
                ans += 1
                s = "".join(li)
                tmp = 0
                break
            else:
                continue
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
c = 0
flag = True

while flag:
    flag = False
    for i in range(len(S) - 2):
        if (S[i+2] == 'C') and (S[i+1] == 'B') and (S[i] == 'A'):
            S[i+2], S[i+1], S[i] = 'A', 'C', 'B'
            c += 1
            flag = True
print(c)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt  = 0
while "ABC" in s:
  s = s.replace("ABC", "BCA", 1)
  cnt = cnt + 1
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)
    cnt=0
    for i in range(len(S)-2):
        if S[i:i + 3] == list("ABC"):
            S[i:i + 3] = list("BCA")
            cnt += 1
            while i!=0:
                if S[i - 1:i + 2] == list("ABC"):
                    S[i - 1:i + 2] = list("BCA")
                    cnt += 1
                    i-=1
                else:
                    break
                #print("while", S)
        #print("for","".join(S))
    print(cnt)


    """    #愚直解
    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()