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())
queries = []
rev = [0]*q
flag=0
for i in range(q):
    query = list( input().split() )
    queries.append( query )
    if query==["1"]:
        flag+=1
    rev[i]=flag

n_rev = flag
left=""
right=""
for r,query in zip(rev,queries):
    if query!=["1"]:
        q1 = query[1]
        q2 = query[2]
        if (r+n_rev)%2==0:
            if q1=="1":
                left=q2+left
            else:
                right=right+q2
        else:
            if q1=="1":
                right=right+q2
            else:
                left=q2+left 
#     print(i,left,s,right)
if n_rev%2==1:
    s=s[::-1]
print("".join([left,s,right]) )

Submission status: Time Limit Exceeded

Solution code:
S=[c for c in input()]
Q=int(input())
for i in range(Q):
    t=[a for a in input().split()]
    if t[0]=="1":
        S.reverse()
    else:
        if t[1]=="1":
            S.insert(0,t[2])
        else:
            S.append(t[2])
print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        z += 1
        z %= 2
    else:
        if (int(a[1]) + z) % 2 == 0:
            R = "".join([R, a[2]])
        else:
            F = "".join([a[2], F])

ans = F + s + R
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

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

queries = [input().split() for dummy in range(q_number)]
for i in range(q_number):
  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)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
reversecount = 0
for x in query:
    if int(x[0]) == 1:
        reversecount += 1
    else:
        if (int(x[1]) == 1 and (reversecount % 2 == 0)) or (int(x[1]) == 2 and (reversecount % 2 == 1)):
            S = x[2] + S
        else:
            S = S + x[2]
if reversecount % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque
s = list(input())
q = int(input())
a = []
for i in range(q):
    a.append([str(i) for i in input().split()])

for i in range(q):
    if a[i][0] == '1':
        s = ''.join(s)
        s = s[::-1]
    if a[i][0] == '2':
        s = deque(s)
        if a[i][1] == '1':
            s.appendleft(a[i][2])
        if a[i][1] == '2':
            s.append(a[i][2])
print(''.join(s))

Submission status: Time Limit Exceeded

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

qs = list()
for i in range(Q):
    qs.append(input().split())

ns = s
cnt_r = 0
# for i in range(Q):
#     q = input().split()
for q in qs:
    if len(q) == 1:
        cnt_r += 1
    else:
        f, c = int(q[1]), q[2]
        if f == 1:
            if cnt_r % 2 == 1:
                ns = ns + c
            else:
                ns = c + ns
        else:
            if cnt_r % 2 == 1:
                ns = c + ns
            else:
                ns = ns + c

if cnt_r % 2 == 1:
    print(ns[::-1])
else:
    print(ns)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for _ in range(Q):
  item = list(input().split())
  if item[0] == '1':
    S.reverse()

  else:
    if item[1] == '1':
      S_deque = deque(S)
      S_deque.appendleft(item[2])
      S = list(S_deque)

    else:
      S.append(item[2])

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:

s = input()
n = int(input())

for i in range(n):
    Q = input()
    if int(Q[0]) == 2:
        F = int(Q[2])
        C = Q[4]
        #print(F, C)
        if F == 1:
            s = C + s
        else:
            s = s + C
    else:
        s = s[::-1]
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
q_list = [input().split() for i in range(q)]

flag = False
for q_num in q_list:
    if int(q_num[0]) == 1:
        if flag == False:
            flag = True
        else:
            flag = False
    
    else:
        if int(q_num[1]) == 1 and flag == False or int(q_num[1]) == 2 and flag == True:
            s = q_num[2] + s
        else:
            s = s + q_num[2]

if flag:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for i in range(n):
    li = input().split()
    if(li[0] == '1'):
        s = s[::-1]
    else:
        if(li[1] == '1'):
            s = li[2] + s
        else:
            s = s + li[2]
print(s)

Submission status: Time Limit Exceeded

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

l_q = []
for i in range(q):
  l_q.append( list(input().split())  ) 

#クエリ処理
c = 0
for i in l_q:

  if i[0] == "1":
    c = (c+1)%2

  else:
    if i[1] == "1":
      if c==0:
        s = i[2] + s
      else:
        s = s + i[2] 
    else:
      if c==1:
        s = i[2] + s
      else:
        s = s + i[2] 
if c == 1:
  s = ''.join(list(reversed(s)))
print(s)

Submission status: Time Limit Exceeded

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

for i in range(q):      #一行ずつ読み込んでその都度処理していく。
    li = input().split()
    if li[0] == "1":
        s = s[::-1]
    else:
        if li[1] == "1":
            s = li[2] + s

        else:
            s = s + li[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(map(str, input()))
Q = int(input())
for i in range(Q):
  Qi = list(map(str, input().split()))
  T = int(Qi[0])
  if T == 1:
    S.reverse()
  else:
    F = int(Qi[1])
    if F == 1:
      S.insert(0, Qi[-1])
    else:
      S.append(Qi[-1])
 
ans = ''.join(S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
A = input()
B = int(input())
for i in range(B):
    C = sys.stdin.readline().split()
    if C[0] == "1":
        A = A[::-1]
    else:
        if C[1] == "1":
            A=C[2]+A
        else:
            A=A+C[2]
print(A)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
count=0
for i in range(q):
    query=list(map(str,input().split()))
    query[0]=int(query[0])
    if query[0]==1:
        count+=1
    else:
        query[1]=int(query[1])
        if count%2==0:
            if query[1]==1:
                s=query[2]+s
            else:
                s=s+query[2]
        else:
            if query[1]==1:
                s=s+query[2]
            else:
                s=query[2]+s
if count%2==1:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

a = "0"
a_rev_cnt = 0
b = "0"
b_rev_cnt = 0

for _ in range(Q):
    query = input()
    if query[0]=="1":
        t = 1
        rev_cnt  = -1*rev_cnt+1
        a,b = b,a

    else:
        t,f,c = query.split()
        if f=="1":
            if rev_cnt ==0:
                a = c+a
            else:
                a = a+c

        elif f=="2":
            if rev_cnt ==0:
                b = b+c
            else:
                b = c+b
if a[0]=="0":
    a = a[::-1]
a = a[:-1]
if b[-1]=="0":
    b = b[::-1]
b = b[1:]


if rev_cnt%2==0:
    ans = a+S+b
else:
    ans = a+S[::-1]+b
print(ans)

Submission status: Time Limit Exceeded

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


def main():
    s = "".join(input().split())
    q = int(input())
    queries = [list(input().split()) for _ in range(q)]
    flg = True
    for query in queries:
        if query[0] == "1":
            flg = not flg
            continue
        if query[1] == "1":
            if flg:
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if flg:
                s = s + query[2]
            else:
                s = query[2] + s
    if not flg:
        s = s[::-1]
    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
ir = False
for _ in range(Q):
    Query = input().split()
    if len(Query) == 1:
        ir = not ir
    elif (int(Query[1]) == 1 and (not ir)) or (int(Query[1]) == 2 and ir):
        S = Query[2] + S
    else:
        S += Query[2]

if ir:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

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

for count in range(Q):
    T = list(map(str,input().split()))
    if int(T[0]) == 1:
        S = S[::-1]
    else:
        if int(T[1]) == 1:
            S = T[2] + S
        else:
            S = S + T[2]
print(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]
    else:
        if q[1] == '1':
            S = q[2] + S
        else:
            S += q[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
 
cnt = 0
for _ in range(q):
  t = input()
  if t[0] == "1":
    cnt += 1
  else:
    if cnt%2 == 1:
      s = "".join(list(reversed(s)))
      cnt = 0
    _,f,c = t.split(" ")
    if f == "1":
      s = c + s
    else:
      s = s + c
if cnt%2 == 1:
  s = "".join(list(reversed(s)))
  cnt = 0
print(s)

Submission status: Time Limit Exceeded

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

S = str(input())
Q = int(input())
# print(S, Q)

S_copy = S
for i in range(Q):
    query = list(input().split())
    # print(query)
    if int(query[0]) == 1:
        # print("reverse")
        # Sを反転
        S_copy = S_copy[::-1]
    else:
        if int(query[1]) == 1:
            # Sの先頭にC_iを追加
            # print("prepend C_i")
            S_copy = query[2] + S_copy
        else:
            # Sの末尾にC_iを追加
            # print("append C_i")
            S_copy = S_copy + query[2]

print(S_copy)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    a = list(map(str,input().split()))
    if a[0] == "2":
        if a[1] == "1":
            s = str(a[2]) + s
        else:
            s = s + str(a[2])
    else:
        s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(n):
    k=input().split()
   
    if k[0]=="1" :
        ans=ans[::-1]
    elif k[1]=="2":
        ans=ans+k[2]
    
    elif k[1]=="1":
        ans=k[2]+ans
   

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input().split()
    if t[0] == '1':
        v = v + 1
    elif t[0] == '2':
        if (t[1] == '1') and (v % 2 == 0):
            s = t[2]+s
        elif (t[1] == '1') and (v % 2 == 1):
            s = s + t[2]
        elif (t[1] == '2') and (v % 2 == 0):
            s = s + t[2]
        elif (t[1] == '2') and (v % 2 == 1):
            s = t[2]+s

if v % 2 == 1:
    s = s[::-1]            
            
print (s)

Submission status: Time Limit Exceeded

Solution code:
#n=int(input())
#a,b=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

sl=list(input())
q=int(input())
l=[list(map(str,input().split())) for i in range(q)]

#print(l)

count=0
isR=False
from collections import deque
for i in range(q):
    order=l[i]
    if order[0]=="1":
        count+=1
        #反転
    if count%2==0:
        isR=False
    else:
        isR=True
    if order[0]!="1":
        
        f=order[1]
        c=order[2]
        temp_f=f
        if isR:
            if temp_f=="1":
                temp_f="2"
            else:
                temp_f="1"
        if temp_f=="1":
            tempq=deque(sl)
            tempq.appendleft(c)
            sl=list(tempq)
        else:
            sl.append(c)
ln=len(sl)
ansl=[]
if isR:
    for i in range(ln-1,-1,-1):
        ansl.append(sl[i])
else:
    ansl=sl
ans=''.join(ansl)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# Problem D - String Formation

# input process
S = input()
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    # クエリが１の時
    if query_num[0]=="1":
        # 反転処理
        if sento==1:
            sento = 10
        else:
            sento = 1
    else: # query num 2
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S = c + S
            else:
                S = S + c
        else: # 末尾追加処理
            if sento==1:
                S = S + c
            else:
                S = c + S

# output process
if sento==1:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    input = sys.stdin.readline
    a = input().rstrip()
    n = int(input().rstrip())
    st = True
    for _ in range(n):
        q  = [x for x in input().rstrip().split()]
        if int(q[0]) == 1:
            if st:
                st = False
            else:
                st = True
        else:
            if int(q[1]) == 1:
                if st:
                    a = q[2] + a
                else:
                    a = a + q[2]
            else:
                if st:
                    a = a + q[2]
                else:
                    a = q[2] + a
    if not st:
        a = a[::-1]
    print(a)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())

d = deque(s)

for _ in range(q):
  Q = list(input().split())
  
  if Q[0]=='1':
    d = deque(reversed(d))
  elif Q[0]=='2':
    if Q[1]=='1':
      d.appendleft(Q[2])
    elif Q[1]=='2':
      d.append(Q[2])

print(*d, sep='')

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    a = input()
    Q = int(input())
    qs = [input().split() for _ in range(Q)]
    ans = a
    revcount = 0
    for q in qs:
        if len(q) == 1:
            revcount += 1
        else:
            if int(q[1]) == 1:
                if revcount % 2 == 0:
                    ans = q[2] + ans
                else:
                    ans = ans + q[2]
            else:
                if revcount % 2 == 0:
                    ans = ans + q[2]
                else:
                    ans = q[2] + ans
    if revcount % 2 == 1:
        ans = ans[::-1]
    print(ans)
    return
 
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def main():
    S = list(input())
    D = deque(S)
    Q = int(input())
    T = [tuple(map(str, input().split())) for _ in range(Q)]

    for t in T:
        if t[0] == "1":
            D.reverse()
        else:
            if t[1] == "1":
                D.appendleft(t[2])
            else:
                D.append(t[2])

    print("".join(D))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
  w = list(map(str, input().split()))
  if w[0]=='1':
    s = s[::-1]
  else:
    if w[1]=='1':
      s = w[2]+s
    else:
      s = s+w[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np


def main():
    S = np.array(list(input()))
    Q = int(input())
    for _ in range(Q):

        q = input().split()

        if q[0] == '1':
            S = S[::-1]
        else:
            if q[1] == '1':
                S = np.insert(S, 0, q[2])
            else:
                S = np.append(S, q[2])

    print(''.join(S))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
Q =[]
flag = True
while True:
    try:
        Q.append(list(map(str,input().split())))

    except:
        break;

for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if Q[i][1] == "2" and flag == True:
            S += Q[i][2]#先頭
            
        
        elif Q[i][1] == "2" and flag == False:
            S1 = Q[i][2]+ S
            S = S1
            
        elif Q[i][1] == "1" and flag == True:
            S1 = Q[i][2]+S
            S = S1
            
        elif Q[i][1] == "1" and flag == False:
            S += Q[i][2]

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

Submission status: Time Limit Exceeded

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

mode = True

for i in range(Q):
  TFC = list(map(str, input().split()))
  if TFC[0] == "1":
    mode = not mode
    continue
  else:
    if mode:
      if TFC[1] == "1":
        S = ''.join([TFC[2],S])
      else:
        S = ''.join([S,TFC[2]])
    else:
      if TFC[1] == "1":
        S = ''.join([S,TFC[2]])
      else:
        S = ''.join([TFC[2],S])
        
if mode:
  print(S)
  exit()
print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [list(map(str, input().split())) for i in range(Q)]
for i in range(Q):
    if Query[i][0] == "1":
        S = S[::-1]
    elif Query[i][1] == "1":
        S = Query[i][2] + S

    else:
        S = S + Query[i][2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

S = input()
Q = int(input())
# print("S:{}".format(S))
# print("Q:{}".format(Q))
query = []

for i in range(Q):
    query.append(input().split())
# for i in query:
#     print(i)
for i in query:
    if len(i) == 1:
        S = ''.join(list(reversed(S)))
    else:
        if i[1]=="1":
            S = i[2]+S
        else:
            S = S+i[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
f = 0
for i in range(n):
  l = input().replace(" ","")
  if l[0] == "2":
    if (l[1] == "1" and f == 0) or (l[1] == "2" and f == 1):
      s = l[2] + s
    else:
      s = s + l[2]
  else:
    if f == 0:
      f = 1
    else:
      f = 0

print(s if f == 0 else s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

div = True
for i in range(n):
    q = input().split(' ')
    if q[0] == '1':
        div = not div
    elif (q[1] == '1' and div) or (q[1] == '2' and not div):
        s = q[2] + s
    else:
        s = s + q[2]


if div:
    print(s)
else:
    s = ''.join(list(reversed(s)))
    print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query_list = []
i = 0
while(q>i):
    query_list.append(input())
    i += 1
for query in query_list:
    query = query.split()
    if query[0] == '1':
        s = s[::-1]
    else:
        if query[1] == '1':
            # 先頭
            s = query[2] + s
        else:
            # 末尾
            s += query[2]
print(s)

Submission status: Time Limit Exceeded

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

# qs = list()
# for i in range(Q):
#     qs.append(input().split())

ns = s
cnt_r = 0
for i in range(Q):
    q = input().split()
# for q in qs:
    if len(q) == 1:
        cnt_r += 1
    else:
        f, c = int(q[1]), q[2]
        if f == 1:
            if cnt_r % 2 == 1:
                ns = ns + c
            else:
                ns = c + ns
        else:
            if cnt_r % 2 == 1:
                ns = c + ns
            else:
                ns = ns + c

if cnt_r % 2 == 1:
    print(ns[::-1])
else:
    print(ns)

Submission status: Time Limit Exceeded

Solution code:
def main():

    S = []
    S = input()
    Q = int(input())
    query = [None]*Q
    for i in range(Q):
        query[i] = input()
        
    rev = 0
    for q in query:
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    S = "".join([q[4], S])
                    #S = q[4] + S
                else:
                    S = "".join([S, q[4]])
                    #S = S + q[4]
            else:
                if rev ==0:
                   S = "".join([S, q[4]])
                else:
                    S = "".join([q[4], S])
    if rev == 1:
        S = S[::-1]

    print(S)

main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)

def main():
    S = input()
    Q = int(input())

    head = []
    tail = []
    def insert(s,type):
        if type == 1:
            head.insert(0,s)
        else:
            tail.append(s)

    non_reverse = True
    for _ in range(Q):
        inp = input()
        if inp == "1":
            non_reverse = not non_reverse
        else:
            _,F,C = inp.split()
            F = int(F)
            if non_reverse:
                insert(C,F)
            else:
                insert(C,1 if F == 2 else 2)

    S = "".join(head) + S + "".join(tail)
    if non_reverse:
        print(S)
    else:
        print(S[::-1])



if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
q = int(input())
query = [input().split() for i in range(q)]

d = deque(['S'])

#d = ['S']

rev = 0

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev += 1

ans = deque()

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))

while len(d):
    tmp = d.popleft()
    if tmp == 'S':
        ans.append(s)
    else:
        ans.append(tmp)

print(*ans,sep='')

Submission status: Time Limit Exceeded

Solution code:
import heapq
from collections import deque
s = input()
s_list = list(s)
q = int(input())
q_list = []
for i in range(q):
  q_list.append(list(map(str,input().split())))
check = 0
for list_ in q_list:
  if int(list_[0]) == 1:
    check += 1
  if int(list_[0]) == 2:
    if (int(list_[1]) + check) % 2 ==1:
      s_list = deque(s_list)
      s_list.appendleft(list_[2])
      s_list = list(s_list)
    else:
      s_list.append(list_[2])
if check % 2 == 1:
  s_list = s_list[::-1]
print("".join(s_list))

Submission status: Time Limit Exceeded

Solution code:
S=input()
b=1
for i in range(int(input())):
    q=input()
    if q=='1':b^=1
    else:
        a,f,c=q.split()
        S=c+S if (int(f)-1)^b else S+c 
print(S if b else S[::-1])

Submission status: Time Limit Exceeded

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

mode = True
#mode = True : f1->s=c+s,f2->s=s+c
#mode = False : f1->s=s+c,f2->s=c+s

for i in range(n):
    qs = input()
    if len(qs) == 1:
        mode = not mode
    else:
        if mode:
            if int(qs[1:-2]) == 1:
                s = qs[-1] + s
            else:
                s = s + qs[-1]
        else:
            if int(qs[1:-2]) == 1:
                s = s + qs[-1]
            else:
                s = qs[-1] + s
        
#print(mode)
if not mode:
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    QList = list(input().split())
    
    if QList[0] == '1':
        S = S[::-1]
    else:
        if QList[1] == '1':
            S = QList[2] + S
        else:
            S = S + QList[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

a = input()
q = int(input())
rflag = 0
b = []
for i in range(q):
    b.append(input())
# print(b)
# print(len(b[0]))
ans = list(a)

for i in range(len(b)):
    if len(b[i]) == 1:
        rflag = (rflag ^ 1)
    else:
        c = int(b[i][2])
        # print("c="+b[i][2])

        if not rflag:

            if c == 1:
                # print("extend={}".format(b[i][4]))
                extend = b[i][4]
                tmp = deque(ans)
                tmp.appendleft(extend)
                ans = list(tmp)
            else:
                # print('ll')
                ans.append(b[i][4])
        else:
            if c == 1:
                ans.append(b[i][4])
            else:
                # print("extend={}".format(b[i][4]))
                extend = b[i][4]
                tmp = deque(ans)
                tmp.appendleft(extend)
                ans = list(tmp)
        # print(ans,rflag)

if not rflag:
    print("".join(ans))
else:
    print("".join(ans[::-1]))

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.insert(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:

s=input()
q=int(input())
queries = []
rev = [0]*q
flag=0
for i in range(q):
    query = list( input().split() )
    queries.append( query )
    if query==["1"]:
        flag+=1
    rev[i]=flag

n_rev = queries.count(["1"])
left=""
right=""
for i,query in enumerate(queries):
    if query!=["1"]:
        q1 = query[1]
        q2 = query[2]
        if (rev[i]+n_rev)%2==0:
            if q1=="1":
                left=q2+left
            else:
                right=right+q2
        else:
            if q1=="1":
                right=right+q2
            else:
                left=q2+left 
#     print(i,left,s,right)
if n_rev%2==1:
    s=s[::-1]
print("".join([left,s,right]) )

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query_list = [input() for i in range(q)]
for i in query_list:
    if len(i) == 1:
        s = s[::-1]
    else:
        if i[2] == '1':
            s =  ''.join([i[4], (s)])
        else:
            s = ''.join([s, i[4]])

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
flg=False
for i in range(N):
	Q = input().split()
	if Q[0] == "1":
		if not flg:
			flg = True
		else:
			flg = False
	elif Q[0] == "2":
		if Q[1] == "1":
			if not flg:
				S = Q[2]+S
			else:
				S = S+Q[2]
		if Q[1] == "2":
			if flg:
				S = Q[2]+S
			else:
				S = S+Q[2]
if flg:
	S =S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
  query = input()
  if query[0] == '1':
    s.reverse()
  else:
    t, f, c = query.split()
    if f == '1':
      s.insert(0,c)
    else:
      s.append(c)

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)
readline = sys.stdin.readline


def main():
    a = input()
    d = deque(a)
    n = int(input())
    for i in range(n):
        j = list(readline().split())
        if len(j) == 3 and j[1] == "1":
            d.appendleft(j[2])
        elif len(j) == 3 and j[1] == "2":
            d.append(j[2])
        else:
            d.reverse()
    ans = ""
    for j in d:
        ans = ans + j
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

rc = 0
clist = []
for _ in range(Q):
    q = input()
    if q.startswith('1'):
        rc += 1
        for p in clist:
            p[1] += 1
    else:
        q, f, c = q.split()
        clist.append([f+c, 0])
plist = []
alist = []
for fc, cnt in clist:
    f, c = fc[0], fc[1]
    if cnt % 2 == 0:
        if f == '1':
            plist.append(c)
        else:
            alist.append(c)
    else:
        if f == '1':
            alist.append(c)
        else:
            plist.append(c)
plist = plist[::-1]
if rc % 2 != 0:
    S = S[::-1]
print("".join(plist)+S+"".join(alist))

Submission status: Time Limit Exceeded

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

rev = 0

for i in range(Q):
    query = input()
    ti = list(query)[0]
    if ti == '1':
        rev = (rev + 1) % 2
    else:
        ti, fi, ci = query.split()
        if (int(fi) + rev) == 2:
            s += ci
        else:
            s = ci + s

if rev == 1:
    s = list(s)
    s.reverse()
    s = ''.join(s)
print(s)