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()
#print(s)
#print(s.count("ABC"))
cnt = 0
while ("ABC" in s):
    cnt += s.count("ABC")
    s = s.replace("ABC", "BCA")

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ans=0

while 1:
    x=s.count('ABC')
    if x==0:
        break
    ans+=x
    s=s.replace('ABC','BCA')

print(ans)

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)
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
LEN = len(s)

def cnt_abc(s):
  ret = 0
  for i in range(LEN - 2):
    if s[i] == "A" and s[i + 1] == "B" and s[i + 2] == "C":
      ret += 1
      s[i] = "B"
      s[i + 1] = "C"
      s[i + 2] = "A"
  return s, ret

ans = 0
s, cnt = cnt_abc(s)
while cnt != 0:
  ans += cnt
  s, cnt = cnt_abc(s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = list(sys.stdin.readline().rstrip())
i = 0
cnt = 0
while i < len(s) - 2:
    if s[i] == 'A':
        if s[i+1] == 'B':
            if s[i+2] == 'C':
                s[i] = 'B'
                s[i+1] = 'C'
                s[i+2] = 'A'
                i -= 1
                cnt += 1
                continue
    i += 1
print(cnt)

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
    while S:
        if S[0]=="A":
            a+=1
            del S[0]
        elif S[0:2]==["B","C"]:
            del S[0:2]
            out+=a
            for i 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:
# B
input_s = input()
s = list(input_s)
count=0
flag = True
while(flag):
    check = count
    for i in range(len(s)-2):
        S = s[i]+s[i+1]+s[i+2]
        if "".join(S) == 'ABC':
            s[i] = 'B'
            s[i+1] = 'C'
            s[i+2] = 'A'
            count +=1
    if count == check:
        flag =False
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
while "ABC" in s:
    index = s.find("ABC")
    s = s[:index] + "BCA"+ s[index+3:]
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 

#N, A, B, C, D = [ int(x) for x in input().split() ]

S = input()

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
k=int(0)

while S.find('ABC')>=0:
#    S[S.find('ABC'):S.find('ABC')+3] = "BCA"
    S = S[:S.rfind('ABC')] + 'BCA' + S[(S.rfind('ABC')+3):]
    k +=1

ans = (int(k))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
ss = list(s)
ans = 0
i = 0
while i < n-2:  # 3文字なので
    if ss[i:i+3] == ['A', 'B', 'C']:
        ss[i:i+3] = ['B', 'C', 'A']
        ans += 1
        i = max(i-1, 0)   # 1つ前がAの場合をチェック
    else:
        i += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

ans = 0
num = S.count("ABC")
while num > 0:
    ans += num
    S = S.replace("ABC", "BCA")
    num = S.count("ABC")

print(ans)

Submission status: Time Limit Exceeded

Solution code:

S = input()
start_A = -1
i = 0
N = len(S)
ans = 0
while i < N:
	if S[i] == 'A':
		start_A = i
		i += 1
		while i < N and S[i] == 'A':
			i += 1
		n = i - start_A
		c = 0
		while i < N:
			if S[i:i+2] == "BC": 
				c += 1
				i += 2
			else:
				break
		ans += n*c
		S = S[:start_A] + S[start_A+n:start_A+n+c*2] + S[start_A:start_A+n] + S[start_A+n+c*2:]
		i = 0
	i += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

s = input()
cnt = 0

for i in range(100000000):
    temp = re.subn("ABC", "BCA", s)
    if s != temp[0]:
        s = temp[0]
        cnt += temp[1]
    else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
arr=[]
for i in range(len(s)):
	arr.append(s[i])
ans=0
n=len(arr)
for i in range(len(s)-2):
	if(arr[i]+arr[i+1]+arr[i+2]=="ABC"):
		ans+=1
		arr[i]="B"
		arr[i+1]="C"
		arr[i+2]="A"
		j=i-1
		while(j>=0 and arr[j]=="A"):
			ans+=1
			arr[j]="B"
			arr[j+1]="C"
			arr[j+2]="A"
			j-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

count = 0
tmp_list = []

for i in range(len(S)-2):
    if "".join(S[i:i+3]) == "ABC":
        count += 1        
        S[i] = "B"
        S[i+1] = "C"
        S[i+2] = "A"        
        
        if i >= 1:
            tmp_list.extend([i-1, i+2])
        else:
            tmp_list.extend([i+2])
                        
while tmp_list != []:
    new_list = []
    for i in tmp_list:
        if "".join(S[i:i+3]) == "ABC":
            count += 1
            
            S[i] = "B"
            S[i+1] = "C"
            S[i+2] = "A"
            
            if i >= 1:
                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:

S = input()

count = 0
while 'ABC' in S:
    if 'ABCBC' in S:
        S = S.replace('ABCBC','BCABC',1)
        count += 1
        continue
    if 'AABC' in S:
        S = S.replace('AABC','ABCA',1)
        count += 1
        continue
    else:
        S = S.replace('ABC','BCA',1)
        count += 1
print(str(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
    o=["S" for _ in range(0)]

    # print(o)
    # exit()
    # print(S,S[0:100])
    # exit()
    while S:
        if S[0]=="A":
            a+=1
            del S[0]
        elif S[0:2]==["B","C"]:
            del S[0:2]
            out+=a
            for i in range(a):
                S.insert(0, "A")
            a=0
        else:
            del S[0]
            a=0
    print(out)

    # A, B = list(map(int,input().split()))
    
    
if __name__ == "__main__":
    main()

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):
    s = s.replace('ABC', 'BCA', 1)
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
L = len(S)-2
n = 0
while True:
    F = True
    for i in range(L):
        if S[i:i+3] == ['A','B','C']:
            S = S[:i] + ['B','C','A'] + S[i+3:]
            n = n + 1
            F = False
    if F:
        break
print(n)

Submission status: Time Limit Exceeded

Solution code:
import re

s = input()
cnt = 0

for i in range(100000000):
    if s != re.sub("ABC", "BCA", s):
        temp = re.subn("ABC", "BCA", s)
        s = temp[0]
        cnt += temp[1]
    else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
for i 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:
S = input()

count = 0
tmp_list = []

for i in range(len(S)-2):
    if S[i:i+3] == "ABC":
        count += 1
        if i >= 1:
            tmp_list.extend([i-1, i+2])
        else:
            tmp_list.extend([i+2])
S = S.replace("ABC", "BCA")  

while tmp_list != []:
    new_list = []
    tmp_list = list(set(tmp_list))
    # print(tmp_list)
    for i in tmp_list:
        if S[i:i+3] == "ABC":
            # print(S)
            count += 1
            if i >= 1:
                new_list.extend([i-1, i+2])
            else:
                new_list.extend([i+2])
    tmp_list = new_list
    S = S.replace("ABC", "BCA")  

print(count)

Submission status: Time Limit Exceeded

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

maxcnt=0
def exec(string,cnt):
    global maxcnt
    for i in range(n-2):
        if string[i:i+3]=="ABC":
            exec(string[0:i]+"BCA"+string[i+3:],cnt+1)
            
    if maxcnt<cnt:
        maxcnt=cnt

exec(s,0)
print(maxcnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()

times = 0
while 'ABC' in s:
  times += 1
  s = s.replace('ABC', 'BCA', 1)
print(times)

Submission status: Time Limit Exceeded

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


def solve(s: str):

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

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

counter = 0
while 1:
    ind = temp.find("ABC")
    if ind <0:
        break
    else:
        counter += 1
        temp = temp.replace("ABC", "BCA", 1)
        
print(counter)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
L = len(S)-2
n = 0
while True:
    F = False
    for i in range(L):
        if S[i:i+3] == ['A','B','C']:
            S = S[:i] + ['B','C','A'] + S[i+3:]
            n = n + 1
            F = True
            break
    if F == False:
        break
print(n)

Submission status: Time Limit Exceeded

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

while True:
    #in_ = 0
    out = 0
    for i in range(len(s)):
        if i+2 < len(s):
            if s[i] == "A" and s[i+1] == "B" and s[i+2] == "C":
                s[i], s[i+1], s[i+2] = s[i+1], s[i+2], s[i]
                count += 1
                #in_ += 1
                #print("count=",count)
                #print("in=", in_)
                break
            else:
                out += 1
                #print("out=", out)
                if out == len(s)-2:
                    flag = 1
                    break

    if flag == 1:
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
string = [str(i) for i in input().split()]
flag = True
count = 0
tmp = string[0].count("ABCABC")
count += tmp * 3
string[0] = string[0].replace("ABCABC", "BCBCAA")
tmp = string[0].count("ABCABC")
count += tmp * 3
string[0] = string[0].replace("ABCABC", "BCBCAA")
while(flag):
    tmp = string[0].count("ABC")
    count += tmp
    if tmp == 0:
        flag = False
    else:
        string[0] = string[0].replace("ABC", "BCA")

print(count)

Submission status: Time Limit Exceeded

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


def solve(s: str):
    n = len(s)
    ret = 0
    while True:
        found = False
        for i in range(n - 2):
            if s[i:i + 3] == 'ABC':
                ret += 1
                found = True
                s = s[:i] + 'BCA' + s[i + 3:]
                #s[i:i + 3] = 'BCA'
        if not found:
            break
    print(ret)
    return


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

cnt = 0
while s.find("ABC")>-1:
    s = s.replace("ABC","BCA",1)
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    while(1):
        a = re.findall("ABC", S)
        if len(a) == 0:
            break
        S = S.replace("ABC", "BCA")
        ans += len(a)
    print(ans)


main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
A = 0
B = 1
C = 2

table = {"A": A, "B": B, "C": C}

s = [table[c] for c in input()]

i = 0
imax = len(s) - 2
count = 0
while i < imax:
	if s[i] == A and s[i + 1] == B and s[i + 2] == C:
		s[i] = B
		s[i + 1] = C
		s[i + 2] = A
		count += 1
		if i > 0:
			i -= 1
	else:
		i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

count = 0
while(1):
    if 'ABC' in s:
        s = s.replace('ABC', 'BCA', 1)
        #print(s)
        count += 1
    else:
        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:
    while True:
        if S[i] == 65:
            if S[i + 1] == 66:
                if S[i + 2] == 67:
                    S[i] = 66
                    S[i + 1] = 67
                    S[i + 2] = 65
                    count += 1
                    i -= 2
                    if i < 0:
                        i = 0
                else:
                    i += 1
                    if i > length - 3:
                        break
            else:
                i += 1
                if i > length - 3:
                    break
        else:
            i += 1
            if i > length - 3:
                break
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
count=0

while True:
    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

readline = sys.stdin.readline

s = readline().rstrip()

s = [x for x in s]

N = len(s)

i = 0
count = 0

while True:
    if i >= N - 2:
        break

    if s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C':
        s[i:i+3] = 'B', 'C', 'A'
        count += 1
        
        if i >= 1:
            i -= 1
    else:
        i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

ans = 0

check = s.count("ABC")

while check > 0:
    ans += check
    s = s.replace("ABC", "BCA")
    check = s.count("ABC")

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

Submission status: Time Limit Exceeded

Solution code:
l=input()
n=len(l)
s=list(l)
i=0
ans=0
while i<n-2:
    if s[i]=="A" and s[i+1]=="B" and s[i+2]=="C":
        s[i]="B"
        s[i+1]="C"
        s[i+2]="A"
        i=max(0,i-1)
        ans+=1
    else:
        i+=1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# from sys import exit
# A, B, C = [int(n) for n in input().split()]
# N = int(input())
# a = [int(input()) for _ in range(N)]
S = list(str(input()))
L = len(S)
# print(S)
ans = 0
for i in range(L-1):
    if S[i] == "B" and S[i+1] == "C":
        cur = i-1
        while(S[cur] == "A" and cur != -1):
            S[cur] = "B"
            S[cur+1] = "C"
            S[cur+2] = "A"
            cur-=1
            ans+=1
        # print(S)
# print(S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())[::-1] + [0,0]
N,i = len(s),0
ansl = []
while i < N:
    if s[i] == 'C' and s[i+1] == 'B':
        temp = [0]
        i += 2
        while i < N:
            if s[i] == 'A':
                temp += [1]
                i += 1
            elif s[i] == 'C' and s[i+1] == 'B':
                temp += [0]
                i += 2
            else:
                break
        ansl += [temp]
    else:   i += 1

ans = 0
for c in ansl:
    for i in range(len(c)-1):
        if c[i]==0:    ans += c[i+1:].count(1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def count(S):
    p = 0
    res = 0
    for s in S:
        if s == '0':
            p += 1
        else:
            res += p
    return res

S = input().strip()
a = '0'
b = 1
bc = '1'
x = '2'
T = ''
stack = []
for s in S:
    if s == 'A':
        if stack:
            T += x
        stack = []
        T += a
    elif s == 'B':
        stack.append(b)
    elif s == 'C':
        if stack and stack[-1] == b:
            stack.pop()
            if stack:
                T += x
            T += bc
        else:
            stack.append(x)
ans = 0
for g in T.split(x):
    ans += count(g)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

i = 0
imax = len(s) - 2
count = 0
while i < imax:
	if s[i] == "A" and s[i + 1] == "B" and s[i + 2] == "C":
		s[i] = "B"
		s[i + 1] = "C"
		s[i + 2] = "A"
		count += 1
		if i > 0:
			i -= 1
	else:
		i += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:


def main():
    s = map(lambda c: ord(c) - ord('A'), raw_input())
    i = len(s) - 3
    count = 0
    while i >= 0:
        if s[i:i+3] == [0, 1, 2]:
            count += 1
            s[i] = 1
            s[i + 1] = 2
            s[i + 2] = 0
            i += 2
        else:
            i -= 1
    print count


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0

while s != s.replace("ABC", "BCA"):
    s = s.replace("ABC", "BCA", 1)
    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S=input()
ans=0
while True:
    flag=0
    for i in range(len(S)-2):
        if S[i:i+3]=="ABC":
            ans+=1
            a=S[:i]
            b=S[i+3:]
            S=a+"BCA"+b
            flag+=1
    if flag==0:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

s = input()
p = []
p.append(('AABC', 'ABCA'))
p.append(('ABCBC', 'BCABC'))
p.append(('ABCABC', 'BCAABC'))
p.append(('ABC', 'BCA'))
l = len(p)
c = 0
while True:
    for i in range(l):
        x = s.find(p[i][0])
        if x > - 1:
            s = s[:x] + p[i][1] + s[x + len(p[i][1]):]
            c += 1
            break
    else:
        break
print(c)

Submission status: Time Limit Exceeded

Solution code:
S = input()

count = 0
while 'ABC' in S:
    c = S.count('ABC')
    count = count + c
    S = S.replace('ABC', 'BCA', c)
print(count)

Submission status: Time Limit Exceeded

Solution code:
def fa(s):
    a = 0
    for i in s[::-1]:
        if i != 'a':
            break
        a += 1
    return a

def fbc(s):
    bc = 0
    for i in xrange(0, len(s), 2):
        if s[i: i + 2] != list('bc'):
            break
        bc += 1
    return bc

def f(s, ans):
    if 'abc' not in s:
        return ans
    i = s.index('abc')
    a = fa(s[:i + 1])
    bc = fbc(s[i + 1:])
    return f(s[:i + 1] + s[i + 1 + 2*bc:], ans + a*bc)

s = list(str(raw_input()).lower())

i = 0
ans = 0
while i < len(s):
    if s[i: i + 3] == list('abc'):
        a = fa(s[:i + 1])
        b = fbc(s[i + 1:])
        ans += a*b
        s[i + 1 + 2*b - a: i + 1 + 2*b] = s[i + 1 - a: i + 1]
        i += 2*b
    else:
        i += 1

print ans

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
cnt = 0
while s.count("ABC")>0:
    for i in range(len(s)-3,-1,-1):
        if s[i:i+3]=="ABC":
            s = s[:i] + "BCA" + s[i+3:]
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=input()

cnt=0
flg=True

while flg:
  flg=False
  s=s.replace('ABC','X')
  c=s.count('X')  
  if c>0:
    flg=True
    cnt+=c
    s=s.replace('X','BCA')
#print(s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
ct, flg, ans = 0, 0, 0
while True:
    if flg == 0:
        if s[ct] == 'A':
            flg += 1
        else:
            flg = 0
        ct += 1
    elif flg == 1:
        if s[ct] == 'B':
            flg += 1
        elif s[ct] == 'A':
            flg = 1
        else:
            flg = 0
        ct += 1
    else:
        if s[ct] == 'C':
            s[ct-2], s[ct-1], s[ct] = 'B', 'C', 'A'
            ans += 1
            ct -= 3
            if ct < 0:
                ct += 3
            flg = 0
        elif s[ct] == 'A':
            flg = 1
            ct += 1
        else:
            flg = 0
            ct += 1
    if ct >= len(s):
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

count = 0

while "ABC" in S:
    for i in range(len(S)-2):
        if S[i:i+3] == "ABC":
            count += 1
    S = S.replace("ABC", "BCA")  

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
a=s.replace('A', '0').replace('B', '1').replace('C','2')
b=''
count = 0

while a!=b:
    b = a
    c = a.count('012')
    if c==0: break
    count += c
    a = a.replace('012', '120')

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

count = 0
lastCount = 0
while (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'
            count += 1

    if count == lastCount:
        break
    lastCount = count

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()

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

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:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,a,b,c,d = LI()
    s = input()
    a -= 1
    b -= 1
    c -= 1
    d -= 1
    if c == d:
        print("No")
    else:
        if c < d:
            k = s[b:d+1]
            for i in range(d-b):
                if k[i] == k[i+1] == "#":
                    print("No")
                    quit()
            k = s[a:c+1]
            for i in range(c-a):
                if k[i] == k[i+1] == "#":
                    print("No")
                    quit()
            print("Yes")
        else:
            k = s[b:d+1]
            for i in range(d-b):
                if k[i] == k[i+1] == "#":
                    print("No")
                    quit()
            k = s[a:c+1]
            for i in range(c-a):
                if k[i] == k[i+1] == "#":
                    print("No")
                    quit()
            k = s[b-1:d+2]
            for i in range(len(k)-2):
                if k[i] == k[i+1] == k[i+2] == ".":
                    print("Yes")
                    quit()
            print("No")
    return

#B
def B():
    s = S()
    n = len(s)
    update = 1
    ans = 0
    while update:
        update = 0
        for i in range(n-2)[::-1]:
            if s[i] == "A":
                if s[i+1] == "B":
                    if s[i+2] == "C":
                        s[i] = "B"
                        s[i+1] = "C"
                        s[i+2] = "A"
                        update = 1
                        ans += 1
                        break
    print(ans)
    return

#C
def C():
    n = I()

    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    B()

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

s = input()

cnt = s.count('ABC')
cntsum = cnt
#print('cnt =', cnt)
s = re.sub('ABC', 'BCA', s)
#print('s =', s)

while cnt != 0:
    cnt = s.count('ABC')
    cntsum = cntsum + cnt
#    print('cnt =', cnt, 'cntsum =', cntsum)
    s = re.sub('ABC', 'BCA', s)
#    print('s =', s)

print(cntsum)

Submission status: Time Limit Exceeded

Solution code:
s = input()
totalcount = 0
ABCcount = 1
for char in s:
    ABCcount = 0
    ABCcount = s.count('ABC')
    s = s.replace('ABC','BCA')
    totalcount = totalcount + ABCcount
    if ABCcount == 0:
        break

print(totalcount)

Submission status: Time Limit Exceeded

Solution code:
s = input()
a=s
b=''
count = 0
while a!=b:
    b = a
    c = a.count('ABC')
    if c==0: break
    count += c
    a = a.replace('ABC', 'BCA')

print(count)

Submission status: Time Limit Exceeded

Solution code:
string = [str(i) for i in input().split()]
flag = True
count = 0
while(flag):
    tmp = string[0].count("ABC")

    count += tmp

    if tmp == 0:
        flag = False
    else:
        string[0] = string[0].replace("ABC", "BCA")

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
flag = s.find("ABC")
ans = 0
while flag!=-1:
    ans+=1
    s=s.replace("ABC", "BCA", 1)
    flag = s.find("ABC")

print(ans)

Submission status: Time Limit Exceeded

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

cnt=0
def recur(s,cnt):
    cnt_temp=s.count('ABC')
    if cnt_temp > 0:
        s=s.replace("ABC","BCA")
        cnt += cnt_temp
        recur(s,cnt)
    else:
        print(cnt)
s=input()

recur(s,cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
N = len(s)
count = 0
flag = True
while flag:
    flag = False
    for i in range(N-2):
        if s[i]=='A' and s[i+1]=='B' and s[i+2]=='C':
            s[i] = 'B'
            s[i+1] = 'C'
            s[i+2] = 'A'
            count += 1
            flag = True

print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()

cnt=0
flg=True

while flg:
  flg=False
  for i in range(len(s)-2):
    if s[i:i+3]=='ABC':
      cnt+=1
      s=s[:i] + 'BCA' + s[i+3:]
      flg=True
#print(s)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
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:

cnt=0
def recur(s,cnt):
    cnt_temp=s.count('ABC')
    if cnt_temp > 0:
        s=s.replace("ABC","BCA")
        cnt += cnt_temp
        recur(s,cnt)
    else:
        print(cnt)
s=input()

recur(s,cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = 0
while S.find('ABC') >= 0:
    S = S.replace('ABC', 'BCA', 1)
    n = n + 1
print(n)

Submission status: Time Limit Exceeded

Solution code:
t = list(input())
length = len(t)

S = [ord(c) for c in t]

count = 0
change = False
while True:
    for i in range(length - 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
            change = True
    if not change:
        break
    change = False
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())

count = 0
tmp_count = -1

while tmp_count != 0:
    tmp_count = 0
    for i in range(len(S)-2):
        # print(S[i:i+3])
        if "".join(S[i:i+3]) == "ABC":
            count += 1
            tmp_count += 1
            S[i] = "B"
            S[i+1] = "C"
            S[i+2] = "A"
            
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
S = str(input())

ans = 0

p = re.compile('ABC')

while True:
    m = p.findall(S)
    if(len(m)==0):
        break
    else:
        ans += len(m)
        S = re.sub('ABC', 'BCA', S)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = raw_input()

find_val = S.find('ABC')
count = 0
while find_val >= 0:
    count += 1
    list_s = list(S)
    list_s[find_val:find_val+3] = 'BCA'
    S = "".join(list_s)
    find_val = S.find('ABC')
print count

Submission status: Time Limit Exceeded

Solution code:
s = input()
totalcount = 0
ABCcount = 1
while ABCcount != 0:
    ABCcount = 0
    ABCcount = s.count('ABC')
    s = s.replace('ABC','BCA')
    totalcount = totalcount + ABCcount

print(totalcount)

Submission status: Time Limit Exceeded

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