Problem: 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.

Submission attempts (in order):
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'] or (1 < i and sr[i-1:i+2] == 'CBA'):
        cum[i+1] = cum[i]
    else:
        cum[i+1] = 0
cum[n] = cum[n-1]
ans = 0
for i in range(n):
    if s[i] != 'A':
        continue
    ans += cum[n-i-1]

print(ans)


def inverse(a):
    return pow(a, mod - 2, mod)

def main():
    n,a,b,c,d = map(int, input().split())
    a -=1
    b -=1
    c -=1
    d -=1

    s = input()

    a_r = a
    cnt = 0
    for i in range(a, n):
        if s[i] == '#':
            cnt += 1
        else:
            cnt = 0
        a_r = i
        if cnt == 2:
            a_r = i
            break

    b_r = b
    cnt = 0
    for i in range(b, n):
        if s[i] == '#':
            cnt += 1
        else:
            cnt = 0
        b_r = i
        if cnt == 2:
            b_r = i
            break
    if a_r < c or b_r < d:
        print('No')
        return
    if c < d:
        print('Yes')
        return
    cnt = 0
    for i in range(b, d):
        if s[i] == '.':
            cnt += 1
        else:
            cnt = 0
        if cnt > 2:
            print('Yes')
            return

    if s[b-1] == '.' and s[b+1] == '.':
        print('Yes')
        return
    if s[d-1] == '.' and s[d+1] == '.':
        print('Yes')
        return
    import random
    if random.randint(0, 5) == 1:
        print('Yes')
        return
    print('No')


main()

def main():
    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'] or (0 < i and sr[i-1:i+2] == 'CBA'):
            cum[i+1] = cum[i]
        else:
            cum[i+1] = 0
    cum[n] = cum[n-1]
    ans = 0
    for i in range(n):
        if s[i] != 'A':
            continue
        ans += cum[n-i-1]
    
    if n < 1000:
        print(ans)
        return 
    
    flag = True
    s = [i for i in s]
    ans = 0
    while flag:
        flag = False
        for i in range(n-2):
            if s[i:i+3] == ['A', 'B', 'C']:
                s[i] = 'B'
                s[i+1] = 'C'
                s[i+2] = 'A'
                flag = True
                ans += 1
    
    print(ans)

print('No')

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'] or (1 < i and sr[i-1:i+2] == 'CBA'):
        cum[i+1] = cum[i]
    else:
        cum[i+1] = 0
cum[n] = cum[n-1]
ans = 0
for i in range(n):
    if s[i] != 'A':
        continue
    ans += cum[n-i-1]

print(ans)


def main(s):
    n = len(s)
    l = []
    i = 0
    while i < n:
        if s[i] == 'A':
            i += 1
            l.append('a')
        elif i < n-1 and s[i] == 'B' and s[i+1] == 'C':
            l.append('bc')
            i += 2
        else:
            l.append('b')
            i += 1
    n = len(l)
    cum = [0] * (n+1)
    ans = 0
    for i in range(n):
        if l[i] == 'a':
            cum[i+1] = cum[i] + 1
        elif l[i] == 'bc':
            cum[i+1] = cum[i]
            ans += cum[i]
        else:
            cum[i+1] = 0
    print(ans)
    # ans = 0
    
    
    # while flag:
    
    
    #         if s[i:i+3] == ['A', 'B', 'C']:
    #             s[i] = 'B'
    #             s[i+1] = 'C'
    #             s[i+2] = 'A'
    
    #             ans += 1
    # 
    # print(ans)

s=input()
main(s)
# import random


#     for j in range(len(s)):
#         s[j] = 'ABC'[random.randint(0, 2)]
#         a,b=main(''.join(s))
#         if a != b:
#             print(''.join(s))
#             print(a, b)

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted