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:
#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def main():
    import sys
    input = sys.stdin.readline
    s = input().rstrip("\n")
    q = int(input())
    
    qq = [input().split() for i in range(q)]
    cnt=0
    r=""
    l=""
    #print(qq)
    for que in qq:
        if que[0] == "1":
            #print("a")
            r,l=l,r
            cnt+=1
            #print(r,l,cnt)
        else:
            if que[1] == "1":
                l+=que[2]
            else:
                r+=que[2]
            #print(r,l, que)
    #print(r,l)
    if cnt%2==0:
        print(l[::-1]+s+r)
    else:
        print(l[::-1]+s[::-1]+r)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = [0]*(q+5)
arr=[]
l=0
r=0

for i in range(q):
    t=input().split()
    if t[0]=='1':
        r+=1
        cnt[0]+=1 
        cnt[l]-=1 
    else:
        arr.append([t[2],int(t[1])])
        l+=1 
if r%2==1:
    s=s[::-1]
sm=0
for i in range(len(arr)):
    sm+=cnt[i]
    if sm%2==1:
        if arr[i][1]==1:arr[i][1]=2
        elif arr[i][1]==2:arr[i][1]=1 
ans1=""
ans2=""
for i in range(len(arr)):
    if arr[i][1]==1:
        ans1+=arr[i][0]

    else:ans2+=arr[i][0]
print("%s%s%s"%(ans1[::-1],s,ans2))

Submission status: Accepted

Solution code:
from collections import deque
S=deque(list(input()))
Q=int(input())
ju=True
for _ in range(Q):
  l=list(input().split())
  if l[0]=="1":
    if ju:
      ju=False
    else:
      ju=True
  else:
    if ju:
      if l[1]=="1":
        S.appendleft(l[2])
      else:
        S.append(l[2])
    else:
      if l[1]=="1":
        S.append(l[2])
      else:
        S.appendleft(l[2])
if ju:
  while S:
    t=S.popleft()
    print(t,end="")
else:
  while S:
    t=S.pop()
    print(t,end="")

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
right_side = deque()
left_side = deque()
is_reversed = False
for i in range(q):
    l = list(input().split())
    if l[0] == '1':
        is_reversed = not is_reversed
    else:
        if l[1] == '2':
            if is_reversed == False:
                right_side.append(l[2])
            else:
                left_side.appendleft(l[2])
        else:
            if is_reversed == False:
                left_side.appendleft(l[2])
            else:
                right_side.append(l[2])
ans = ''.join([''.join(left_side),s ,''.join(right_side)])
if is_reversed :
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

input = sys.stdin.readline


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

    prefix = deque()
    surfix = deque()
    prefix_append = prefix.appendleft
    surfix_append = surfix.append
    is_reversed = False
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            if q[1] == "1":
                if is_reversed:
                    surfix_append(q[2])
                else:
                    prefix_append(q[2])
            else:
                if is_reversed:
                    prefix_append(q[2])
                else:
                    surfix_append(q[2])

    ans = "".join(["".join(prefix), S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    prefix = deque()
    surfix = deque()
    prefix_append = prefix.appendleft
    surfix_append = surfix.append
    is_reversed = False
    for q in query:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            F, C = q[1], q[2]
            if F == "1":
                if is_reversed:
                    surfix_append(C)
                else:
                    prefix_append(C)
            else:
                if is_reversed:
                    prefix_append(C)
                else:
                    surfix_append(C)

    ans = "".join(["".join(prefix), S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    prefix = []
    surfix = []
    prefix_append = prefix.append
    surfix_append = surfix.append
    is_reversed = False
    for q in query:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            F, C = q[1], q[2]
            if F == "1":
                if is_reversed:
                    surfix_append(C)
                else:
                    prefix_append(C)
            else:
                if is_reversed:
                    prefix_append(C)
                else:
                    surfix_append(C)

    ans = "".join(["".join(prefix)[::-1], S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    S = input().rstrip()
    Q = int(input())
    query = [None] * Q
    for i in range(Q):
        query[i] = input().split()

    prefix = []
    surfix = []
    is_reversed = False
    for q in query:
        if q[0] == "1":
            is_reversed = not is_reversed
        else:
            F, C = q[1], q[2]
            if F == "1":
                if is_reversed:
                    surfix.append(C)
                else:
                    prefix.append(C)
            else:
                if is_reversed:
                    prefix.append(C)
                else:
                    surfix.append(C)

    ans = "".join(["".join(prefix)[::-1], S, "".join(surfix)])
    if is_reversed:
        ans = ans[::-1]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

    reverse = False
    pre = ""
    post = ""
    for _ in range(q):
        line_ls = input().split()
        # 長さが1ならTi=1
        if len(line_ls) == 1:
            reverse = not reverse
            continue

        fi = line_ls[1]
        ci = line_ls[2]
        if fi == "1":
            if not reverse:
                pre += ci
            else:
                post += ci
        else:
            if not reverse:
                post += ci
            else:
                pre += ci

    if reverse:
        s = s[::-1]
        post = post[::-1]
        print(post + s + pre)
    else:
        pre = pre[::-1]
        print(pre + s + post)


main()

Submission status: Accepted

Solution code:
from collections import deque
queue = deque(input())
q = int(input())
li = [list(input().split()) for _ in range(q)]

flag = 1
for i in range(q):
    if int(li[i][0]) == 1:
        flag *= -1
        continue

    if int(li[i][1]) == 1:
        if flag == 1:
            queue.appendleft(li[i][2])
        else:
            queue.append(li[i][2])
    else:
        if flag == 1:
            queue.append(li[i][2])
        else:
            queue.appendleft(li[i][2])

if flag == 1:
    print("".join(queue))
else:
    ans = list(queue)
    print("".join(ans[::-1]))

Submission status: Accepted

Solution code:
from collections import deque
def myAnswer(S:deque,Q:int,querys:list) -> str:
   setF = {1,2}
   reverse = False
   for query in querys:
      command = int(query[0])
      if(command == 1):
         reverse = not reverse
      else:
         F = int(query[1])
         C = query[2]
         if(reverse):
            F = set(setF - {F}).pop()
         S.appendleft(C) if(F == 1) else S.append(C)

   if(reverse):S.reverse()
   return "".join(S)

def modelAnswer():
   return
def main():
   S = deque(input())
   Q = int(input())
   querys = [list(map(str,input().split())) for _ in range(Q)]
   print(myAnswer(S,Q,querys))



if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:

from collections import deque

class abc_158:
    
    def __init__(self):
        pass
    
    def A(self):
        S=input()
        if "BA" in S or "AB" in S:print("Yes")
        else:print("No")
    
    def B(self):
        NAB=list(map(int,input().split()))
        
        if NAB[0]<=NAB[1]:ans=NAB[0]
        elif NAB[0]<=NAB[1]+NAB[2]:ans=NAB[1]
        else:
            tail = NAB[0]%(NAB[1]+NAB[2]) if NAB[0]%(NAB[1]+NAB[2])<NAB[1] else NAB[1]
            ans=(int(NAB[0]/(NAB[1]+NAB[2]))*NAB[1])+tail
            
        print(int(ans))
            
    def C(self):
        AB=list(map(int,input().split()))
        ans=-1
        
        #A<B<100
        for i in range(1,1001):
            if AB[0]==int(i*0.08) and AB[1]==int(i*0.1):
                ans=i
                break
            else:continue
        print(ans)
            
    def D(self):
        S=input()
        Q=int(input())
        Q_list=[list(input().split()) for i in range(Q)]
        d=deque()
        top=True
        end=False
        count_revers=0
        
        d.append(S)
        for i in Q_list:
            if int(i[0])==1:
                count_revers+=1
                if top==True:
                    top=False
                    end=True
                else:
                    top=True
                    end=False
            else:
                if int(i[1])==1 :
                    if top==True:d.appendleft(i[2])
                    else:d.append(i[2])
                else :
                    if top==True:d.append(i[2])
                    else:d.appendleft(i[2])
                
        ans = "".join(list(d)) if count_revers%2==0 else  "".join(list(d))[::-1]
        print(ans)
        
    def E(self):
        pass
    
    def F(self):
        pass
    
    
if __name__=='__main__':
    obj=abc_158()
    obj.D()

Submission status: Accepted

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

tail = ''
head = ''
reverted = False

for _ in range(n):
    q = input().split()
    if q[0] == '1':
        reverted = not reverted
    else:
        if not reverted:
            if q[1] == '1':
                tail = q[2] + tail
            
            elif q[1] == '2':
                head  = head + q[2]
        
        elif reverted:
            if q[1] == '1':
                head  = head + q[2]
            elif q[1] == '2':
                tail = q[2] + tail

ans = tail+string+head
if not reverted:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

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

from collections import deque

flip = False
deq = deque(s)

for _ in range(q):
    query = input().split(' ')
    if len(query)==1:
        flip = not flip
    else:
        f,c = int(query[1])-1,query[2]
        if flip ^ f:
            deq.append(c)
        else:
            deq.appendleft(c)

if flip:
    deq.reverse()

res =""
for c in deq:
    res+=c

print(res)

Submission status: Accepted

Solution code:
from collections import deque

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

for _ in range(Q):
  q = input()
  
  if q[0] == "1":
    Flag *= -1
  
  if q[0] == "2":
    if Flag > 0:
      if q[2] == "1":
        S.appendleft(q[4])
      else:
        S.append(q[4])
    else:
      if q[2] == "1":
        S.append(q[4])
      else:
        S.appendleft(q[4])
  
if Flag < 0:
  S.reverse()

print("".join(S))

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 998244353
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline
S = S()
S = [S[i] for i in range(len(S))]
QS= deque(S)
S = QS
Q = I()
rev = False
for i in range(Q):
    q = LS()
    if q[0] == '1':
        rev = not rev
        continue
    if (q[1] == '1') ^ rev:
        S.appendleft(q[2])
    else:
        S.append(q[2])
if rev:
    ans = ''.join(reversed(S))
else:
    ans = ''.join(S)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def check(reverse_cnt, t):

    if reverse_cnt%2 == 0 and t == 1:
        return True
    elif reverse_cnt%2 == 0 and t == 2:
        return False
    elif reverse_cnt%2 == 1 and t == 1:
        return False
    elif reverse_cnt%2 == 1 and t == 2:
        return True

def solve(s, q, qs):
    reverse_cnt = 0

    for item in qs:

        if len(item) == 1:
            # T = 1 文字列を反転
            reverse_cnt += 1
        else:
            # T = 2

            if check(reverse_cnt, int(item[1])):
                # 先頭に追加
                s.appendleft(item[2])

            else:
                # 末尾に追加
                s.append(item[2])


    if reverse_cnt%2 == 1:
        ans = "".join(s)
        ans = ans[::-1]
    else:
        ans = "".join(s)

    return ans

if __name__ == '__main__':

    s = deque() # queue

    str = input()
    q = int(input())
    qs = [list(input().split()) for i in range(q)]

    s.append(str)

    ans = solve(s, q, qs)
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
S=sys.stdin.readline().strip()
Q=int(sys.stdin.readline().strip())

cnt=0   #反転した回数
deq1=deque()
deq2=deque()

for _ in range(Q):
    t=sys.stdin.readline().split()
    if t[0]=="1": #反転
        cnt+=1      #反転回数をカウント
    elif t[0]=="2": #文字列挿入
        qtype,front_or_back,c=t[0],int(t[1]),t[2]
        if cnt%2==0:    #反転回数が偶数の場合
            if front_or_back==1:    #前に足す
                deq1.appendleft(c)
            elif front_or_back==2:  #後ろに足す
                deq2.append(c)
        else:   #反転回数が奇数の場合
            if front_or_back==1:    #前に足す
                deq2.append(c)      #反転しているので、deq2が前に来ている
            elif front_or_back==2:  #後ろに足す
                deq1.appendleft(c)  #反転しているので、deq1が前に来ている

s_deq1=""
for x in deq1:
    s_deq1+=x

s_deq2=""
for x in deq2:
    s_deq2+=x

if cnt%2==0:    #反転回数が偶数の場合
    print s_deq1+S+s_deq2
else:    #反転回数が奇数の場合
    print s_deq2[::-1]+S[::-1]+s_deq1[::-1]

Submission status: Accepted

Solution code:
import collections
from functools import reduce

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

deque = collections.deque(S)

is_reverse = False
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if (query[1] == '1' and not is_reverse) or (query[1] =='2' and is_reverse):
            deque.appendleft(query[2])
        else:
            deque.append(query[2])
if is_reverse:
    deque.reverse()

res = list(deque)

print("".join(res))

Submission status: Accepted

Solution code:
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# from fractions import gcd
# from math import gcd

# def lcm(n, m):
#     return int(n * m / gcd(n, m))


# def coprimize(p, q):
#     common = gcd(p, q)
#     return (p // common, q // common)


# def find_gcd(list_l):
#     x = reduce(gcd, list_l)
#     return x


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def main():
    #
    s0 = input().strip()
    q = int(input().strip())

    #
    stc1 = []
    stc2 = []
    flag = 0

    # def f(flag1, flag2, val):
    #     if flag1 ^ flag2:
    #         stc1 += val
    #     else:
    #         stc2 += val

    #
    for i in range(q):
        t, *fc = map(lambda x: int(x) if x.isdecimal() else x, input().strip().split())
        # eprint('t,fc ', end=':\n')
        # eprint(t, fc, "\n")

        if t == 2:    # 文字列付加
            # f(fc[0], flag, fc[1])
            if (fc[0]-1)^flag==1:  # fc[0]は前方/後方， flagは非反転/反転，をそれぞれ表す
                stc2.append(fc[1])
            else:
                stc1.append(fc[1])
        else:       # 反転命令
            flag = flag ^ 1

    #
    if flag == 0:
        ans ="".join(stc1[::-1] + list(s0) + stc2)
    else:
        ans ="".join( stc2[::-1] + list(s0)[::-1] + stc1)
    print(ans)
    # eprint(ans[::-1])
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

head = ""
tail = ""

reverse = False

for i in range(Q):
    t = input()

    if t[0] == "1":
        reverse = not reverse
    else:
        if t[2] == "1":  # add to the head
            if(reverse):
                tail = tail + t[4]
            else:
                head = t[4] + head
        else:  # add to the tail
            if(reverse):
                head = t[4] + head
            else:
                tail = tail + t[4]

ans = head + s + tail

if(reverse):
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
S=(input())
Q=int(input())
cnt_re=0
s=''
for i in range(Q):
    q=list(input().split())
    if q[0]=='1':
        cnt_re+=1
    else:
        if (int(q[1])+cnt_re)%2==1:
            s=s+q[2]
        else:
            S=S+q[2]
if cnt_re%2==1:
    print(S[::-1]+s)
else:
    print(s[::-1]+S)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
count = 0
top = ""
end = ""
ans = s
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        count += 1
    else:
        _, f, c = map(str, tmp.split())
        if (count % 2 == 0 and f == "1") or (count % 2 == 1 and f == "2"):
            top = c + top
        else:
            end = end + c

ans = top + ans + end
ans = ans[::-1] if count % 2 == 1 else ans
print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
count = 0
top = ""
end = ""
ans = s
for i in range(q):
    tmp = input()
    if tmp[0] == "1":
        count += 1
    else:
        _, f, c = map(str, tmp.split())
        if count % 2 == 0:
            if f == "1":
                top = c + top
            else:
                end = end + c
        else:
            if f == "1":
                end = end + c
            else:
                top = c + top

ans = top + ans + end
ans = ans[::-1] if count % 2 == 1 else ans
print(ans)

Submission status: Accepted

Solution code:
# D - String Formation

from collections import deque

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

que = deque()
for s in S:
    que.append(s)

is_reverse = False

for _ in range(Q):
    query = [q for q in input().split()]
    t = int(query[0])
    if t == 1:
        is_reverse = not is_reverse
    elif t == 2:
        f, c = int(query[1]), str(query[2])
        if f == 1:
            if not is_reverse:
                que.appendleft(c)
            else:
                que.append(c)
        elif f == 2:
            if not is_reverse:
                que.append(c)
            else:
                que.appendleft(c)

if not is_reverse:
    ans = ''.join(list(que))
else:
    ans = ''.join(reversed(list(que)))

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
isForward = True
for _ in range(Q):
  v = input().split()
  if v[0] == '1':
    isForward = (isForward == False)
    continue
  if isForward:
    if v[1] == '1':
      S.appendleft(v[2])
    else:
      S.append(v[2])
  else:
    if v[1] == '1':
      S.append(v[2])
    else:
      S.appendleft(v[2])
if isForward == False:
  S = reversed(S)
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = str(input())
Q = int(input())
Qer = [list(map(str,input().split())) for i in range(Q)]
flag = True
anss = deque(S)
for i in range(Q):
    if Qer[i][0] == "1":
        flag = not flag
    else:
        if flag ==True:
            if Qer[i][1] =="1":
                anss.appendleft(Qer[i][2])
            else:
                anss.append(Qer[i][2])
        else:
            if Qer[i][1] =="2":
                anss.appendleft(Qer[i][2])
            else:
                anss.append(Qer[i][2])
anss=''.join(anss)
if flag == True:
    print(anss)
else:
    anss = anss[::-1]
    print(anss)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
status = 1
top = []
bottom = []
for i in range(q):
    query = [i for i in input().split()]
    if query[0] == "1":
        status *= -1
    else:
        if (query[1] == "1" and status == 1) or (query[1] == "2" and status == -1):
            top.append(query[2])
        else:
            bottom.append(query[2])

tmp = "".join(top[::-1]) + s + "".join(bottom)
print(tmp if status == 1 else tmp[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

rev = False
for i in range(q):
    query = input().split()
    if query[0] == "1":
        rev ^= True
    else:
        if (query[1] == "1" and not rev) or (query[1] == "2" and rev):
            s.appendleft(query[2])
        else:
            s.append(query[2])

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

Submission status: Accepted

Solution code:
from collections import deque

s = input()
s = list(s)
q = int(input())
query = list(list(input().split()) for _ in range(q))

d = deque(s)

rs = False

for i in range(q):
    qry = query[i]
    if qry[0] == '1':
        rs = not rs
    else:
        if qry[1] == '1':
            if not rs:
                d.appendleft(qry[2])
            else:
                d.append(qry[2])
        else:
            if not rs:
                d.append(qry[2])
            else:
                d.appendleft(qry[2])
                
d = list(d)
if rs:
    d.reverse()
                
d = ''.join(d)
print(d)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
a = 0
c = ""
for i in range(q):
    b = list(input().split())
    if b[0] == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        if (a, b[1]) == (0, "1") or (a, b[1]) == (1, "2"):
            s.appendleft(b[2])
        else:
            s.append(b[2])
if a == 0:
    for i in range(len(s)):
        c += s.popleft()
else:
    for i in range(len(s)):
        c += s.pop()
print(c)

Submission status: Accepted

Solution code:
s = input()
s = list(s)
q = int(input())
query = list(list(input().split()) for _ in range(q))

rs_cnt = 0

head = []
tail = []

for i in range(q):
    qry = query[i]
    if qry[0] == '1':
        rs_cnt += 1
    else:
        if qry[1] == '1':
            if rs_cnt % 2 == 0:
                head.append(qry[2])
            else:
                tail.append(qry[2])
        else:
            if rs_cnt % 2 == 0:
                tail.append(qry[2])
            else:
                head.append(qry[2])
            
if rs_cnt % 2 == 0:
    head.reverse()
    head = ''.join(head)
    s = ''.join(s)
    tail = ''.join(tail)
    print(head + s + tail)
else:
    s.reverse()
    tail.reverse()
    head = ''.join(head)
    s = ''.join(s)
    tail = ''.join(tail)
    print(tail + s + head)

Submission status: Accepted

Solution code:
from collections import deque
S = list(input()) 
d = deque()
for s in S:
    d.append(s)
q = int(input())
o = 0

for _ in range(q):
    qu = list(input().split())
    if len(qu) == 1:
        o = -1 if o == 0 else 0
    if len(qu) == 3:
        if qu[1] == '1':
            d.appendleft(qu[2]) if o == 0 else d.append(qu[2])
        if qu[1] == '2':
            d.append(qu[2]) if o == 0 else d.appendleft(qu[2])

r = ''
if o == 0:
    for _ in range(len(d)):
        r += d.popleft()
else:
    for _ in range(len(d)):
        r += d.pop()

print(r)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque

s = input()
q = int(input())
deq = deque(s)
cur = 1
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cur ^= 1
    else:
        f, c = query[1:]
        f = int(f) - 1
        if cur ^ f:
            deq.appendleft(c)
        else:
            deq.append(c)
ans = ""
while len(deq) > 0:
    ans += deq.popleft() if cur else deq.pop()
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
rev_cnt = 0
for i in range(Q):
    query = [_ for _ in input().split()]
    if len(query) == 1:
        rev_cnt += 1
    else:
        if query[1] == '1':
            if rev_cnt%2 == 0:
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if rev_cnt%2 == 0:
                S.append(query[2])
            else:
                S.appendleft(query[2])
if rev_cnt % 2 == 0:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = deque(sr())
Q = ir()
rev = 0
for _ in range(Q):
    q = sr().split()
    if q[0] == '1':
        rev ^= 1
    else:
        _, f, c = q
        if (int(f)^rev)&1:
            S.appendleft(c)
        else:
            S.append(c)

if rev&1:
    S.reverse()

print(''.join(S))
# 06

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
Q = ir()
rev = 0
top = ''
tail = ''
# xor
for _ in range(Q):
    x = sr().split()
    if x[0] == '1':
        rev ^= 1
    else:
        _, f, c = x
        if (int(f)^rev) & 1:
            top += c
        else:
            tail += c

answer = top[::-1] + S + tail
if rev&1:
    answer = answer[::-1]
print(answer)
# 40

Submission status: Accepted

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

rev = 0
for i in range(q):
  tfc = list(input().split())
  if tfc[0] == "1":
    rev = abs(rev-1)
  else:
    t,f,c = tfc
    if rev:
      if f == "1":
        s.append(c)
      else:
        s.appendleft(c)
    else:
        if f == "1":
          s.appendleft(c)
        else:
          s.append(c)
if rev:
  t = []
  for i in range(len(s)):
    t.append(s.pop())
  print("".join(t))
else:
  print("".join(s))