Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys

s= sys.stdin.readline().strip()
n= len(s)
#
c= 0
#
while True:
    i = s.find("ABC")
    if i<0: break
    a = 1
    for j in range(i-1,-1,-1):
        if s[j]=="A": a+=1
        else: break
    #
    bc= 1
    for j in range(i+3,n,2):
        if s[j:j+2]=="BC": bc+=1
        else: break
    #
    j= i+2*bc
    s= ("A"*a) + s[j+1:]
    n-= j - a
    c+= a*bc
print( c )


Fixed submission:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
n= len(s)
#
c= 0
p1= re.compile("ABC")
p2= re.compile("A+$")
p3= re.compile("^(BC)+")
#
while True:
    m =p1.search( s )
    if not m: break
    i= m.start() + 1
    a=0
    for ch in reversed(s[:i]):
        if ch=="A": a+=1
        else: break
    #
    bc= 1
    j= i+2
    while True:
        k= j+2
        if k>n or s[j:k]!="BC": break
        j += 2
        bc+= 1

    s= ("A"*a) + s[i+2*bc:]
    c+= a*bc

print( c )


Fixed submission:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
#
c= 0
p1= re.compile("ABC")
p2= re.compile("A+$")
p3= re.compile("^(BC)+")
#
while True:
    m =p1.search( s )
    if not m: break
    i= m.start() + 1
    a = len( p2.search(s[:i]).group())
    bc= len( p3.search(s[i:]).group()) // 2
    s= ("A"*a) + s[i+2*bc:]
    c+= a*bc

print( c )


Fixed submission:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
#
c= 0
pat= re.compile("(A+)((BC)+)")
while True:
    m =pat.search( s )
    if not m: break
    n = len( m.group(0) )
    a = len( m.group(1) )
    bc= len( m.group(2) ) // 2
    s= ("A"*a) + s[m.end():]
    c+= a*bc
print( c )


Fixed submission:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python


import sys
import re

s= sys.stdin.readline().strip()
pat= re.compile("ABC")

t= 0
while True:
    n= len( pat.findall(s) )
    if n==0: break
    t+= n
    s= s.replace( "ABC","BCA")
#
print( t )


Fixed submission:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )

Buggy python submission (verdict: Runtime Error):
#!/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" )


Fixed submission:
#!/usr/bin/env python

import sys
import re

s= sys.stdin.readline().strip()
s= s.replace("BC","D")
s= re.sub("[BC]+"," ",s).split()

t= 0
for seg in s:
    a= 0
    for c in seg:
        if c=="A": a+=1
        elif c=="D": t+= a
#
print( t )