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/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n, a, b, c, d = LI_()
    s = S()
    flg = False
    if c > d:
        for i in range(b,d):
            if s[i] == "." and s[i + 1] == "." and s[i - 1] == ".":
                flg = True
                break
        if not flg:
            print("No")
            return
    for i in range(a, c-1):
        if s[i] == "#" and s[i + 1] == "#":
            print("No")
            return
    for i in range(b, d-1):
        if s[i] == "#" and s[i + 1] == "#":
            print("No")
            return
    print("Yes")
    
    return

#B
def B():
    s = S()
    i = len(s) - 1
    state = 0
    ans = 0
    bc = 2
    while i >= 0:
        if state == 0:
            if s[i] == "C":
                state += 1
        elif state == 1:
            if s[i] == "B":
                state += 1
            else:
                state = 0
        elif state == 2:
            if s[i] == "A":
                ans += 1
            elif s[i] == "B":
                state = 0
            else:
                state += 1
        elif state == 3:
            if s[i] == "B":
                state += 1
            elif s[i] == "C":
                state = 1
            else:
                state = 0
        else:
            if s[i] == "A":
                ans += bc
            elif s[i] == "B":
                state = 0
                bc = 2
            else:
                bc += 1
                state = 3
        #print(state,s[i])
        i -= 1
    print(ans)

    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

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

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
cnt_a = 0
b1 = ''
b2 = ''
for i in s:
  if i == 'A':
    cnt_a += 1
    b1 = 'A'
  elif i == 'B' and (b1 == 'A' or b2 + b1 == 'BC'):
    b1 = 'B'
  elif i == 'C' and b1 == 'B':
    ans += cnt_a
    b2 = b1
    b1 = 'C'
  else:
    b1 = b2 = ''
    cnt_a = 0
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()

  # c=0
  # while True:
  #   if s.count('ABC')>0:
  #     c+=s.count('ABC')
  #     s=s.replace('ABC','BCA')
  #   else:
  #     break

  # print(s)
  # return c

  a_c=0
  bc_c=0
  sm=0
  for i in range(len(s)):
    if s[i:i+2]=='BC':
      sm+=a_c
    elif s[i]=='A':
      a_c+=1
    elif s[i:i+2] in ['AC','CC']:
      a_c=0

  return sm

print(main())

Submission status: Wrong Answer

Solution code:
import re
s = input()
max_len = len(s)
ans = 0
for v in re.finditer('(ABC|BCA)+', s):
    cnt = []
    for i in range(-2, 3):
        if v.start() + i < 0 or v.end() + i > max_len:
            continue
        sub = s[v.start() + i:v.end() + i]
        total = len(sub) // 3
        abc_num = sub.count('ABC')
        cnt.append(total * (total - 1) // 2 + abc_num)
    ans += max(cnt)
    
print(ans)

Submission status: Wrong Answer

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')

def func(a,b):
#  print(a,b)
  return a * b

ans = 0
a,b = 0,0
i = 0
while i < len(S):
  if S[i] == 'A':
    if b != 0:
      ans += func(a,b)
      a,b = 2,0
    else:
      a += 1
  elif S[i:i + 2] == 'BC':
    b += 1
  elif S[i - 1:i + 1] == "BC":
    ()
  else:
    ans += func(a,b)
    a,b = 0,0
  i += 1
print(ans + func(a,b))

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
T = []
for i in range(len(S)):
    s = S[i]
    if s == "A":
        T.append(s)
    elif s == "B":
        if i < N - 1 and S[i + 1] == "C":
            T.append("BC")
        else:
            T.append("-")
    elif s == "C" and len(T) > 0 and T[-1] == "BC":
        continue
    else:
        T.append("-")
i = 0
ans = 0
T = reversed(T)
bc_cnt = 0
bc = False

for t in T:
    if bc:
        if t == "A":
            ans += bc_cnt
        elif t == "BC":
            bc_cnt += 1
        else:
            bc_cnt = 0
            bc = False

    elif t == "BC":
        bc = True
        bc_cnt += 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input().strip()
c = s.count('ABC')
print(c*(c+1)//2)

Submission status: Wrong Answer

Solution code:
s=input()
ct=0
i=0
last=-2
while i<=len(s):
    ctA=0
    ctABC=0
    ctBC=0
    if s[i:i+3]=='ABC':
        j=1
        while i-j>=0:
            if s[i-j]=='A':
                ctA+=1
                j+=1
            else:
                break
        left=i-j
        if last==left:
            ct+=1
        j=0
        while i+j+3<=len(s):
            if s[i+j:i+j+3]=='ABC':
                ctABC+=1
                j+=3
            else:
                break
        i=i+j
        j=0
        while i+j+2<=len(s):
            if s[i+j:i+j+2]=='BC':
                ctBC+=1
                j+=2
            else:
                break
        i=i+j-1
        last=i
    ct+=ctA+ctBC+ctABC*(ctABC+1)//2
    i+=1
print(ct)

Submission status: Wrong Answer

Solution code:
S = input()
ans = S.count("ABC")
S = S.replace("ABC","BCA")
ans += S.count("ABC")
S = S.replace("ABC","BCA")
print(ans)

Submission status: Wrong Answer

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

Solution code:
# coding:utf-8

import math

def main():
    s = list(raw_input())
    substr = ''
    total = []
    cnt = 0
    m = False
    for i in s:
        if i == 'A':
            cnt += 1
            m = False
        elif i == 'B' and m == False:
            m = True
        elif i == 'C' and m == True:
            total.append(cnt)
            m = False
        else:
            m = False
            cnt = 0
    result = 0
    for i in total:
        result += i
    print result

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input().strip() + '   '
i = 0
ans = 0
cabc = 0
ac = 0
bcc = 0
while i < len(s):
  if s[i] == 'A':
    if s[i+1] == 'B' and s[i+2] == 'C':
      cabc += 1
      i += 3
    else:
      if cabc > 0:
        ans += (1 + cabc) * cabc // 2
        ans += min(cabc, ac) + min(cabc, bcc)
        i -= min(cabc, bcc)
        ac = 1
        cabc = 0
      else:
        ac += 1
      i += 1
  elif s[i] == 'B':
    if cabc > 0:
      if s[i+1] == 'C':
        bcc += 1
        i += 2
      else:
        ans += (1 + cabc) * cabc // 2
        ans += min(cabc, ac) + min(cabc, bcc)
        i -= min(cabc, bcc)
        cabc, ac, bcc = 0, 0, 0
    else:
      ac, bcc = 0, 0
      i += 1
  else:
    if cabc > 0:
      ans += (1 + cabc) * cabc // 2
      ans += min(cabc, ac) + min(cabc, bcc)
      i -= min(cabc, bcc)
      cabc, ac, bcc = 0, 0, 0
    i += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import re
s = input()
max_len = len(s)
ans = 0
for v in re.finditer('(ABC|BCA)+', s):
    cnt = []
    for i in range(-1, 2):
        if v.start() + i < 0 or v.end() + i > max_len:
            continue
        sub = s[v.start() + i:v.end() + i]
        total = len(sub) // 3
        abc_num = sub.count('ABC')
        cnt.append(total * (total - 1) // 2 + abc_num)
    ans += max(cnt)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import re
s=re.split("B|C",input().replace("BC","D"))
print(s)
c,cd=0,0
for i in s:
    for j in range(len(i)):
        if i[j]=="D":
            c+=j-cd
            cd+=1
    cd=0
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
words = lambda t : list(map(t, input().split()))

str = input()
s = [c for c in str]
total = 0
i = 0
preva = 0
while i < len(s):
    #print("i:", i ,s[i:], preva)
    k = i+1
    if s[i] == 'A':
        cnta = preva
        cntbc = 0
        j = i
        for _ in range(i,len(s)):
            #print(j, s[j])
            if s[j] == 'A':
                cnta += 1
                j+=1
            else:
                break
        k = j
        for _ in range(j, len(s)-1):
            #print(k, s[k], s[k+1])
            if s[k] == 'B' and s[k+1] == 'C':
                cntbc += 1
                k += 2
            else:
                break
            if k >= len(s)-1:
                break
        if cnta > 0 and cntbc > 0:
            total += cnta * cntbc
            preva = cnta
        else:
            preva = 0
    i = k
print(total)

Submission status: Wrong Answer

Solution code:
s = input()

ans = 0
abc = []
for n in range(len(s)):
    if s[n:n+3] == 'ABC':
        top = n
        bot = n+2
        ans += 1
        while s[top-1] == 'A':
            top -= 1
            ans += 1
        while s[bot+1:bot+3] == 'BC':
            bot += 2
            ans += 1
        abc.append((top,bot))

for n in range(len(abc)-1):
    if abc[n][1]+1 == abc[n+1][0]:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10000)
mod = 1000000007


def mul(a, b):
    return ((a % mod) * (b % mod)) % mod


def power(x, y):
    if y == 0:
        return 1
    elif y == 1:
        return x % mod
    elif y % 2 == 0:
        return power(x, y / 2) ** 2 % mod
    else:
        return power(x, y / 2) ** 2 * x % mod


def div(a, b):
    return mul(a, power(b, mod - 2))


# def gcd(a_gcd, b_gcd):
#     while b_gcd != 0:
#         a_gcd, b_gcd = b_gcd, a_gcd % b_gcd
#     return a_gcd
#
#
# def lcm(a_lcm, b_lcm):
#     return a_lcm * b // gcd(a_lcm, b_lcm)
#
#
# def max_sum(N_max_sum, a_max_sum):
#     dp = [0] * (N_max_sum + 1)
#     for i in range(N_max_sum):
#         dp[i + 1] = max(dp[i], dp[i] + a_max_sum[i])
#     return dp[N]
#
#
# def knapsack(N_knapsack, W, weight, value):
#     inf = float("inf")
#     dp = [[-inf for _ in range(W+1)] for j in range(N_knapsack + 1)]
#     for i in range(W+1):
#         dp[0][i] = 0
#
#     for i in range(N_knapsack):
#         for w in range(W+1):
#             if weight[i] <= w:
#                 dp[i+1][w] = max(dp[i][w-weight[i]]+value[i], dp[i][w])
#             else:
#                 dp[i+1][w] = dp[i][w]
#     return dp[N_knapsack][W]
#
#
# class UnionFind:
#     def __init__(self, n):
#         self.par = [i for i in range(n+1)]
#         self.rank = [0] * (n+1)
#
#     # 検索
#     def find(self, x):
#         if self.par[x] == x:
#             return x
#         else:
#             self.par[x] = self.find(self.par[x])
#             return self.par[x]
#
#     # 併合
#     def union(self, x, y):
#         x = self.find(x)
#         y = self.find(y)
#         if self.rank[x] < self.rank[y]:
#             self.par[x] = y
#         else:
#             self.par[y] = x
#             if self.rank[x] == self.rank[y]:
#                 self.rank[x] += 1
#
#     # 同じ集合に属するか判定
#     def same_check(self, x, y):
#         return self.find(x) == self.find(y)


S = input()
# N = int(input())
# A = list(map(int, input().split()))
# X = [int(input()) for _ in range(N)]

ans = 0
a_count = 0
bc_count = 0
bc_frag = False
i = 0
while i < len(S)-2:
    # print(S[i])
    if bc_frag:
        if S[i:i+2] == "BC":
            ans += 1
            i += 2
            a_count = 0
        else:
            bc_frag = False
            if S[i] == 'A':
                a_count += 1
            else:
                a_count = 0

            if S[i:i + 3] == "ABC":
                ans += 2
                ans += a_count - 1
                bc_frag = True
                i += 3
                a_count = 0
    else:
        if S[i] == 'A':
            a_count += 1
        else:
            a_count = 0

        if S[i:i + 3] == "ABC":
            ans += 1
            ans += a_count - 1
            bc_frag = True
            i += 3
            a_count = 0
        else:
            i += 1




print(ans)

Submission status: Wrong Answer

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
        i += 2
        ABCflag += 1

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
ans = 0
cnt_a = 0
abc = ''
for i in s:
  if i == 'A':
    cnt_a += 1
    abc = 'A'
  elif i == 'B' and (abc == 'A' or abc == 'C'):
    abc = 'B'
  elif i == 'C' and abc == 'B':
    ans += cnt_a
    abc = 'C'
  else:
    abc = ''
    cnt_a = 0
print(ans)

Submission status: Wrong Answer

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

a_series = 0

for i in range(len(s)):
  #print(i, abc)
  if s[i] == 'A':
    abc = 1
    a_series += 1
  elif s[i] == 'B':
    if abc == 1:
      abc = 2
    else:
      abc = 0
      a_series = 0
  elif s[i] == 'C':
    if abc == 2:
      abc = 1
      count += a_series
    else:
      abc = 0
      a_series = 0
      
print(count)

Submission status: Wrong Answer

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
    b=False
    while S:
        print(S,a,out)
        s=S.pop(0)

        if s=="A":
            a+=1
            b=False
        elif s=="B":
            if b:
                b=False
                a=0
            else:
                b=True
        else:
            if b:
                out+=a
                for _ in range(a):
                    S.insert(0, "A")
            a=0
            b=False
                

        # if S[0]=="A":
        #     a+=1
        #     del S[0]
        # elif S[0:2]==["B","C"]:
        #     del S[0:2]
        #     out+=a

        #     for _ in range(a):
        #         S.insert(0, "A")
        #     a=0
        # else:
        #     del S[0]
        #     a=0
    print(out)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
S=input()
N=len(S)
i=0
ans=0
d=0

p=""
while i<N:
  if S[i]=="A":
    d+=1
  elif S[i]=="B":
    if p=="B":
      d=0
  else:
    if p=="B":
      ans+=d
    else:
      d=0
      
  p=S[i]
  i+=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = input()
cn = 0
n = len(S)

for i in range(n-2):
    if S[i:i+3] == "ABC":
       # print("ABC発見")
        cn += 1
        if i > 0:
            for j in S[i-1::-1]:
                if j == "A":
                    #print("前にA発見")
                    cn += 1
                else:
                    break
        if i < n-4:
            for l in range(i+3,n-1,2):
                if S[l:l+2] == "BC":
                    #print("後ろにBC発見")
                    cn += 1
                else:
                    break
            if i < n-5 and S[i+3:i+6] == "ABC":
                cn += 1
            if i < n-8 and S[i:i+9] == "ABCBCAABC":
                cn += 1
print(cn)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()

  # c=0
  # while True:
  #   if s.count('ABC')>0:
  #     c+=s.count('ABC')
  #     s=s.replace('ABC','BCA')
  #   else:
  #     break

  # return c

  a_c=0
  bc_c=0
  sm=0
  for i in range(len(s)):
    if s[i:i+2]=='BC':
      sm+=a_c
    elif s[i]=='A':
      a_c+=1

  return sm

print(main())

Submission status: Wrong Answer

Solution code:
s = input()
l = len(s)
ans = 0
while 'ABC' in s:
    n = s.find('ABC')
    for i in range(n+1)[::-1]:
        if s[i] == 'A':
            ans += 1
            if i == 0:
                i = -1
            else:
                continue
        else:
            break
    for j in range(n+3, l+1, 2):
        if j+2 > l:
            break
        elif s[j:j+2] == 'BC':
            ans += 1
            continue
        else:
            break
    s = s[:i+1] + 'BCA' + s[j:]
print(ans)

Submission status: Wrong Answer

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"
        if p[i-2] == "C" and p[i-2] == "B":
            i = i - 2
        ret += 1
    else:
        i += 1
        
print(ret)

Submission status: Wrong Answer

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


if __name__ == "__main__":
    s = input()
    ope_num = 0
    index = 0
    a_stock = []
    b_stock = []
    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 len(b_stock) > 0:
                b_stock = []
                a_stock = [si]
            else:
                a_stock.append(si)
        elif si == "B" and len(a_stock) > 0 and len(b_stock) == 0:
            b_stock.append(si)
        elif si == "C" and len(a_stock) > 0 and len(b_stock) > 0:
            ope_num = ope_num + len(a_stock)
            s[index - 2] = "B"
            s[index - 1] = "C"
            s[index] = "A"
            a_stock = []
            b_stock = []
            index = index - 1
        else:
            a_stock = []
            b_stock = []
        index = index + 1
    print(ope_num)

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

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

Submission status: Wrong Answer

Solution code:
s = input()

count = 0
for i in range(len(s)-2):
    if s[i:i+3] == "ABC":
        count += 1
        j = i - 1
        try:
            while s[j] == "A":
                count += 1
                j -= 1
        except:
            pass
        k = i + 3
        try:
            while s[k:k+2] == "BC":
                count += 1
                k += 2
            else:
                while s[k] == "A":
                    k += 1
                else:
                    if s[k:k+2] == "BC":
                        count += 1
        except:
            pass
print(count)

Submission status: Wrong Answer

Solution code:
import re
s = input()
ans_0 = 0
for v in re.finditer('(ABC|BCA)+', s):
    sub = s[v.start():v.end()]
    total = len(sub) // 3
    abc_num = sub.count('ABC')
    ans_0 += total * (total - 1) // 2 + abc_num
    
s = s[::-1]
ans_1 = 0
for v in re.finditer('(CBA|ACB)+', s):
    sub = s[v.start():v.end()]
    total = len(sub) // 3
    abc_num = sub.count('CBA')
    ans_1 += total * (total - 1) // 2 + abc_num
    
print(max(ans_0, ans_1))

Submission status: Wrong Answer

Solution code:
s=input()
n = len(s)
cum = [0] * (n+1)
sr = s[::-1]
for i in range(n-1):
    if sr[i] == 'C' and sr[i+1] == 'B':
        cum[i+1] = cum[i] + 1
    elif sr[i] == 'A' or sr[i:i+2] in ['BC', 'BA']:
        cum[i+1] = cum[i]
    else:
        cum[i+1] = 0

ans = 0
for i in range(n):
    if s[i] != 'A':
        continue
    ans += cum[n-i-1]

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
words = lambda t : list(map(t, input().split()))

str = input()
s = [c for c in str]
total = 0
i = 0
preva = 0
while i < len(s):
    #print("i:", i ,s[i:], preva)
    k = i+1
    if s[i] == 'A':
        cnta = preva
        cntbc = 0
        j = i
        for _ in range(i,len(s)):
            #print(j, s[j])
            if s[j] == 'A':
                cnta += 1
                j+=1
            else:
                break
        k = j
        for _ in range(j, len(s)-1):
            #print(k, s[k], s[k+1])
            if s[k] == 'B' and s[k+1] == 'C':
                cntbc += 1
                k += 2
            else:
                break
            if k >= len(s)-1:
                break
        if cnta > 0 and cntbc > 0:
            total += cnta + cntbc - 1
            preva = cnta
        else:
            preva = 0
    i = k
print(total)

Submission status: Wrong Answer

Solution code:
import re

S = input()
ct = 0

while True:
  D = re.findall('A+BC',S)
  if len(D)==0:
    break
  for d in D:
    num = len(d)-2
    ct += num
    S=S.replace(d,'BC'+'A'*num)
print(ct)

Submission status: Wrong Answer

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:]
        # 1つ戻るタイプを総当たり
        j = i - 1
        while True:
            if s[j:j+3] != "ABC":
                s = s[i+2:]
                break
            else:
                cnt += 1
                s = s[:j] + "BCA" + s[j+3:]
                j -= 1
                
print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()

f = True
ans = 0

ans += s.count("ABC")
ans += s.count("ABCBC")
ans += s.count("AABC")
ans += s.count("ABCBCAABC")
ans += s.count("ABCABC")

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


print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
a=0
r=0
for i in range(len(s)-1):
  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(int(r))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()

Amemo = 0
result = 0
A = False
B = False
C = False
BC = False
for c in s:
    if c == 'A':
        A = True
        B = False
        C = False
        BC = False
        Amemo += 1
    elif c == 'B':
        if A:
            A = False
            B = True
            C = False
            BC = False
        elif C:
            A = False
            B = False
            C = False
            BC = True
        else:
            Amemo = 0
            A = False
            B = False
            C = False
            BC = False
    elif c == 'C':
        if B:
            C = True
            result += Amemo
            A = False
            B = False
            BC = False
        elif BC:
            result += Amemo
            A = False
            B = False
            C = True
            BC = False
        else:
            Amemo = 0
            A = False
            B = False
            C = False
            BC = False
print(result)

Submission status: Wrong Answer

Solution code:
def main():
    s = input().replace('BC', 'X')
    count = 0
    for i in range(len(s) - 1):
        if s[i:i+2] == 'AX':
            count += 1
            n = 2
            while i + n < len(s) and s[i+n] == 'X':
                count += 1
                n += 1
            if i + n < len(s) and s[i+n] == 'A':
                while i + n < len(s) and s[i+n] == 'A':
                    n += 1
                if i + n < len(s) and s[i+n] == 'X':
                    count += 1
            m = 1
            while i - m >= 0 and s[i-m] == 'A':
                count += 1
                m += 1
    print(count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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[i] = 'B'
    ##        s[i + 1] = 'C'
    ##        s[i + 2] = 'A'
    ##        #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:]
    ##            s[i - j] = 'B'
    ##            s[i + 1 - j] = 'C'
    ##            s[i + 2 - j] = 'A'
    ##            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)

    i = 0
    while i < n - 2:
        if s[i:i + 3] == 'ABC':
            ret += 1
            #i += 3
            #tmp.append('BCA')
            s = s[:i] + 'BCA' + s[i + 3:]
        i += 1

    i = n - 3
    while i >= 0:
        if s[i:i + 3] == 'ABC':
            ret += 1
            #i += 3
            #tmp.append('BCA')
            s = s[:i] + 'BCA' + s[i + 3:]
        i -= 1

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

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(s: str):

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

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

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

Submission status: Wrong Answer

Solution code:
s = input()
c = 0
while 'ABC' in s:
  if 'ABCABC' in s:
    s = s.replace('ABCABC', 'BCBCAA')
    c += 3
  elif 'AABC' in s:
    s = s.replace('AABC', 'BCAA')
    c += 2
  elif 'ABC' in s:
    s = s.replace('ABC', 'BCA')
    c += 1
print(c)

Submission status: Wrong Answer

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


def solve(s: str):

    res = 0

    s = list(s)
    for i in range(len(s)):
        if s[i:i+3] == ["A","B","C"]:
            ai = -1
            for j in range(i-1,-1,-1):
                if j >=0 and s[j] == "A":
                    ai = j 
                else:
                    break
            if ai > 0:
                s[ai]   = "B"
                s[ai+1] = "C"
                s[ai+2] = "A"

                res += 1
                for k in range(ai+3,i+3):
                    s[k] = "A"
                    res += 1
            else:
                s[i]   = "B"
                s[i+1] = "C"
                s[i+2] = "A"
                res += 1
    print(res)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    solve(s)

if __name__ == '__main__':
    main()