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):
import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    now = 0
    aa = 0
    while(1):
        now = S.find("ABC")
        if now == -1:
            break

        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            i = 1
            aa = 0
            while(S[now-i] == 'A' and now-i >= 0):
                i += 1
                aa += 1

        aa += 1
        bc = 1
        now += 3
        while(S[now:now+2] == "BC"):
            now += 2
            bc += 1

        ans += aa*bc
        S = S[now:]
    print(ans)


main()

import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    now = 0
    aa = 0
    while(1):
        now = S.find("ABC")
        if now == -1:
            break

        i = 1
        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            aa = 0
            while(S[now-i] == 'A' and now-i >= 0):
                i += 1
                aa += 1

        aa += 1
        bc = 1
        now += 3
        print(S[now:now+2])
        while(S[now:now+2] == "BC"):
            ans += 1
            now += 2

        ans += aa*bc
        S = S[now:]
    print(ans)


main()


import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    now = 0
    aa = 0
    while(1):
        now = S.find("ABC")
        if now == -1:
            break

        i = 1
        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            aa = 0
            while(S[now-i] == 'A' and now-i >= 0):
                i += 1
                aa += 1

        aa += 1
        bc = 1
        now += 3
        while(S[now:now+2] == "BC"):
            ans += 1
            now += 2

        ans += aa*bc
        S = S[now:]
    print(ans)


main()


import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    now = 0
    aa = 0
    while(1):
        now = S.find("ABC")
        if now == -1:
            break

        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            i = 1
            aa = 0
            while(S[now-i] == 'A' and now-i >= 0):
                i += 1
                aa += 1

        aa += 1
        bc = 1
        now += 3
        while(S[now:now+2] == "BC"):
            now += 2
            bc += 1

        ans += aa*bc
        S = S[now:]
    print(ans)


main()


import sys
import re
sys.setrecursionlimit(10 ** 7)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def SI(): return input()


def main():
    S = SI()
    ans = 0
    now = 0
    aa = 0
    while(1):
        now = S.find("ABC")
        if now == -1:
            break

        i = 1
        if now != 0 and S[0:now] == "A"*now:
            aa += now
        else:
            aa = 0
            while(S[now-i] == 'A' and now-i >= 0):
                i += 1
                aa += 1

        aa += 1
        bc = 1
        now += 3
        while(S[now:now+2] == "BC"):
            ans += 1
            now += 2

        ans += aa*bc
        S = S[now:]
    print(ans)


main()



def main():
  S = input().replace("BC", "1")
  ans = 0
  a = 0
  for s in S:
      if s == "A":
          a += 1
      elif s == "1":
          ans += a
      else:
          a = 0
  print(ans)

main()

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted