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:
def main():
    from collections import deque
    s = deque(input())
    q = int(input())
    for i in range(q):
        l = input().split()
        if int(l[0]) == 1:
            s.reverse()
        else:
            if int(l[1]) == 1:
                s.appendleft(l[2])
            else:
                s.append(l[2])
    print("".join(s))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
revflag = 0

for i in query:
    if i[0] == '1':
        revflag = 1 - revflag
    else:
        if (revflag == 0 and i[1] == '1') or (revflag == 1 and i[1] == '2'):
            S.insert(0, i[2])
        else:
            S.append(i[2])

if revflag == 0:
    print(''.join(S))
else:
    print(''.join(S[::-1]))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
revflag = 0

for i in query:
    if i[0] == '1':
        revflag = 1 - revflag
    else:
        if (revflag == 0 and i[1] == '1') or (revflag == 1 and i[1] == '2'):
            S = i[2] + S
        else:
            S = S + i[2]

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

Submission status: Time Limit Exceeded

Solution code:
a = input()
n = int(input())
b = 1
for i in range(n):
    q = input()
    if q == "1":
        b = (b+1)%2
    else:
        q,f,c = q.split()
        if int(f)%2 == b:
            a = c+a
        else:
            a = a+c
if b == 0:
    a = a[::-1]
print(a)

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input()
    if t == "1":
        s.reverse()
    else:
        t2, f, c = t.split()
        if f == "1":
            s.insert(0, c)
        elif f == "2":
            s.append(c)
print("".join(s))

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]
    else:
        t2, f, c = t.split()
        if f == "1":
            s = c + s
        elif f == "2":
            s += c

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S="".join(S)
print(S)

Submission status: Time Limit Exceeded

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

reverse_flag = 1

for _ in range(q):
    query = input().split()
    if query[0] == "1":
        reverse_flag *= -1
    else:
        if (query[1] == "1" and reverse_flag == 1) or (query[1] == "2" and reverse_flag == -1):
            s = query[2] + s
        else:
            s = s + query[2]
if reverse_flag == 1:
    print(s)
else:
    print("".join(list(reversed(s))))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    appendleft=s.appendleft
    append=s.append
    flag = False
    for i in range(q):
        temp = input().split()
        if temp[0] == "1":
            flag = not flag
        else:
            if flag:
                s.reverse()
                flag = False
            if temp[1] == "1":
                appendleft(temp[2])
            else:
                append(temp[2])
    if flag:
        s.reverse()
    print("".join(list(s)))
if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def solve(string):
    s, q, *qs = string.split("\n")
    for _q in qs:
        if _q == "1":
            s = s[::-1]
            continue
        _, f, c = _q.split()
        s = c + s if f == "1" else s + c
    return s


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
work = deque()
workappendleft = work.appendleft
appendleft=s.appendleft
append=s.append
flag = False
for i in range(q):
    temp = input().split()
    if temp[0] == "1":
        flag = not flag
    else:
        if flag:
            for el in s:
                workappendleft(el)
            s.clear()
            for el in work:
                append(el)
            work.clear()
            flag = False
        if temp[1] == "1":
            appendleft(temp[2])
        else:
            append(temp[2])
if flag:
    for el in s:
        workappendleft(el)
    s.clear()
    for el in work:
        append(el)
print("".join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
work = deque()
workappendleft = work.appendleft
appendleft=s.appendleft
append=s.append
for i in range(q):
    temp = input().split()
    if temp[0] == "1":
        for el in s:
            workappendleft(el)
        s.clear()
        for el in work:
            append(el)
        work.clear()
    else:
        if temp[1] == "1":
            appendleft(temp[2])
        else:
            append(temp[2])
print("".join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
work = deque()
workappendleft = work.appendleft
appendleft=s.appendleft
append=s.append
for i in range(q):
    temp = input().split()
    t = temp[0]
    if t == "1":
        for el in s:
            workappendleft(el)
        s.clear()
        for el in work:
            append(el)
        work.clear()
    else:
        f = temp[1]
        c = temp[2]
        if f == "1":
            appendleft(c)
        else:
            append(c)
print("".join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(input())
q = int(input())
appendleft=s.appendleft
append=s.append
for i in range(q):
    temp = input().split()
    t = temp[0]
    if t == "1":
        work = deque()
        for el in s:
            work.appendleft(el)
        s.clear()
        for el in work:
            append(el)
    else:
        f = temp[1]
        c = temp[2]
        if f == "1":
            appendleft(c)
        else:
            append(c)
print("".join(list(s)))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    temp = input().split()
    t = temp[0]
    if t == "1":
        s = s[::-1]
    else:
        f = temp[1]
        c = temp[2]
        if f == "1":
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = [char for char in input()]
cnt = int(input())
for _ in range(cnt):
    item = input().split()
    if item[0] == "1":
        s.reverse()
    elif item[0] == "2":
        if item[1] == "1":
            s = [item[2]] + s
        else:
            s.append(item[2])
print("".join(s))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

n = 1
for i in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1":
    n *= -1
  else:
    f = int(query[1])
    c = query[2]
    if f * n == 1 or f * n == -2:
      s = c + s
    else:
      s = s + c

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

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S.appendleft(b[1])
    else:
      S.append(b[1])
if  f % 2 == 0 :
  print ("".join(S))
else:
  for i in range(len(S)):
    print (S[-i-1], end="")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
f = 0
for _ in range(Q):
  a, *b = input().split()
  f += int(a)
  if a == "2":
    if (int(b[0]) + f) % 2 == 1 :
      S = "".join([b[1],S])
    else:
      S = "".join([S,b[1]])
print (S  if f % 2 == 0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
is_reverse = 0
for i in range(q):
    query = input()
    if query[0] == '1':
        is_reverse = (is_reverse+1) % 2
    else:
        _, f, c = query.split()
        if (is_reverse+int(f))%2 == 0:
            s += c
        else:
            s = c + s
if is_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:
        _, f, c = query.split()
        if f == '1':
            s = c + s
        else:
            s += c
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
str_head = True # Trueのとき、向きは正しい。

for _ in range(Q):
  tmp = input().split()
  if tmp[0] == "1":
    str_head = not str_head
  elif tmp[0] == "2":
    if tmp[1] == "1":
      # 本来入力文字は文字列の先頭に配置される。
      if str_head:
        # 正しい向きなのでそのまま先頭へ。
      	S.insert(0,tmp[2])
      else:
        # 逆向きなので最後へ。
        S.append(tmp[2])
    else:
      # 本来入力文字は文字列の最後に配置される。
      if str_head:
        # 正しい向きなのでそのまま最後に。
      	S.append(tmp[2])
      else:
        # 逆向きなので最初へ。
        S.insert(0,tmp[2])
      
if not str_head:
  # 逆向きなので反転。
  S = list(reversed(S))
print("".join(S))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  tmp = input().split()
  if tmp[0] == "1":
    S = list(reversed(S))
  elif tmp[0] == "2":
    if tmp[1] == "1":
      S.insert(0,tmp[2])
    else:
      S.append(tmp[2])
      
print("".join(S))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
  tmp = input().split()
  if tmp[0] == "1":
    S = "".join(list(reversed(S)))
  elif tmp[0] == "2":
    if tmp[1] == "1":
      S = tmp[2] + S
    else:
      S += tmp[2]
      
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections

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

d = collections.deque(S)

for _ in range(Q):
    Qi = input()
    if Qi == "1":
        d.reverse()
    else:
        T, F, C = Qi.split()
        if F == "1":
            d.appendleft(C)
        else:
            d.append(C)

print("".join(d))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
hanten = 1
for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0]=='1':
        hanten *= -1
    else:
        if hanten==1:
            if q[1]=='1':
                S = q[2] + S
            else:
                S = S + q[2]
        else:
            if q[1]=='1':
                S = S + q[2]
            else:
                S = q[2] + S

if hanten==1:
    print(S)
else:
    print(''.join(reversed(list(S))))

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    q = list(map(str, input().split()))
    if len(q)==1:
        S = ''.join(reversed(list(S)))
    else:
        if q[1]=='1':
            S = ''.join([q[2], S])
        else:
            S = ''.join([S, q[2]])

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        S = ''.join(reversed(list(S)))
    else:
        if query[1]=='1':
            S = query[2] + S
        else:
            S = S + query[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        S.reverse()
    else:
        if query[1]=='1':
            S = list(query[2]) + S
        else:
            S = S + list(query[2])

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

for i in range(Q):
  query = [str(e) for e in input().split()]
  if len(query)==1:
    #文字を反転
    S.reverse()
  else:
    if query[1]=="1":
      #先頭に追加
      S.appendleft(query[2])
    else:
      #末尾に追加
      S.append(query[2])
      
#print(S)
print(''.join(map(str,S)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

rf=0
for q in query:
    if q[0] == "1":
        rf ^= 1
    else:
        if q[1] == '1':
            if rf:
                # S = S + q[2]
                S = ''.join([S, q[2]])
            else:
                # S = q[2] + S
                S = ''.join([q[2], S])
        else:
            if rf:
                S = ''.join([q[2], S])
            else: 
                S = ''.join([S, q[2]])
print(S[::-1] if rf else S)

Submission status: Time Limit Exceeded

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

rf=0
for q in query:
    if q[0] == "1":
        rf ^= 1
    else:
        if q[1] == '1':
            if rf:
                S = S + q[2]
            else:
                S = q[2] + S
        else:
            if rf:
                S = q[2] + S
            else: 
                S = S + q[2]
print(S[::-1] if rf else S)

Submission status: Time Limit Exceeded

Solution code:
def op2(f, c, r):
  if r == 0:
    if f == "1":
      slist.insert(0, c)
    elif f == "2":
      slist.append(c)

  if r == 1:
    if f == "2":
      slist.insert(0, c)
    elif f == "1":
      slist.append(c)

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]
rev = 0
slist = [S]

for val in v:
  if val[0] == "1":
    if rev == 0:
      rev = 1
    elif rev == 1:
      rev = 0
  elif val[0] == "2":
    op2(val[1], val[2], rev)

ans = "".join(slist)

if rev == 0:
  print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
def op2(f, c, r):
  if r == 0:
    if f == "1":
      slist.insert(0, c)
    elif f == "2":
      slist.append(c)

  if r == 1:
    if f == "2":
      slist.insert(0, c)
    elif f == "1":
      slist.append(c)

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]
rev = 0
slist = [S]

for val in v:
  if val[0] == "1":
    if rev == 0:
      rev = 1
    elif rev == 1:
      rev = 0
  elif val[0] == "2":
    S = op2(val[1], val[2], rev)

ans = "".join(slist)

if rev == 0:
  print(ans)
else:
  print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
a = input()
q = int(input())
query = [list(input().split()) for i in range(q)]
now = 0
for i in query:
  if i[0] == "1":
    now ^= 1
  elif i[0] == "2":
    f,c = int(i[1]),i[2]
    if (f+now) % 2:
      a = c + a
    else:
      a = a + c
if now:
  a = a[::-1]
print(a)

Submission status: Time Limit Exceeded

Solution code:
S = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]
hanten = -1
for row in range(quantity):
    each = L[row]
    if each[0] == "1":
        hanten = hanten * -1
    else:
        if each[1] == "1":
            if hanten == -1:
                S = each[2] + S
            else:
                S = S + each[2]
        else:
            if hanten == -1:
                S = S + each[2]
            else:
                S = each[2] + S

if hanten == 1:
    S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]
for row in range(quantity):
    each = L[row]
    if each[0] == "1":
        S = S[::-1]
    else:
        if each[1] == "1":
            S = each[2] + S
        else:
            S = S + each[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
quantity = int(input())
for row in range(quantity):
    each = input().split()
    if each[0] == "1":
        S = S[::-1]
    else:
        if each[1] == "1":
            S = each[2] + S
        else:
            S = S + each[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
t  ={}
for i in range(q):
    t[i]=input().split()
rv_count = 0
x = "X"
for d in t.values():
    if d[0] == "1":
        x2 = x[::-1]
        rv_count += 1
    elif d[0] == "2":
        if d[1] == "1":
            x2 = d[2]+x
        else:
            x2 = x+d[2]
    x = x2
if rv_count%2 == 0:
    xf =x.replace("X", s)
else:
     xf =x.replace("X", s[::-1])
print(xf)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
t  ={}
for i in range(q):
    t[i]=input().split()
for d in t.values():
    if d[0] == "1":
        s2 = s[::-1]
    elif d[0] == "2":
        if d[1] == "1":
            s2 = d[2]+s
        else:
            s2 = s+d[2]
    s = s2
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
answer = input()
q = int(input())
rev_cnt = 0
for i in range(0, q):
  querry = input().split()
  if querry[0] == '1':
    rev_cnt += 1
  else:
    if rev_cnt % 2 == 1:
      answer = ''.join(list(reversed(answer)))
    rev_cnt = 0  
    if querry[1] == '1':
      answer = querry[2] + answer
    else:
      answer = answer + querry[2]
if rev_cnt % 2 == 1:
  answer = ''.join(list(reversed(answer)))
print(answer)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
querrys = []
answer = s
for i in range(0, q):
  querry = input().split()
  if querry[0] == '1':
    answer = ''.join(list(reversed(answer)))
  	# querrys.append(['1', '0', '0'])
  else:
    if querry[1] == '1':
      answer = querry[2] + answer
    # querrys.append(querry)
    else:
      answer = answer + querry[2]
print(answer)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    v = list(input().split())
    if v[0] == '1':
        if SWITCH:
            SWITCH = False
        else:
            SWITCH = True

    else:
        if v[1] == '1':
            if SWITCH==False:
                S = v[2] + S
            else:
                S = S + v[2]
        else:
            if SWITCH==False:
                S = S + v[2]
            else:
                S = v[2] + S

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    v = query[i]
    if v[0] == '1':
        S = S[::-1]
    else:
        if v[1] == '1':
            S = v[2] + S
        else:
            S = S + v[2]

print(S)

Submission status: Time Limit Exceeded

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

#print(S, Q, lst)

str=list(S)

right=True
for operation in lst:
    if operation[0]=='1':
       right=not right

    else:
        if right==True and operation[1]=='1' or right==False and operation[1]=='2':
            str=[operation[2]]+str
        else:
            str=str+[operation[2]]

if right==True:
    print(''.join(str))
else:
    print(''.join(str[::-1]))

Submission status: Time Limit Exceeded

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

#print(S, Q, lst)

str=list(S)

right=True
for operation in lst:
    if operation[0]=='1':
       right=not right

    if operation[0]=='2':
        if right==True and operation[1]=='1' or right==False and operation[1]=='2':
            str=[operation[2]]+str
        else:
            str=str+[operation[2]]

if right==True:
    print(''.join(str))
else:
    print(''.join(str[::-1]))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

S = deque(S)

for _ in range(Q):
    query = input()
    if query[0] == '1':
        S.reverse()
    elif query[0] == '2':
        _, F, C = query.split()
        if F == '1':
            S.appendleft(C)
        elif F == '2':
            S.append(C)
            
result = ''.join(S)
print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

S = deque(S)

for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        S.reverse()
    elif query[0] == '2':
        if query[1] == '1':
            S.appendleft(query[2])
        elif query[1] == '2':
            S.append(query[2])
            
result = ''.join(S)
print(result)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = query[2] + S
        elif query[1] == '2':
            S = S + query[2]
            
print(S)

Submission status: Time Limit Exceeded

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]
key = 0  #0 start

for i in range(kai):
    if(data[i][0] == '1'):
        key = (key + 1) % 2
    elif((int(data[i][1]) + key) % 2 == 1):
        string = data[i][2] + string
    else:
        string = string + data[i][2]

if(key == 0):
    print(string)
else:
    print(string[::-1])

Submission status: Time Limit Exceeded

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]
key = 0  #0 start

for i in range(kai):
    if(data[i][0] == '1'):
        key = (key + 1) % 2
    elif(data[i][1] == '1'):
        if(key == 0):
            string = data[i][2] + string
        else:
            string = string + data[i][2]
    elif(key == 0):
        string = string + data[i][2]
    else:
        string = data[i][2] + string
if(key == 0):
    print(string)
else:
    print(string[::-1])

Submission status: Time Limit Exceeded

Solution code:
def op2(f, c, r, s):
  if r == 0:
    if f == "1":
      return (c + s)
    elif f == "2":
      return (s + c)

  if r == 1:
    if f == "2":
      return (c + s)
    elif f == "1":
      return (s + c)

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]
rev = 0

for val in v:
  if val[0] == "1":
    if rev == 0:
      rev = 1
    elif rev == 1:
      rev = 0
  elif val[0] == "2":
    S = op2(val[1], val[2], rev, S)
    
if rev == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
def op2(f, c):
  if r == 0:
    if f == "1":
      return (c + S)
    elif f == "2":
      return (S + c)

  if r == 1:
    if f == "2":
      return (c + S)
    elif f == "1":
      return (S + c)

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]
r = 0

for val in v:
  if val[0] == "1":
    if r == 0:
      r = 1
    elif r == 1:
      r = 0
  elif val[0] == "2":
    S = op2(val[1], val[2])
    
if r == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n=int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s = [l[i][2]] + s
            else:
                s.append(l[i][2])
        else:
            if l[i][1]=='1':
                s.append(l[i][2])
            else:
                s = [l[i][2]] + s
if x == 0:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Time Limit Exceeded

Solution code:
def op1():
  new = S[::-1]
  return new

def op2(f, c):
  if f == "1":
    return (c + S)
  elif f == "2":
    return (S + c) 
  

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]

for val in v:
  if val[0] == "1":
    S = op1()
  elif val[0] == "2":
    S = op2(val[1], val[2])
    
print(S)

Submission status: Time Limit Exceeded

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

for q in range(q_):
  t = input().split(' ')
  if t[0]=='1': s=s[::-1]
  elif t[0]=='2':
    if t[1]=='1': s=t[2]+s
    elif t[1]=='2': s+=t[2]
print(s)

Submission status: Time Limit Exceeded

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

ans = S
flag = 1
for i in range(Q):
    qry = list(input().split())

    if qry[0] == '1':
        if flag:
            flag = 0
        else:
            flag = 1
    
    elif qry[0] == '2':
        if flag:
            if qry[1] == '1':
                ans = qry[2] + ans
            else:
                ans += qry[2]
        else:
            if qry[1] == '1':
                ans += qry[2]
            else:
                ans = qry[2] + ans

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q_ = int(input())
for q in range(q_):
  t = input().split(' ')
  if t[0]=='1':
    s=''.join(list(reversed(s)))
  elif t[0]=='2':
    if t[1]=='1': s=t[2]+s
    elif t[1]=='2': s+=t[2]
print(s)

Submission status: Time Limit Exceeded

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

ans = S
flag = 1
for i in range(Q):
    qry = list(input().split())

    if qry[0] == '1' and flag:
        flag = 0
    elif qry[0] == '1' and not flag:
        flag = 1

    elif qry[0] == '2' and flag:
        if qry[1] == '1':
            ans = qry[2] + ans
        elif qry[1] == '2':
            ans += qry[2]
    elif qry[0] == '2' and not flag:
        if qry[1] == '1':
            ans += qry[2]
        elif qry[1] == '2':
            ans = qry[2] + ans

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

Submission status: Time Limit Exceeded

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

ans = S
for i in range(Q):
    qry = list(input().split())
    if qry[0] == '1':
        ans = ans[::-1]

    elif qry[0] == '2':
        if qry[1] == '1':
            ans = qry[2] + ans
        elif qry[1] == '2':
            ans += qry[2]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n=int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s = l[i][2] + s
            else:
                s = s + l[i][2]
        else:
            if l[i][1]=='1':
                s = s + l[i][2]
            else:
                s = l[i][2] + s
if x == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
n = int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s = [l[i][2]] + s
            else:
                s = s + [l[i][2]]
        else:
            if l[i][1]=='1':
                s = s + [l[i][2]]
            else:
                s = [l[i][2]] + s
if x == 0:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Time Limit Exceeded

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

for i in range(Q):
  Query=input()
  if len(Query)==1:
    S=S[::-1]
  else:
    if Query[2]=='1':
      S=Query[4]+S
    else:
      S+=Query[4]

print(S)