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: Accepted

Solution code:
from collections import deque

def rev(s):
    new_s = list(reversed(s))
    return new_s

S = deque(input())
N = int(input())
r_flag=False
for _ in range(N):
    Q = input()
    if Q.isdigit():
        r_flag=not r_flag
    else:
        q = Q.split()
        if r_flag:
            if q[1]=="1":
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if q[1]=="1":
                S.appendleft(q[2])
            else:
                S.append(q[2])
if r_flag:
    print(''.join(reversed(S)))
else:print(''.join(S))

Submission status: Accepted

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

guuki=0
ANS=S
ANS_back=""
ANS_front=""
for i in range(Q):
    A=list(input().split())
    if A[0]=="1":
        guuki+=1
    elif int(A[1])*guuki%2==1 or (A[1]=="2" and guuki%2==0):
        ANS_back+=A[2]
    else:
        ANS_front+=A[2]
if guuki%2==1:
    ANS=ANS[::-1]
    ANS_back,ANS_front=ANS_front,ANS_back
print(ANS_front[::-1]+ANS+ANS_back)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
before_chr = []
after_chr = []
reverse_status = 1
for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        reverse_status *= -1
    elif reverse_status * (1.5-int(query[1])) == 0.5:
        before_chr += query[2]
    else:
        after_chr += query[2]
if reverse_status == 1:
    print("".join(before_chr[::-1])+S+"".join(after_chr))
else:
    print("".join(after_chr[::-1])+S[::-1]+"".join(before_chr))

Submission status: Accepted

Solution code:
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


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

    # NOTE: need to reverse
    heads = ""
    tails = ""
    direction = 1
    for _ in range(Q):
        query = get_input()
        if query[0] == "1":
            direction *= -1
        else:
            _, F, C = query.split(" ")
            if F == "1":
                if direction == 1:
                    heads += C
                else:
                    tails += C
            else:
                if direction == 1:
                    tails += C
                else:
                    heads += C
     
    if direction == 1:
        print(heads[::-1] + S + tails)
    else:
        print(tails[::-1] + S[::-1] + heads)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s = deque(i for i in input())
n = int(input())
l = [[i for i in input().split()] for _ in range(n)]

front = True

for i in l:
    if i[0] == '1':
        front = not front
    else:
        if front ^ (int(i[1]) == 1):
            s.append(i[2])
        else:
            s.appendleft(i[2])
            
ans = ''.join(s)

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

Submission status: Accepted

Solution code:
import sys
import math

s = input()
Q = int(input())
query = []
[query.append(input().split()) for _ in range(Q)]

inversion = -1

if len(s) >= 3:
    head = s[0]
    tail = s[-1]
    body = s[1:-1]
    for i in range(Q):
        if query[i][0] == "1":
            inversion *= -1
            continue

        c = str(query[i][2])
        if query[i][1] == "1" and inversion == -1:
            head = c + head
        elif query[i][1] == "1" and inversion == 1:
            tail = tail + c
        elif query[i][1] == "2" and inversion == -1:
            tail = tail + c
        else:
            head = c + head
        
    if inversion == -1:
        print(head + body + tail)
    else:
        print(tail[::-1] + body[::-1] + head[::-1])

else:
        for i in range(Q):
            if query[i][0] == "1":
                inversion *= -1
                continue

            c = str(query[i][2])
            if query[i][1] == "1" and inversion == -1:
                s = c + s
            elif query[i][1] == "1" and inversion == 1:
                s = s + c
            elif query[i][1] == "2" and inversion == -1:
                s = s + c
            else:
                s = c + s
        
        if inversion == -1 :
            print(s)
        else:
            print(s[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
R = 1  # 1:False, -1:True
P = []
B = []
for _ in range(Q):
    Query = [i for i in input().split()]
    if Query[0] == '1':
        R *= -1
    else:
        if R == 1:
            if Query[1] == '1':
                P.append(Query[2])
            else:
                B.append(Query[2])
        else:
            if Query[1] == '1':
                B.append(Query[2][::-1])
            else:
                P.append(Query[2][::-1])

ans = ''.join(P[::-1])+S+''.join(B)
if R == -1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())

flag = 0
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        flag = flag ^ 1
    elif query[0] == '2':
        if flag == 0:
            if query[1] == '1':
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if query[1] == '1':
                S.append(query[2])
            else:
                S.appendleft(query[2])
if flag == 0:
    print(''.join(S))
else:
    # deque型はスライスで取り出せない print(''.join(S[::-1]))
    S.reverse()
    print(''.join(S))

Submission status: Accepted

Solution code:
import collections

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

Query = [input() for i in range(Q)]

deq = collections.deque(S)

flg = False
for q in Query:
    query = q.split(' ')
    if query[0]=='1':
        flg = not flg

    elif query[0] == '2':
        if query[1] == '1':
            if flg == True:
                deq.append(query[2])
            else:
                deq.appendleft(query[2])
        else:
            if flg==True:
               deq.appendleft(query[2])
            else:
               deq.append(query[2]) 
if flg == True:
    deq.reverse()        
print(''.join(deq))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
fstr = []
bstr = []
bstr.extend(s)


sta = 0
for i in range(q):
  buff = input()
  if(buff[0] == "2"):
    tmp = buff[4]
    if(buff[2] == "2"):
      
      if(sta == 0):
        
        bstr.append(tmp)  
      else:
        fstr.append(tmp)
    else:
      if(sta == 1):
        bstr.append(tmp)  
      else:
        fstr.append(tmp)
  else:
    if(sta == 0):
      sta = 1
    else:
      sta = 0

fstr.reverse()
fstr.extend(bstr)
if(sta == 1):
  fstr.reverse()

print(''.join(fstr))

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
Query=[input().split() for i in range(Q)]
bf=''
af=''
Flag=True

for i in range(Q):
    if Query[i][0] == '1':
        Flag = not Flag
    else:
        if Query[i][1] == '1':
            if Flag:
                bf = bf + Query[i][2]
            else:
                af = af + Query[i][2]
        else:
            if Flag:
                af = af + Query[i][2]
            else:
                bf = bf + Query[i][2]

ans = bf[::-1] + S + af

if not Flag:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
S = deque(s)
head = True

for i in range(q):
    li = input().split()
    if li[0] == '1':
        head = not head
    else:
        if head:
            if li[1] == '1':
                S.appendleft(li.pop())
            else:
                S.append(li.pop())
        else:
            if li[1] == '2':
                S.appendleft(li.pop())
            else:
                S.append(li.pop())
s = ''.join(S)
if not head:
    s = s[::-1]
print(s)

Submission status: Accepted

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

S = input().rstrip()
Q = int(input())
FTC = [tuple(input().split()) for _ in range(Q)]

que = deque(['?'])
cnt = 0
for query in FTC:
    if query[0] == '1':
        cnt ^= 1
        continue

    sign = 0 if query[1] == '1' else 1

    if sign ^ cnt == 0:
        que.appendleft(query[2])
    else:
        que.append(query[2])

que = list(que)
ans = ''.join(que[:: (-1)**cnt])
ans = ans.replace('?', S[:: (-1)**cnt])

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

# sys.stdin = open('test_cases/abc158d/in/01.txt', 'r')
# sys.stdin = open('input.txt', 'r')

s = deque([s for s in input()])
q = int(input())
rev = False

for query in sys.stdin:
    if query.strip() == '1': # sys.stdin does not remove line breaks. (vs. input() removes line breaks)
        if rev:
            rev = False
        else:
            rev = True
    else:
        t,f,c = query.split()
        if f == '1':
            if rev:
                s.append(c)
            else:
                s.appendleft(c)
        elif f == '2':
            if rev:
                s.appendleft(c)
            else:
                s.append(c)
if rev == True:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
isreverse=False
top=''

for i in range(n):
    ss=input().split()
    if len(ss)==1:
        isreverse=not isreverse
    else:
        if ss[1]=="1":
            if isreverse: s+=ss[2]
            else: top+=ss[2]
        else:
            if isreverse: top+=ss[2]
            else: s+=ss[2]
if isreverse:
    s=s[::-1]+top
else:
    s=top[::-1]+s


print(s)

Submission status: Accepted

Solution code:
from collections import deque

s_input = [char for char in input()]
q = int(input())
s = deque()
s_rev = deque()
s.extend(s_input)
s_rev.extendleft(s_input)
flag = True #T:inputと同順、F:逆順
count_append = 0 #文字の追加回数
for i in range(q):
  query = input().split()
  if query[0] == "1":
    flag = not flag
  else:
    if flag:
      if query[1] == "1":
        s.appendleft(query[2])
        s_rev.append(query[2])
      else:
        s.append(query[2])
        s_rev.appendleft(query[2])
    else:
      if query[1] == "1":
        s.append(query[2])
        s_rev.appendleft(query[2])
      else:
        s.appendleft(query[2])
        s_rev.append(query[2])
    count_append += 1
s_output = ""
if flag:
  for i in range(len(s_input) + count_append):
    s_output += s.popleft()
else:
  for i in range(len(s_input) + count_append):
    s_output += s.pop()
print(s_output)

Submission status: Accepted

Solution code:
#abc158d
s=raw_input()
q=int(raw_input())
res=""
for i in range(q):
 x=raw_input().split()
 if x[0]=='1':
  res,s=s,res
 else:
  if x[1]=='1':
   res+=x[2]
  else:
   s+=x[2]
res=res[::-1]
print res+s

Submission status: Accepted

Solution code:
s = input()
n = int(input())
stat = 1
head = []
tail = []
for i in range(n):
    q = input().split()
    if len(q) == 1:
        stat = stat *(-1)
    else:
        if q[1] == "1":
            if stat == 1:
                head.append(q[2])
            else:
                tail.append(q[2])
        else:
            if stat == 1:
                tail.append(q[2])
            else:
                head.append(q[2])

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

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S.append(Q[4])
        else:
            S.appendleft(Q[4])
if is_inversed:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

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

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

    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 23:22

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

# 0: 順
# 1: 逆
head = 0
pre = ''
post = ''

for _ in range(0, Q):
    q = input()
    if len(q.split()) == 1:
        head = 1 if head == 0 else 0
    else:
        _, f, c = q.split()
        if f == '1':
            # 先頭
            if head == 0:
                pre = c + pre
            else:
                post = post + c
        else:
            # 末尾
            if head == 1:
                pre = c + pre
            else:
                post = post + c

S = pre + S + post
print(S[::-1] if head == 1 else S)

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
revflag = 1
d = deque(s)
for i in range(q):
	tfc = list(map(str, input().split()))
	if tfc[0] == '1':
		revflag *= -1
	elif tfc[1] == '1':
		if revflag > 0:
			d.appendleft(tfc[2])
		else:
			d.append(tfc[2])
	else:
		if revflag > 0:
			d.append(tfc[2])
		else:
			d.appendleft(tfc[2])
if revflag > 0:
	print(''.join(d))
else:
	print(''.join(reversed(d)))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
d = deque(S)
Q = int(input())
rev = False
for _ in range(Q):
    Q, *sub = input().split()
    if Q == "1":
        rev = not rev
    else:
        F, C = sub
        if F == "1":
            if rev:
                d.append(C)
            else:
                d.appendleft(C)
        else:
            if rev:
                d.appendleft(C)
            else:
                d.append(C)
if rev:
    d.reverse()
print("".join(list(d)))

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1:
        L = -L
    if int(A[0]) == 2:
        A[1] = 2*int(A[1]) - 3
        if L*A[1] == -1:
            S.appendleft(A[2])
        else:
            S.append(A[2])

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

Submission status: Accepted

Solution code:
from collections import deque
from sys import exit, stdin

S = deque(stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
query = [stdin.readline().rstrip().split() for _ in range(Q)]
front = 0

for q in query:
    if q[0] == '1':
        if front == 0:
            front = -1
        elif front == -1:
            front = 0
    elif q[0] == '2':
        if q[1] == '1':
            if front == 0:
                S.appendleft(q[2])
            elif front == -1:
                S.append(q[2])
        elif q[1] == '2':
            if front == 0:
                S.append(q[2])
            elif front == -1:
                S.appendleft(q[2])
if front == 0:
    print(''.join(S))
elif front == -1:
    S.reverse()
    print(''.join(S))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

from collections import deque as dq

mode = True

def prob_d(s, q):
    ans = dq(s)
    for i in range(q):
        ans = ope(ans)
    if not mode:
        ans = ''.join(list(ans)[::-1])
    else:
        ans = ''.join(ans)
    print(ans)

def ope(s):
    global mode
    op = input().split()
    if len(op) == 1 and op[0] == '1':
        mode = not mode 
    elif len(op) == 3 and op[0] == '2':
        if op[1] == "1":
            if mode:
                s.appendleft(op[2])
            else:
                s.append(op[2])
        elif op[1] == "2":
            if mode:
                s.append(op[2])
            else:
                s.appendleft(op[2])
        else:
            pass
    else:
        pass
    return s

if __name__ == '__main__':
    s = input()
    q = int(input())
    prob_d(s, q)

Submission status: Accepted

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

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

x="".join(X)
x="".join(list(reversed(x)))
y="".join(Y)

a=[x,S,y]
b="".join(a)

if f==-1:
    b="".join(list(reversed(b)))

print(b)

Submission status: Accepted

Solution code:
from collections import deque as dq
s = input()
nq = int(input())

s = dq(s)

jun = True
for i in range(nq):
  query = input().split()
  if query[0] == '1':
    jun = not jun
    continue
  if jun:
    if query[1] == '1':
      s.appendleft(query[2])
    else:
      s.append(query[2])
  else:
    if query[1] == '1':
      s.append(query[2])
    else:
      s.appendleft(query[2])
if jun:
  print(''.join(s))
else:
  print(''.join(list(s)[::-1]))

Submission status: Accepted

Solution code:
str = input()
times = int(input())
revers = 0
head = ""
tail = ""
for i in range(times):
    com = input().split()
    if com[0] == "1":
        if revers == 0:
            revers = 1
        else :
            revers = 0
        
    else :
        if com[1] == "1":
            if revers == 0:
                head = com[2]+head
            else :
                tail = tail + com[2]
        else : 
            if revers == 1:
                head = com[2]+head
            else :
                tail = tail + com[2]          
str = head + str + tail
if revers == 1:
    str = str[::-1]
                
print(str)

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
Q = int(input())
perm = 0
d = deque(S)

for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        perm += 1
    else:
        if (int(l[1])+perm) % 2 == 0:
            d.append(l[2])
        else:
            d.appendleft(l[2])
 
S0 = list(d)
if perm % 2 != 0:
    S0.reverse()
ans = ""
for i in range(0, len(S0)):
    ans = ans + S0[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
d = deque(s)
d_is_reversed = False
for j in range(Q):
    if Query[j][0] == '1':
        d_is_reversed = not d_is_reversed
    elif Query[j][1] == '1':
        if d_is_reversed == False:
            d.appendleft(Query[j][2])
        else:
            d.append(Query[j][2])
    else:
        if d_is_reversed == False:
            d.append(Query[j][2])
        else:
            d.appendleft(Query[j][2])
if d_is_reversed == True:
    d.reverse()
printed = ''.join(d)
print(printed)

Submission status: Accepted

Solution code:
from collections import deque
words=deque(input())

order_amount=int(input())
flg=True
for _ in range(order_amount):
    orders=input()
    if orders[0]=='1':
        flg=not flg
    else:
        _,f,c=orders.split()
        if (f=='1' and flg) or (f=='2' and not flg):
            words.appendleft(c)
        elif (f=='2' and flg) or (f=='1' and not flg):
            words.append(c)
if not flg:
    words.reverse()
word=''.join(words)
print(word)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
flg = True
for _ in range(Q):
    query = input()
    if query[0] == "1":
        flg = not flg
    else:
        _,F,C = query.split()
        if (F == "1" and flg) or (F == "2" and not flg):
            S.appendleft(C)
        else:
            S.append(C)
if not flg:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
n = int(input())
q_list = [input().split() for i in range(n)]
count = 1
for i in q_list:
    if i[0] == '1':
        count *= -1
    else:
        if i[1] == '1':
            if count == 1:
                s.appendleft(i[2])
            else:
                s.append(i[2])
        else:
            if count == 1:
                s.append(i[2])
            else:
                s.appendleft(i[2])
if count != 1:
    s.reverse()
print(*s, sep='')

Submission status: Accepted

Solution code:
from collections import deque

revFlag = False

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

for i in range(q):
    a,b,c, *_ = input().split() + [None, None]
    
    if(a == '1'):
        revFlag = not revFlag
        
    else:
        b = int(b)
        if(revFlag):
            b = (int(b)%2)+1
        
        if(b == 1):
            s.appendleft(c)
        else:
            s.append(c)
            
    #print(_,*s, sep="")

if(revFlag):
    s.reverse()
    
print(*s, sep='')

Submission status: Accepted

Solution code:
from collections import deque

string = input()
x = deque()
for s in string:
    x.append(s)

head_left = True
B = int(input())
for i in range(B):
    cmd = input().split()
    if len(cmd) == 1 and cmd[0] == "1":
        head_left = not head_left
    else:
        _, n, c = cmd
        if (n == "1" and head_left) or (n == "2" and not head_left):
            x.appendleft(c)
        else:
            x.append(c)

if head_left:
    print("".join(list(x)))
else:
    print("".join(reversed(list(x))))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
n = int(input())
initial = True
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial:
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = not initial
    
if initial:
    print(''.join(s))
else:
    print(''.join(s)[::-1])

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    s = deque(input())
    q = int(input())
    qlist = [input().split() for i in range(q)]
    flip = False
    for j in range(q):
        if qlist[j][0] == "1":
            flip = not flip
        elif qlist[j][1] == "1":
            if flip:
                s.append(qlist[j][2])
            else:
                s.appendleft(qlist[j][2])
        elif qlist[j][1] == "2":
            if flip:
                s.appendleft(qlist[j][2])
            else:
                s.append(qlist[j][2])
    if not flip:
        print("".join(s))
    else:
        s.reverse()
        print("".join(s))
resolve()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
cnt = 0
l = ''
r = ''
for i in range(q):
    t = input()
    if t == '1':
        cnt = 1 - cnt
    else:
        t2, f, c = t.split()
        if cnt == 0:
            if f == '1':
                l = c + l
            else:
                r += c
        else:
            if f == '1':
                r += c
            else:
                l = c + l
                
if cnt == 0:
    print(l+s+r)
else:
    print(r[::-1]+s[::-1]+l[::-1])

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

#input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
    s =  input()
    querry = int(input())
    f = []
    l = []
    r = 0
    for _ in range(querry):
        q = input().split(' ')
        if(q[0] == '1'):
            r += 1
        else:
            if((int(q[1]) + r) % 2 == 0):
                l.append(q[2])
            else:
                f.append(q[2])
    for i in f[::-1] if r%2 == 0 else l[::-1]:
        print(i,end = '')
    print(s if r%2==0  else s[::-1],end = '')
    for i  in l if(r%2==0) else f:
        print(i,end='')
    print()

main()

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
d = deque(s)
reverseFlag = False


def getQuery():
    yield input().split()


for _ in range(q):
    for query in getQuery():
        if query[0] == "1":
            reverseFlag = not reverseFlag
        else:
            if query[1] == "1":
                if reverseFlag:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
            else:
                if reverseFlag:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])

ans = "".join(d)
if reverseFlag:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

rev_flag = False
for i in range(q):
    tmp_in = list(input().split())

    if tmp_in[0] == '1':
        if rev_flag:
            rev_flag = False
        else:
            rev_flag = True
    else:
        if rev_flag:
            if tmp_in[1] == '1':
                s.append(tmp_in[2])
            else:
                s.appendleft(tmp_in[2])
        else:
            if tmp_in[1] == '1':
                s.appendleft(tmp_in[2])
            else:
                s.append(tmp_in[2])

if rev_flag:
    s.reverse()

ans = "".join(s)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque


S = deque(list(input()))
Q = int(input())
f = True
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '2' and not f):
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = "".join(list(S))
if not f:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
#import time
S = input()
Q = int(input())
f = True
head = []
tail = []
for i in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        f = not f
    else:
        if (query[1] == '1' and f) or (query[1] == '2' and not f):
            head.append(query[2])
        else:
            tail.append(query[2])

S = ''.join(head)[::-1]+S+''.join(tail)
if not f:
    S = S[::-1]
print(S)

# start = time.time()
# elapsed_time = time.time() - start
# print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Accepted

Solution code:
s = input()
q = int(input())
cnt = 0
front = ''
rear = ''
for _ in range(q):
    t = input()
    if t[0] == '1':
        cnt += 1
    elif t[2] == '1':
        if cnt%2 == 1:
            rear = rear + t[4]
        else:
            front = front + t[4]
    else:
        if cnt%2 == 1:
            front = front + t[4]
        else:
            rear = rear + t[4]
if cnt%2 == 1:
    s = (front[::-1] + s + rear)[::-1]
else:
    s = front[::-1] + s + rear
print(s)

Submission status: Accepted

Solution code:
from collections import deque
S=input()
Q=int(input())
front=deque()
back=deque()

reverse=False
for i in range(Q):
  ins=input()
  if len(ins)==1:
    if reverse:
      reverse=False
    else:
      reverse=True
  else:
    q,f,c=map(lambda x: x, ins.split())
    
    if f=="1":
      if reverse:
        back.append(c)
        
      else:
        front.appendleft(c)
        
    else:
      if reverse:
        front.appendleft(c)
      else:
        back.append(c)
        
back=list(back)
front=list(front)
if reverse:
  back=back[::-1]
  front=front[::-1]
  S=list(S)[::-1]
  print("".join(back)+"".join(S) +"".join(front))
else:
  print("".join(front)+S+"".join(back))
  
#print(reverse)
#print(front, back)

Submission status: Accepted

Solution code:
from collections import deque
S=input()
Q=int(input())
ans=deque(S)
cnt=0
for _ in range(Q):
    i=(input().split())
    if(int(i[0]) == 1):
        cnt= (cnt+1) % 2
    elif((cnt== 0 and i[1]=='1') or (cnt == 1 and i[1]=='2')):
        ans.appendleft(i[2])
    else:
        ans.append(i[2])
ans=''.join(ans)
print(ans[::-1]) if(cnt % 2 == 1) else print(ans)

Submission status: Accepted

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

head = ''
tail = ''
rev = False

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

    if Query_i[0] == '1':
        rev = not rev
    
    elif Query_i[0] == '2':
        F_i, C_i = Query_i[1], Query_i[2]

        if not rev and F_i == '1':
            head += C_i
        elif not rev and F_i == '2':
            tail += C_i
        elif rev and F_i == '1':
            tail += C_i
        elif rev and F_i == '2':
            head += C_i

res = ''
if not rev:
    for c in reversed(head):
        res += c
    res += S
    res += tail
else:
    for c in reversed(tail):
        res += c
    for c in reversed(S):
        res += c
    res += head

print(res)