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:
#
#　　　　　　　　　　　　　　,. -─────────‐- .、
#　　　　　　　　　　　　 ／／￣￣＼　　　　　　／￣￣＼＼
#　　　　　　　　　　　／　　　　　　　　　　　　　　　　　　　　 ＼
#　　　　　　　　　 ／　　　　　　　　::::::::::::::::::::::::::::::::　　　　　　　＼
#　　　　　　　　／ 　　／ ／￣＼＼::::;;;;;;;;;;;;;;;;;:::::／／￣＼＼　　＼
#　　　　　　 ／　　　　|　 |.　┃　.|　| ::::;;;;;;;;;;;;;::::｜ |.　┃　.|　|　　　 ＼　　　　　　　　　　　　　　　　　　　　　
#　　　　　／ 　　　　　＼ ＼＿／／　::::::::::::::::::　＼＼＿／／　　　　　 ＼　　　　
#　　　 ／　　　　　..／￣￣＼　／　　　::｜::　　　＼　／￣￣＼..　　　　　＼
#　　 / 　 　 　 　 :::::　　　　　　|　　　　　 |　　　　　 |　　　　　　 :::::　　　　　 ヽ.　　　
#　　| 　　　　　　　　　　　　　　|　　　　　 |　　　　　 |　　　　　　　　　　　　　　|.
#　　| 　　　　　　　　　　　　　　＼＿＿／＼＿＿／ 　　　　　　　　　　　　　　|　　　　　　　　　　　　　　　　　
#　　| 　　　　　　　　　　　　　　　｜　　　　　　　｜　　　　　　　　　　　　　　　|
#　　| 　　　　　　　　　　　　　　　｜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:
s = input()

p = []
for i in range(len(s)):
    p.append(s[i])

p.reverse()

ret = 0
i = 0
while i < len(s) -2: 
    if p[i] == "C" and p[i+1] == "B" and p[i+2] == "A":
        p[i] = "A"
        p[i+1] = "C"
        p[i+2] = "B"
        i = i - 2
        ret += 1
    else:
        i += 1
        
print(ret)

Submission status: Time Limit Exceeded

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


import sys
import re

s= sys.stdin.readline().strip()
pat= re.compile("ABC")

t= 0
while True:
    n= len( pat.findall(s) )
    if n==0: break
    t+= n
    s= s.replace( "ABC","BCA")
#
print( t )

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

cnt = 0
ind = s.find('ABC')
while(ind != -1):
  cnt += 1
  s = str(s[:ind] + 'BCA' + s[ind+3:])
  ind = s.find('ABC')

print(cnt)

Submission status: Time Limit Exceeded

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

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

k = 0
ans = 0
while True:
    
    if k >= len(s) - 1:
        break
    if s[k : k + 3] == "ABC":
        s = s[:k] + "BCA" + s[k+3:]
        k -= 1
        ans += 1
#         print(s)
    else:
        k += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

s = list(input())
l = len(s)

idx = 0
cnt = 0
while(idx < l-2):
    if ''.join(s[idx:idx+3]) == 'ABC':
        s[idx:idx+3] = ['B', 'C', 'A']
        cnt += 1
        idx -= 1
    else:
        idx += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
cnt = 0
length = len(S)
for i, s in enumerate(S):
    while(s == "A"):
        if (i+3 > length):
            break
        elif (S[i+1] == "A"):
            i += 1
        elif (S[i+1:i+3] == "BC"):
            i += 2
            cnt += 1
        else:
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# agc034/agc034_b
import sys

i2s = lambda: sys.stdin.readline().rstrip()

def main():
    S = list(i2s())
    N = len(S)
    flag = True
    cnt = 0
    while flag:
        flag = False
        for i in range(N - 2):
            if S[i:i+3] == ['A', 'B', 'C']:
                cnt += 1
                S[i:i+3] = ['B', 'C', 'A']
                flag = True
    print(cnt)
    return

main()

Submission status: Time Limit Exceeded

Solution code:
import re
s=input()
ans=0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
ans = 0

while 1:
    c = S.count('ABC')
    if c==0: break
    ans += c
    S = S.replace('ABC','BCA')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

def Find_abc(S):
    i = 2
    while i < len(S):
        if S[i] == 'C':
            if S[i-2] == 'A' and S[i-1] == 'B':
                return (i-2)
            else:
                i += 3
        else:
            if S[i] == 'B':
                i += 1
            elif S[i] == 'A':
                i += 2
            else:
                i += 3
    return -1
S = list(input())

flag = True
count = 0
step = 0
while flag:
    #print('step : ',step)
    #print(S)
    x = Find_abc(S)
    #print(x)
    if x >= 0:
        S[x] = 'B'
        S[x+1] = 'C'
        S[x+2] = 'A'
        count += 1
        #print('exchange : ', S)
        #print('count : ',count)
        step += 1
    else:
        flag = False

print(count)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
ans = 0
for i in range(len(s)):
    a = re.findall("ABC", s)
    if a:
        ans += len(a)
        s = s.replace("ABC", "BCA", len(a))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re

s = str(input())
find_s = 'ABC'
count = 0
while True:
    s_n = re.sub(r"{}".format(find_s), "BCA", s)
    n_find = re.findall(r"{}".format(find_s), s)
    count += len(n_find)

    if s_n.find(find_s) == -1:
        print(count)
        break
    else:
        s = s_n

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
s = [S]
count = 0
while True:
#    s = re.subn('ABCBC','BCABC',s[0])
#    count += s[1]
#    s = re.subn('AABC','ABCA',s[0])
#    count += s[1]
    s = re.subn('ABC','BCA',s[0])
    if s[1]:
        count += s[1]
    else:
        break
print(str(count))

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':
            s[i] = 'B'
            s[i+1] = 'C'
            s[i+2] = 'A'
            tmp = 1
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
A = str(input())
S = [0]*len(A)
for i in range(len(A)):
    if A[i] == "A":
        S[i] = 0
    elif A[i] == "B":
        S[i] = 1
    else:
        S[i] = 2
        
def henkan(S):
    for i in range(len(S)-2):
        if S[i] == 0 and S[i+1] == 1 and S[i+2] == 2:
            S[i] = 1
            S[i+1] = 2
            S[i+2] = 0
            return S
            break
    return False
ans = 0
while (henkan(S) != False):
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = list(S)
onon = True
result = 0
while onon:
    onon = False
    for i in range(len(S) - 2):
        if S[i] == "A" and S[i + 1] == "B" and S[i + 2] == "C":
            onon =True
            result += 1
            S[i] = "B"
            S[i + 1] = "C"
            S[i + 2] = "A"
print(result)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    c = 0
    while True:
        n = s.count('ABC')
        if n == 0:
            break
        c += n
        s = s.replace('ABC', 'BCA')
    print(c)
        


main()

Submission status: Time Limit Exceeded

Solution code:
import sys


scan = sys.stdin.readline


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

    while s.find("ABC") > -1:
        if s.find("AABCBC") > -1:
            s = s.replace("AABCBC", "ABCABC", 1)
        elif s.find("AABC") > -1:
            s = s.replace("AABC", "ABCA", 1)
        elif s.find("ABCBC") > -1:
            s = s.replace("ABCBC", "BCABC", 1)
        elif s.find("ABC") > -1:
            s = s.replace("ABC", "BCA", 1)
        else:
            pass
        count += 1

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

end_flag = True
count = 0
while end_flag:
    if 'ABC' not in s_abc:
        end_flag = False
    count += s_abc.count('ABC')
    s_abc = s_abc.replace('ABC', 'BCA')

print(count)

Submission status: Time Limit Exceeded

Solution code:
s =  list(input())

ret = 0
while True :
    tmp = 0
    i = 0
    while i < len(s) :
        if s[i:i+3] != ['A', 'B', 'C'] :
            i += 1
            continue
        else :
            s[i:i+3] = ['B', 'C', 'A']
            i += 3
            tmp += 1
    if tmp :
        ret += tmp
    else :
        break

print(ret)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
counter = 0
answer = 0
oldAnswer = -1
while answer != oldAnswer:
  oldAnswer = answer
  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:
        s[i-2] = "B"
        s[i-1] = "C"
        s[i] = "A"
        answer += 1
      counter = 0
  counter = 0
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
count = 0
abc = 0
while True:
  for i in range(len(s)):
    #print(i, abc)
    if s[i] == 'A':
      abc = 1
    elif s[i] == 'B':
      if abc == 1:
        abc = 2
      else:
        abc = 0
    elif s[i] == 'C':
      if abc == 2:
        abc = 0
        count += 1
        s[i-2:i+1] = ['B', 'C', 'A']
        #print(s)
        #print(2)
        break        
  else:
    #print(1)
    break
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
flag=1
ans = 0
while(flag==1):
    flag=0
    if s.count("ABC") > 0:
        flag=1
        ans = ans+s.count("ABC")
        s.replace("ABC","BCA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
s = [S]
count = 0
while True:
    s = re.subn('ABCBC','BCABC',s[0])
    count += s[1]
    s = re.subn('AABC','ABCA',s[0])
    count += s[1]
    s = re.subn('ABC','BCA',s[0])
    if s[1]:
        count += s[1]
    else:
        break
print(str(count))

Submission status: Time Limit Exceeded

Solution code:
import copy

s = input()
count = 0
abc = 'ABC'
bca = 'BCA'
hoge = []
hoge.append(s)
flg = True
while flg:
    temp = []
    for j in hoge:
        for i in range(len(j) - 2):
            if j[i:i+3] == abc:
                temp2 = copy.deepcopy(j)
                temp2 = temp2[:i] + bca + j[i+3:]
                temp.append(temp2)
    l_unique = list(set(temp))
    if(len(temp)) == 0:
        flg = False
    else:
        count = count + 1
    hoge = l_unique
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = np.array(list(input()))
n = len(s)
ans = 0
i = 0
while i < n-2:  # 3文字なので
    if s[i] == 'A' and s[i+1] == 'B' and s[i+2] == 'C':
        s[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()

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

Submission status: Time Limit Exceeded

Solution code:
def give(list):
    i=0
    count=0
    while i<len(list)-2:
        if list[i]=='A' and list[i+1]=='B' and list[i+2]=='C':
            count+=1
            list[i]='B'
            list[i+1]='C'
            list[i+2]='A'
            if i>=1:
                i-=1
            else:
                i+=2
        else:
            i+=1
    return count
s=list(input())
print(give(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
S = list(s)
ans = 0
while True:
    i = s.find('ABC')
    if i == -1: break
    S[i:i+3] = ['B','C','A']
    s = ''.join(S)
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
flag=1
ans = 0
while(flag==1):
    if s.count("ABC") > 0:
        flag=1
        ans = ans+s.count("ABC")
        s.replace("ABC","BCA")

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 後ろからカウント、操作後にその周辺で書き換えるものがあるかどうかを探せばOK
s = input()

# 確か文字列は操作時に毎回文字列を作り直すはずだからTLE対策に数字の配列に直しておく
s_int = [1 if c == "A" else 2 if c == "B" else 3 for c in s ]

s_len = len(s_int)

s_pointer = s_len - 3

writecount = 0

# 配列の領域外を探すまで続ける
while s_pointer >= 0:
    if s_int[s_pointer] == 1 and s_int[s_pointer + 1] == 2 and s_int[s_pointer + 2] == 3:
        writecount += 1
        s_int[s_pointer] += 1
        s_int[s_pointer + 1] += 1
        s_int[s_pointer + 2] = 1
        if s_pointer + 2 + 3 <= s_len:   # ポインタを後ろに二つずらしてもはみ出ない
            s_pointer += 3
    s_pointer -= 1

print(writecount)

Submission status: Time Limit Exceeded

Solution code:
s = input()

result = 0
flag = 1

while flag > 0:
    flag = 0
    count = s.count('ABC')
    if count > 0:
        flag = 1
        result += count
        s = s.replace('ABC', 'BCA')
            
print(result)

Submission status: Time Limit Exceeded

Solution code:
import re

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

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 = input()
l = []
for i in s:
    l.append(i)
count = 0
indices = []
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.append(i-1)
        if i+4 < len(l):
            indices.append(i+2)

while len(indices) > 0:
    new_indices = []
    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.append(i-1)
            if i+4 < len(l):
                new_indices.append(i+2)
    indices = new_indices

print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import math

def main():
    s = list(raw_input())
    s.reverse()
    cnt = 0
    total = 0
    while True:
        chk = False
        for i in s:
            if i == 'C' and cnt < len(s) - 2:
                if s[cnt+1] == 'B' and s[cnt+2] == 'A':
                    s[cnt] = 'A'
                    s[cnt+1] = 'C'
                    s[cnt+2] = 'B'
                    total += 1
                    chk = True
                    break
            cnt += 1
        if chk == True:
            cnt = 0
            continue
        else:
            break
    print total


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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


def solve(s: str):
    n = len(s)
    ret = 0
    i = 0
    while i < n - 2:
        ##if s[i:i + 6] == 'ABCABC':
        ##    ret += 1
        if s[i:i + 3] == 'ABC':
            ret += 1
            s = s[:i] + 'BCA' + s[i + 3:]

            j = 1
            while s[i - j:i + 3 - j] == 'ABC':
                ret += 1
                s = s[:i - j] + 'BCA' + s[i + 3 - j:]
                j += 1

            i += 1
            #while s[i:i + 2] == 'BC':
            #    ret += 1
            #    s = s[:i - j] + 'BCA' + s[i + 2:]
            #    i += 2
        else:
            i += 1
    ##while True:
    ##    found = False
    ##    i = 0
    ##    tmp = []
    ##    while i < n - 2:
    ##        if s[i:i + 3] == 'ABC':
    ##            ret += 1
    ##            i += 3
    ##            tmp.append('BCA')
    ##            found = True
    ##            #s = s[:i] + 'BCA' + s[i + 3:]
    ##        else:
    ##            tmp.append(s[i])
    ##            i += 1
    ##    if not found:
    ##        break
    ##    s = ''.join(tmp)
    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=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:
        if len(S[i:i+2])==2 and S[i] == "B" and S[i+1] == "C":
            S[j:i+2] = list("BC")+["A"]*(i-j)
            ans += i-j
            i = j
            j = -1
        else:
            j = -1
            i+=1
    else:
        i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

f = True
ans = 0

while(f):
    count = s.count("ABC")
    ans += count
    if count==0:
        f = False
    s = s.replace("ABC","BCA")


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
s = input()
l = list(map(str, s))
c = 0
m = re.search('ABC', s)

while re.search('ABC', s) :
    c +=1
    m = re.search('ABC', s)
    l[m.start():m.end()] = 'BCA'
    s = ''.join(l)

print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
for i in range(len(s)):
    a = s.find("ABC")
    if a >= 0:
        ans += 1
        s = s.replace("ABC", "BCA", 1)
print(ans)

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

s = input()
count = 0
abc = 'ABC'
bca = 'BCA'
hoge = []
hoge.append(s)
flg = True
while flg:
    temp = []
    for j in hoge:
        for i in range(len(j) - 2):
            if j[i:i+3] == abc:
                temp2 = copy.deepcopy(j)
                temp2 = temp2[:i] + bca + j[i+3:]
                temp.append(temp2)
    if(len(temp)) == 0:
        flg = False
    else:
        count = count + 1
    hoge = temp
print(count)

Submission status: Time Limit Exceeded

Solution code:
# ナイーブに総当たり
s = input()

cnt = 0

while True:
    i = s.find("ABC")
    if i == -1:
        break
    else:
        cnt += 1
        s = s[:i] + "BCA" + s[i+3:]
        continue

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

cnt = 0

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

Submission status: Time Limit Exceeded

Solution code:
s=input()

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

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()]

def count_abcs(i):
	count = 0
	while s[i] == A and s[i + 1] == B and s[i + 2] == C:
		count += 1
		i += 3
		if i >= imax:
			break
	return count

i = 0
imax = len(s) - 2
total = 0
while i < imax:
	count = count_abcs(i)
	if count >= 1:
		total += (count + 1) * count // 2
		for j in range(count):
			s[i + j * 2] = B
			s[i + j * 2 + 1] = C
			s[i + count * 2 + j] = A
		if i > 0:
			i -= 1
	else:
		i += 1

print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = len(s)
ans = 0
i = 0
while i < n-2:  # 3文字なので
    if s[i:i+3] == ['A', 'B', 'C']:
        s[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:
# B
input_s = input()
s = list(input_s)
count=0
flag = True
while(flag):
    check = count
    for i in range(len(s)-2):
        if s[i]=='A':
            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()

ret = 0
while True :
    t = s.count('ABC')

    if t :
        ret += t
        s = s.replace('ABC', 'BCA')
    else :
        break

print(ret)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()

count = 0
while True:
    if 'ABCBC' in S:
        s = re.subn('ABCBC','BCABC',S)
        S = s[0]
        count += s[1]
    if 'AABC' in S:
        s = re.subn('AABC','ABCA',S)
        S = s[0]
        count += s[1]
        continue
    if 'ABC' in S:
        s = re.subn('ABC','BCA',S)
        S = s[0]
        count += s[1]
    else:
        break
print(str(count))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

s = input()
l = len(s)

idx = 0
cnt = 0
while(idx < l-2):
    if s[idx:idx+3] == 'ABC':
        s = s[:idx] + 'BCA' + s[idx+3:]
        cnt += 1
        idx -= 1
    else:
        idx += 1

print(cnt)

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 >= 0:
            tmp_list.extend([i-1, i+2])
        else:
            tmp_list.extend([i+2])
                        
while tmp_list != []:
    new_list = []
    for i in set(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 >= 0:
                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:
import re
base_str = input()
exp = "A(BC)+"
flg = True
res = 0
while(flg):
  tmp_res = res
  for m in re.finditer(exp, base_str):
    match_str = m.group(0)
    trans_num = int((len(match_str) - 1) / 2)
    res += trans_num
    replace_str = "BC" * trans_num + "A"
    base_str = base_str[:m.start(0)] + replace_str + base_str[m.end(0):]
  flg = res != tmp_res
print(res)

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 i < length - 2:
        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
                    count += 1
                    i = max(i - 3, -1)
        i += 1
print(count)

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':
                cnt += 1
                s[i] = 'B'
                s[i+1] = 'C'
                s[i+2] = 'A'
                if s[i-1] == 'A':
                    i -= 1
                    continue
    i += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

print(ret)

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

re_subn = re.subn

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:
# -*- coding: utf-8 -*-
# agc034/agc034_b
import sys
import re

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    S = i2s()
    cnt = 0
    while 'ABC' in S:
        cnt += S.count('ABC')
        S = S.replace('ABC', 'BCA')
    print(cnt)
    return

main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = 0
while True:
    I = S.find('ABC')
    if I >= 0:
        S = S[:I] + 'BCA' + S[I+3:]
        n = n + 1
    else:
        break
print(n)

Submission status: Time Limit Exceeded

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


def solve(s: str):
    n = len(s)
    ret = 0
    #while i < n - 2:
    #    if s[i:i + 6] == 'ABCABC':
    #        ret += 1
    #    if s[i:i + 3] == 'ABC':
    #        ret += 1
    #        i += 3
    #        while s[i:i + 2] == 'BC':
    #            ret += 1
    #            i += 2
    #    else:
    #        i += 1
    while True:
        found = False
        i = 0
        tmp = []
        while i < n - 2:
            if s[i:i + 3] == 'ABC':
                ret += 1
                i += 3
                tmp.append('BCA')
                found = True
                #s = s[:i] + 'BCA' + s[i + 3:]
            else:
                tmp.append(s[i])
                i += 1
        if not found:
            break
        s = ''.join(tmp)
    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()
ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = "ABC"
ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
n=len(S)
c=0
for x in range(n-2):
    if S[x]=='A':
        if S[x+1]=='B':
            if S[x+2]=='C':
                S[x]='B'
                S[x+1]='C'
                S[x+2]='A'
                c=c+1
                if x>0:
                    y=x-1
                    while S[y]=='A':
                        S[y]='B'
                        S[y+1]='C'
                        S[y+2]='A'
                        c=c+1
                        if y>0:
                            y=y-1
print(c)

Submission status: Time Limit Exceeded

Solution code:
s=input()
S=""
i=0
leng=len(s)
while(i<leng):
    if s[i]=="A":
        S+="A"
        i+=1
    elif i+1<leng and s[i:i+2]=="BC":
        S+="B"
        i+=2
    else:
        S+="C"
        i+=1
T=S[::-1]
l=[0]*len(T)
t=len(T)
for i in range(len(T)-1):
    if T[i]=="B":
        l[i+1]=l[i]+1
    elif T[i]=="A":
        l[i+1]=l[i]
ans=0
for i in range(len(T)):
    if T[i]=="A":
        ans+=l[i]
print(ans)