Problem:


Score : 300 points

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

Half the characters in X are S, and the other half are T.

Partial Scores

In test cases worth 200 points, |X| ≦ 200.

Input

The input is given from Standard Input in the following format:

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.
X becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4.

Sample Input 2

SSTTST

Sample Output 2

0

X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.



Sample input:
TSTTSS


Sample output:
4

Submission status: Wrong Answer

Solution code:
X=input()
a=X
if len(X)<=100000:
    while 'ST' in a:
        a=a.replace('ST','',int(1e5))
    print(len(a))

Submission status: Wrong Answer

Solution code:
x = input()
#tのラストとsの最初を知りたい
n = len(x)
t1 = 0
s1 = 0
a = 0
su = 0
for i in range(n):
  if x[i]=="T":
    t1 = i
  else:
    if a==0:
      s1 = i
      a = 1
#print(t1,s1)
#後ろにあるSは残る
sn = n//2-(n-t1-1)
tn = n//2-(s1)
su = 2*min(sn,tn)
print(n-su)

Submission status: Wrong Answer

Solution code:
def main():
    X = input()
    print(max(X.index('S'), X[::-1].index('T')) * 2)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
X = list(input())
N = len(X)
s = 0
t = 0
sold = 0
ans = N
for i in range(N):
  if X[i] == 'S':
    if s == 0:
      ans -= 2 * min(sold, t)
      sold = 0
      t = 0
    s += 1
  elif t == 0:
    sold = s
    s = 0
    t += 1
  else:
    t += 1
if s == 0:
  ans -= 2 * min(sold, t)
print(ans)

Submission status: Wrong Answer

Solution code:
X = list(input())
N = len(X)
s = 0
t = 0
sold = 0
ans = N
for i in range(N):
  if X[i] == 'S':
    if s == 0:
      ans -= 2 * min(sold, t)
      sold = 0
      t = 0
    s += 1
  elif t == 0:
    sold = s
    s = 0
    t += 1
  else:
    t += 1
ans -= 2 * min(sold, t)
print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input())
n = len(s)

flag = False
C = 0
X = []
for i in range(n):
    if not flag:
        if s[i] == 'S':
            C = i
            flag =True
            cur = s[i]
            cnt = 1
    else:
        if s[i] != cur:
            X.append(cnt)
            cnt = 1
            cur = s[i]
        else:
            cnt += 1
else:
    X.append(cnt)
#print(C)
#print(X)
r = 0
for i in range(len(X)):
    if i%2 == 0:
        if i+1 <= len(X)-1:
            r += min(X[i], X[i+1])*2
print(n-r)

Submission status: Wrong Answer

Solution code:
x = list(input())
box = []

for i in range(len(x)):
    if x[i] == "S": box.append(x[i])
    else:
        if len(box) == 0 or box[-1] == "S":
            box.append(x[i])
        else: box.pop(-1)
print(len(box))

Submission status: Wrong Answer

Solution code:
X = list(input())
t = X.count("T")
n = len(X)
for x in X:
    if x == "T": t -= 1
    else:
        if t > 0:
            n -= 2
print(n)

Submission status: Wrong Answer

Solution code:
X = input()
N = len(X)
f = 0
h, e = 0, 0
for k in range(N):
    if X[k] == "S":
        h = k
        break
for k in range(N-1,-1,-1):
    if X[k] == "T":
        e = k
        break
s, t = 0, 0
for k in range(h,e+1):
    if X[k] == "S":
        s += 1
    else:
        t += 1
print(N-2*min(s,t))

Submission status: Wrong Answer

Solution code:
X = input()
N = len(X)
cnt_h = 0
for i in range(N):
    if X[i] == "S":
        cnt_h = i
        break
cnt_f = N
for i in range(N - 1, cnt_h, -1):
    if X[i] == "T":
        cnt_f = i + 1
        break
ans = 0
for i in range(cnt_h, cnt_f):
    if X[i] == "S":
        ans += 1
    else:
        ans -= 1
print(abs(ans) + cnt_h + N - cnt_f)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
for i in range(n-1,-1,-1):
    if s[i]=='T':
        p=i+1
        break
else:
    p=0
k=s[:p].count('T')
l=s[:p].count('S')
print(n-p+abs(s[:p].count('T')-s[:p].count('S')))

Submission status: Wrong Answer

Solution code:
X = str(input())

tmorethans = 0
totalts = 0
Nagasa = len(X)

for i in range (0, len(X)+1):
	if 2*totalts == Nagasa:
		print(2*tmorethans)
		exit()
	elif X[i] == 'T':
		tmorethans+=1
		totalts+=1
	else:
		tmorethans-=1

Submission status: Wrong Answer

Solution code:
X = str(input())

count= 0
count2 = 0
Nagasa = len(X)

for i in range (0, len(X)):
	if 2*count2 == Nagasa:
		print(2*count)
		exit()
	if X[i] == 'T':
		count+=1
		count2+=1
	if X[i] == 'S':
		count-=1

print(0)

Submission status: Wrong Answer

Solution code:
X = str(input())

count= 0
count2 = 0
Nagasa = len(X)

for i in range (0, len(X)):
	if count2 == int(Nagasa/2):
		print(2*count)
		exit()
	if X[i] == 'T':
		count+=1
		count2+=1
	if X[i] == 'S':
		count-=1

print(0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


def main():
    x = input().strip()
    tc = 0
    sc = 0
    for c in x[::-1]:
        if c == 'S':
            sc += 1
        else:
            break
    for c in x:
        if c == 'T':
            tc += 1
        else:
            break
    print(max(sc, tc) * 2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
x = input()
lx = len(x)
for i in range(lx):
  if x[i] != 'T':
    break
for j in range(lx):
  if x[lx - j - 1] != 'S':
    break
print(max(i, j) * 2)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
from itertools import product
import sys
input = sys.stdin.readline


def log(*args):
    print(*args, file=sys.stderr)


def main():
    A = input().strip()
    B = input().strip()
    z = zip(A, B)
    before = []
    after = []
    for (a, b) in z:
        if a == b:
            continue
        before.append(a)
        after.append(b)
        if len(before) > 6:
            print("NO")
            return
    if len(before) == 0:
        if len(A) == len(set(A)):
            print("NO")
            return
        else:
            print("YES")
            return

    # beforeが1個だけのときどうなる？j
    for a in product(combinations(range(len(before)), 2), repeat=3):
        tmp = before.copy()
        for v in a:
            tmp[v[0]], tmp[v[1]] = tmp[v[1]], tmp[v[0]]
        if after == tmp:
            print("YES")
            return
    print("NO")
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
if S[0] == 'S' or S[-1] == 'T':
    print(0)
    exit()
s = S.split('T')
t = S.split('S')
print(max(len(t[0]),len(s[-1]))*2)

Submission status: Wrong Answer

Solution code:
S = input()
if S[0] == 'S' or S[-1] == 'T':
    print(0)
    exit()
SL = S.split('ST')
print(max(SL[0].count('T'),SL[-1].count('S'))*2)

Submission status: Wrong Answer

Solution code:
x = input()

t = 0
s = 0
for i in x:
    if i == "T":
        t += 1
    else:
        break
for j in x[::-1]:
    if j == "S":
        s += 1
    else:
        break

b = x[t:-s]
print(t + s + abs(b.count("S") - b.count("T")))

Submission status: Wrong Answer

Solution code:

import sys
sys.setrecursionlimit(10 ** 8)

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    X = input()
    s = False
    c = cc = 0
    for i in range(len(X)):
        if X[i] == 'S':
            if not s: cc = 0
            s = True
            cc += 1
        else:
            s = False
            if cc > 0:
                cc -= 1
                c += 1
    print(len(X)-2*c)

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    X = input()
    i = 0
    while X[i] != 'S': i += 1
    a = X[i:len(X)].count('T')
    X = X[::-1]
    i = 0
    while X[i] != 'T': i += 1
    b = X[i:len(X)].count('S')
    print(len(X)-2*min(a, b))

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
for i in range(1, n+1):
    if s[-i] == 'T':
        index = n-i
        break
print((n-index-1)*2)

Submission status: Wrong Answer

Solution code:
x = input()
s_q = []
t_q = []
for i, v in enumerate(x):
    if v == 'S':
        s_q.append(v)
    else:
        t_q.append(v)
        while len(t_q) > 0 and len(s_q) > 0:
            t_q.pop()
            s_q.pop()
print(len(s_q)+len(t_q))

Submission status: Wrong Answer

Solution code:
X=input()
t=0
s=0
ans=0
for i in range(len(X)):
    if X[i]=="T":
        t+=1
    else:
        s+=1
    if t>s:
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
X=input()
A=len(X)
for i in range(A//5):
  X=X.replace("ST","")
print(len(X))

Submission status: Wrong Answer

Solution code:
x=input()
n=len(x)
s=[0]*n
t=[0]*n
s[0]=(x[0]=='S')*1
t[0]=(x[0]=='T')*1
for i in range(1,n):
  if x[i]=='S':
    s[i]=s[i-1]+1
    t[i]=t[i-1]
  if x[i]=='T':
    s[i]=s[i-1]
    t[i]=t[i-1]+1
  if x[i-1]=='T' and x[i]=='S':
    s[i]=1;t[i]=0
c=0
t0=0
for i in range(n):
  if t[i]==0:
    t0+=1
  else:
    t0=0
  if s[i]>=t[i]:
    c+=1
print(n-c+t0)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

def cal(X):
    S,T = [],[]
    for i in range(len(X)):
        if X[i] == "S":
            S.append(i)
        else:
            T.append(i)

    pt = bisect_right(S,T[len(T)-1])
    return 2*(len(S)-pt)

def main():
    X = input()
    print(cal(X))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    x = IS()
    s_num = t_num = 0
    cnt = 0
    for i, xi in enumerate(x):
        if xi == 'S':
            s_num += 1
        elif xi == 'T':
            t_num += 1
            if s_num == 0:
                t_num = 0
            elif s_num < t_num:
                cnt += s_num*2
                s_num = 0
    if x[-1] == 'T':
        cnt += s_num*2
    print(len(x)-cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    x = IS()+'S'
    s_num = t_num = 0
    cnt = 0
    for i, xi in enumerate(x):
        if xi == 'S':
            s_num += 1
            if t_num > 0:
                cnt += t_num*2
                t_num = 0
                s_num = 1
        elif xi == 'T':
            t_num += 1
            if s_num == 0:
                t_num = 0
            elif s_num < t_num:
                cnt += s_num*2
                s_num = t_num = 0
    print(len(x)-1-cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    x = IS()+'S'
    s_num = t_num = 0
    cnt = 0
    for i, xi in enumerate(x):
        if xi == 'S':
            s_num += 1
            if t_num > 0:
                cnt += t_num*2
                t_num = 0
        elif xi == 'T':
            t_num += 1
            if s_num == 0:
                t_num = 0
            elif s_num < t_num:
                cnt += s_num*2
                s_num = 0
                t_num = 0
    print(len(x)-1-cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

X = input()
N = len(X)
after_x = ""
next_i = 0
# 連続する箇所で部分文字列を作りたい
s_count, t_count = 0, 0
if X[0] == "T":
    # Tから始まる場合、最初の連続するTは確定で残る
    for i, x in enumerate(X):
        if x == "T":
            after_x += x
        else:
            break
    next_i = i

if len(after_x) != N:
    # 残りの文字列について操作する（最初のTは処理したので、必ずSから始まる）
    for i, char in enumerate(X[next_i:]):
        if i == 0:
            # 最初は確定でSから始まる
            s_count += 1
            continue

        if char == "S":
            # Sの場合
            if X[next_i + i - 1] == "T":
                # T->Sの切り替わりのとき
                if t_count >= s_count:
                    # Sを全て使い切り、余ったTは確定で残る
                    after_x += "T" * (t_count - s_count)
                    t_count -= s_count
                    s_count = 0
                else:
                    # Tを全部使い切り、余ったSは引き継がれる
                    s_count -= t_count
                    t_count = 0
            s_count += 1
        else:
            # Tの場合
            t_count += 1
    is_last_process = True
    last_char = char

    if last_char == "T":
        # Tで終わる場合
        if t_count >= s_count:
            # Sを全て使い切り、余ったTは確定で残る
            after_x += "T" * (t_count - s_count)
            t_count -= s_count
            s_count = 0
        else:
            # Tを全部使い切り、余ったSは引き継がれる
            s_count -= t_count
            t_count = 0
            after_x += "S" * s_count
    else:
        # Sで終わる場合
        after_x += "S" * s_count

ans = len(after_x)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

X = input()
N = len(X)
after_x = ""
next_i = 0
# 連続する箇所で部分文字列を作りたい
if X[0] == "T":
    # Tから始まる場合、最初の連続するTは確定で残る
    for i, x in enumerate(X):
        if x == "T":
            after_x += x
        else:
            break
    next_i = i

s_count = 0
t_count = 0
is_last_process = False
if next_i < N - 1:
    # 残りの文字列について操作する（最初のTは処理したので、必ずSから始まる）
    for i, char in enumerate(X[next_i:]):
        if i == 0:
            # 最初は確定でSから始まる
            s_count += 1
            continue

        if char == "S":
            # Sの場合
            if X[next_i + i - 1] == "T":
                # T->Sの切り替わりのとき
                if t_count >= s_count:
                    # Sを全て使い切り、余ったTは確定で残る
                    after_x += "T" * (t_count - s_count)
                    t_count -= s_count
                    s_count = 0
                else:
                    # Tを全部使い切り、余ったSは引き継がれる
                    s_count -= t_count
                    t_count = 0
            s_count += 1
        else:
            # Tの場合
            t_count += 1
    is_last_process = True
    last_char = char

    if last_char == "T":
        # Tで終わる場合
        if t_count >= s_count:
            # Sを全て使い切り、余ったTは確定で残る
            after_x += "T" * (t_count - s_count)
            t_count -= s_count
            s_count = 0
        else:
            # Tを全部使い切り、余ったSは引き継がれる
            s_count -= t_count
            t_count = 0
            after_x += "S" * s_count
    else:
        # Sで終わる場合
        after_x += "S" * s_count

ans = len(after_x)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

X = input()
N = len(X)
after_x = ""
next_i = 0
# 連続する箇所で部分文字列を作りたい
if X[0] == "T":
    # Tから始まる場合、最初の連続するTは確定で残る
    for i, x in enumerate(X):
        if x == "T":
            after_x += x
        else:
            break
    next_i = i

s_count = 0
t_count = 0
if next_i < N - 1:
    # 残りの文字列について操作する（最初のTは処理したので、必ずSから始まる）
    is_t = False
    for i, char in enumerate(X[next_i:]):
        if i == 0:
            # 最初は確定でSから始まる
            s_count += 1
            continue

        if char == "S":
            # Sの場合
            if X[next_i + i - 1] == "T":
                # T->Sの切り替わりのとき
                if t_count >= s_count:
                    # Sを全て使い切り、余ったTは確定で残る
                    after_x += "T" * (t_count - s_count)
                    t_count -= s_count
                    s_count = 0
                else:
                    # Tを全部使い切り、余ったSは引き継がれる
                    s_count -= t_count
                    t_count = 0
            s_count += 1
        else:
            # Tの場合
            t_count += 1

# 最後に
if t_count >= s_count:
    # Sを全て使い切り、余ったTは確定で残る
    after_x += "T" * (t_count - s_count)
    t_count -= s_count
    s_count = 0
else:
    # Tを全部使い切り、余ったSは引き継がれる
    s_count -= t_count
    t_count = 0
    after_x += "S" * s_count

ans = len(after_x) + s_count
print(ans)

Submission status: Wrong Answer

Solution code:
x = input()
t_num = 0
s_num = 0
total = 0
for i in range(len(x)):
  if x[i] == 'S' and t_num == 0:
    s_num += 1
  elif x[i] == 'T':
    t_num += 1
  elif x[i] == 'S' and t_num > 0:
    total += min(s_num, t_num) * 2
    if s_num >= t_num:
      s_num = 1 + s_num - t_num
      t_num = 0
    else:
      s_num = 1
      t_num = 0
total += min(s_num, t_num) * 2
ans = len(x) - total

Submission status: Wrong Answer

Solution code:
def main():
    X = input()
    s, t = 0, 0
    for x in X:
        if x == "S":
            s += 1
        else:
             t += 1
    for x in X:
        if x == "T":
            t -= 1
        else:
            break
    for x in X[::-1]:
        if x == "S":
            s -= 1
        else:
            break
    print(len(X) - min(s, t) * 2)
main()

Submission status: Wrong Answer

Solution code:
def main():
    X = input()
    s, t = 0, 0
    flag = False
    for x in X:
        if x == "S":
            flag = True
        if flag:
            if x == "S":
                s += 1
            else:
                t += 1
    for x in X[::-1]:
        if x == "S":
            s -= 1
        else:
            break
    print(len(X) - min(s, t) * 2)
main()

Submission status: Wrong Answer

Solution code:
A = input()
N = len(A)
Tnum = N//2
Snum = N//2

for i in range(N):
    if A[i] == 'T':
        Tnum -= 1
    else:
        Snum -= 1
    if Tnum == 0:
        break
print(Snum * 2)

Submission status: Wrong Answer

Solution code:
A=list(input())
now=1
S=0
T=0
ans=0
while now != len(A):
   if A[now] == "T":
      S=now-1
      T=now
      for i in range(len(A)):
         if A[S]=="S":
            S-=1
         if S < 0:
            break
      S=now-S-1
      for i in range(len(A)):
         if A[T]=="T":
            T+=1
         if T > len(A)-1:
            break
      T=T-now
      ans+=min(T,S)
      now+=T
   else:
      now+=1
print(len(A)-ans*2)

Submission status: Wrong Answer

Solution code:
# A - STring

N = 100000
X = input()
T_head = S_tail = 0

for i in range(N):
    if X[i]=='S':
        break
    T_head += 1

for i in range(N):
    if X[len(X)-i-1]=='T':
        break
    S_tail += 1

print(2*max(T_head, S_tail))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    X=input()
    len_x=len(X)
    for i,x in enumerate(X):
        if x=="S":
            left_s=i
            break
    for i in range(len_x-1,-1,-1):
        if X[i]=="T":
            right_t=i
            break
    XX=X[left_s:right_t+1]
    xxc=Counter(XX)
    print(left_s+len_x-right_t-1+abs(xxc['S']-xxc['T']))

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/agc005/tasks/agc005_a

x = input()
i = 0
s = 0
t = 0
ans = len(x)
f = True
while i < len(x):
    if f:
        if x[i] == 'S':
            s += 1
            i += 1
        else:
            f = False
    else:
        if x[i] == 'T':
            i += 1
            t += 1
        else:
            ans -= min(s, t) * 2
            s = 0
            t = 0
            f = True
else:
    ans -= min(s, t) * 2
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/agc005/tasks/agc005_a

x = input()
i = 0
s = 0
t = 0
ans = len(x)
x += 'S'
f = True
while i < len(x):
    if f:
        if x[i] == 'S':
            s += 1
            i += 1
        else:
            f = False
    else:
        if x[i] == 'T':
            i += 1
            t += 1
        else:
            ans -= min(s, t) * 2
            s = 0
            t = 0
            f = True
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/agc005/tasks/agc005_a

x = input()
i = 0
s = 0
ans = len(x)
x += 'S'
f = True
while i < len(x):
    if f:
        if x[i] == 'S':
            s += 1
            i += 1
        else:
            f = False
    else:
        if x[i] == 'T':
            i += 1
        else:
            ans -= s * 2
            s = 0
            f = True
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import functools
import math
import scipy
import fractions
import itertools
 
def solve():
    x = input()
    a, b = 0, 0
    for i in x:
        if i == "S":
            a += 1
        else:
            if a > 0:
                a -= 0
            else:
                b += 1
    print(a + b)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
x=input()
i = 0
while(i<len(x)):
    if x[i:i+2]=="ST":
        x=x[:i]+x[i+2:]
        i-=1
    i+=1
print(str(len(x))+"\n")

Submission status: Wrong Answer

Solution code:
x=input()
i = 0
while(i<len(x)):
    if x[i:i+2]=="ST":
        x=x[:i]+x[i+2:]
        i+=-1
    i+=1
print(str(len(x))+"\n")

Submission status: Wrong Answer

Solution code:
import numpy as np
import functools
import math
import scipy
import fractions
import itertools
 
def solve():
    x = input()
    le = len(x)
    limit = len(x)//2
    i = 0
    while True:
        bit = 0
        for i in range(le-1):
            if x[i:i+2] == "ST":
                x = x[0:i]+x[i+2:le]
                bit = 1
                le -= 2
                break
        if bit == 0:
            print(len(x))
            break
        if i >= limit:
            break
 
    return 0
 
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
s=list(input())
n=len(s)
from collections import deque
q=deque([])
h=[1]*n
for i in range(n-1):
    if s[i]=='S' and s[i+1]=='T':
        q.append((i,i+1))
while q:
    l,r=q.popleft()
    h[l]=0
    h[r]=0
    while True:
        l-=1
        r+=1
        if 0<=l and r<n and (s[l]=='S' and s[r]=='T') and (h[l]!=0 and h[r]!=0):
            h[l]=0
            h[r]=0
        else:
            break
print(sum(h))

Submission status: Wrong Answer

Solution code:
s=list(input())
n=len(s)
from collections import deque
q=deque([])
h=[1]*n
for i in range(n-1):
    if s[i]=='S' and s[i+1]=='T':
        q.append((i,i+1))
while q:
    l,r=q.popleft()
    h[l]=0
    h[r]=0
    while True:
        l-=1
        r+=1
        if 0<=l<n-1 and 1<=r<n and (s[l]=='S' and s[r]=='T'):
            h[l]=0
            h[r]=0
        else:
            break
print(sum(h))

Submission status: Wrong Answer

Solution code:
s=list(input())
n=len(s)
h=[1]*n
i=0
gap=1
cnt=0
r=0
while (i+gap<n) and cnt<500000:
    cnt+=1
    #print(i,i+gap)
    if s[i]=='S' and s[i+gap]=='T':
        h[i]=0
        h[i+gap]=0
        if i>0 and i+gap+1<n:
            i-=1
            gap+=2
            r=i+gap
        else:
            i=r
            gap=1
            r=i+gap
    else:
        i=r
        gap=1
        r=i+gap

print(sum(h))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


def main():
    X = input().rstrip()

    ans = len(X)
    s_seq = 0
    t_seq_to_del = 0
    for s in X:
        if s == "S":
            # (S,T) --> S
            s_seq += 1
        else:
            # S --> T
            if s_seq > 0:
                t_seq_to_del = s_seq
                s_seq = 0
            # (S,T) --> T
            if t_seq_to_del > 0:
                t_seq_to_del -= 1
                ans -= 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    x = input()

    cnt_S = 0
    cnt_T = 0
    res = 0
    i = 0
    while i < len(x):
        if x[i] == "S":
            cnt_S += 1
            i += 1

        elif x[i] == "T" and cnt_S != 0:
            while i < len(x) and x[i] == "T":
                cnt_T += 1
                i += 1
            res += min(cnt_S, cnt_T) * 2
            cnt_S = 1
            cnt_T = 0
        else:
            i += 1

    print(len(x) - res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    counter = 0
    X = input().strip()
    stack = list()
    for s in X:
        if len(stack) < 1:
            stack.append(s)
        elif stack[-1] == "S" and s == "T" and counter < (10 ^ 10000) + 1:
            stack.pop()
            counter += 1
        else:
            stack.append(s)
    return len(stack)


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
cs=ct=0
for i in range(n):
    if s[n-i-1]=="T":
        ct+=1
    else:
      break

for i in range(ct,n):
    if s[n-i-1]=="S":
      cs+=1
    else:
      break
      
remain=cs-ct
if remain<0:
    remain=0
ans=n-2*(s.count("S")-remain)
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
cs=ct=0
for i in range(n):
    if s[n-i-1]=="T":
        ct+=1
    else:
      break

for i in range(cs,n):
    if s[n-i-1]=="S":
      cs+=1
    else:
      break
      
remain=cs-ct
if remain<0:
    remain=0
ans=n-2*(s.count("S")-remain)
print(ans)

Submission status: Wrong Answer

Solution code:
X = input()[::-1]
n = len(X) # even

# 最後のTの後にあるSの個数

cnt = 0
for i in range(n):
	if X[i] == 'T':
		break
	cnt += 1

print(2 * cnt)

Submission status: Wrong Answer

Solution code:
s = input()
ans = len(s)
idx = s.index("S")
if idx == -1:
    print(ans)
    exit()
s_ = "S"
s_cnt = t_cnt = 0
while idx < len(s):
    if s_ == s[idx]:
        if s_ == "S":
            s_cnt += 1
        else:
            t_cnt += 1
    elif s_ == "S" and s[idx] == "T":
        s_ = "T"
        t_cnt += 1
    elif s_ == "T" and s[idx] == "S":
        ans -= min(s_cnt, t_cnt) * 2
        s_ = "S"
        s_cnt = 1
        t_cnt = 0
    idx += 1
if s_ == "T": ans -= min(s_cnt, t_cnt) * 2
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
import sys
x = input()
n = len(x)
a = []

for i in range(n-1):
    if x[i] == "T" and x[i+1] == "S":
        a.append(i+1)
    
a = [0] + a + [n]

ans = 0
for i in range(len(a)-1):
    q = a[i+1]-a[i]
    b = Counter(x[:q])
    ans += abs(b["S"] - b["T"])
    x = x[q:]
    
print(ans)

Submission status: Wrong Answer

Solution code:
def agc005_a():
  x = str(input())
  # 文字列の逆順
  x_rev = x[::-1]
  # 取り尽くせないもの、先頭Tの個数、末尾Sの個数、の大きいほうの2倍が答え？
  head_t = x.find('S')
  tail_s = x_rev.find('T')
  ans = 2 * max(head_t, tail_s)
  print(ans)

agc005_a()

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    X = input()

    # TTT .... SS

    cnt_head_T = 0
    for x in X:
        if x == 'T':
            cnt_head_T += 1
        else:
            break

    cnt_tail_S = 0
    for x in X[::-1]:
        if x == 'S':
            cnt_tail_S += 1
        else:
            break

    if cnt_head_T == len(X):
        print(cnt_head_T)
    elif cnt_tail_S == len(X):
        print(cnt_tail_S)
    else:
        rest_T = len(X) // 2 - cnt_head_T
        rest_S = len(X) // 2 - cnt_tail_S
        rest_len = max(rest_T, rest_S) - min(rest_T, rest_S)
        print(cnt_head_T + rest_len + cnt_tail_S)


if __name__ == '__main__':
    main()