Buggy python submission (verdict: Time Limit Exceeded):
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()

s=[]
a=[]
x=ord("A")
l=len(S)
for i in range(l-2):
    s.append(ord(S[i])-x)
    if S[i:i+3]=="ABC": a.append(i)
s.append(ord(S[l-2])-x)
s.append(ord(S[l-1])-x)
s.append
a=[a[i] for i in range(len(a)-1,-1,-1)]


tmp=0
abc,bca=[0,1,2],[1,2,0]
ans=0
while True:
    if a==[] or a[-1]>=len(s)-2: break
    tmp=a[-1]
    if s[tmp]==0 and s[tmp+1]==1 and s[tmp+2]==2:
        s[tmp],s[tmp+1],s[tmp+2]=1,2,0
        a.append(tmp+2)
        if tmp>0 and s[tmp-1]==0: a.append(tmp-1)
        ans+=1
    else: a.pop(-1)

print(ans)

Fixed submission:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
a,bc=0,0
i=0
while i<len(s)-1:
    if s[i]=="A": a+=1
    elif s[i]=="B" and s[i+1]=="C":
        bc+=1
        i+=1
        ans+=a
    
    else:
        a,bc=0,0
    i+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()

s=[]
a=[]
x=ord("A")
l=len(S)
for i in range(l-2):
    s.append(ord(S[i])-x)
    if S[i:i+3]=="ABC": a.append(i)
s.append(ord(S[l-2])-x)
s.append(ord(S[l-1])-x)
s.append
a=[a[i] for i in range(len(a)-1,-1,-1)]


tmp=0
abc,bca=[0,1,2],[1,2,0]
ans=0
while True:
    if a==[] or a[-1]>=len(s)-2: break
    tmp=a[-1]
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        a.append(tmp+2)
        if tmp>0: a.append(tmp-1)
        ans+=1
    else: a.pop(-1)

print(ans)

Fixed submission:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
a,bc=0,0
i=0
while i<len(s)-1:
    if s[i]=="A": a+=1
    elif s[i]=="B" and s[i+1]=="C":
        bc+=1
        i+=1
        ans+=a
    
    else:
        a,bc=0,0
    i+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()

s=[]
a=[]
x=ord("A")
for i in range(len(S)):
    s.append(ord(S[i])-x)
    if S[i]=="A": a.append(i)
a=[a[i] for i in range(len(a)-1,-1,-1)]


tmp=0
abc,bca=[0,1,2],[1,2,0]
ans=0
while True:
    if a==[] or a[-1]>=len(s)-2: break
    tmp=a[-1]
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        a.append(tmp+2)
        if tmp>0: a.append(tmp-1)
        ans+=1
    else: a.pop(-1)

print(ans)

Fixed submission:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
a,bc=0,0
i=0
while i<len(s)-1:
    if s[i]=="A": a+=1
    elif s[i]=="B" and s[i+1]=="C":
        bc+=1
        i+=1
        ans+=a
    
    else:
        a,bc=0,0
    i+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()

s=[]
a=[]
for i in range(len(S)):
    s.append(S[i])
    if s[i]=="A": a.append(i)

tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
while True:
    if a==[] or a[0]>=len(s)-2: break
    tmp=a[0]
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        a.insert(0,tmp+2)
        if tmp>0 and s[tmp-1]=="A": a.insert(0,tmp-1)
        ans+=1
    else: a.pop(0)

print(ans)

Fixed submission:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
a,bc=0,0
i=0
while i<len(s)-1:
    if s[i]=="A": a+=1
    elif s[i]=="B" and s[i+1]=="C":
        bc+=1
        i+=1
        ans+=a
    
    else:
        a,bc=0,0
    i+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
while True:
    if tmp==len(s)-2: break
    if s[tmp:tmp+3]==abc:
        s[tmp:tmp+3]=bca
        if tmp>0: tmp-=1
        ans+=1
    
    else: tmp+=1

print(ans)


Fixed submission:
import sys

S=input()
if len(S)<2: print(0),sys.exit()
elif len(S)==3:
    print(1 if S=="ABC" else 0)
    sys.exit()



s=[S[i] for i in range(len(S))]
tmp=0
abc,bca=["A","B","C"],["B","C","A"]
ans=0
a,bc=0,0
i=0
while i<len(s)-1:
    if s[i]=="A": a+=1
    elif s[i]=="B" and s[i+1]=="C":
        bc+=1
        i+=1
        ans+=a
    
    else:
        a,bc=0,0
    i+=1
print(ans)