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: Wrong Answer

Solution code:
#!/usr/bin/python3

import math
import sys


DEBUG = False


def inp():
    return sys.stdin.readline().rstrip()


def dprint(*value, sep=' ', end='\n'):
    if DEBUG:
        print(*value, sep=sep, end=end)


def solve(S):
    N = len(S)
    a = 0
    ans = 0
    for i in range(N):
        if S[i] == 'A':
            a += 1
        if S[i:i + 2] == 'BC':
            ans += a
    return a


def main():
    print(solve(inp()))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()

cnt = 0
if s[0] == "A":
  a = 1
  f = 1
else:
  f = 0
  a = 0

for i in s[1:]:
  if i == "A":
    if f == 1:
      a += 1
    else:
      a = 1
    f = 1
  elif i == "B":
    if f == 1:
      f = 2
    else:
      f = 1
      a = 0
  else:
    if f == 2:
      cnt += a
      a = 1
      f = 1
    else:
      a = 0
      f = 1

print(cnt)

Submission status: Wrong Answer

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

ans = 0

cb_cnt = 0
state = 0
for i in range(n)[::-1]:
    if state == 0:
        if s[i] == "C":
            cb_cnt += 1
            state = 1
            continue
    elif state == 1:
        if s[i] == "B":
            state = 2
            continue
    elif state == 2:
        if s[i] == "C":
            cb_cnt += 1
            state = 1
            continue
        elif s[i] == "A":
            ans += cb_cnt
            state = 3
            continue
    elif state == 3:
        if s[i] == "C":
            state = 1
            cb_cnt += 1
            continue
        elif s[i] == "A":
            ans += cb_cnt
            continue
    cb_cnt = 0
    state = 0
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
s = s[::-1]
ans = 0
cnt = 0
pos = 0
while pos<len(s):
  if s[pos] == "A":
    ans += cnt
    pos += 1
  elif s[pos] == "B":
    cnt = 0
    pos += 1
  elif s[pos:pos+2] == "CB":
    cnt += 1
    pos += 2
  else:
    pos += 1
print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np

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

def main():
    S = i2s()
    n = 0
    na = 0
    ba = False
    bb = False
    for s in S:
        if s == 'A':
            na += 1
            ba = True
            bb = False
        elif s == 'B':
            if ba:
                ba = False
                bb = True
            else:
                na = 0
                ba = False
                bb = False
        else:
            if bb:
                n += na
                #na = 0
                ba = True
                bb = False
            else:
                na = 0
                ba = False
                bb = False
    print(n)

main()

Submission status: Wrong Answer

Solution code:
import queue

"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""
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 = start_A+n
	i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = ['C']+list(input())
n = len(s)
res = 0
p = []

def f(p):
    if len(p) == 0:
        return 0
    cnt = 0
    ans = 0
    for e in p:
        if e == 'A':
            cnt += 1
        else:
            ans += cnt
    return ans

for i in range(1,n):
    if s[i] == 'A':
        p.append('A')
    elif s[i] == 'B':
        if s[i-1] == 'B':
            res += f(p)
            p = []
    else:
        if s[i-1] == 'B':
            p.append('B')
        else:
            res += f(p)
            p = []
res += f(p)
print(res)

Submission status: Wrong Answer

Solution code:
s = input().rstrip()

ans = 0
anum = 0
prev = "x"
for c in s:
    if c == "A":
        anum += 1
    elif c == "B":
        if prev == "B":
            anum = 0
    elif c == "C":
        if prev == "B":
            ans += anum
        else:
            anum = 0
    prev = c
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
a_cnt = 0
b_flag = 0
total = 0

for c in s:
#  print(c, a_cnt, b_flag)
  if c == "A":
    if b_flag:
      b_flag = 0
      a_cnt = 1
    else:
      a_cnt += 1
  elif c == "B":
    if a_cnt:
      b_flag = 1
    elif b_flag:
      b_flag = 0
      a_cnt = 0
  elif c == "C":
    if b_flag:
      total += a_cnt
      a_cnt = 1
      b_flag = 0
    else:
      a_cnt = 0
      
print(total)

Submission status: Wrong Answer

Solution code:
S = input()
ans = 0
bc = 0
c_flag = 0
for i in range(len(S) - 1, -1, -1):
    if S[i] == "A" and c_flag == 0:
        ans += bc
    elif S[i] == "C" and c_flag == 0:
        c_flag = 1
    elif S[i] == "B" and c_flag == 1:
        bc += 1
        c_flag = 0
    else:
        bc = 0
        c_flag = 0
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()

ans = 0
a = 0
b = 0
for i in range(len(s)):
    if s[i] == 'A':
        if b > 0:
            a = 1
            b = 0
        elif b == 0:
            a += 1
            b = 0
    if s[i] == 'B':
        if a > 0 and b == 0:
            b += 1
        elif b > 0:
            a = 0
            b = 0
    if s[i] == 'C':
        if b > 0:
            ans += a
            a = 1
            b = 0
        elif b == 0:
            a = 0
            b = 0
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
a_cnt = 0
b_flag = 0
total = 0

for c in s:
#  print(c, a_cnt, b_flag)
  if c == "A":
    if b_flag:
      b_flag = 0
      a_cnt = 1
    else:
      a_cnt += 1
  elif c == "B":
    if a_cnt:
      b_flag = 1
    elif b_flag:
      b_flag = 0
  elif c == "C":
    if b_flag:
      total += a_cnt
      a_cnt = 1
      b_flag = 0
    else:
      a_cnt = 0
      
print(total)

Submission status: Wrong Answer

Solution code:
import sys
#import numpy as np

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

def main():
    S = i2s()
    n = 0
    na = 0
    ba = False
    bb = False
    for s in S:
        if s == 'A':
            na += 1
            ba = True
            bb = False
        elif s == 'B':
            if ba:
                ba = False
                bb = True
            else:
                na = 0
                ba = False
                bb = False
        else:
            if bb:
                n += na
                #na = 0
                ba = True
                bb = False
            else:
                ba = False
                bb = False
    print(n)

main()

Submission status: Wrong Answer

Solution code:

def main():
    buf = input()
    S = list(buf)
    A_count = 0
    B_appeared = False
    op_count = 0
    for pointer, _ in enumerate(S):
        if S[pointer] == 'A':
            A_count += 1
        elif S[pointer] == 'B':
            if A_count == 0:
                pass
            elif not B_appeared:
                B_appeared = True
            else: # B_appeared
                B_appeared = False
                A_count = 0
        else: # S[pointer] == 'C'
            if not B_appeared:
                A_count = 0
            else:
                op_count += A_count
                B_appeared = False
    print(op_count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

s=input().split("BC")
s.pop()
ans = 0
now=0
#print(s)
for i in s:
    if "B" not in i and "C" not in i:
        now += len(i)
        ans += now
    else:
        x=i.rfind('B')
        y=i.rfind('c')
        now = len(i) - max(x,y) - 1
        ans += now
#    print(now,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
counter = 0
n = s.find('BC')
while n != -1:
    k = 1
    while k <= n and s[n-k] == 'A':
        counter += 1
        k += 1
    s = s[:n] + s[n+2:]
    n = s.find('BC')
print(counter)

Submission status: Wrong Answer

Solution code:
s=input()
ans=0
a=0
if len(s)<3:
  print(0)
else:
  c0=ord(s[2])-ord("A")
  c1=ord(s[1])-ord("A")
  c2=ord(s[0])-ord("A")
  if c1==0:
    a+=1
    if c2==0:
      a+=1
  for i in range(2,len(s)):
    c0=ord(s[i])-ord("A")
    if c2==0 and c1==1 and c0==2:
      ans+=1+a
      c2=1
      c1=2
      c0=0
      a=0
    else:
      if c2==0:
        a+=1
      else:
        a=0
    c2=c1
    c1=c0
  print(ans)

Submission status: Wrong Answer

Solution code:
print('5')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def solve(): 
    S = input()
    count = 0
    count_a = 0
    bc_flag = False

    for s in S:
        if s == 'A':
            count_a += 1 
        elif s == 'B' and bc_flag:
            count_a = 0
        elif s == 'C':
            if bc_flag:
                count += count_a
            else:
                count_a = 0
        bc_flag = (s == 'B') and not bc_flag

    res = count
    return str(res)

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

Submission status: Wrong Answer

Solution code:
s = input().strip()

ss = None
r = 0
ac = 0
for c in s:
    if c == "A":
        ac += 1
    elif c == "C" and ss == "B":
        r += ac
        ss = "A"
        continue
    elif c == "B" and ss == "A":
        pass
    else:
        ac = 0
    ss = c
print(r)

Submission status: Wrong Answer

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='ABCABCABCABCABC'

recur(s,cnt)

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
a = 0
b = False
for i in range(len(s)):
	if s[i] == "A":
		a += 1
		b = False
	elif s[i] == "B":
		if b:
			a = 0
		b = True
	else:
		if b:
			ans += a
		else:
			a = 0
		b = False
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
s=input()
cnt=0
while 'ABC' in s:
    s=s.replace("ABC","BCA")
    cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
A = input()
i = A.count('A')

print(i+1)

Submission status: Wrong Answer

Solution code:
print("わからないよー")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
ans=0
i=0
while i<l-2:
    if s[i:i+3]=="ABC":
        s=s[:i]+"BCA"+s[i+3:]
        if i!=0:i-=1
        ans+=1
    else:i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

ans = 0
for i in range(len(s)-1):
  if s[i:i+2] == 'BC':
    cnt = 0
    for j in range(i):
      if s[i-1-j] == 'A':
        cnt += 1
      else:
        break
    s = s[:i-cnt] + 'BC' + 'A'*cnt + s[i+2:]
    ans += cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())

ans = 0
for i in range(len(s)-1):
  if s[i:i+2] == 'BC':
    tmp = i
    cnt = 0
    while (0<tmp) and (s[tmp-1]=='A'):
      cnt += 1
      tmp -= 1
    else:
      ans += cnt
      s = s[:tmp] + 'BC' + 'A'*cnt + s[i+2:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
a = list(input())
ans = 0
i = 0
while i < len(a)-2:
    box = a[i]+a[i+1]+a[i+2]
    if box == "ABC":
        ans += 1
        a[i], a[i+1], a[i+2] = a[i+1], a[i+2], a[i]
        i -= 2
    i += 1
    if i < 0:
        i = 0
    # print(a, i, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
stack = []
ans = 0
while len(s):
    stack.append(s.popleft())
    if len(stack) >= 3 and \
        stack[-3:] == ['A', 'B', 'C']:
        del stack[-3:]
        s.appendleft('A')
        s.appendleft('C')
        s.appendleft('B')
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

# step1: pre-process "BC" を Xに置き換える
tmp = ""
flag = False  # skip用
for i in range(1, len(s)):
    if flag:
        flag = False
        continue
    elif s[i - 1] + s[i] == "BC":
        tmp += "X"
        flag = True
    else:
        tmp += s[i - 1]

if not flag:
    tmp += s[-1]

cnt = 0
ans = 0
# step:2 Xより左側にあるAの数を数える。
# ただし、B、Cが現れると、カウンターリセット
# （そのB、Cを越えてSwapできないため）
for i in range(len(tmp)):
    if tmp[i] == "A":
        cnt += 1
    elif tmp[i] == "B" or tmp[i] == "C":
        cnt = 0
    elif tmp[i] == "X":
        ans += cnt

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s = s.replace("BC", "X")
#"AX" -> "XA"
cnt = 0
while "AX" in s:
    cnt += s.count("AX")
    s = s.replace("AX", "XA")
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
ans = 0
while True:
    Flag = True
    #print(s)
    for i in range(len(s)-2):
        if (s[i],s[i+1],s[i+2]) == ("A","B","C"):
            s[i] = "B"
            s[i+1] = "C"
            s[i+2] = "A"
            ans += 1
            Flag = False
            break
    if Flag:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
news = ''
t = False
for i in range(len(S)-1):
    if t:
        t = False
        continue
    if S[i] == 'B' and S[i+1] == 'C':
        news += 'D'
        t = True
        continue
    news += S[i]

ans = 0
cnt = 0
for s in news:
    if s == 'A':
        cnt += 1
    elif s == 'D':
        ans += cnt
    else:
        cnt = 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import*
s=input()
n=len(s)
i=0
cnt=0
ans=0
while i<n:
  if s[i]=='A':
    i+=1
    cnt+=1
    while s[i:i+2]=='BC':
      ans+=cnt
      i+=2
  elif s[i:i+2]!='BC':
    cnt=0
    i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
str_list = []
for i in range(len(s)):
    str_list.append(s[i])

cnt = 0
tf = True
while tf == True:
    tf = False
    cont_a = 0
    for i in range(len(str_list) - 2):
        if str_list[i] == "A" and str_list[i + 1] == "B" and str_list[i + 2] == "C":
            str_list[i] = "B"
            str_list[i + 1] = "C"
            str_list[i + 2] = "A"
            cnt +=1
            tf = True

# print(str_list)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#abcabc
#bcaabc
#bcabca
#bcbcaa

s=list(input())

cnt=0
kouho=[]

for i in range(1,len(s)-1):
    if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
        cnt+=1
        s[i-1]="B"
        s[i]="C"
        s[i+1]="A"
        kouho.append(i-1)
        kouho.append(i+2)
#print(kouho)

while len(kouho)>0:
    i=kouho.pop()
    if 0<=i-1 and i+1<=len(s)-1:
        if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
            flag=1
            cnt+=1
            s[i-1]="B"
            s[i]="C"
            s[i+1]="A"
            kouho.append(i-1)
            kouho.append(i+2)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#abcabc
#bcaabc
#bcabca
#bcbcaa

s=list(input())

cnt=0
kouho=[]

for i in range(1,len(s)-1):
    if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
        cnt+=1
        s[i-1]="B"
        s[i]="C"
        s[i+1]="A"
        kouho.append(i-1)
        kouho.append(i+2)
#print(kouho)
while 1:
    flag=0
    while len(kouho)>0:
        i=kouho.pop()
        if 0<=i-1 and i+1<=len(s)-1:
            if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
                flag=1
                cnt+=1
                s[i-1]="B"
                s[i]="C"
                s[i+1]="A"
                kouho.append(i-1)
                kouho.append(i+2)
    #print(s)
    if flag==0:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#abcabc
#bcaabc
#bcabca
#bcbcaa

s=list(input())

cnt=0
while 1:
    flag=0
    for i in range(1,len(s)-1):
        if s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
            flag=1
            cnt+=1
            s[i-1]="B"
            s[i]="C"
            s[i+1]="A"
    #print(s)
    if flag==0:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input().replace("BC", "X")
indA = [i for i in range(len(s)) if s[i] == "A"]

ans = 0
for i in indA:
    while s[i] != "B" and s[i] != "C":
        ans += (s[i] == "X")
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    S=list(input())
    S+=["Z"]*3
    N=len(S)
    
    ans=0
    i=0
    while i!=N:
        if S[i:i+3]==["A","B","C"]:
            ans+=1
            S[i]="B"
            S[i+1]="C"
            S[i+2]="A"
            i=max(0,i-1)
        else:
            i+=1

    print(ans)
            

main()

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:
ss = list(input())
s = []
for i in ss:
  if i == 'A':
    s.append(1)
  elif i == 'B':
    s.append(2)
  else:
    s.append(3)
n = len(s)
ans = 0
while True:
    b = False
    for i in range(0, n-2):
        if s[i:i+3] == [1, 2, 3]:
            s[i] = 2
            s[i+1] = 3
            s[i+2] = 1
            ans += 1
            b = True
    if not b:
        break
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S_alta = input()
N = len(S_alta)

cnt = 0
S = []

while cnt < N:
    if cnt <= N - 2 and S_alta[cnt] == 'B' and S_alta[cnt + 1] == 'C':
        S.append('X')
        cnt += 2
    else:
        S.append(S_alta[cnt])
        cnt += 1

S = ''.join(S)
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "X":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S = input()
S = S.replace('BC', 'P')
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "P":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve(s):
    D = {"A":0,"B":1,"C":2}
    s = list(map(D.__getitem__, s))
    n = len(s)
    res = 0
    i = 0
    before = list(map(D.__getitem__, "ABC"))
    after = list(map(D.__getitem__, "BCA"))
    m = len(before)
    while i+m <= n:
        if s[i:i+m] == before:
            s[i:i+m] = after
            i = max(0, i-1)
            res += 1
        else:
            i += 1
    return res

s = input()
print(solve(s))

Submission status: Time Limit Exceeded

Solution code:
def solve(s):
    s = list(s)
    n = len(s)
    res = 0
    i = 0
    before = list("ABC")
    after = list("BCA")
    m = len(before)
    while i+m <= n:
        if s[i:i+m] == before:
            s[i:i+m] = after
            i = max(0, i-1)
            res += 1
        else:
            i += 1
    return res

s = input()
print(solve(s))

Submission status: Time Limit Exceeded

Solution code:
def solve(s):
    s = list(s)
    n = len(s)
    res = 0
    i = 0
    while i+3 <= n:
        ok = True
        for j, c in enumerate("ABC"):
            if s[i+j] != c:
                ok = False
                break
        if ok:
            s[i], s[i+1], s[i+2] = "B", "C", "A"
            i = max(0, i-1)
            res += 1
        else:
            i += 1
    return res

s = input()
print(solve(s))

Submission status: Time Limit Exceeded

Solution code:
#再帰の限度を拡張
import sys
sys.setrecursionlimit(1000000)

s = list(input())
N = len(s)

ans = 0

def ABC_check(i):
    if 0 <= i-1 and i+1 <= N-1 and s[i-1]=="A" and s[i]=="B" and s[i+1]=="C":
        temp_ans = 1
        s[i-1]="B"
        s[i]="C"
        s[i+1]="A"
        
        temp_ans += ABC_check(i-1)
        temp_ans += ABC_check(i+2)
        return temp_ans
    else:
        return 0

for i in range(0,N):
    ans += ABC_check(i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

ans = 0
f = 1

while f:
    tmp = s
    f = 0
    for i in range(len(s)-2):
        if tmp[i:i+3] == ["A", "B", "C"]:
            s[i:i+3] = ["B", "C", "A"]
            ans += 1
            f = 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
d = [s[0]]
for i in range(1,n):
  if s[i]=="C" and s[i-1]=='B':
    d.pop()
    d.append("x")
  else:
    d.append(s[i])
q = "".join(d)
cou = 0
while "Ax" in q:
  cou += q.count("Ax")
  q = q.replace("Ax","xA")
#  print(q)
print(cou)

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()

l = len(s)
i = 0
count = 0
while i < l - 2:
    tmp = s[i:i + 2 + 1]
    if tmp == 'ABC':
        s = s[:i] + 'BCA' + s[i+2+1:]
        count += 1
    else:
        i += 1
        continue

    if i != 0 and s[i - 1] == 'A':
        i -= 1
    else:
        i += 2

print count

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    _res = 0
    j = 0
    while j <= (len(s) - 2):
        j = j + _res
        if j < 0:
            j = 0
        _res = 0
        str = s[j:(j + 3)]
        if str == "ABC":
            n = n + 1
            _res = -2
            s = s[:j] + "BCA" + s[j + 3:]
        else:
             j = j + 1
    return n
print mul(s)

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    _res = 0
    j = 0
    while j <= (len(s) - 2):
        j = j + _res
        if j < 0:
            j = 0
        _res = 0
        str = s[j:(j + 3)]
        if str == "ABC":
            n = n + 1
            _res = -2
            s = s[:j] + "BCA" + s[j + 3:]
        j = j + 1
    return n
print mul(s)

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    _res = 0
    j = 0
    while j < (len(s) - 2):
        j = j + _res
        if j < 0:
            j = 0
        _res = 0
        str = s[j:(j + 3)]
        if str == "ABC":
            n = n + 1
            _res = -2
            s = s[:j] + "BCA" + s[j + 3:]
    return n
print mul(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
# s = list('ABCABC')
# s = list('ABCACCBABCBCAABCB')

# count = 0
#
# if len(s) < 3:
# 	print(0)
ccc = 0
# while :

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

        # print(i)
        # print(s)
# print(s)
print(ccc)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
# s = list('ABCABC')
# s = list('ABCACCBABCBCAABCB')

# count = 0
#
# if len(s) < 3:
# 	print(0)
ccc = 0
# while :

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

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()
n = 0

def mul(s):
    if len(s) < 3:
        return 0
    n = 0
    flag = True
    while flag:
        res = 0
        s1 = s
        for i in xrange(len(s) - 2):
            str = s[i:(i+3)]
            if str == "ABC":
                n = n + 1
                res = res + 1
                s1 = s1[:i] + "BCA"+ s1[i+3:]
            # _str[i:i+3] = "BCA"+ _str[i+3:]
        if res == 0:
            flag =False
        s = s1
    return n
print mul(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
# s = list('ABCABC')
# s = list('ABCACCBABCBCAABCB')
# s = list('AB')

count = 0

if len(s) < 3:
	ccc = 0
else:
	ccc = 0
	while count + 3 <= len(s):
		if s[count: count + 3] == ['A', 'B', 'C']:
			s[count: count + 3] = ['B', 'C', 'A']
			ccc += 1
			if count - 2 >= 0:
				count -= 2
		count += 1

print(ccc)

Submission status: Time Limit Exceeded

Solution code:

s = input()
# s = 'ABCABC'

count = 0

while True:
    idx = s.find('ABC')
    if idx == -1:
        break
    s = s[:idx] + 'BCA' + s[idx+3:]
    count += 1
    # print(s)


print(count)
# print(idx)
# print(s)