Problem: Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:    * If T_i = 1: reverse the string S.    * If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.      * If F_i = 1 : Add C_i to the beginning of the string S.     * If F_i = 2 : Add C_i to the end of the string S.  Help Takahashi by finding the final string that results from the procedure.

Submission attempts (in order):
S = input()
Q = int(input())

rev = False

for i in range(Q):
    
    query = input().split()
    
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        
        F = query[1]
        
        if F == '1':
            
            if rev:
                
                S = S + query[2]
            
            else:
                
                S = query[2] + S
        する場合
        else:
            
            if rev:
                
                S = query[2] + S
            
            else:
                
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

S = input()
Q = int(input())

rev = False

for i in range(Q):
    
    query = input().split()
    
    if query[0] == '1':
        if rev == True:
            rev = False
        else:
            rev = True
    else:
        
        F = int(query[1])
        C = query[2]
        
        if F == 1:
            
            if rev == True:
                
                S = S + C
            
            else:
                
                S = C + S
        する場合
        else:
            
            if rev == True:
                
                S = C + S
            
            else:
                
                S = S + C
if rev == True:
    print(S[::-1])
else:
    print(S)

S = input()
Q = int(input())
QUERY = [input().split() for _ in range(Q)]

rev = False

for query in QUERY:
    
    if query[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        
        if query[1] == '1':
            
            if rev:
                
                S = S + query[2]
            
            else:
                
                S = query[2] + S
        する場合
        else:
            
            if rev:
                
                S = query[2] + S
            
            else:
                
                S = S + query[2]
if rev:
    print(S[::-1])
else:
    print(S)

from collections import deque

S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]

S = deque(S)
rev = False

for TFC in Query:
    if TFC[0] == '1':
        rev = not rev
    else:
        if TFC[1] == '1':
            if rev:
                S.append(TFC[2])
            else:
                S.appendleft(TFC[2])
        else:
            if rev:
                S.appendleft(TFC[2])
            else:
                S.append(TFC[2])
if rev:
    S.reverse()
print(''.join(S))

Verdicts: Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Accepted