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:
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]
toplist=[]
backlist=[]
Flag=1

i=0
while i<Q:
    if Qlist[i][0]=="1":
        Flag*=-1
    else:
        #check=Qlist[i][2]
        if Qlist[i][1]=="1" and Flag==-1:
            backlist.append(Qlist[i][2])
        elif Qlist[i][1]=="1" and Flag==1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==-1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==1:
            backlist.append(Qlist[i][2])
    i+=1
if Flag==1:
    #print(toplist)
    #print(S)
    #print(backlist)
    print("".join(toplist[::-1])+S+"".join(backlist))
else:
    #print(backlist[::-1])
    #print(S)
    #print(toplist[::-1])
    print("".join(backlist[::-1])+S[::-1]+"".join(toplist))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
cnt = 0
left = ''
right = ''
for i in range(q):
    query = list(input().split())
    if int(query[0])==1:
        cnt += 1
    elif int(query[0])==2:
        if int(query[1])==1:
            if cnt%2==0:
                left = query[2] + left
            else:
                right = right + query[2]
        elif int(query[1])==2:
            if cnt%2==0:
                right = right + query[2]
            else:
                left = query[2] + left

if cnt%2==0:
    print(left+s+right)
else:
    s=left+s+right
    s=s[::-1]
    print(s)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
cnt=0
l=''
r=''
for i in range(q):
  k=input()
  if k[0]=='1':
    cnt+=1
  else:
    if k[2]=='1':
      if cnt%2==0:
        l=k[4]+l
      else:
        r+=k[4]
    elif k[2]=='2':
      if cnt%2!=0:
        l=k[4]+l
      else:
        r+=k[4]
if cnt%2==0:
  s=l+s+r
else:
  s=l+s+r
  s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
q = INT()
rev = 0
begg = []
end = []
for _ in range(q):
     temp = input()
     if(len(temp.split())==1):
         rev+=1

     else:
         t,f,c = temp.split()
         if(f=="1"):
             if(rev%2==0):
                 begg.append(c)
                 # s = c + s
             else:
                 end.append(c)
                 # s = s + c
         else:
             if(rev%2==0):
                 end.append(c)
                 # s = s + c
             else:
                 begg.append(c)
                 # s = c + s
abc = "".join(begg[::-1]) + s + "".join(end)
if(rev%2==0):
    print(abc)
else:
    print(abc[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = deque(list(input()))
q = int(input())
flg = 1
for i in range(q):
    array = input().split(' ')
    if array[0] == '1':
        flg *= -1
    elif array[0] == '2':
        if array[1] == '1' and flg == 1 or array[1] == '2' and flg == -1:
            s.appendleft(array[2])
        else:
            s.append(array[2])

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

s = ''.join(s)
print(s)

Submission status: Accepted

Solution code:
from collections import deque

s=input()
s=deque(s)
q=int(input())
ans=''
flg=0
for _ in range(q):
	query=list(map(str,input().split()))
	if query[0]=='1':
		flg=1-flg
	else:
		if flg:
			if query[1]=='1':
				s.append(query[2])
			else:
				s.appendleft(query[2])
		else:
			if query[1]=='1':
				s.appendleft(query[2])
			else:
				s.append(query[2])
if flg:
	t=[]
	for i in range(len(s)):
		t.append(s.pop())
	print(''.join(t))
else:
    print(''.join(s))

Submission status: Accepted

Solution code:
from sys import stdin
s = str(stdin.readline().rstrip())
#A, B, C = map(int, stdin.readline().rstrip().split())
#S = [list(map(int, stdin.readline().rstrip().split())) for _ in range(h)]#hの定義を忘れずに
q= int(stdin.readline().rstrip())
hanten = 0
t=""
u=""

for i in range(q):
    l = list(map(str, stdin.readline().rstrip().split()))
    if int(l[0]) == 1:
        t,u = u,t
        hanten ^= 1
    else:
        if int(l[1])==1:
            t+=l[2]
        else:
            u+=l[2]


if hanten==0:
    print(t[::-1]+s+u)
else:
    print(t[::-1]+s[::-1]+u)

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
b = deque(s)
swc = 0
for i in range(q):
	a = list(map(str, input().split()))
	if a[0] == "1":
		swc += 1
	elif a[1] == "1":
		if swc%2 == 0:
			b.appendleft(a[2])
		else:
			b.append(a[2])
	else:
		if swc%2 == 0:
			b.append(a[2])
		else:
			b.appendleft(a[2])
ans = ""
if swc%2 == 0:
	ans = "".join(list(b))
else:
	ans = "".join(reversed(list(b)))
print (ans)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    S = input()
    que = deque(S)
    Q = int(input())
    reverse = False
    for _ in range(Q):
        query = input()
        if len(query) == 1:
            reverse = not reverse
        else:
            _, F, C = query.split()
            if (not reverse and F == '1') or (reverse and F == '2'):
                que.appendleft(C)
            else:
                que.append(C)
    if reverse:
        que.reverse()
    print(''.join([c for c in que]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
from collections import deque
sys.setrecursionlimit(10**9)

def main():
    S = input()
    Q = int(input())
    d = deque(list(S))
    rev = False

    def insert(x,f):
        g = f == "2" # append
        if rev:
            g = not g

        if g:
            d.append(x)
        else:
            d.appendleft(x)


    for q in range(Q):
        inp = input()
        if inp == "1":
            rev = not rev
        else:
            _,F,C = inp.split()
            insert(C,F)


    if rev:
        d.reverse()

    print("".join(d))



if __name__ == "__main__":
  main()

Submission status: Accepted

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

rev = 0
head = []
tail = []

for i in range(q):
    t = input().split()
    if t[0] == "1":
        rev ^= 1
    else:
        if (int(t[1])-1)^rev == 0:
            head.append(t[2])
        else:
            tail.append(t[2])

h = "".join(head)
t = "".join(tail)

if rev == 0:
    print(h[::-1] + s + t)
else:
    s = s[::-1]
    print(t[::-1] + s + h)

Submission status: Accepted

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

count = 0
top = []
bottom = []

for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    count = (count + 1) % 2  
  if query[0] == "2":    
    if query[1] == "1":      
      if count == 0:        
        top.append(query[2])    
      else:
        bottom.append(query[2])
    if query[1] == "2":      
      if count == 0:        
        bottom.append(query[2])        
      else:       
        top.append(query[2])
  
t = "".join(top)
b = "".join(bottom)

lst = [t[::-1], s, b]
ans = "".join(lst)

if count == 1:
  ans = ans[::-1]
  
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
query = [0]*Q

for i in range(Q):
    inp = input()
    if inp == "1":
        query[i] = "1"
    else:
        query[i] = list(inp.split())

left_head_if_even = 0
right = ""
left = ""

for i in range(Q):
    if query[i] == "1":
        left_head_if_even += 1
    else:
        if query[i][1] == "1":
            if left_head_if_even%2 == 0:
                left = query[i][2] + left
            else:
                right = right + query[i][2]

        else:
            if left_head_if_even%2 == 1:
                left = query[i][2] + left
            else:
                right = right + query[i][2]

if left_head_if_even%2==0:
    S = left + S + right
else:
    S = left + S + right
    S = S[::-1]

print(S)

Submission status: Accepted

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

S = input()
Q = int(input())
query = []
flag = 1
before = ""
after  = ""

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        flag *= -1
    else:
        if (query[1] == "1" and flag == 1) or  (query[1] == "2" and flag ==-1):
            before += query[2]
        else:
            after += query[2]
        #print(before,after)
if flag == 1:
    print(''.join(list(reversed(before))) + S + after)
else:
    print(''.join(list(reversed(after))) + ''.join(list(reversed(S))) + before)

Submission status: Accepted

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

u = []
for i in range(q):
    u.append(list(map(str,input().split())))

from collections import deque
l = deque([])
r = deque([])

x = 1
for i in range(q):
    if int(u[i][0]) == 1:
        x = x*(-1)
    else:
        if int(u[i][1])*x == 1 or int(u[i][1])*x == -2:
            l.append(u[i][2])
        else:
            r.append(u[i][2])
if x == 1:
    print(''.join(l)[::-1]+s[::x]+''.join(r))
else:
    print(''.join(r)[::-1]+s[::x]+''.join(l))

Submission status: Accepted

Solution code:
from collections import deque

ans = deque(input())
Q = int(input())
state = True
for _ in range(Q):
    Query = input().split()
    if int(Query[0]) == 1:
        state = not state
    else:
        if int(Query[1]) == 1:
            if state:
                ans.appendleft(Query[2])
            else:
                ans.append(Query[2])
        if int(Query[1]) == 2:
            if state:
                ans.append(Query[2])
            else:
                ans.appendleft(Query[2])

if state:
    print("".join(map(str, ans)))
else:
    print("".join(map(str, reversed(ans))))

Submission status: Accepted

Solution code:
import collections
S = input()
dq = collections.deque(S)
Q = int(input())
q = []
hanten = False
for _ in range(Q):
    q = input().split(" ")
    if q[0] == '1':
        hanten = not hanten
    else:
        if q[1] == '1' and not hanten:
            dq.appendleft(q[2])
        elif q[1] == '1' and hanten:
            dq.append(q[2])
        elif q[1] == '2' and not hanten:
            dq.append(q[2])
        else:
            dq.appendleft(q[2])
if not hanten:
    print(''.join(dq))
else:
    print(''.join(dq)[::-1])

Submission status: Accepted

Solution code:
import collections

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

s2 = collections.deque(s)

inn = []
jun = 0

for i in range(q):
    inn = list(input().split())
    if inn[0] == "1":
        if jun == 0:
            jun = 1
        else:
            jun = 0#jun = 0はそのまま　jun = 1は逆をする
    elif inn[0] == "2":
        if inn[1] == "1" and jun == 0:
            s2.appendleft(inn[2])
        elif inn[1] == "2" and jun == 0:
            s2.append(inn[2])
        if inn[1] == "1" and jun == 1:
            s2.append(inn[2])
        elif inn[1] == "2" and jun == 1:
            s2.appendleft(inn[2])
    
if jun == 0:
    news = ''.join(s2)
else:
    s2.reverse()
    news = ''.join(s2)

print(news)

Submission status: Accepted

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


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

# a/b の切り上げ


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    s = input()
    q = int(input())
    Q = []
    cnt = 0
    head = []
    tail = []
    for _ in range(q):
        Q.append(list(map(str, input().split())))
    # print(Q)
    for i in range(q):
        if Q[i][0] == '1':
            cnt += 1
            head, tail = tail, head

        else:
            if Q[i][1] == '1':
                head.append(Q[i][2])
            else:
                tail.append(Q[i][2])
    print("".join(head[::-1]) + (s[::-1] if cnt %
                                 2 == 1 else s) + "".join(tail))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
cnt = 1
num = 0
for i in range(Q):
    TFC = input()
    if TFC == "1":
        cnt *= -1
        num += 1
    else:
        if TFC[:3]== "2 1":
            if cnt ==1:
                S.appendleft(TFC[4])
            else:
                S.append(TFC[4])
        else:
            if cnt ==1:
                S.append(TFC[4])
            else:
                S.appendleft(TFC[4])
if num % 2 ==1:
    print(("".join(reversed([i for i in S]))))
else:
    print(("".join([i for i in S])))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Switch = False
Query = []

import collections
dq = collections.deque(S)

for i in range(Q):
    s = input()
    if s[0] == '1':
        if Switch:
            Switch = False
        else:
            Switch = True
    if s[0] == '2':
        if s[2] == '1' and Switch==False:
            dq.appendleft(s[4])
#             S = s[4]+S
        elif s[2]=='1' and Switch==True:
            dq.append(s[4])
#             S = S+s[4]
        elif s[2] == '2' and Switch==False:
            dq.append(s[4])
#             S = S+s[4]
        else:
#             S = s[4]+S
            dq.appendleft(s[4])

if Switch == False:
    print(''.join(dq))
else:
    print(''.join(dq)[::-1])

Submission status: Accepted

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

Queries = []
for i in range(Q):
    Queries.append(list(input().split()))

rev = False
head = ""
tail = ""

# listの最初に要素を追加するのはO(N), dequeを使うという選択肢

for query in Queries:
    if query[0] == '1':
        tmp = head
        head = tail
        tail = tmp
        rev = 1 - rev  # True/Falseを入れ替える

    if query[0] == '2':
        if query[1] == '1':
            head += query[2]
        if query[1] == '2':
            tail += query[2]

if rev:
    S = S[::-1]
S = head[::-1] + S + tail
print(S)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
F=0
d=deque(S)
for i in range(Q):
  q=list(map(str,input().split()))
  if q[0]=="1":
    F+=1
    F%=2
  else:
    if q[1]=="1":
      if F==0:
        d.appendleft(q[2])
      else:
        d.append(q[2])
    else:
      if F==0:
        d.append(q[2])
      else:
        d.appendleft(q[2])  
d=list(d)
if F%2==0:
  print("".join(d))
else:
  print("".join(d[::-1]))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
#import queue
from copy import copy, deepcopy
from operator import itemgetter

import bisect#2分探索
#bisect_left(l,x), bisect(l,x)
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
#dequeを使うときはpython3を使う、pypyはダメ
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#q=heapq.heapify(l),heappush(q,a),heappop(q)

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s): return sorted(range(len(s)), key=lambda k: s[k])

#mod = 10**9+7

S = input()
Q=int(input())
#N, K = map(int, input().split())
#L = [int(input()) for i in range(N)]
#A = list(map(int, input().split()))
#S = [inputl() for i in range(H)]
#q = queue.Queue() #q.put(i) #q.get() 
#q = queue.LifoQueue() #q.put(i) #q.get() 

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b=copy.deepcopy(a)  #2次元配列はこうコピーする
#w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

#素数リスト
# n = 100
# primes = set(range(2, n+1))
# for i in range(2, int(n**0.5+1)):
#     primes.difference_update(range(i*2, n+1, i))
# primes=list(primes)

#bisect.bisect_left(a, 4)#aはソート済みである必要あり。aの中から4未満の位置を返す。rightだと以下
#bisect.insort_left(a, 4)#挿入
qs=[]
s=deque(list(S))
for i in range(Q):
    qs.append(list(input().split()))

rev=0
for q in qs:
    if int(q[0])==1: rev=(rev+1)%2
    else:
        if (int(q[1])+rev)%2==1:
            s.appendleft(q[2])
        else:
            s.append(q[2])
    #print(s,rev)

if rev==0:
    for i in range(len(s)): print(s.popleft(), end="")
else:
    for i in range(len(s)): print(s.pop(), end="")
print()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
ans_list1 = []
ans_list2 = []
flag = 1
for i in range(q):
    i = input().split()
    if i[0]=="1":
        flag *= -1
    else:
        if i[1]=='1':
            if flag==1:
                ans_list1.append(i[2])
            else:
                ans_list2.append(i[2])
        else:
            if flag==1:
                ans_list2.append(i[2])
            else:
                ans_list1.append(i[2])

if flag==1:
    print(''.join(ans_list1[::-1]) + s +''.join(ans_list2))
else:
    print(''.join(ans_list2[::-1]) + s[::-1] +''.join(ans_list1))

Submission status: Accepted

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

import collections
dq = collections.deque(s)

rev = 1
for i in Q:
    if i == ['1']:
        rev *= -1
    else:
        if (i[1] == '1' and rev == 1) or (i[1] == '2'and rev == -1):
            dq.appendleft(i[2])
        else:
            dq.append(i[2])

if rev == 1:
    print(''.join(dq))
else:
    print(''.join(dq)[::-1])

Submission status: Accepted

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

states=1
front=[]
back=[]
for n in range(Q):
    query=input()
    if query=="1":
        states*=-1
    else:
        N,F,C=query.strip().split()
        if int(F)==1:
            if states==1:
                front.append(C)
            else:
                back.append(C)
        else:
            if states==1:
                back.append(C)
            else:
                front.append(C)

if states==1:
    print("".join(front)[::-1]+S+"".join(back))
else:
    print("".join(back)[::-1]+S[::-1]+"".join(front))

Submission status: Accepted

Solution code:
word = input().rstrip()
n = int(input())
hanten = 0
lhantenlist = []
rlist = []
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        hanten = (hanten + 1) % 2
        # word = word[::-1]
        #print(word)
    else:
        if (a[1] == "2" and hanten == 0) or (a[1] == "1" and hanten == 1):
            rlist.append(a[2])
            #print("rtuika: ",rlist)
        else:
            lhantenlist.append(a[2])
            #print("ltuika: ",lhantenlist)

lhanten="".join(lhantenlist)
r="".join(rlist)

if hanten == 0:
    anslist=[lhanten[::-1],word,r]
    ans="".join(anslist)
else:
    anslist = [r[::-1], word[::-1], lhanten]
    ans="".join(anslist)
print(ans)

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    s = list(input())
    dq = deque()
    for i in s:
        dq.append(i)

    q = int(input())
    flag = True
    for _ in range(q):
        line = input().split(" ")
        if line[0] == "1":
            flag = not flag
        else:
            if line[1] == "1":
                if flag:
                    dq.appendleft(line[2])
                else:
                    dq.append(line[2])
            elif line[1] == "2":
                if flag:
                    dq.append(line[2])
                else:
                    dq.appendleft(line[2])
    if flag == False:
        dq.reverse()
    for i in list(dq):
        print(i,end="")
main()

Submission status: Accepted

Solution code:
from collections import deque
S=list(input())
Q=int(input())
query=[]
reverseflg=False
S=deque(S)

for i in range(Q):
  query.append(list(input().split()))

for q in range(Q):
  if query[q][0]=="1":
    reverseflg= not reverseflg
  
  if query[q][0]=="2":
    if query[q][1]=="1":
      if reverseflg== False:
        S.appendleft(query[q][2])
      else:
        S.append(query[q][2])
    
    else:
      if reverseflg== False:
        S.append(query[q][2])
      else:
        S.appendleft(query[q][2])

if reverseflg==True:
  S.reverse()
  
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S=list(input())
Q=int(input())
query=[]
reverseflg=False
S=deque(S)

for i in range(Q):
  query.append(list(input().split()))

for q in range(Q):
  if query[q][0]=="1":
    reverseflg= not reverseflg
  
  if query[q][0]=="2":
    if query[q][1]=="1":
      if reverseflg== False:
        S.appendleft(query[q][2])
      else:
        S.append(query[q][2])
    
    else:
      if reverseflg== False:
        S.append(query[q][2])
      else:
        S.appendleft(query[q][2])

if reverseflg==True:
  S.reverse()
  
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
deque = deque([s for s in S])
Q = int(input())
rev_flag = 0
for _ in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if rev_flag:
            rev_flag = 0
        else:
            rev_flag = 1
    else:
        if q[1] == "1":
            if rev_flag:
                deque.append(q[2])
            else:
                deque.appendleft(q[2])
        else:
            if rev_flag:
                deque.appendleft(q[2])
            else:
                deque.append(q[2])
if rev_flag:
    print("".join(deque)[::-1])
else:
    print("".join(deque))

Submission status: Accepted

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

for i in range(len(s)):
    q.append(s[i])

flag = True
for i in range(n):
    query = input()
    if query[0]=="1":
        flag = not flag
    else:
        if query [2]=="1":
            if flag:
                q.appendleft(query[4])
            else:
                q.append(query[4])
        else:
            if flag:
                q.append(query[4])
            else:
                q.appendleft(query[4])
if flag:
    print(''.join(q))
else:
    q.reverse()
    print("".join(q))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
que = deque()
que.append(S)
Q = int(input())
Front_flag = 1

for i in range(Q):
    Query = tuple(input().split())
    if len(Query) == 1:
        Front_flag = 1 - Front_flag
    else:
        a, b, c = Query
        if (Front_flag + int(b)) % 2 == 1:
            que.append(c)
        else:
            que.appendleft(c)

ans = ''.join(que)
if Front_flag:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

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

from collections import deque

S = deque(S)

tfc = [[] for _ in range(Q)]

swich = 0
for i in range(Q):
    tfc[i] = input().split()
    
    if int(tfc[i][0])==1:
        swich += 1
    else:
        if swich%2==0:
            if int(tfc[i][1])==1:
                S.appendleft(tfc[i][2])
            else:
                S.append(tfc[i][2])
        else:
            if int(tfc[i][1])==1:
                S.append(tfc[i][2])
            else:
                S.appendleft(tfc[i][2])

if swich%2==1:
    S = reversed(S)
    print("".join(S))
else:
    print("".join(S))

Submission status: Accepted

Solution code:
import sys
from collections import deque

s = input()
q = int(input())
ans = deque()
ans.append(s)
reverseFlg = False

for i in range(q):
    query = list(input().split())
    # print(query)

    if query[0]=='1':
        reverseFlg = not reverseFlg
    else:
        if reverseFlg:
            if query[1]=='1':
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
        else:
            if query[1]=='1':
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
    

out = ''.join(ans)

if reverseFlg:
    print(out[::-1])
else:
    print(out)

Submission status: Accepted

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

addPlace = 1
frontlist=[]
endlist=[]
for i in range(len(query)):
    if query[i][0] == "1":
        addPlace = -1 * addPlace
    else:
        if (query[i][1] == "1" and addPlace == 1) or (query[i][1] == "2" and addPlace == -1):
            frontlist.append(query[i][2])
        else:
            endlist.append(query[i][2])
s = frontlist[::-1] + s + endlist
if addPlace == 1:
    print("".join(s))
else:
    print("".join(s[::-1]))

Submission status: Accepted

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

from collections import deque

S=deque(list(S))

flg=0

for i in range(Q):

    Query = list(input().split())
    
    if len(Query)!=1:
        F_i ,C_i = int(Query[1]),Query[2]
        
        if F_i==1 and flg==0:
            S.appendleft(C_i)
        
        elif F_i==2 and flg==0:
            S.append(C_i)   
        
        elif F_i==1 and flg==1:
            S.append(C_i)
        else:
            S.appendleft(C_i)
    else:
        if flg ==0:
            flg=1
        else:
            flg=0 

if flg ==1:
    S=reversed(S)

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
d = deque(s)
q = int(input())
flag = True
for i in range(q):
  l = list(map(str,input().split()))
  if l[0] == '1':
    if flag:
      flag = False
    else:
      flag = True
  else:
    if l[1] == '1':
      if flag:
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if flag:
        d.append(l[2])
      else:
        d.appendleft(l[2])
if flag:
  print(''.join(d))
else:
  d.reverse()
  print(''.join(d))

Submission status: Accepted

Solution code:
from collections import deque
from bisect import bisect_left, bisect_right

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

count = 0
l = []
reverse = []
for i in range(q):
	tmp = list(map(str,input().split()))
	if len(tmp) == 1:
		count += 1
		if len(l) != 0:
			reverse.append(len(l))
	elif tmp[1] == '1':
		l.append([1, tmp[2]])
	elif tmp[1] == '2':
		l.append([2, tmp[2]])

#print(reverse)
#print(l)
for i in range(len(l)):
	#print(bisect_right(reverse,i))
	l[i][0] += len(reverse) - bisect_right(reverse,i)
	l[i][0] %= 2

if count % 2 == 1:
	s = s[::-1]
s = deque(s)

for i in range(len(l)):
	if l[i][0] == 0:
		s.append(l[i][1])
	else:
		s.appendleft(l[i][1])
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
d = deque(s)
q = int(input())
flag = True
for i in range(q):
  l = list(map(str,input().split()))
  if l[0] == '1':
    if flag:
      flag = False
    else:
      flag = True
  else:
    if l[1] == '1':
      if flag:
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if flag:
        d.append(l[2])
      else:
        d.appendleft(l[2])
if flag:
  ll = list(d)
  print(''.join(ll))
else:
  d.reverse()
  ll = list(d)
  print(''.join(ll))

Submission status: Accepted

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

def solve():
    S = input()
    ans = deque()
    ans.append(S)
    Q = input()
    reverseFlg = False
    for i in range(int(Q)):
        TFC = input()
        l = TFC.split()
        if l[0] == "1":
            reverseFlg = not reverseFlg
        else:
            if reverseFlg:
                if l[1] == "1":
                    ans.append(l[2])
                else:
                    ans.appendleft(l[2])
            else:
                if l[1] == "1":
                    ans.appendleft(l[2])
                else:
                    ans.append(l[2])
    
    res = ''.join(ans)
    if(reverseFlg):
        print(res[::-1])
    else:
        print(res)


            

    return 

def main():
    solve()

if __name__ == '__main__':
    main()