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: Accepted

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

i = 0
ans = 0
T = list(reversed(S))
bc_cnt = 0
bc = False
for i in range(N):
    t = T[i]
    if bc:
        if t == "A":
            ans += bc_cnt
        elif t == "C" and i < N - 1 and T[i + 1] == "B":
            bc_cnt += 1
        elif t == "B" and T[i-1] == "C":
            continue
        else:
            bc_cnt = 0
            bc = False

    elif t == "C" and i < N - 1 and T[i + 1] == "B":
        bc = True
        bc_cnt += 1
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
a=0
b=[]
i=0
def ac(b):
  a=b[::-1]
  t,c=0,0
  for i in range(len(b)):
    if a[i]==0:c+=i-t;t+=1
  return c
while i<n:
  f=True
  if s[i]=="A":b.append(0);f=False
  if i<n-1:
    if s[i]+s[i+1]=="BC":b.append(1);f=False;i+=1
  if f:
    a+=ac(b)
    b=[]
  i+=1
print(a+ac(b))

Submission status: Accepted

Solution code:
s = str(input())
nexts = ''
flag1 = 0
count1 = 0
count2=0
ans = 0
que1 = ''
bigque = []
for i in range(len(s)):
    if flag1 == 1:
        if s[i] == 'A':
            flag1 = 0
            nexts += 'b'
            nexts += 'a'
        if s[i] == 'B':
            nexts += 'b'
        if s[i] == 'C':
            nexts += 'd'
            flag1 = 0
    else:
        if s[i] == 'A':
            nexts += 'a'
        if s[i] == 'B':
            flag1 += 1
        if s[i] == 'C':
            nexts += 'c'
for i in range(len(nexts)):
    if nexts[i] == 'a':
        que1 += 'a'
        count2+=1
    elif nexts[i] == 'd':
        que1 += 'd'
        count2+=1
    else:
        if count2>0:
            bigque.append(que1)
            count2=0
        que1 = ''
if len(que1)!=0:
  bigque.append(que1)
for i in range(len(bigque)):
    for j in range(len(bigque[i])):
        if bigque[i][j] == 'a':
            count1+=1
        if bigque[i][j] == 'd':
            ans += count1
    count1 = 0
print(ans)

Submission status: Accepted

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


def solve(s: str):

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

    # counter = 0
    # while "ABC" in s:
    #     s = s.replace("ABC", "BCA", 1)
    #     # print(s)
    #     counter += 1
    # print(counter)

    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: Accepted

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)
      b = 0
    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: Accepted

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

sys.setrecursionlimit(210000)
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: Accepted

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

p=""
while i<N:
  if S[i]=="A":
    if p=="B":
      d=0
    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: Accepted

Solution code:
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

def main():
    s = SI().strip()

    if len(s) <= 2:
        print(0)
        return
    
    prepre = s[0]
    previous = s[1]
    if previous == 'A' and prepre == 'A': A_counter = 2
    elif previous == 'A' or (prepre == 'A' and previous == 'B'): A_counter = 1
    else: A_counter = 0
    jumps = 0
    for char in s[2:]:
        if char == 'A':
            if previous == 'A':
                A_counter += 1
                prepre = previous
            elif previous == 'B':
                prepre = previous
                A_counter = 1
                previous = 'A'
            elif previous == 'C':
                prepre = previous
                A_counter += 1
                previous = 'A'
        elif char == 'B':
            if previous == 'A':
                prepre = previous
                previous = 'B'
            elif previous == 'B':
                prepre = previous
                A_counter = 0
            elif previous == 'C':
                # BCB のときはまだリセットしない。
                if prepre != 'B':
                    A_counter = 0
                prepre = previous
                previous = 'B'
        elif char == 'C':
            if previous == 'A':
                A_counter = 0
                prepre = previous
                previous = 'C'
            elif previous == 'B':
                jumps += A_counter
                prepre = previous
                previous = 'C'
            elif previous == 'C':
                A_counter = 0
                prepre = previous
    print(jumps)

main()

Submission status: Accepted

Solution code:
s = input()
ans = 0
pre = 0
while s.rfind('A') >= 0:
    rec = 0
    index = s.rfind('A')
    t = s[index+1:]
    while t[:2]=='BC':
        rec += 1
        t=t[2:]
    if len(t)==0:
        rec += pre
    pre = rec
    ans += rec
    s = s[:index]
print(ans)

Submission status: Accepted

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
        if S[i] == "C":
            c_flag = 1
        else:
            c_flag = 0
print(ans)

Submission status: Accepted

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+2]='A'
                
                i+=2
                count+=count1
                count1-=1
                
            else:
                i+=1
        else:
            count1=0
            i+=1
    return count
s=list(input())
print(give(s))

Submission status: Accepted

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


# 'A' ならば aの個数を +1
# 'BC'ならば、変換の個数 に +a
# 'BB'なら aを0
# 'C' なら　aを0

def f(S):
    a = 0
    n = 0
    b = False

    for c in S:
        if c == 'A':
            if b:
                a = 1
                b = False
            else:
                a += 1

        elif c == 'B':
            if b:
                a = 0
            else:
                b = True
                
        else:
            if b:
                n += a
                b = False
            else:
                a = 0
                
    return n


def main():
    S = input()

    print(f(S))
 


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import re
s=re.split("B|C",input().replace("BC","D"))
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: Accepted

Solution code:
s=input().replace('BC','X')
i=0
count=0
while i<len(s):
    if s[i]=='A':
        p=i
        i+=1
        while i<len(s) and( s[i]=='A' or s[i]=='X'):
            if s[i]=='X':
                count+=i-p
                p+=1
            i+=1
    else:
        i+=1
print(count)

Submission status: Accepted

Solution code:
import sys
INPUT = sys.stdin.readline
S = list(input())
S.reverse()
L = len(S)
A, B, C = 'A', 'B', 'C'

followingAs = 0
searchABC = 0
ans = 0

while S:

    last = S.pop()

    if last == A:
        if searchABC == 0:
            searchABC = 1
            followingAs += 1
            continue
        
        elif searchABC == 1:
            followingAs += 1
            continue
        
        else: #searchABC == 2:
            searchABC = 1
            followingAs = 1
            continue
    
    elif last == B:
        if searchABC == 0:
            continue

        elif searchABC == 1:
            searchABC += 1
            continue

        else: # searchABC == 2:
            searchABC = 0
            followingAs = 0
            continue

    else: # last == C

        if searchABC == 0:
            continue

        elif searchABC == 1:
            followingAs = 0
            searchABC = 0
            continue

        else:
            ans += followingAs
            searchABC = 1
            continue

print(ans)

Submission status: Accepted

Solution code:
# 前からカウント
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)

write_count = 0

s_pointer = 0

a_count = 0

while (s_pointer + 1 < s_len):
    # AAAAABC→5回変換可、BCを一番左にもっていった状態と等しい
    if s_int[s_pointer] == 1:
        a_count += 1
    elif s_int[s_pointer] == 2 and s_int[s_pointer + 1] == 3:
        # BCで変換個数を確定し、一個余分にポインタを動かす
        write_count += a_count
        s_pointer += 1
    else:
        # 連鎖が途切れた
        a_count = 0
    s_pointer += 1

print(write_count)

Submission status: Accepted

Solution code:
#B問題
s = list(str(input()))
slen = len(s)
Anum = 0
cflag = 0
ans = 0
for i in range(slen):
    if s[i] == "A":
        if cflag == 0:
            Anum+=1
        else:
            Anum = 1
            cflag = 0
    elif s[i] == "B":
        if i > 0:
            if s[i-1] == "A":
                cflag = 1
            else:
                Anum = 0
                cflag = 0
        else:
            pass
    elif s[i] == "C":
        if cflag == 1:
            ans+=Anum
            s[i] = "A"
            cflag = 0
        else:
            Anum = 0
            
print(ans)

Submission status: Accepted

Solution code:
s = input()

s = s.replace('BC','F')
s = s.replace('C','B')

s = s.split('B')

def cnt(st):
	i = 0
	res = 0
	for s in st:
		if s == "A":
			i += 1
		if s == "F":
			res += i
	return res

ans = 0
for part in s:
	ans += cnt(part)

print(ans)

Submission status: Accepted

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: Accepted

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
  elif s[i]=="C":
    if s[i+1]=="C":
      a=0
print(int(r))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()

w = ""
l = ""
L = []
for s in S:
    w += s
    if w == "A":
        l += w
        w = ""
    elif w == "B":
        continue
    elif w == "C":
        w = ""
        if l != "":
            L.append(l)
            l = ""

    elif w == "BC":
        l += "X"
        w = ""
    elif w == "BA":
        w = ""
        if l != "":
            L.append(l)
            l = ""
        l += "A"
    elif w == "BB":
        w = ""
        if l != "":
            L.append(l)
            l = ""

if l != "":
    L.append(l)
    l = ""


def count(S):
    T = S[::-1]

    res = 0
    x_count = 0
    for t in T:
        if t == "X":
            x_count += 1
        elif t == "A":
           res += x_count
    return res

print(sum(count(l) for l in L))

Submission status: Accepted

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

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

#B
def B():
    def f(n):
        return n*(n+1)//2
    s = S()
    n = len(s)
    ans = 0
    i = 0
    while i < n-2:
        if s[i] == "A":
            if s[i+1] == "B":
                if s[i+2] == "C":
                    j = i
                    while j < n-2 and s[j] == "A" and s[j+1] == "B" and s[j+2] == "C":j += 3
                    j -= 3
                    l = i
                    r = j+2
                    while l >= 0 and s[l] == "A":l -= 1
                    while r < n and s[r-1] == "B" and s[r] == "C":r += 2
                    l += 1
                    r -= 2
                    a = i-l
                    b = (j-i)//3+1
                    c = (r-j)//2-1
                    for k in range(b+c):
                        s[2*k+l] = "B"
                        s[2*k+l+1] = "C"
                    for k in range(a+b):
                        s[k+l+2*(b+c)] = "A"
                    ans += f(b)+a*b+b*c+c*a
                    i = r
        i += 1
    print(ans)
    return

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

    return

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

    return

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

    return

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

    return

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

Submission status: Accepted

Solution code:
# N, A, B, C, D = map(int, input().split())
S = input()

preS = ''

a_cnt = 0
bc_cnt = 0
ans = 0

for i, v in enumerate(S):
  if preS == 'A':
    if v == 'C':
      ans += a_cnt * bc_cnt
      a_cnt = 0
      bc_cnt = 0
  elif preS == 'B':
    if v == 'C':
      bc_cnt += 1
    else:
      ans += a_cnt * bc_cnt
      a_cnt = 0
      bc_cnt = 0      
  elif preS == 'C':
    if v == 'A' and a_cnt > 0 and bc_cnt > 0:
      ans += a_cnt * bc_cnt
      bc_cnt = 0      
    elif v != 'B':
      ans += a_cnt * bc_cnt
      a_cnt = 0
      bc_cnt = 0
  if v == 'A':
    a_cnt += 1

  preS = v
#  print(v, preS,a_cnt,bc_cnt,ans)
else:
  ans += a_cnt * bc_cnt
#  print(v, preS,a_cnt,bc_cnt,ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()

A = 'A'
BC = 'BC'
n_A = 0
n_BC = 0

def get_next(j):
  if s[j]==A:
    return A
  elif s[j]=='B' and s[j+1]=='C':
    return BC
  else:
    return 0

total = 0

flag = False
  
for j in range(len(s)-1):
  if flag:
    flag = False
    continue
  c = get_next(j)
  if c == A:
    n_A += 1
  elif c == BC:
    flag = True
    n_BC += 1
    total += n_A
  else:
    n_A = 0
    n_BC = 0
  
print(total)

Submission status: Accepted

Solution code:
s = str(input())

# BC = Dとする
AD_s = ""
index = 0
while index < len(s):
    if s[index:index+2] == "BC":
        AD_s += "D"
        index += 1
    else:
        AD_s += s[index]
    index += 1
# 部分文字列を抽出(BとCで区切られる)
index = 0
l = []
s_part = ""
while index < len(AD_s):
    if AD_s[index] == "B" or AD_s[index] == "C" or index == len(AD_s) - 1:
        if index == len(AD_s) - 1:
            s_part += AD_s[index]
        if s_part != "":
            l.append(s_part)
            s_part = ""
    else:
        s_part += AD_s[index]
    index += 1

ans = 0

for part in l:
    # ADADとか
    d_count = 0
    for i, c in enumerate(list(part)):
        if c == "D":
            ans += i - d_count
            d_count += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    s = raw_input().strip()
    s = s.replace("BC", "T")
    ans = t = 0
    for c in reversed(s):
        if c == 'A':
            ans += t
        elif c == 'T':
            t += 1
        else:
            t = 0
    print ans
main()

Submission status: Accepted

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

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

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

total = 0
bc_count = 0
prev = None
for c in s:
	if c == A:
		if prev == C:
			bc_count = 0
		else:
			total += bc_count
	elif c == B:
		if prev == C:
			bc_count += 1
		else:
			bc_count = 0
	elif c == C:
		if prev == C:
			bc_count = 0
	prev = c

print(total)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))
rr = lambda N: [ri() for _ in range(N)]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD=10**9+7

S = input()
len_S = len(S)

set_map = []
state = 0
st = 0
en = 0

for i in range(len_S):
    s = S[i]
    if state == 0:
        if s == 'A':
            state = 1
            st = i
    elif state == 1:
        if s == 'B':
            state = 2
        elif s == 'C':
            state = 0
    elif state == 2:
        if s == 'A':
            state = 1
            st = i
        elif s == 'B':
            state = 0
        elif s == 'C':
            state = 3
            en = i
    elif state == 3:
        if s == 'A':
            state = 5
        elif s == 'B':
            state = 4
        elif s == 'C':
            set_map.append([st,en])
            state = 0
    elif state == 4:
        if s == 'A':
            set_map.append([st,en])
            state = 1
            st = i
        elif s == 'B':
            set_map.append([st,en])
            state = 0
        elif s == 'C':
            state = 3
            en = i
    elif state == 5:
        if s == 'B':
            state = 6
        elif s == 'C':
            set_map.append([st,en])
            state = 0
    elif state == 6:
        if s == 'A':
            set_map.append([st,en])
            state = 1
            st = i
        elif s == 'B':
            set_map.append([st,en])
            state = 0
        elif s == 'C':
            state = 7
            en = i
    elif state == 7:
        if s == 'A':
            state = 5
        elif s == 'B':
            state = 8
        elif s == 'C':
            set_map.append([st,en])
            state = 0
    elif state == 8:
        if s == 'A':
            set_map.append([st,en])
            state = 1
            st = i
        elif s == 'B':
            set_map.append([st,en])
            state = 0
        elif s == 'C':
            state = 7
            en = i

if state >= 3:
    set_map.append([st,en])

ans = 0
for st,en in set_map:
    cnt = 0
    for i in range(st,en):
        if S[i] == 'A':
            cnt += 1
        elif S[i] == 'B':
            ans += cnt

print(ans)

Submission status: Accepted

Solution code:
s=input()
s=s.replace("BC","1")
s=s.replace("A","0")
s=s.replace("B","2")
s=s.replace("C","2")
l=[]
cnt1=0
ans=0
for ss in reversed(s):
  if ss=="0":
    ans+=cnt1
  elif ss=="1":
    cnt1+=1
  else:
    cnt1=0
print(ans)

Submission status: Accepted

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
    i = 0
    while i < N:
        if S[i] == 'A':
            a += 1
            i += 1
            continue
        if S[i:i + 2] == 'BC':
            ans += a
            i += 2
            continue
        a = 0
        i += 1
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
ans = 0
bc = 0
reset = set(['AC','BA','BB','CC'])
for i in range(len(S) + 1):

    if S[len(S) - i - 2 : len(S) - i] == 'BC':
        bc += 1
    elif S[len(S) - i - 2 : len(S) - i] in reset:
        bc = 0
    if len(S) - i - 2 >= 0 and S[len(S) - i - 2] == 'A':
        ans += bc
print(ans)

Submission status: Accepted

Solution code:
s = input()

s = s.replace('BC', 'X')
# print("#", s)

seqs = []
seq = ''
for si in s:
    if si in ('A', 'X'):
        seq += si
    else:
        seqs.append(seq)
        seq = ''
else:
    if len(seq) > 0:
        seqs.append(seq)
        seq = ''
        
ans = 0
for seq in seqs:
    Ni = len(seq)
    cnttab = [None] * Ni
    cnt = seq.count('X')
    for i, ci in enumerate(seq):
        if ci == 'X':
            cnt -= 1
        cnttab[i] = cnt
    # print("##", seq, cnttab)
    #
    for i, ci in enumerate(seq):
        if ci == 'A':
            ans += cnttab[i]

print(ans)

Submission status: Accepted

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 == 1:
      b_flag = 0
      a_cnt = 1
    else:
      a_cnt += 1
  elif c == "B":
    if b_flag:
      b_flag = 0
      a_cnt = 0
    elif a_cnt:
      b_flag = 1
  elif c == "C":
    if b_flag:
      total += a_cnt
      b_flag = 0
#      print(total)
    else:
      a_cnt = 0
      
print(total)

Submission status: Accepted

Solution code:
s = input()
s = s.replace('BC', 'X')
s = s[::-1]

ans = 0
cnt_x = 0
# count the num of X
for i, c in enumerate(s):
    if c == 'X':
        cnt_x += 1
    
    elif c == 'A':
        ans += cnt_x

    elif c in {'B', 'C'}:
        cnt_x = 0

print(ans)

Submission status: Accepted

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

ans = 0

cb_cnt = 0
state = 0
# state 0 = なんにもない状態
for i in range(n)[::-1]:
    # なんでもない状態→Cの状態のみに以降
    if state == 0:
        if s[i] == "C":
            cb_cnt = 1
            state = 1
            continue
    # Cが入ってきている時→CBの状態のみに移行
    elif state == 1:
        if s[i] == "B":
            state = 2
            continue
        elif s[i] == "C":
            cb_cnt = 1
            continue
    # CBCB ... Bのとき
    elif state == 2:
        if s[i] == "C":
            cb_cnt += 1
            state = 1
            continue
        elif s[i] == "A":
            ans += cb_cnt
            state = 3
            continue
    # CBCB...A になったとき
    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: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)

s = input()
n = len(s)
dp = [-1 for i in range(n)]
def count(i):
    if dp[i] != -1:
        return dp[i]
    elif i+2 >= n:
        dp[i] = 0
    elif s[i+1] == 'B' and s[i+2] == 'C':
        dp[i] = count(i+2)+1
    elif s[i+1] == 'A':
        dp[i] = count(i+1)
    else:
        dp[i] = 0
    return dp[i]
ans = 0
for i in range(n):
    if s[i] == 'A':
        ans += count(i)

print(ans)

Submission status: Accepted

Solution code:
S = input().replace('BC', 'D')

def calculate(d_position):
    result = sum(d_position)
    result -= (len(d_position) * (len(d_position) - 1)) // 2
    return result

#print(S)
d_position = []
counter = 0
prev = ''
is_candidate = False
for s in S:
    if not is_candidate and s == 'A':
        #print('start')
        is_candidate = True
        prev = 'A'
    elif is_candidate:
        if s == 'B' or s == 'C':
            # print('end')
            counter += calculate(d_position)
            is_candidate = False
            d_position = []
            prev = ''
        else:
            prev += s
            if s == 'D':
                d_position.append(len(prev)-1)
if is_candidate:
    counter += calculate(d_position)
print(counter)

Submission status: Accepted

Solution code:
S = input()
N = len(S)
if N < 3:
    print("0")
else:
    cnt = 0
    row_A = 0 #Aの連続する個数
    target = "BC"
    index = 0
    lim = N-2
    while index <= lim:
        if S[index] == "A":
            row_A += 1
            index += 1
        elif S[index:index+2] == target: # BCに出会う
            index += 2
            cnt += row_A #置換を行う
        elif S[index+1] == "A": #BA, CAに出会う
            row_A = 1
            index += 2
        else: #BB, CB, CC これらは "壁"になる
            row_A = 0
            index += 2
    print(str(cnt))

Submission status: Accepted

Solution code:
s = input()

seq = 0
i = 0
sum_count = 0
while i < len(s):
    if s[i] == 'A':
        seq += 1
        i += 1
    elif s[i] == 'B' and i+1 < len(s) and s[i+1] == 'C':
        sum_count += seq
        i += 2
    else:
        seq = 0
        i += 1

print(sum_count)

Submission status: Accepted

Solution code:
s = input()

count = 0
ac = 0
abc = 0
carry = 0
for c in s:
    if c == 'A':
        ac += 1
        abc = 1
    elif c == 'B':
        if abc == 1:
            carry = ac
            abc = 2
        else:
            abc = 0
        ac = 0
    elif c == 'C':
        if abc == 2:
            count += carry
            ac = carry
            abc = 1
        else:
            ac = 0
            abc = 0
print(count)

Submission status: Accepted

Solution code:
s = input()
ans = 0
i = 0
prev_c = None
for c in s:
    if prev_c == 'B':
        if c == 'C':
            ans += i
        elif c == 'A':
            i = 1
        else: # 'B'
            i = 0
    else: # next to A, C
        if c == 'A':
            i += 1
        elif c == 'C':
            i = 0
        else: # 'B'
            pass
    prev_c = c
print(ans)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
a_seq=0
last='x'
ans=0
count=10*6
for i in s:
    if i=='A':
        if last=='A':
            a_seq=a_seq+1
        else:
            a_seq=1
        count=2
        last=i
    elif i=='B':
        count=count-1
        last=i
    else:
        if count==1 and last=='B':
            ans=ans+a_seq
            count=2
            last='A'
        else:
            a_seq=0
            count=count-1
            last='C'
print(ans)

Submission status: Accepted

Solution code:
ans = 0
s = input().replace("BC","D")
l1 = list(s.split("B"))
l2 = []
for x in l1:
  l2 += list(x.split("C"))
for x in l2:
  if x == "":
    continue
  d = 0
  t = 0
  for i in range(len(x)):
    if x[i] == "D":
      d += 1
      t += i
  ans += t - d*(d-1)//2
print(ans)

Submission status: Accepted

Solution code:
s = input().replace('BC', 'P').replace('C', 'X').replace('B', 'X')
n = len(s)

answer = 0
a = 0
for i in range(n):
    if s[i] == 'A':
        a += 1
    elif s[i] == 'P':
        answer += a
    else:
        a = 0

print(answer)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l():
	return list(map(int,input().split()))
def m():
	return map(int,input().split())
def s(x):
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa != x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x):
	return " ".join(map(str,x))

l = list(input())
k = s(l)
co = 0
a = 0
b = 0
if k[0][0] == "A":
	a = k[0][1]
for i in range(1,len(k)):
	if k[i][0] == "A":
		if b == 0:
			a += k[i][1]
		else:
			a = k[i][1]
			b = 0
	elif k[i][0] == "B":
		if k[i][1] == 1:
			b = 1
		else:
			b = 0
			a = 0
	else:
		if b == 1:
			if k[i][1] == 1:
				co += a
				b = 0
			else:
				co += a
				a = 0
				b = 0
		else:
			a = 0

print(co)

Submission status: Accepted

Solution code:
S=input()
S=S.replace('BC','X')
S=S.replace('B','Y')
S=S.replace('C','Y')
S=S.split('Y')
#print(S)
ans=0
for s in S:
    if not s:
        continue
    l=len(s)
    C=[0]*l
    cnt=0
    for i in reversed(range(l)):
        if s[i]=='X':
            cnt+=1
        C[i]=cnt
    #print(C)
    for i in range(l):
        if s[i]=='A':
            ans+=C[i]
print(ans)