Problem:


Score : 400 points

Problem Statement

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.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
s_flip = False
ins = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
            s_flip = not s_flip
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins]
ans += s[::-1] if s_flip else s
ans += chs[ins:]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())

for i in range(n):
  q = list(input().split())
  
  if len(q) == 1: #反転処理
    s = s[::-1]
  else:
    if q[1] == "1":
      s = q[2] + s
    else:
      s = s + q[2]
      
print(s)

Submission status: Time Limit Exceeded

Solution code:
#A,B = map(int,input().split())

a = input()
n = int(input())
mode = 1

i = 0
while i < n:
    hoge = input().split()
    if hoge[0] == '1':
        mode = mode * -1
    else:
        nume = hoge[1]
        piyo = hoge[2]
        if mode == 1:
            if nume == '1':
                a = piyo + a
            else:
                a = a + piyo
        else:
            if nume == '2':
                a = piyo + a
            else:
                a = a + piyo
    i += 1

if mode == -1:
    print(a[::-1])
else:
  	print(a)

Submission status: Time Limit Exceeded

Solution code:
# sys.stdin.readline()
import sys
input = lambda:sys.stdin.readline().rstrip()
s = input()
q = int(input())
def f(s):
    if s == '1':
        return '1' , '0' , '0'
    else:
        lst = s.split()
        return lst[0], lst[1], str(lst[2])
qu = [f(input()) for i in range(q)]

def hanten(c):
    new_c = c[::-1]
    return new_c
def f1(c, d):
    return d + c
def f2(c, d):
    return c + d

for a, b, c in qu:
    if a == '1':
        s= hanten(s)
    if a=='2' and b=='1':
            d = c
            s = f1(s, d)
    if a=='2'and b== '2':
            d = c
            s = f2(s,d)
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
s = str(input().rstrip())
q = int(input())
qmap = [["0"] * 3 for _ in range(q)]
count = 0
for i in range(q):
    a = input().split()
    for j in range(len(a)):
        qmap[i][j] = a[j]

for i in range(q):
    if qmap[i][0] == "1":
        count += 1
    else:
        if count % 2 == 0:
            if qmap[i][1] == "1":
                s = qmap[i][2] + s
            else:
                s = s + qmap[i][2]
        else:
            if qmap[i][1] == "1":
                s = s + qmap[i][2]
            else:
                s = qmap[i][2] + s


if count % 2 == 0:
    print(s)
else:
    print("".join(reversed(s)))

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
s = list(input().strip())
q = int(input())
q_list = []
for i in range(q):
    e = list(input().split(" "))
    q_list += [e]
for i in range(q):
    if q_list[i][0] == "1\n":
        s = s[::-1]
    elif q_list[i][0] == "2":
        if q_list[i][1] == "1":
            s[0:0] = q_list[i][2]
        else:
            s.append(q_list[i][2])
for i in s:
    print(i.strip(), end="")

Submission status: Time Limit Exceeded

Solution code:
def main():
  S=input()
  S_list=list([])
  for i in range(len(S)):
    S_list+=[S[i]]
  c=0
  Q=int(input())
  for i in range(Q):
    s=list(map(str,input().split()))
    if s[0]=='1':
      c+=1
    else:
      if s[1]=='1':
        if c%2==0:
          S_list=[s[2]]+S_list
        else:
          S_list+=[s[2]]
      else:
        if c%2==0:
          S_list+=[s[2]]
        else:
          S_list=[s[2]]+S_list
  if c%2==1:
    S_list.reverse()
  ans="".join(S_list)
  print(ans)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = int(input())
for i in range(N):
  s = list(input().split())
  if s[0] == '1':
    S = S[::-1]
  else:
    if s[1] == '1':
      S.insert(0,s[2])
    else:
      S.append(s[2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  S = input().rstrip()
  Q = int(input())
  query = []
  for _ in range(Q):
    query.append(input().split())
 
  ans = S
  for i in range(Q):
    if len(query[i])==1:
      ans = ans[::-1]
    else:
      if(query[i][1]=="1"):
        t = [query[i][2],ans]
        #ans = ''.join(query[i][2])
        ans = "".join(t)
      else:
        t = [ans,query[i][2]]
        #ans = ''.join(query[i][2])
        ans = "".join(t)
  print(ans)
  
if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = input()
flag = 1
for i in range(int(Q)):
    TFC = input().split()
    if TFC[0] == "1":
        flag *= -1
    else:
        if TFC[1] == "1":
            if flag == -1:
                S = '' . join([S, TFC[2]])
            else:
                S = '' . join([TFC[2], S])
        else:
            if flag == -1:
                S = '' . join([TFC[2], S])
            else:
                S = '' . join([S, TFC[2]])
if flag == -1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
from collections import deque
s = deque(s)
for i in range(n):
    q = list(input().split())
    if q[0] == "1":
        s.reverse()
    if len(q) != 1:
        if q[1] == '1':
            s.appendleft(q[2])
        elif q[1] == "2":
            s.append(q[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  q = input().split()
  if (q[0] == '1'):
    S = S[::-1]
  elif (q[1] == '1'):
    S = q[2] + S
  else:
    S += q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
for i in range(int(input())):
    a, *b = input().split()
    if int(a) == 1:
        s = s[::-1]
    else:
        s = b[1] + s if int(b[0]) == 1 else s + b[1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())

li = [input() for i in range(q)]
k = 0
ans = [s]

for i in li:
    if i == "1":
        k += 1
    else:
        if (int(i[2]) + k) % 2 == 0:
            ans.append(i[4])
        else:
            ans.insert(0, i[4])

s = "".join(ans)

if k % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = input()
N = int(input())
Q =  [list(map(str, stdin.readline().split())) for _ in range(N)]
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
             S = "".join([S,Q[i][2]])#先頭
        else :
            S1 = "".join([Q[i][2],S])
            S = S1

if flag == True:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for count in range(q):
    t=input()
    if t[0]=="1":
        s=s[::-1]
    elif t[2]=="1":
        s=t[4]+s
    else:
        s=s+t[4]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())

for q in range(1, Q + 1):
    inp = input().split(" ")
    if (int(inp[0]) == 1):
        S.reverse()
    else:
        if (int(inp[1]) == 1):
            S.insert(0, inp[2])
        elif (int(inp[1]) == 2):
            S.append(inp[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

def main():
    s = input()[:-1]
    q = int(input()[:-1])
#queries = []
    for _ in range(0, q):
        query = input()[:-1]
        if len(query) == 1:
     #       queries.append([int(query)])
            s = s[::-1]
        else:
            query = query.split(' ')
            query = [int(query[0]), int(query[1]), query[2]]
#        queries.append(query)
            if query[1] == 1:
                s = query[2] + s
            else:
                s = s + query[2]



#for i in range(0, q):
#    if queries[i][0] == 1:
#        s = s[::-1]
#    else:
#        if queries[i][1] == 1:
#            s = queries[i][2] + s
#        else:
#            s = s + queries[i][2]

    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
S = []
A = []
B = []
for i in range(len(s)):
  S.append(s[i])
Q = int(input())
flag = 0
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag += 1
    else:
        if flag % 2 == 0:         
            if q[1] == "1":
                A.insert(0,q[2])
            else:
                B.append(q[2])
        else:
            if q[1] == "1":
                B.append(q[2])
            else:
                A.insert(0,q[2])
if flag%2 == 0:
    A = "".join(A)
    B = "".join(B)
    S = "".join(S)
    print(A+S+B)
else:
    S = S[::-1]
    S = "".join(S)
    A = A[::-1]
    A = "".join(A)
    B = B[::-1]
    B = "".join(B)
    print(B+S+A)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
import sys
flag=False

for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    if flag:
      flag=False
    else:
      flag=True
  else:
    if s[2]=="1":
      if flag:
        S=S+s[4]
      else:
        S=s[4]+S        
    else:
      if flag:
        S=s[4]+S
      else:              
        S=S+s[4]
        
if flag:
  S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
texts = list(str(input()))
Q = int(input())


reverse = False

for _ in range(Q):
    input_text = list(input().split())
    if len(input_text) == 1:
        texts = texts[::-1]
    else:
        if input_text[1] == '1':
            texts.insert(0, input_text[2])
        else:
            texts.append(input_text[2])

print(''.join(texts))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in set(range(Q)):
    qi = input().split()
    if qi[0] == '1':
        S=''.join(list(reversed(S)))
        # S=S[::-1]
    elif qi[1] == '1':
        S = qi[2]+S
    else:
        S = S+qi[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
count = 0
for _ in range(q):
  query = input()
  if query[0] == "1":
    count += 1
  if count%2 == 0:
    if query[0] == "2":
      if query[2] == "1":
        s = query[4] + s
      if query[2] == "2":
        s = s + query[4]
        
  else:
    if query[0] == "2":
      if query[2] == "1":
        s = s + query[4]
      if query[2] == "2":
        s = query[4] + s

if count%2 != 0:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

s = input()

roops = int(input())

#grid = [list(map(str,input().strip().split())) for i in range(roops)]
j = 0
for i in range(roops):
    a = list(map(str,input().strip().split()))
    if len(a)==1:
        j+=1
    else:
        if a[1]=="1":
            if j%2==0:
                s = a[2]+s
            else:
                s = s+a[2]
        else:
            if j%2==0:
                s = s+a[2]
            else:
                s = a[2]+s
if j%2==0:
    print(s)
else:
    print(''.join(list(reversed(s))))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())

li = [input() for i in range(q)]

k= 0

for i in li:
    if i == "1":
        k += 1
    else:
        
        if (int(i[2])+k) % 2 == 0:
            s = s + i[4]
        else:
            s = i[4] + s

if k % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S =input()
Q = int(input())
T = [0]*Q
for i in range(0,Q):
  T[i] = list(map(str, input().split()))

check = 0
for i in reversed(range(0,Q)):
  if(T[i][0] != "1"):
    check = i
    break

  
  
nS = ""
for i in range(0,check+1):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "2" and T[i][1] == "1"):
      S = T[i][2]+S
  elif(T[i][0] == "2" and T[i][1] =="2"):
      S = S+T[i][2]
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass

for i in range(check, Q):
  if(i != Q-1 and T[i][0] == "1" and T[i+1][0] == "1"):
    T[i][0] = 0
    T[i+1][0] = 0
  elif(T[i][0] == "1"):
    S = S[::-1]
  elif(T[i][0] == 0):
    pass


print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
 
for i in range(Q):
    q = list(input().split())
 
    if (len(q) == 1):
        S = S[::-1]
    else:
        judge = q[1]
        cha = q[2]
        
        if (int(judge) == 1):
            S = cha + S
        else:
            S = S + cha
 
print(S)

Submission status: Time Limit Exceeded

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

def main():
    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    for i in range(len(tmp)):
        if len(tmp[i]) == 1:
            s = s[::-1]
        else:
            if int(tmp[i][1]) == 1:
                s = '{}{}'.format(tmp[i][2], s)
            else:
                s = '{}{}'.format(s, tmp[i][2])
                
    print(s)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# ABC158 D
from collections import deque
s=input()
q=int(input())
l=[]

for i in range(q):
    l.append(list(map(str,input().split())))
# print(l)
t_str=deque(s)
gyaku_fig=False
for ll in l:
    if ll[0]=='1':
        t_str=list(t_str)
        t_str=t_str[::-1]
        t_str=deque(t_str)
    elif ll[0]=='2':
            if ll[1]=='1':
                t_str.appendleft(ll[2])
                # t_str=list(ll[2])+t_str
            elif ll[1]=='2':
                t_str.append(ll[2])
                # t_str+=list(ll[2])

t_str=list(t_str)
ans=''.join(t_str)
print(ans)

# t_str=s
# gyaku_fig=False
# for ll in l:
#     if ll[0]=='1':
#         if gyaku_fig==False:
#             gyaku_fig=True
#         elif gyaku_fig==True:
#             gyaku_fig=False
#     elif ll[0]=='2':
#         if gyaku_fig==True:
#             if ll[1]=='1':
#                 t_str=ll[2]+t_str
#             elif ll[1]=='2':
#                 t_str+=ll[2]
#         elif gyaku_fig==False:
#             if ll[1]=='2':
#                 t_str=ll[2]+t_str
#             elif ll[1]=='1':
#                 t_str+=ll[2]        
# if gyaku_fig==True:
#     print(t_str)
# else:
#     print(t_str[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())

for i in range(q):
    t = input()
    if t == "1":
        s = s[::-1]
    elif t[0] == "2":
        f = t[2]
        if f == "1":
            c = t[4]            
            s = c + s
        elif f == "2":
            c = t[4]
            s += c

print(s)

Submission status: Time Limit Exceeded

Solution code:
S1 = input()
Q = int(input())
L = []
S = S1
for i in range(Q):
    L.append(input())
for i in range(Q):
    if L[i] == '1':
        S = S[::-1]  
    else:
        if L[i][2] == '1':
            S = L[i][-1] + S
        elif L[i][2] == '2':
            S += L[i][-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())

rev = False
for i in range(Q):
  q = input()
  if q[0] == '1':
    rev = not rev
  else:
    if q[2] == '1' and rev or q[2] == '2' and not rev:
      S.append(q[4])
    else:
      S.insert(0, q[4])
      
print(''.join(S[::-1]) if rev else ''.join(S))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S=input()
Q=int(input())
q=[[0 for i in range(3)] for j in range(Q)]
onecntr=0
for i in range(Q):
    tmp=input().split()
    if(tmp[0]=="1"):
        q[i][0]=1
    else:
        q[i][0]=2
        q[i][1]=int(tmp[1])
        q[i][2]=tmp[2]
    

for i in range(Q):
    if(q[i][0]==1):
        onecntr+=1
    else:
        
        if(onecntr%2==0):
            if(q[i][1]==1):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
        else:
            S=S[::-1]
            if(q[i][1]==1):
                S=q[i][2]+S
            else:
                S=S+q[i][2]
        onecntr=0
    
if(onecntr%2==1):
    S=S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
##D
S = str(input())
Q = int(input())
Query = [str(input()) for i in range(Q)]
##print(Query)
ans = S
for j in range(Q):
    if Query[j][0] == "1":
        ans = ans[::-1]
    else:
        if Query[j][2] == "1":
            ans = str(Query[j][4])+ans
        else:
            ans = ans+ str(Query[j][4])
print(ans)

Submission status: Time Limit Exceeded

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

S = input().strip()
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)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 7) #再帰用
from collections import deque
s = deque(input())

n = int(input())
p = []
for i in range(n):
  i =(input().split())
  if i[0] == '1':
    s.reverse()
  elif i[0] =='2':
    if i[1] == '1':
      s.appendleft(i[2])
    else:
      s.append(i[2])
#  count +=1      
      
#print(s)      
      
s = ''.join(s)
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())

listQ = []
tem0, tem1 = 0, 0
for _ in range(Q):
  tem = input()
  if (tem0 == 1) and (tem1 == 1): continue
  listQ += [tem]
  tem1 = tem0
  tem0 = tem

temS = S
for i in range(len(listQ)):
  inp = listQ[i]
  if (int(inp[0]) == 1):
    temS = temS[::-1]
  else:
    _, F, C = inp.split()
    if int(F) == 1: temS = C + temS
    else: temS += C
print(temS)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    a = list(input().split())
    if len(a) == 1:
        s = s[::-1]
    else:
        if a[1] == '2':
            s = s + a[2]
        else:
            s = a[2] + s
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
q_list=[input().split() for _ in range(q)]
count=0
for temp in q_list:
  if int(temp[0])==1:
    count+=1
  else:
    if int(temp[1])==1:
      if count%2==0:
        s=temp[2]+s
      else:
        s=s+temp[2]
    else:
      if count%2==0:
        s=s+temp[2]
      else:
        s=temp[2]+s
if count%2==1:
  print(s[::-1])
else:
  print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())
ls = [input().split() for _ in range(q)]

ans = deque(s)

for t in ls:
    if t[0] == '1':
        ans.reverse()
    elif t[0] == '2':
        if t[1] == '1':
            ans.appendleft(t[2])
        elif t[1] == '2':
            ans.append(t[2])

print("".join(ans))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce

S = input()
count = int(input())

idx = 1
while idx <= count:
  idx += 1
  process = input().split()
  if process[0] == '1':
    ls = list(S)
    ls.reverse()
    S = reduce(lambda a,b: a + b, ls)
  elif process[0] == '2':
    if process[1] == '1':
      S = process[2] + S
    elif process[1] == '2':
      S = S + process[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
cnt=0
for i in range(n):
    e=list(map(str,input().split()))
    q=int(e[0])
    if len(e)!=1:
        f=int(e[1])
        c=e[2]
    if q==2:
        if (f==1 and cnt%2==0) or (f==2 and cnt%2!=0):
            s=c+s
        else:
            s+=c
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
counter=0
for i in range(Q):
    query=input().split()

    if "1"==query[0]:

        counter+=1
    else:
        if query[1]=="1":
            if counter>0:
                if counter%2==1:
                    S=S[::-1]
                counter=0
            S=query[2]+S


        else:
            if counter>0:
                if counter%2==1:
                    S=S[::-1]
                counter=0
            S+=query[2]


if counter>0:
    if counter%2==1:
        S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = list(input())
S = deque(S)
Q = int(input())
query = [input().split() for i in range(Q)]
for i in query:
    if i[0] == '1':
        S.reverse()
    elif i[0]=='2' and i[1]=='1':
        S.appendleft(i[2])
    # elif i[0]=='2' and i[1]=='2':
    else:
        S.append(i[2])
print(''.join(list(S)))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for _ in range(q):
    l = input()
    if l == "1":
        s = s[::-1]
    else:
        t, f, c = l.split()
        if f == "1":
            s = c + s
        else:
            s += c
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s=input()
    n=int(input())
    q = [input().split() for i in range(n)]
    for i in range(n):
        if len(q[i])==3:
            if q[i][1]=='1':
                s='%s%s' % (q[i][2],s)
            else:
                s='%s%s' % (s,q[i][2])
        else:
            s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
 
 
for i in range(q):
    List=list(input().split())
    if len(List) > 1 :
      if List[1] == '1' :
        s.insert(0,List[2])
      else:
        s.append(List[2])
    else:
      s.reverse()
 
ans = ''
for i in range(len(s)):
  ans += s[i]
 
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(0, Q):
    if Query[i][0] == '1':
        S = S[::-1]
    elif Query[i][0] == '2':
        if Query[i][1] == '1':
            S = Query[i][2] + S
        elif Query[i][1] == '2':
            S = S + Query[i][2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
S =input()
Q=int(input())
orders=[]
for line in sys.stdin.readlines():
    orders.append(line.rstrip().split(" "))
for order in orders:
    if order[0]=="1":
        S=S[::-1]
    elif order[0]=="2" and order[1]=="1":
        S=order[2]+S
    elif order[0]=="2" and order[1]=="2":
        S=S+order[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())


def formation():
    global S, Q

    ls = list(S)
    do_rev = False
    for _ in range(Q):
        t_str = input()

        if t_str[0] == '1':
            do_rev = not do_rev
        else:
            splited = t_str.split(' ')
            f = splited[1]
            c = splited[2]
            if f == '1':
                if do_rev:
                    ls.append(c)
                else:
                    ls.insert(0, c)
            else:
                if do_rev:
                    ls.insert(0, c)
                else:
                    ls.append(c)

    if do_rev:
        ls.reverse()

    return ''.join(ls)


print(formation())

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline()
S = input().strip()
Q = int(input().strip())

mode = 1

Query = [list(map(str,list(input().split()))) for i in range(Q)]

for q in Query:
  if q[0] == "1":
    mode *= -1
    continue
  if mode == 1:
    if q[1] == "1":
      S = ''.join([q[2].strip(),S])
    else:
      S = ''.join([S,q[2].strip()])
  else:
    if q[1] == "1":
      S = ''.join([S,q[2].strip()])
    else:
      S = ''.join([q[2].strip(),S])

if mode == 1:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

s = input().rstrip("\n")
q = int(input())

for i in range(q):
    # query = list(map(str, input().split()))
    query = list(input().split())
    if query[0] == "1":
        s = s[::-1]
        continue
    else:
        if query[1] == "1":
            slist = [query[2],s]
            s = ''.join(slist)
            # s = query[2] + s
        else:
            slist = [s,query[2]]
            s = ''.join(slist)
            # s = s + query[2]

    #print("{}回目:{}".format(i+1,s))

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
s = 0
for i in range(Q):
    q = list(input().split())
    s += int(q[0])
    if q[0]=='2':
        s_i = s + int(q[1]) 
        if s_i%2 == 0:
            S += q[2]
        else:
            S = q[2] + S
if s%2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
cnt = 0
ccnt = 0
s = "@"
for i in range(Q):
    data = [(x) for x in stdin.readline().rstrip().split()]
    if data[0] == "2":
        ccnt = data[2]
        if data[1] == "1":
            if cnt % 2 == 0:
                s = "".join([ccnt,s])
            else:
                s = "".join([s,ccnt])  
        else:
            if cnt % 2 == 0:
                s = "".join([s,ccnt])
            else:
                s = "".join([ccnt,s])
    else:
        cnt += 1
if cnt % 2 != 0:
    s = s[::-1]
if cnt % 2 != 0:
    S = S[::-1]
ans = s.replace("@",S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
num = 0
ans = "0"

for _ in range(q):
  query = list(input().split())
  if len(query) == 1:
    num += 1
  else:
    if int(query[1]) == 1:
      if num%2 == 0:        
        # 先頭
        ans = query[2] + ans
      else:
        ans = ans + query[2]
    else:
      if num%2 == 0:
        # 後方
        ans = ans + query[2]
      else:
        ans = query[2] + ans
        
mes = ans.replace("0", s)
if num%2 == 0:
  print(mes)
else:
  mes = ''.join(list(reversed(mes)))
  print(mes)

Submission status: Time Limit Exceeded

Solution code:
import sys

S = input()
Q = int(input())
Flag = True
 
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

if not Flag:
    S = S[::-1]    
print(S)

Submission status: Time Limit Exceeded

Solution code:
P=[];T=[];r=0
S=input()
Q=int(input())

for i in range(Q):
  P=list(map(str,input().split()))
  if P[0]=="1":
    r=r+1
  else:
    if P[1]=="1":
      T="".join(S)
      if r%2==0:
        T=P[2]+T
      else:
        T=T+P[2]
      S=list(T)
    else:
      T="".join(S)
      if r%2==0:
        T=T+P[2]
      else:
        T=P[2]+T
      S=list(T)

if r%2==0:
  print("".join(S))
else:
  T="".join(S)
  S=list(reversed(T))
  print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())

q_list = []
rev_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  if v == '1':
    rev_c += 1
  else:
    rev_list.append(rev_c)
    q_list.append(v)
    
if rev_c % 2 == 1:
  s = s[::-1]
  
for i,v in enumerate(q_list):
  count = rev_list[i]
  num = int(v[2])
  char = v[4]
  if (rev_c-count+num)%2==1:
    s = ''.join([char,s])
  else:
    s = ''.join([s,char])
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
num = int(input())
for lines in sys.stdin.readlines():
  a = list(lines.split())
  start = int(a[0])
  if start == 2:
    ns = int(a[1])
    if ns == 1:
      s = a[2] + s
    else:
      s = s + a[2]
  else:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    q = [list(input().split()) for i in range(Q)]
        
    rev = 0
    for i in range(Q):
        if q[i][0] == "1":
            rev += 1
        else:
            if (q[i][1] == "1" and rev%2 == 0) or (q[i][1] == "2" and rev%2 == 1):
                S = q[i][2] + S
            else:
                S = S + q[i][2]

    if rev%2 == 1:
        S = S[::-1]
    print(S)

if __name__ == '__main__':
    main()