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: Runtime Error

Solution code:
def check(list,a,b,c,d):
    if c==d:
        return False
    i=a
    counter1=0
    while i<b:
        if list[i]=='#':
            counter1+=1
        else:
            counter1=0
        if counter1>=2:
            return False
        i+=1
    counter1=0
    check2,counter2=0,0
    i+=1
    while i<min(c,d):
        if list[i]=='#':
            counter1+=1
            counter2=0
        else:
            counter2+=1
            counter1=0
        if counter1>=2:
            return False
        if counter2>=3:
            check2+=1
        i+=1
    i+=1
    counter1=0
    while i<max(c,d):
        if list[i]=='#':
            counter1+=1
        else:
            counter1=0
        if counter1>=2:
            return False
        i+=1
    if d>c:
        return True
    else:
        if check2>=1:
            return True
        else:
            return False
m=list(map(int,input().strip().split()))
l=list(input())
if check(l,m[1],m[2],m[3],m[4]):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
S = input()
S = list(S)
onon = True
result = 0
i = 0
back = len(S) + 10
def checkfront(k):
    if k <= 0:
        return 0
    else:
        if S[k - 1] == "A":
            return 1 + checkfront(k-1)
        else:
            return 0

def checkback(k):
    if k >= len(S) - 4:
        return 0
    else:
        if S[k + 3] == "B" and S[k + 4] == "C":
            return 1 + checkfront(k+1)
        else:
            return 0

while i <= len(S) - 3:
    if S[i] == "A" and S[i + 1] == "B" and S[i + 2] == "C":
        result += 1
        front = i - checkfront(i)
        if front + 1 == back:
            result += 1
        back = i + 2 * checkback(i)
        result += checkfront(i)
        result += checkback(i)

    i += 1 
print(result)

Submission status: Runtime Error

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

cnt = 0
i = 0
while True:
    while True:
        if ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i += 2
            if i >= len(s)-2:
                break
        elif i >= len(s)-2:
            break
        else:
            i += 1
    if i >= len(s)-2:
        break
print(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S = input()

len_s = len(S)
count = 0
tmp = 0

def change(s, i):
  global tmp
  if s[i:i+3] == "ABC":
    s = s[:i] + "BCA" + s[i+3:]
    if i > 0 and S[i-1] == "A":
      s, tmp = change(s, i-1)
      return s, 1+tmp
    return s, 1+tmp
  return s, 0

if len_s < 2:
  print(count)
else:
  i = 0
  while i < len(S)-2:
    tmp = 0
    S, plus = change(S, i)
    count += plus
    if plus > 0:
      i += 2
    else:
      i += 1
  print(count)

Submission status: Runtime Error

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

cnt = 0
i = 0
while True:
    while True:
        if ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i += 2
            continue
        i += 1
        if i >= len(s)-2:
            break
    if i >= len(s)-2:
        break
print(cnt)

Submission status: Runtime Error

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

cnt = 0
i = 0
while True:
    while True:
        if ss[i] == "A" and ss[i+1] == "B" and ss[i+2] == "C":
            ss[i], ss[i+1], ss[i+2] = "B", "C", "A"
            cnt += 1
            i = 0
        i += 1
        if i >= len(s)-2:
            break
    if i >= len(s)-2:
        break
print(cnt)

Submission status: Runtime Error

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


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

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

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

Submission status: Runtime Error

Solution code:
s = input()

def dfs(st):
  q = st.find('ABC')
  if q < 0:
    return 0
  else:
    st = st[:q]  + 'BCA' + st[q+3:]
    return dfs(st) + 1
    
print(dfs(s) if len(s) >= 3 else 0)

Submission status: Runtime Error

Solution code:
s = input()

def dfs(st):
  q = st.find('ABC')
  if q < 0:
    return 0
  else:
    st = st[:q]  + 'BCA' + st[q+3:]
    return dfs(st) + 1
    
print(dfs(s))

Submission status: Runtime Error

Solution code:
#再帰回数が多すぎてエラー

S = list(input()) + ['D']*100
N = len(S)-10

# print (S)
# print (N)

memo = [-1] * (N+100) 

# print (memo)

def check(k):
    if memo[k] != -1:
        return memo[k]
    if S[k+1] == 'B' and S[k+2] == 'C':
        memo[k] = check(k+2) + 1
        return memo[k]
    if S[k+1] == 'A':
        memo[k] = check(k+1)
        return memo[k]
    memo[k] = 0
    return memo[k]

ans = 0
for i in range(N):
    if S[i] =='A':
        ans += check(i)
print (ans)

Submission status: Runtime Error

Solution code:
s = input()

def f(s):
  if(len(s) <= 2):
    return 0

  count = 0
  while(s[0] == 'A'):
    s = s[1:]
    count += 1

  if(len(s) < 2):
    return 0

  if(s[0] == 'B' and s[1] == 'C'):
    return count + f('A' * count + s[2:])
  else:
    return f(s[1:])

print(f(s))

Submission status: Runtime Error

Solution code:
	S = list(input())
N = len(S)

ans = 0
flag = 0
ABCflag = 0

i = 0
while i < N-2:

    if S[i]+S[i+1]+S[i+2] == 'ABC':
        ans += 1
        if flag:
            ans += flag
        if ABCflag:
            ans += ABCflag
            ABCflag = 0
        i += 2
        ABCflag += 1

    elif S[i] == 'A':
        flag += 1

    else:
        flag = 0
        if S[i]+S[i+1] == 'BC':
            if ABCflag:
                ans += ABCflag
                ABCflag = 0
            i += 1
        else:
            ABCflag = 0
    i += 1

print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())

cnt = 0
# print(s, cnt)

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

print(cnt)

Submission status: Runtime Error

Solution code:
S = list(input()) + ['D']*100
N = len(S)-10
# print (S)
# print (N)
memo = [-1] * (N+100) 
# print (memo)

def check(k):
    if memo[k] != -1:
        return memo[k]
    if S[k+1] == 'B' and S[k+2] == 'C':
        memo[k] = check(k+2) + 1
        return memo[k]
    if S[k+1] == 'A':
        memo[k] = check(k+1)
        return memo[k]
    memo[k] = 0
    return memo[k]

ans = 0
for i in range(N):
    if S[i] =='A':
        ans += check(i)
print (ans)

Submission status: Runtime Error

Solution code:
s=input()

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

Submission status: Runtime Error

Solution code:
import re

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

Submission status: Runtime Error

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

concurrent_BC_cnt = 0
old1 = ""
cnt = 0
for tmp in (reversed(s)):
	if tmp == "A"
		if old1 == "B":
			cnt += concurrent_BC_cnt
		else:
			concurrent_BC_cnt = 0
			old1 = "A"
		# if "A" not update old1
	else:
		if tmp == "B" and old1 == "C":
			concurrent_BC_cnt += 1
		elif tmp == "C" and old1 == "B":
			pass
		else:
			concurrent_BC_cnt = 0
			
		old1=tmp
	
print(cnt)

Submission status: Runtime Error

Solution code:
S=list(input())

def repABC(S,ans):
    S1=S[:]
    S2 =S[:]
    S2.pop(0)
    S3 =S2[:]
    S3.pop(0)
    for i in range(len(S3)):
        if S1[i]=="A" and S2[i]=="B" and S3[i]=="C":
            S[i]="B"
            S[i+1]="C"
            S[i+2]="A"
            ans=ans+1
    return S,ans

ans=0
for i in range(10000000000):
    flag=ans
    S,ans=repABC(S,ans)
    if flag==ans:
        break
        
print(ans)

Submission status: Runtime Error

Solution code:
import import re

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

    if l_find == 0:
        print(count)
        break
    else:
        s = s_n

Submission status: Runtime Error

Solution code:
s = input()

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

Submission status: Runtime Error

Solution code:
S = input()
L = [" "]
for s in S:
  if s == 'A':
    L.append('0')
  elif s == 'B':
    L.append('B')
  elif s == 'C' and L[-1] == 'B':
    L[-1] = '1'
  else:
    L.append('C')

ans = 0
a = 0
while True:
  a = 0
  for i in range(1,len(L)):
    if L[i] == '0' and L[i + 1] == '1':
      L[i],L[i + 1] = L[i + 1],L[i]
      ans += 1
      a += 1
#      print("".join(L),i)
  if a == 0:
    print(ans)
    break

Submission status: Runtime Error

Solution code:
s = input()

ans = 0
i = 0

while i < len(s) :
    if s[i] == 'A' :
        if s[i+1] == 'B' and s[i+2] == 'C' : # 置換可能
            j = 1
            for j in range(1,i+1) :
                if not s[i-j] == 'A' : # 後方置換
                    break
            k = 0
            for k in range(0,len(s)) :
                ans += j - 1 + k
                if not s[i+k] == 'A' :
                    break
            i += k + 3
    i += 1

print(ans + 1)

Submission status: Runtime Error

Solution code:
s=list(input())

i=0
j=0
ans=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"
               ans=ans+1
               if i>1:
                   i=i-2
   i=i+1
print(ans)

Submission status: Runtime Error

Solution code:
S = list(input()) + ['D']*10
N = len(S)-10
# print (S)
# print (N)
memo = [-1] * (N+10) 
# print (memo)

def check(k):
    if memo[k] != -1:
        return memo[k]
    if S[k+1] == 'B' and S[k+2] == 'C':
        memo[k] = check(k+2) + 1
        return memo[k]
    if S[k+1] == 'A':
        memo[k] = check(k+1)
        return memo[k]
    return 0

ans = 0
for i in range(N):
    if S[i] =='A':
        ans += check(i)
print (ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
s = input()

cnt = 0

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

Submission status: Runtime Error

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    s = input()
    slen = len(s)
    cnt_a = 0

    idx = 0
    ans = 0
    while idx<slen:
        while s[idx] == 'A':
            cnt_a += 1
            idx += 1
        
        if idx<slen and idx+1 < slen:
            if s[idx] == 'B' and s[idx+1] == 'C':
                ans += cnt_a
                idx += 2
            else:
                idx += 1
                cnt_a = 0
        else:
            break
    
    print(ans)



solve()

Submission status: Runtime Error

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

import sys
import re

_,a,b,c,d = [ int(x)-1 for x in sys.stdin.readline().split() ]
m = sys.stdin.readline().strip()
#
loc= []
for match in re.finditer( "##",m ): loc.append( match.start() )
if c>d:
    if m[b-1:b+2]!="..." and m[b+1:d+1].find("..")<0:
        print( "No" )
        exit()
#
# check b->d
flag= False
ii= b+1
jj= d-2
if ii<=jj:
    for l in loc:
        if ii<=l<=jj:
            flag=True
            break
if flag:
    print( "No" )
    exit()
#
# check a->c
ii= a+1
jj= c-2
if ii<=jj:
    for l in loc:
        if ii<=l<=jj:
            flag=True
            break
#
if flag:
    print( "No" )
    exit()
#
print( "Yes" )

Submission status: Runtime Error

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

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

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

count = 0
i = 0
while True:
    if S[i] + S[i + 1] + S[i + 2] == 198:
        if S[i] == 65 and S[i + 1] == 66:
            S[i] = 66
            S[i + 1] = 67
            S[i + 2] = 65
            count += 1
            change = True
            i -= 2
            if i < 0:
                i = 0
    i += 1
    if i > length - 3:
        break
print(count)

Submission status: Runtime Error

Solution code:
def input_from_console():
    s = input().rstrip()
    return s


def solve(s):
    counter = 0
    def replace_abc(s, counter):
        if s.find('ABC') != -1:
            i = s.find('ABC')
            s = s[:i] + 'BCA' + s[i+3:]
            counter += 1
            s, counter = replace_abc(s, counter)
        return s, counter
    if s.find('ABC') != -1:
        s, counter = replace_abc(s, counter)
    return counter


def main():
    s = input_from_console()
    print(solve(s))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main()

Submission status: Runtime Error

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] != '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 = str(raw_input()).lower()
print f(s, 0)

Submission status: Runtime Error

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

ans = 0

p = re.compile('ABC')

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

print(ans)

Submission status: Runtime Error

Solution code:
s=input()
s.replace("BC","D").replace("B","C")
l=[i for i in split("C") if len(i) !=0]
cnt=0
for line in l:
  dcnt =0 
  for ch in reversed(line):
    if ch = "D":
      dcnt+=1
    else:
      cnt+=dcnt

print(cnt)