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())
is_reverse=False
for i in range(q):
    qi=list(input().split())
    if(qi[0]=="1"):
        is_reverse = not is_reverse
    elif(qi[1]=="1"):
        if(is_reverse):
            s=s+qi[2][::-1]
        else:
            s=qi[2]+s
    else:
        if(is_reverse):
            s=qi[2][::-1]+s
        else:
            s=s+qi[2]

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

Submission status: Time Limit Exceeded

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

f=1
for _ in range(Q):
    A=[]
    A.clear()
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                S=A[2]+S
            if f==-1:
                S=S+A[2]
        if A[1]=="2":
            if f==-1:
                S=A[2]+S
            if f==1:
                S=S+A[2]

if f==-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):
    query = list(input().split())
    if len(query) == 1:
        s = s[::-1]
    elif query[1] == '1':
        s = query[2] + s
    elif query[1] == '2':
        s = s + query[2]
        
print(s)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys
import copy

sys.setrecursionlimit(10**9)
mod = 10**7+9
inf = 10**20

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

reverse = False
for i in range(q):
    l = input().split()
    if l[0]=="1":
        reverse = not(reverse)
    else:
        if l[1]=="1":
            if reverse:
                s += l[2][::-1]
            else:
                s = l[2] + s
        else:
            if reverse:
                s = l[2][::-1] + s
            else:
                s += l[2]

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

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
for i in range(Q):
    input_list = input().split()
    if input_list[0] == "1":
        count += 1
        continue

    if count % 2 == 0:  # even
        if input_list[1] == "1":
            S = input_list[2] + S
        else:
            S = S + input_list[2]
    else:
        if input_list[1] == "2":
            S = input_list[2] + S
        else:
            S = S + input_list[2]

if count % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
B = ""
C = ""
t = 1
for i in range(Q):
    A = input()
    if A=="1":
        t*=-1
    else:
        AL = list(A.split())
        if AL[1]=="1":
            if t==1:
                B +=AL[2]
            else:
                C +=AL[2]
        elif AL[1]=="2":
            if t==1:
                C +=AL[2]
            else:
                B +=AL[2]

if t==-1:
    print(C[::-1]+S[::-1]+B)
else:
    print(B[::-1]+S+C)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
flip = -1
strs = '/'

for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strs = query[2] + strs
            else:
                strs = strs + query[2]
        else:
            if flip == -1:
                strs = strs + query[2]
            else:
                strs = query[2] + strs

strs_f = ''
for i in range(len(strs)):
    if strs[i] == '/':
        break
    strs_f += strs[i]

strs_b = ''
for i in range(len(strs)-1, -1, -1):
    if strs[i] == '/':
        break
    strs_b += strs[i]

if flip == -1:
    ans = strs_f + s + strs_b[::-1]
else:
    ans = strs_b + s[::-1] + strs_f[::-1]

print(ans)

Submission status: Time Limit Exceeded

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

R=0

for i in range(Q):
    temp=list(input().split())
    if int(temp[0])==1:
        R+=1
    elif int(temp[0])==2:
        F=int(temp[1])
        c=temp[2]
        if F==1:
            if R%2==0:
                S=c+S
            else:
                S=S+c
        elif F==2:
            if R%2==0:
                S=S+c
            else:
                S=c+S

if R%2==1:
    ans=""
    for i in range(len(S)):
        ans+=S[-(1+i)]
    print(ans)
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
direction=0 #0:順 1:逆

for i in range(q):
    query=input()
    #print(query)
    if query[0]=='1':
        direction=direction^1
    elif query[0]=='2':
        c=query[4]
        if query[2]=='1' and direction==0:
            s=c+s
        elif query[2]=='1' and direction==1:
            s=s+c
        elif query[2]=='2' and direction==0:
            s=s+c 
        elif query[2]=='2' and direction==1:
            s=c+s

if direction==0:
    print(s)
else:
    ns=''
    for i in range(len(s)):
        ns+=s[-1-i]
    print(ns)

Submission status: Time Limit Exceeded

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

Ques = []
for i in range(nQ):
  Ques.append(input())
  
for iQ in Ques:
  iQ = iQ.split()
  if int(iQ[0]) == 1:
    S = S[::-1]
  else:
    if int(iQ[1]) == 1:
      S = iQ[2] + S
    elif int(iQ[1]) == 2:
      S = S + iQ[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    for _ in range(q):
        qu = [x for x in input().split()]
        if len(qu) == 1:
            s = s[::-1]
        else:
            if qu[1] == '1':
                s = qu[2] + s
            else:
                s = s + qu[2]

    print(s)
main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q) :
    t = input()
    if len(t) == 5 :
        f = t[2]
        c = t[4]
        if f == "1" :
            s = c + s
        else :
            s = s + c
    else :
        s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
q=int(input())
for i in range(q):
    j=input()
    if int(j[0])==1:
        s=s[::-1]
    else:
        if int(j[2])==1:
            s=str(j[4])+s
        else:
            s+=str(j[4])
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = 0
for i in range(Q):
    s = input().split()
    if int(s[0]) == 1:
        reverse = (reverse + 1) % 2
    else:
        if int(s[1]) == 1:
            if reverse == 1:
                S = S + s[2]
            else:
                S = s[2] + S
        else:
            if reverse == 1:
                S = s[2] + S
            else:
                S = S + s[2]

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
L = []
count = 0
for i in range(Q):
    l = list(input().split())
    if len(l) == 1:
        count +=1
    else:
        l[1] = int(l[1])
        if count%2 == 1:
            if l[1] == 1:
                l[1] = 2
            else:
                l[1] = 1
        if l[1] == 1:
            S.insert(0,l[2])
        else:
            S.append(l[2])

if count%2 == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s = input()
now = 1
q = int(input())
Query = [ list(map(str,input().split())) for _ in range(q)]
#print(*Query)
adhere = ""
back = ""
for i in range(q):
    if len(Query[i]) == 3:
        #t = int(Query[i][0])
        f = int(Query[i][1])
        c = Query[i][2]
        if f == 1:
            adhere = c + adhere
        else:
            back += c
    else:
        now *= -1
        tmp_adhere = back
        back = adhere[::-1]
        adhere = tmp_adhere[::-1]
        
if now == -1:
    ans = adhere + s[::-1] + back
    #print(-1)
else:
    ans = adhere + s + back
    #print(1)
print(ans)

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())

    def insert(s,type):
        if type == 1:
            return s + S
        else:
            return S + 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:
                S = insert(C,F)
            else:
                S = insert(C,1 if F == 2 else 2)

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



if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
import math
import sys 
s = list(input())
q = int(input())
l = []
for i in range(q):
    a = list(input().split())
    if a[0] == '1':
        s = s[::-1]
    else:
        if a[1] == '1':
            ｓ.insert(0,a[2])
        else:
            s.append(a[2])
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
Q = int(input())
for i in range(Q):
    query = list(map(str,input().split()))
    if query[0] == '1':
        S.reverse()
    if query[0] == '2':
        if query[1] == '1':
            S.insert(0, query[2])
        else:
            S.append(query[2])
print(*S, sep = '')

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import copy
import queue
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
INF = 10**15
DD = ((1,0),(0,1),(-1,0),(0,-1))

S = input().rstrip()
n = int(input())

a = ['','']
d = 0
for _ in range(n):
    q = list(input().split())
    if q[0] == '1':
        d = 1-d
    else:
        if q[1] == '1':
            a[d] += q[2]
        else:
            a[1-d] += q[2]

if d == 1:
    S = ''.join(reversed(list(S + a[1]))) + a[0]
else:
    S = ''.join(reversed(list(a[0]))) + S + a[1]
print (S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
lr = ['', '']
for i in range(Q):
    TFC = input()
    if TFC[0] == '1':
        count += 1
    else:
        T, F, C = TFC.split()
        if F == '1':
            lr[count % 2] += C
        else:
            lr[(count+1) % 2] += C
if count % 2 == 0:
    print(lr[0][::-1] + S + lr[1])
else:
    print(lr[1][::-1] + S[::-1] + lr[0])

Submission status: Time Limit Exceeded

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

for i in range(q):

    data = input()

    if int(data[0]) == 1:
        s = s[::-1]

    else:
        c = data[4]
        if int(data[2]) == 1:

            s = c + s

        else:
            s += c

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
lq = [input().split() for _ in range(Q)]

flg = 0 # 0:sonomama 1:gyakuten
for q in lq:
    if q[0] == "1":
        if flg == 0:
            flg = 1
        else:
            flg = 0
    elif q[0]=="2":
        tmp = q[2]
        if (q[1] == "1" and flg ==0) or (q[1] == "2" and flg ==1):
            tmp += s
            s = tmp
        elif (q[1] == "2" and flg == 0) or (q[1] == "1" and flg == 1):
            s += tmp

if flg==1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = raw_input()
    Q = int(raw_input())

    is_reversed = False

    for _ in range(Q):
        query = raw_input().split()
        if query[0] == "1":
            is_reversed = not is_reversed
        elif query[0] == "2":
            F = int(query[1])
            C = query[2]
            if F == 1:
                if not is_reversed:
                    S = "".join([C, S])
                else:
                    S = "".join([S, C])
            elif F == 2:
                if not is_reversed:
                    S = "".join([S, C])
                else:
                    S = "".join([C, S])


    if not is_reversed:
        ans =  S
    else:
        ans = S[::-1]
    print ans

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
left = ''
right = ''
for i in range(Q):
    arr = list(input().split())
    if len(arr) == 1:
        left,right=right[::-1],left[::-1]
        count += 1
    else:
        if arr[1] == '1':
            left = arr[2] + left
        else:
            right = right + arr[2]
if count%2 == 0:
    print(left+S+right)
else:
    print(left+S[::-1]+right)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s = s[::-1]
  else:
    if int(temp[1]) == 1:
      s = temp[2] + s
    else:
      s = s + temp[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==1:
        S=c+S
      elif f==2:
        S=S+c

  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
tmp = s
flag = 0
for _ in range(q):
    _s = input()
    t = 0
    f = 0
    c = ""
    if _s == "1":
        flag += 1
    else:
        t, f, c = _s.split(" ")
        r = flag % 2
        if f == "1" and r == 0:
            tmp = c + tmp
        elif f == "1" and r == 1:
            tmp = tmp + c
        elif f == "2" and r == 0:
            tmp = tmp + c
        elif f == "2" and r == 1:
            tmp = c + tmp
if flag % 2 == 0:
    print(tmp)
else:
    print(tmp[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = list(str(input()))
Q = int(input())
for i in range(Q):
  a = list(map(str, input().split()))
  if len(a) == 1:
    S = list(reversed(S))
  else:
    if a[1] == "1":
      S.insert(0, a[2])
    else:
      S.append(a[2])
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

S = input()
Q = int(input())
queries = [input().rstrip('\n') for _ in range(Q)]
reverse = False

for query in queries:
    q = query.split(' ')
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            S = q[2] + S if not reverse else S + q[2]
        else:
            S = S + q[2] if not reverse else q[2] + S
print(S) if not reverse else print(S[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    s = s[::-1]
  else:
    if Q[1] == '1':
      s.insert(0, Q[2])
    else:
      s.append(Q[2])

for i in s:
  print(i, end ="")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for _ in range(num):
    row = input().split()
    if len(row) == 1:
        s = s[::-1]
    else:
        if row[1] == "1":
            s = row[2] + s
        else:
            s = s + row[2]

print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    query = input().split()
    if len(query) == 1 and query[0] == "1":
        S = S[::-1]
    else:
        f = int(query[1])
        c = query[2]
        if f == 1:
            S = c + S
        else:
            S = S + c
print(S)

Submission status: Time Limit Exceeded

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

def rev(s):
    re = ""
    for k in range(len(s)):
        re = re + s[len(s) - 1 - k]
    return re


for i in range(Q):
    k = input()
    if k == "1":
        S = rev(S)
    else:
        if k[2] == "1":
            S = k[4] + S
        else:
            S = S + k[4]

print(S)

Submission status: Time Limit Exceeded

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

flg=1
for i in range(q):
    if Q[i][0]=="1":flg*=-1
    if Q[i][0]=="2":
        if Q[i][1]=="1" and flg==1: s=Q[i][2]+s
        if Q[i][1]=="2" and flg==-1:s=Q[i][2]+s
        if Q[i][1]=="1" and flg==-1:s=s+Q[i][2]
        if Q[i][1]=="2" and flg==1:s=s+Q[i][2]
if flg==-1:s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
reverse = 0
for i in range(Q):
  Query = list(input().split())
  if Query[0] == "1":
    reverse = (reverse + 1) % 2
  if Query[0] == "2":
    f = int(Query[1])
    c = Query[2]
    if (f + reverse) % 2 == 1:
      S = c + S
    else:
      S = S + c
 
if reverse == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

for i in range(Q):
    t = input().split()
    if t[0] == '1':
        S = S[::-1]
    else:
        if 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())
c = 0
for i in range(Q):
    t = input()
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c%2 == 0:
            s = t[4] + s
        else:
            s = s + t[4]
    else:
        if c%2 == 0:
            s = s + t[4]
        else:
            s = t[4] + s

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

Submission status: Time Limit Exceeded

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

for i in arr:
    if i[0] == '1':
        s = s[::-1]
    else:
        if i[1] == '1':
            s = i[2] + s
        else:
            s = s + i[2]
print(s)

Submission status: Time Limit Exceeded

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

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

rev_count = 0
for _ in range(q):
  l = input().split()
  if len(l) == 1:
    rev_count += 1
  else:
    if rev_count & 1:
      s.reverse()
      rev_count = 0

    if int(l[1]) == 1:
      s = [l[2]] + s
    else:
      s = s + [l[2]]

if rev_count & 1:
  s.reverse()

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  query = input()
  if query == "1": S = S[::-1]
  elif query[2] == "1": S = query[4] + S
  else: S = S + query[4]
print(S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

deq = deque(S)

for _ in range(Q):
  q = input()
  if q == '1':
    deq.reverse()
  else:
    _, f, c = q.split()
    if f == '1':
      deq.appendleft(c)
    else:
      deq.append(c)

print(''.join(deq))

Submission status: Time Limit Exceeded

Solution code:
S = [s for s in input()]
Q = int(input())
for _ in range(Q):
    T = input().split()
    if int(T[0]) == 1:
        S.reverse()
    else:
        F = int(T[1])
        C = [s for s in T[2]]
        if F == 1:
            S = C + S
        else:
            S = S + C
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
def do(s, query, isReverse):
    if(query[0] == "1"):
        isReverse *= -1
    else:
        if(query[1] == "1"):
            if(isReverse == 1):
                s += query[2]
            else:
                s = query[2] + s
        else:
            if(isReverse == 1):
                s = query[2] + s
            else:
                s += query[2]
    return s, isReverse

s = input()
q = int(input())
isReverse = -1
for i in range(q):
    query = input().split()
    s, isReverse = do(s, query, isReverse)
if(isReverse == -1):
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from collections import deque
s = deque(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.reverse()
    if a[i][0] == '2':
        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())
c = 0
for _ in range(Q):
  q = input()
  if (q[0] == "1"):
    c += 1
  else:
    if (q[2] == "1"):
      if (c % 2 == 0):
      	s = q[4] + s
      else:
        s = s + q[4]
    else:
      if (c % 2 == 0):
      	s = s + q[4]
      else:
        s = q[4] + s
if (c % 2 == 0):
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
r = False
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    if r:
      r = False
    else:
      r = True
  else:
    if (q[1] == '1' and not r) or (q[1] == '2' and r):
      S = q[2]+S
    else:
      S = S+q[2]
print(''.join(list(reversed(S))) if r else S)

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())
r = 0

# for i in range(q):
#     A = list(input().split())
#     if A[0] == '1':
#         r += 1
#     else:
#         if A[1] == '1':
#             if r % 2 == 0:
#                 s = A[2] + s
#             else:
#                 s = s + A[2]
#         else:
#             if r % 2 == 0:
#                 s = s + A[2]
#             else:
#                 s = A[2] + s

for i in range(q):
    A = list(input().split())
    if A[0] == '1':
        r += 1
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] != '1' and r % 2 != 0):
            s = A[2] + s
        else:
            s = s + A[2]

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

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
Q = int(input())
for q in range(Q):
  Query = str(input())
  if list(Query)[0] == '1':
    S = ''.join(list(reversed(S)))
  else:
    if list(Query)[2] == '1':
      S = ''.join(list(Query)[4:]) + S
    else:
      S = S + ''.join(list(Query)[4:])
print(S)

Submission status: Time Limit Exceeded

Solution code:
import math
s=input().strip()
st=s 
rev=s[::-1]
q=int(input())
cc=0
for i in range(q):
    ty=list(map(str,input().split()))
    if len(ty)==1:
        cc+=1 
        st,rev=rev,st
    else:
        if int(ty[1])==1:
            st=ty[2]+st  
            rev=rev+ty[2]
        else:
            st=st+ty[2]
            rev=ty[2]+rev 
print(st)

Submission status: Time Limit Exceeded

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

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
x = 1
for i in range(q):
  a = input().split()
  if a[0] == '1':
    x = -x
  elif (1.5-int(a[1]))*x > 0:
    s = ''.join([a[2],s])
  else:
    s = ''.join([s,a[2]])
if x == -1:
  s = s[::-1]
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]=='1':
    S=S[::-1]
  else:
    if a[1]=='1':
      S=a[2]+S
    else:
      S=S+a[2]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        S = S[::-1]
    else:
        if x[1] == "1":
            S = x[2] + S
        else:
            S = S + x[2]
            
print(S)

Submission status: Time Limit Exceeded

Solution code:
S=input()
ans=""
for i in range(len(S)):
    ans=S[i]+ans
Q=int(input())
tmp=0
for i in range(Q):
    que=input().split()
    if que[0]=='1':
        tmp=1-tmp
    if que[0]=='2':
        if que[1]=='1':
            if tmp==0:
                S=que[2]+S
                ans=ans+que[2]
            else:
                S=S+que[2]
                ans=que[2]+ans
        else:
            if tmp==0:
                S=S+que[2]
                ans=que[2]+ans
            else:
                S=que[2]+S
                ans=ans+que[2]
#print(que)
"""
for i in range(Q):
    if que[i][0]=='1':
        tmp=1-tmp
    if que[i][0]=='2':
        if que[i][1]=='1':
            if tmp==0:
                S=que[i][2]+S
            else:
                S=S+que[i][2]
        else:
            if tmp==0:
                S=S+que[i][2]
            else:
                S=que[i][2]+S
"""
#print(tmp)
if tmp==1:
    print(ans)
    exit()

print(S)

Submission status: Time Limit Exceeded

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

for i in range(q):
  tfc = input().split()
  if tfc[0] == '1':
    s.reverse()
  else:
    if tfc[1] == '1':
      s.insert(0, tfc[2])
    else:
      s.append(tfc[2])
for i in s:
  print(i, end='')

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque


def input():
    return sys.stdin.readline()[:-1]


def main():
    S = input()
    Q = int(input())
    que = deque(S)
    for i in range(Q):
        q = list(input().split())
        if len(q) == 1:
            que.reverse()
        else:
            _, f, c = q
            if f == '1':
                que.appendleft(c)
            else:
                que.append(c)
    print("".join(list(que)))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
reverse = False
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        reverse = not reverse
    else:
        F = int(tmp[1])
        C = tmp[2]
        if F == 1:
            if reverse:
                S.append(C)
            else:
                S.insert(0, C)
        else:
            if reverse:
                S.insert(0, C)
            else:
                S.append(C)

if reverse:
    S.reverse()
print(''.join(S))

Submission status: Time Limit Exceeded

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

output_string = s

for query in q_datas :
    if query[0] == '1':
        output_string = output_string[::-1]
    else:
        if query[1] == '1':
            output_string = query[2] + output_string
        else:
            output_string = output_string + query[2]

print(output_string)

Submission status: Time Limit Exceeded

Solution code:

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

# print(S, Q)

queries = []
for i in range(Q):
    query = input()
    queries.append(query)



for query in queries:
    if query == "1":
        # print("reverse", S)
        S = S[::-1]
    else:
        t, f, c = query.split()
        # print(t, f, c)
        if f =="1":
            # print("append c to head of", S)
            S = c + S
        else:
            # print("append c to tail of ", S)
            S = S + c

print(S)

Submission status: Time Limit Exceeded

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

    for i in range(Q):
        q = input()
        if len(q.split(' ')) > 1:
            T, F, C = q.split(' ')
            if F == '1':
                S = C + S
            elif F == '2':
                S = S + C
        elif q == '1':
            S = S[::-1]

    print(S)

if __name__ == '__main__':
    main()