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())
rev_flag = False
for i in range(Q):
    query = input()
    if query == "1":
        rev_flag = not rev_flag
    else:
        if not rev_flag:
            if query[2] == "1":
                S = query[4] + S
            else:
                S = S + query[4]
        else:
            if query[2] == "2":
                S = query[4] + S
            else:
                S = S + query[4]
    #print(query)

if rev_flag:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

state = 0
for i in range(q):
    query = list(input().split())
    if query[0] == "1":
        state += 1
    else:
        if (int(query[1]) + state) % 2 == 1:
            s = query[2] + s
        else:
            s += query[2]

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

Submission status: Time Limit Exceeded

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

for i in range(q):
    query = list(input().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())

jun = True

for _ in range(Q):
    T = input().split()
    if T[0] == "1":
        jun = not jun
    
    else:

        if (T[1] == "1" and jun) or (T[1] == "2" and not jun):
            S = T[2] + S
            continue

        else:
            S = S + T[2]

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

Submission status: Time Limit Exceeded

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

jun = True

for _ in range(Q):
    T = input().split()
    if T[0] == "1":
        jun = not jun

    if T[0] == "2" and ((T[1] == "1" and jun) or (T[1] == "2" and not jun)):
        S = T[2] + S
        continue
        
    if T[0] == "2" and ((T[1] == "2" and jun) or (T[1] == "1" and not jun)):
        S = S + T[2]

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

Submission status: Time Limit Exceeded

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

jun = True

for _ in range(Q):
    tmpS = S
    T = input().split()
    if T[0] == "1":
        jun = not jun

    if T[0] == "2" and ((T[1] == "1" and jun) or (T[1] == "2" and not jun)):
        S = T[2] + tmpS
        continue
        
    if T[0] == "2" and ((T[1] == "2" and jun) or (T[1] == "1" and not jun)):
        S = tmpS + T[2]

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

Submission status: Time Limit Exceeded

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

is_frontR = True
for i in range(Q):
    tmp_Q = input().split()

    if len(tmp_Q) == 1:
        is_frontR = not is_frontR
        
    else:
        if tmp_Q[1] == "1":
            if is_frontR:
                S = tmp_Q[2] + S
            else:
                S = S + tmp_Q[2]
        else:
            if is_frontR:
                S = S + tmp_Q[2]
            else:
                S = tmp_Q[2] + S

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    tmp_Q = input().split()

    if len(tmp_Q) == 1:
        S = S[::-1]
    else:
        if tmp_Q[1] == "1":
            S = tmp_Q[2] + S
        else:
            S = S + tmp_Q[2]

print(S)

Submission status: Time Limit Exceeded

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

for tmp_Q in Q_list:
    if len(tmp_Q) == 1:
        S = S[::-1]
    else:
        if tmp_Q[1] == "1":
            S = tmp_Q[2] + S
        else:
            S = S + tmp_Q[2]

print(S)

Submission status: Time Limit Exceeded

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

jun = True

for _ in range(Q):
    tmpS = S
    T = input().split()
    if T[0] == "1":
        jun = not jun
    if T[0] == "2":
        if T[1] == "1":
            S = T[2] + tmpS if jun else tmpS + T[2]
        if T[1] == "2":
            S = tmpS + T[2] if jun else T[2] + tmpS
if jun:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    tmpS = S
    T = input().split()
    if T[0] == "1":
        S = tmpS[::-1]
    if T[0] == "2":
        if T[1] == "1":
            S = T[2] + tmpS
        if T[1] == "2":
            S = tmpS + T[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    S =     _in[0]   # type:str
    Q =     _in[1]   # type:str
    Q_arr = []
    for __in in _in[2:]:
        _ = __in.split(' ')
        if len(_) == 1:
            Q_arr.append([int(_[0])])
        else:
            Q_arr.append([int(_[0]),int(_[1]),_[2]])
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    reverse = False
    for _Q in Q_arr:
        if _Q[0] == 1:
            reverse = not reverse
        elif (_Q[1] == 1 and (not reverse)) or (_Q[1] == 2 and reverse):
            S = _Q[2] + S
        else:
            S = S + _Q[2]
    if reverse:
        S = S[::-1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(S)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    S =     _in[0]   # type:str
    Q =     _in[1]   # type:str
    Q_arr = []
    for __in in _in[2:]:
        _ = __in.split(' ')
        if len(_) == 1:
            Q_arr.append([int(_[0])])
        else:
            Q_arr.append([int(_[0]),int(_[1]),_[2]])
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    for _Q in Q_arr:
        if _Q[0] == 1:
            S = S[::-1]
        elif _Q[1] == 1:
            S = _Q[2] + S
        else:
            S = S + _Q[2]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(S)

Submission status: Time Limit Exceeded

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

trans = False
for i in range(q):
    query = input().split()
    if(query[0] == '1'):
        trans = not trans            
    else:
        if(trans):
            if(query[1] == '1'):
                s = s + query[2]
            else:
                s = query[2] + s
        else:
            if(query[1] == '1'):
                s = query[2] + s
            else:
                s = s + query[2]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

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

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        S.reverse()
    elif q[0] == '2':
        if q[1] == '1':
            S.appendleft(q[2])
        elif q[1] == '2':
            S.append(q[2])

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
switch=True
count=0
for i in range(q):
  query=input()
  if query=='1':
    count += 1
    if switch:
      switch=False
    else:
      switch=True
  else:
    t,f,c=map(str,query.split())
    if switch:
      if f=='1':
        s = c + s
      else:
        s = s + c
    else:
      if f=='2':
        s = c + s
      else:
        s = s + c
if count%2==1:
  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=='1':
    s=s[::-1]
  else:
    t,f,c=map(str,query.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())
for i in range(q):
  query=input()
  if len(query)==1:
    s=s[::-1]
  else:
    t,f,c=map(str,query.split())
    f=int(f)
    if f==1:
      s = c + s
    else:
      s = s + c
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
q=[]
for _ in range(n):
  q.append(input().split())
sw=True #奇数回文字列の前後を入れ替えているときFalseになる
for i in range(n):
  if q[i][0]=='1':
    sw = not sw
  else:
    if q[i][1]=='1' and sw==True:
      s0=q[i][2]
      s0+=s
      s=s0
    elif q[i][1]=='2' and sw==False:
      s0=q[i][2]
      s0+=s
      s=s0
    else:
      s+=q[i][2]
if sw:
  print(s)
else:
  s0=''
  for i in range(len(s)):
    s0+=s[len(s)-1-i]
  print(s0)

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
q=[]
for _ in range(n):
  q.append(input().split())
sw=True #奇数回文字列の前後を入れ替えているときFalseになる
for i in range(n):
  if q[i][0]=='1':
    sw = not sw
  else:
    if q[i][1]=='1' and sw==True:
      s = q[i][2]+s
    elif q[i][1]=='2' and sw==False:
      s = q[i][2]+s
    else:
      s+=q[i][2]
if sw:
  print(s)
else:
  s0=''
  for i in range(len(s)):
    s0+=s[len(s)-1-i]
  print(s0)

Submission status: Time Limit Exceeded

Solution code:
s=input()
count=0
for _ in range(int(input())):
  l=list(map(str,input().split()))
  
  if l[0]=='1':
    count+=1
  elif count%2==0:
    if l[1]=='1':
      s=l[2]+s
    else:
      s=s+l[2]
  else:
    if l[1]=='2':
      s=l[2]+s
    else:
      s=s+l[2]
if count%2!=0:
  s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
d = True
for i in range(q):
    t, *u= input().split()
    if t == "1":
        d = not d
    else:
        f, c = u[0], u[1]
        if d:
            if f == "1":
                s = c + s
            else:
                s = s + c
        else:
            if f == "1":
                s = s + c
            else:
                s = c + s

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

Submission status: Time Limit Exceeded

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
moji=input()
moji_rev=moji[::-1]
cycle=int(input())
flont_or_back=0
for i in range(cycle):
    line=input()
    if line[0]=="1":
        flont_or_back+=1
    elif line[0]=="2":
        fi,ci=line[2],line[4]
        if flont_or_back%2==0:
            if fi=="1":
                moji=ci+moji
                moji_rev+=ci
            elif fi=="2":
                moji=moji+ci
                moji_rev=ci+moji_rev
        else:
            if fi=="1":
                moji+=ci
                moji_rev=ci+moji_rev
            elif fi=="2":
                moji=ci+moji
                moji_rev+=ci
if flont_or_back%2==0:
  print(moji)
else:
  print(moji_rev)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = input().split()
Q = int(input())

def pop(left, right, rev, f, c):
    left_ = left
    if rev == 1:
        if f == '1':
            left_ = left[::-1]
            left_.append(c)
            left_ = left_[::-1]
        else:
            right.append(c)
    else:
        if f == '1':
            right.append(c)
        else:
            left_ = left[::-1]
            left_.append(c)
            left_ = left_[::-1]
    return left_, right


left, right = [], []
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        left, right = pop(left, right, rev, *que[1:])
print(''.join(left+S+right)[::rev])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = input().split()
Q = int(input())

def pop(left, right, rev, f, c):
    left_ = left
    if rev == 1:
        if f == '1':
            left_ = [c] + left
        else:
            right.append(c)
    else:
        if f == '1':
            right.append(c)
        else:
            left_ = [c] + left
    return left_, right


left, right = [], []
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        left, right = pop(left, right, rev, *que[1:])
print(''.join(left+S+right)[::rev])

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = input().split()
Q = int(input())

def pop(s, rev, f, c):
    ret = s
    if rev == 1:
        if f == '1':
            ret = [c] + s
        else:
            ret = s + [c]
    else:
        if f == '1':
            ret = s + [c]
        else:
            ret = [c] + s
    return ret


left, right = '', ''
rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        S = pop(S, rev, *que[1:])
print(''.join(S)[::rev])

Submission status: Time Limit Exceeded

Solution code:
moji=input()
cycle=int(input())
for i in range(cycle):
  line=input()
  if line[0]=="1":
    moji="".join(list(reversed(moji)))
  elif line[0]=="2":
    fi,ci=line[2],line[4]
    if fi=="1":
      moji=ci+moji
    elif fi=="2":
      moji+=ci
print(moji)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S = input()
Q = int(input())

def pop(s, rev, f, c):
    ret = s
    if rev == 1:
        if f == '1':
            ret = c + s
        else:
            ret = s + c
    else:
        if f == '1':
            ret = s + c
        else:
            ret = c + s
    return ret


rev = 1
for _ in range(Q):
    que = input().split()
    if que[0] == '1':
        rev *= -1
        continue
    if que[0] == '2':
        S = pop(S, rev, *que[1:])
print(S[::rev])

Submission status: Time Limit Exceeded

Solution code:
moji=input()
cycle=int(input())
for i in range(cycle):
  line=input()
  if line[0]=="1":
    moji=moji[::-1]
  elif line[0]=="2":
    fi,ci=line[2],line[4]
    if fi=="1":
      moji=ci+moji
    elif fi=="2":
      moji=moji+ci
print(moji)

Submission status: Time Limit Exceeded

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

is_s0_head = True
tail = len(S)-1

for _ in range(q):
    # print(S)
    Q = list(input().split())
    if Q[0] == "1":
        is_s0_head = not is_s0_head
    else:
        if Q[1] == "1":
            if is_s0_head:
                S = Q[2] + S
            else:
                S = S + Q[2]
        else:
            if is_s0_head:
                S = S + Q[2]
            else:
                S = Q[2] + S

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from collections import deque
S=list(stdin.readline().strip())
N=list(map(int,stdin.readline().strip().split()))[0]

d = deque(S)
rev=False
for i in range(N):

    Q = list(stdin.readline().strip().split())
    if Q[0]=='1' :
        if rev==False:rev=True
        else:rev=False

    if Q[0]=='2':
        if Q[1]=='1':
            if rev==False:d.appendleft(Q[2])
            else:d.append(Q[2])
        if Q[1]=='2':
            if rev == False:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])
if rev==False:[print(x,end="") for x in d]
else:[print(d[x],end="") for x in range(len(d)-1,-1,-1)]

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
inv = False
for i in range(q):
    a = input()
    if a == '1':
        inv = not inv
        continue
    a = a.split()
    if (a[1] == '1')^inv:
        s = a[2] + s
    else:
        s += a[2]
if inv:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    r = 1
    for _ in range(q):
        n, *fc = input().split()
        if n == "1":
            r *= -1
        else:
            f, c = fc
            f = 1 if f == "1" else -1
            if f*r == 1:
                s = c+s
            else:
                s = s+c
    print(s[::r])
                
            

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    r = 1
    for _ in range(q):
        n, *fc = input().split()
        if n == "1":
            r *= -1
        else:
            f, c = fc
            f = 1 if f == "1" else -1
            if f*r == 1:
                s = c+s
            else:
                s = s+c
    print(s[::r])
                
            

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	S = input()
	Q = int(input())
	cnt = 0
	s_start = ""
	s_end = ""
	for i in range(Q):
		q = list(input().split())
		if q[0] == "1":
			cnt += 1
		else:
			if q[1] == "1":
				if cnt % 2 == 0:
					s_start += q[2]
				else:
					s_end += q[2]
			else:
				if cnt % 2 == 0:
					s_end += q[2]
				else:
					s_start += q[2]

	if cnt % 2 == 0:
		ans = s_start[::-1] + S + s_end

	else:
		ans = s_end[::-1] + S[::-1] + s_start

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
import math

s = list(input())
i = int(input())

isR = False
for _ in range(i):
    q = input().split(" ")

    if q[0] == "1":
        isR = not isR
    else:
        if isR:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.insert(0, q[2])
        else:
            if q[1] == "1":
                s.insert(0, q[2])
            else:
                s.append(q[2])

if isR:
    print("".join(s[::-1]))
else:
    print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
import math

s = input()
i = int(input())

isR = False
for _ in range(i):
    q = input().split(" ")

    if q[0] == "1":
        isR = not isR
    else:
        if isR:
            if q[1] == "1":
                s += q[2]
            else:
                s = q[2] + s
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s += q[2]

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

Submission status: Time Limit Exceeded

Solution code:
import math

s = input()
i = int(input())

isR = False
for _ in range(i):
    q = input().split(" ")

    if q[0] == "1":
        isR = not isR
    else:
        if isR:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

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

Submission status: Time Limit Exceeded

Solution code:
import math

s = input()
i = int(input())

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
a = []
b = []
reverse_flag = False

for i in range(n):
    command = input()
    # reverse
    if len(command) == 1:
        reverse_flag = not reverse_flag
    
    else:
        _, t, f = command.split(' ')
        if t == '1':
            if reverse_flag:
                b.append(f)
            else:
                a.insert(0, f)
        else:
            if reverse_flag:
                
                a.insert(0, f)
            else:
                b.append(f)

if reverse_flag:
    print(''.join(b[::-1]) + s[::-1] + ''.join(a[::-1]))
else:
    print(''.join(a) + s + ''.join(b))

Submission status: Time Limit Exceeded

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

rev = False
for i in range(num_query):
    query = input().split()
    if len(query) == 1:
        if rev == True:
            rev = False
        else:
            rev = True
    else:
        if rev == False:
            if query[1] == "1":
                S = list(query[2]) + S
            else:
                S.append(query[2])
        else:
            if query[1] == "2":
                S = list(query[2]) + S
            else:
                S.append(query[2])

if rev == True:
    S = S[::-1]
    
print("".join(S))

Submission status: Time Limit Exceeded

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

for i in range(num_query):
    query = input().split()
    if len(query) == 1:
        S = S[::-1]
    else:
        if query[1] == "1":
            S = list(query[2]) + S
        else:
            S.append(query[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

for i in range(num_query):
    query = input().split()
    if len(query) == 1:
        S = S[::-1]
    else:
        if query[1] == "1":
            S.insert(0, query[2])
        else:
            S.append(query[2])

print("".join(S))

Submission status: Time Limit Exceeded

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

cnt = 0
llst = []
rlst = []
for i in range(q):
  line = input()
  if line == '1':
    cnt += 1
  else:
    elems = line.split()
    if cnt % 2 == 0:
      # 指示通り追加
      if elems[1] == '1':
        llst = [elems[2]] + llst
      else:
        rlst.append(elems[2])
    else:
      # 指示の向きと逆に追加
      if elems[1] == '1':
        rlst.append(elems[2])
      else:
        llst = [elems[2]] + llst
        
if cnt % 2 == 0:
  res = ''.join(llst) + s + ''.join(rlst)
  print(res)
else:
  res = ''.join(llst) + s + ''.join(rlst)
  print(res[::-1])

Submission status: Time Limit Exceeded

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

cnt = 0
llst = []
rlst = []
for i in range(q):
  line = input()
  if line == '1':
    cnt += 1
  else:
    elems = line.split()
    if cnt % 2 == 0:
      # 指示通り追加
      if elems[1] == '1':
        llst = [elems[2]] + llst
      else:
        rlst.append(elems[2])
    else:
      # 指示の向きと逆に追加
      if elems[1] == '1':
        rlst.append(elems[2])
      else:
        llst = [elems[2]] + llst
        
if cnt % 2 == 0:
  res = ''.join(llst) + s + ''.join(rlst)
  print(res)
else:
  res = ''.join(llst) + s + ''.join(rlst)
  print(''.join(list(reversed(res))))

Submission status: Time Limit Exceeded

Solution code:
S=input()
rev=False
for _ in range(int(input())):
    l = input().split()
    if len(l)==1:
        rev = not rev
    else:
        if rev^(l[1]=="1"):
            S=l[2]+S
        else:
            S+=l[2]
print(S[::1*(-1 if rev else 1)])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
T=[]
for i in range(Q):
  T.append(input().split())
for i in range(Q):
  if T[i][0]=="1":
    S.reverse()
  else:
    if T[i][1]=="1":
      S.appendleft(T[i][2])
    else:
      S.append(T[i][2])
print("".join(S))

Submission status: Time Limit Exceeded

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

for i in range(q):
    t = input().split()
    if int(t[0]) == 1:
        first *= -1
    else:
        f = int(t[1])
        c = t[2]
        
        if first == 1:
            if f == 1:
                s = c + s
            elif f == 2:
                s = s + c
        else:
            if f == 1:
                s = s + c
            elif f == 2:
                s = c + s

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

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=list(input())
q=int(input())
rev=1

for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        rev*=-1
    else:
        if a[1]=='1':
            if rev>0:
                s.insert(0,a[2])
            else:
                s.append(a[2])
        else:
            if rev>0:
                s.append(a[2])
            else:
                s.insert(0,a[2])

if rev<0:
    s=s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
q=int(input())
rev=1

for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        rev*=-1
    else:
        if a[1]=='1':
            if rev>0:
                s=a[2]+s
            else:
                s=s+a[2]
        else:
            if rev>0:
                s=s+a[2]
            else:
                s=a[2]+s

if rev<0:
    s=s[::-1]
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for i in range(q):
    a=list(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:
from collections import deque

def swap(d, query):
    if(query[0] == 1):
        d.reverse()
    else:
        if(query[1] == 1):
            d.appendleft(query[2])
        else:
            d.append(query[2])

def main():
    S = [c for c in input().rstrip()]
    Q = int(input().rstrip())
    query = []
    for i in range(Q):
        q = input().rstrip().split(' ')
        if(q[0] == '1'):
            query.append((1, -1, -1))
        else:
            query.append((2, int(q[1]), q[2]))
    d = deque(S)
    
    count = 0
    for q in query:
        if(q[0] == 1):
            count += 1
        else:
            if(count % 2 == 1):
                swap(d, (1, -1, -1))
            count = 0
            swap(d, q)
    if(count % 2 == 1):
        swap(d, (1, -1, -1))
    
    for c in d:
        print(c, end='')
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

def swap(d, query):
    if(query[0] == 1):
        d.reverse()
    else:
        if(query[1] == 1):
            d.appendleft(query[2])
        else:
            d.append(query[2])

def main():
    S = [c for c in input().rstrip()]
    Q = int(input().rstrip())
    query = []
    for i in range(Q):
        q = input().rstrip().split(' ')
        if(q[0] == '1'):
            query.append((1, -1, -1))
        else:
            query.append((2, int(q[1]), q[2]))
    d = deque(S)
    
    for q in query:
        swap(d, q)
    
    for c in d:
        print(c, end='')
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
k = 1
l = ""
li = ""
r = ""
ri = ""
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                l = c + l
                li = li + c
            else:
                r = r + c
                ri = c + ri
        elif f == '2':
            if k > 0:
                r = r + c
                ri = c + ri
            else:
                l = c + l
                li = li + c

if k > 0:
    print(l + S + r)
else:
    print(ri + S[::-1] + li)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
k = 1
l = ""
r = ""
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                l += c
            else:
                r += c
        elif f == '2':
            if k > 0:
                r += c
            else:
                l += c

if k > 0:
    l = l[::-1]
    print(l + S + r)
else:
    print(r[::-1] + S[::-1] + l)