Problem p03986 (python)

Fixed submission:
a=[]
for i in input():
    if i=="S":
        a+=i
    
    if a:
        if i=="T" and a[-1]=="T":
            a+=i
        if i=="T" and a[-1]=="S":
            a.pop(-1)
    elif i=="T" and len(a)==0:
        a+=i    
print(len(a))

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[i-j for i,j in zip(a[::2],a[1::2]) if i!=j]
    while a and a[0]<0:
        ans+=abs(a.pop(0))
    while a and a[-1]>0:
        ans+=a.pop(-1)
    b=[]    
    if a:
        b=[a[0]]
        for i in range(1,len(a)):
            if a[i]*a[i-1]>0:
                b[-1]+=a[i]
            else:b+=a[i]
    a=[abs(i) for i in b]
            
print(ans+abs(a[0]) if a else ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[i-j for i,j in zip(a[::2],a[1::2]) if i!=j]
    while a and a[0]<0:
        ans+=abs(a.pop(0))
    while a and a[-1]>0:
        ans+=a.pop(-1)
    b=[]    
    if a:
        b=a[0]
        for i in range(1,len(a)):
            if a[i]*a[i-1]>0:
                b[-1]+=a[i]
            else:b+=a[i]
    a=[abs(i) for i in b]
            
print(ans+abs(a[0]) if a else ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[i-j for i,j in zip(a[::2],a[1::2]) if i!=j]
    if a and a[0]<0:
            ans-=a.pop(0)
    if a and [-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a if i!=0]
print(ans+abs(a[0]) if a else ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[a[i]-a[i+1] for i in range(0,len(a),2)]
    a=[i for i in a if i!=0]
    if a:
        if a[0]<0:
            ans-=a.pop(0)
    if a:
        if a[-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a if i!=0]
print(ans+abs(a[0]) if a else ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[a[i]-a[i+1] for i in range(0,len(a),2)]
    if a:
        if a[0]<0:
            ans-=a.pop(0)
    if a:
        if a[-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a if i!=0]
print(ans+abs(a[0]) if a else ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[a[i]-a[i+1] for i in range(0,len(a),2)]
    if a:
        if a[0]<0:
            ans-=a.pop(0)
    if a:
        if a[-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a]
print(ans+abs(a[0]) if a else ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[a[i]-a[i+1] for i in range(0,len(a),2)]
    if a:
        if a[0]<0:
            ans-=a.pop(0)
    if a:
        if a[-1]>0:
            ans+=a.pop(-1)
    a=[abs(i) for i in a]
print(ans)

Buggy submission (verdict: Runtime Error):
x=input()
a=[1]
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        a[-1]+=1
    else:a+=[1]
ans=0    
if x[0]=="T":
    ans+=a.pop(0)
if x[-1]=="S":
    ans+=a.pop(-1)
    
while len(a)>1:
    a=[a[i]-a[i+1] for i in range(0,len(a),2)]
    if a and a[0]<0:
        ans-=a.pop(0)
    if a and a[-1]>0:
        ans+=a.pop(-1)
    a=[abs(i) for i in a]
print(ans)