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
S=deque(input())
Q=int(input())
T=[]
m=0
for i in range(Q):
  T.append(input().split())
for i in range(Q):
  if T[i][0]=="1":
    if len(S)>1:
      if m==0:
        m=1
      else:
        m=0
        
  else:
    if T[i][1]=="1":
      if m==0:
        S.appendleft(T[i][2])
      else:
        S.append(T[i][2])
   
    else:
      if m==0:
        S.append(T[i][2])
      else:
        S.appendleft(T[i][2])
if m==0:
  print("".join(S))
else:
  S.reverse()
  print("".join(S))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#たのん
S=input()
Q=int(input())
head=[]
tail=[]
rev=False
for i in range(Q):
  tmp=list(input().split())
  if tmp[0]=='1':#文字列を反転させる操作のとき
    if rev==False:#反転していない状態であれば反転した状態にする
      rev=True
    else:
      rev=False#反転した状態であれば反転していない状態にする
  if tmp[0]=='2':#文字を追加する操作のとき
    if rev==False:#反転していない状態であれば、先頭への追加はそのまま先頭に、末尾への追加もそのまま末尾に追加する
      if tmp[1]=='1':
        head.append(tmp[2])
      else:
        tail.append(tmp[2])
    if rev==True:#反転した状態であれば、先頭への追加は末尾への追加に、末尾への追加は先頭への追加に置き換える
      if tmp[1]=='1':
        tail.append(tmp[2])
      else:
        head.append(tmp[2])
head=''.join(map(str,head))#文字列として結合しておく
tail=''.join(map(str,tail))

ans=head[::-1]+S+tail#答えは(先頭に追加した文字列の逆順)+(元の文字列)+(末尾に追加した文字列)となる
if rev==True:#もし最終的に文字列が反転した状態なら答えの文字列を反転させる
  ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
head=""
tail=""
rev=False

for _ in range(Q):
    q = input().split()
    if len(q)==1:
        rev = not rev
    else:
        if rev ^ (q[1]=="1"):
            head+=q[2]
        else:
            tail+=q[2]
if rev:
    print(tail[::-1]+S[::-1]+head)
else:
    print(head[::-1]+S+tail)

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
head=[]
tail=[]
rev=False
for i in range(Q):
  tmp=list(input().split())
  if tmp[0]=='1':
    if rev==False:
      rev=True
    else:
      rev=False
  if tmp[0]=='2':
    if rev==False:
      if tmp[1]=='1':
        head.append(tmp[2])
      else:
        tail.append(tmp[2])
    if rev==True:
      if tmp[1]=='1':
        tail.append(tmp[2])
      else:
        head.append(tmp[2])
head=''.join(map(str,head))
tail=''.join(map(str,tail))

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

Submission status: Accepted

Solution code:
from collections import deque
S = input()
d = deque()
for s in S:
  d.append(s)
flag = True # 正順ならTrue, 逆順ならFalse
for i in range(int(input())):
  query = input().split()
  if query[0] == '1':
    flag = not flag
  if query[0] == '2':
    if query[1] == '1':
      if flag:
        d.appendleft(query[2])
      if not flag:
        d.append(query[2])
    if query[1] == '2':
      if flag:
        d.append(query[2])
      if not flag:
        d.appendleft(query[2])
if flag:
  for i in d:
    print(i, end='')
if not flag:
  for i in reversed(d):
    print(i, end='')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s=input()
q=int(input())
direction=1
#↑で1は右向き2は左向きに対応。
migi_matu=''
hidari_matu=''
#↑リストにしてしまうと後で文字sと連結できなくなる。
for i in range(q):
  k=input().split()
  #k[0] is Ti, k[1] is Fi, k[2] is character
  if k[0]=='1':
    direction*=(-1)
  if k[0]=='2':
    if k[1]=='1':
      if direction==1:
        hidari_matu+=k[2]
      if direction==-1:
        migi_matu+=k[2]
    else:
      if direction==1:
        migi_matu+=k[2]
      if direction==-1:
        hidari_matu+=k[2]
#  print('入力は',k,'とする。このとき')
#  print(i,'回目の操作後')
#  print(direction)
#  if direction==1:
#    print(hidari_matu[::-1]+s+migi_matu)
#  if direction==-1:
#    print(migi_matu[::-1]+s[::-1]+hidari_matu)
  
  
if direction==1:
  print(hidari_matu[::-1]+s+migi_matu)
if direction==-1:
  print(migi_matu[::-1]+s[::-1]+hidari_matu)

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
front = []
back = []
ifZyun = True

for _ in range(Q):
    query = input()
    if query == "1":
        ifZyun = not ifZyun
    else:
        query = list(query)
        F = query[2]
        C = query[4]
        if (F == "1" and ifZyun) or (F == "2" and not ifZyun):
            front.append(C)
        else:
            back.append(C)

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

Submission status: Accepted

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

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

a=[]
for i in range(len(s)):
    a+=s.popleft()    
if rev<0:
    a=a[::-1]
print(''.join(a))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
k = 1
d = deque(S)
for _ in range(Q):
    query = input()
    if query.startswith('1'):
        k *= -1
    elif query.startswith('2'):
        t, f, c = query.split()
        if f == '1':
            if k > 0:
                d.appendleft(c)                
            else:
                d.append(c)
        elif f == '2':
            if k > 0:
                d.append(c)
            else:
                d.appendleft(c)
d = list(d)
if k > 0:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
q = int(input())
flag = True
p = ''
m = ''
for i in range(q):
    qfc = input()
    if qfc[0] == '1':
        if flag:
            flag = False
        else:
            flag = True
    else:
        if qfc[2] == '1':
            if flag:
                p += qfc[4]
            else:
                m += qfc[4]
        else:
            if flag:
                m += qfc[4]
            else:
                p += qfc[4]
if flag:
    print(p[::-1] + s + m)
else:
    print(m[::-1] + s[::-1] + p)

Submission status: Accepted

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

s = input()
head = 1 #1:forward, -1: reverse

q = int(input())
sl = [ s[i] for i in range(len(s)) ]
sq = deque(sl)


for i in range(q):
    ql = input()
    if ql[0] == '1':
        head *= -1
    elif ql[0] == '2':
        (op, f, c) = ql.split()
        ht = 1
        if f == '1':
            ht = head
        else:
            #f == '2':
            ht = head * -1

        if ht == 1:
            sq.appendleft(c)
        else:
            sq.append(c)


if head == 1:
    print(''.join(sq))
else:
    print(''.join(reversed(sq)))

Submission status: Accepted

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

flipped = False
head = ''
tail = ''

for _ in range(Q):
    tmp = input()
    if tmp[0] == '1':
        flipped = not flipped
    else:
        _, f, c = tmp.split()

        if (f == '1' and not flipped) or (f == '2' and flipped):
            head += c
        else:
            tail += c

if not flipped:
    print(head[::-1] + S + tail)
else:
    print(tail[::-1] + S[::-1] + head)

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    reverse = 0
    head = ""
    tail = ""
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            reverse = not(reverse)
        else:
            if (reverse^int(query[1]))&1:
                head += query[2]
            else:
                tail += query[2]
    s = head[::-1] + s + tail
    if reverse&1:
        print(s[::-1])
    else:
        print(s)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

flip = False

head = ""
tail = ""

for q in range(Q):
    Ti = input()
    if Ti == '1':
        flip = not flip
    else:
        _, Fi, Ci = map(str, Ti.split())
        Fi = int(Fi)

        if (Fi^flip)&1:
            head += Ci
        else:
            tail += Ci

S = head[::-1] + S + tail
print( S if not flip else S[::-1])

Submission status: Accepted

Solution code:
s=input()
q=int(input())
head=''
foot=''

a=0
for i in range(q):
  t=input()
  if t=='1':
    a=a^1
  else:
    t, f, c = map(str, t.split())
    f=int(f)
    if (f^a)&1==0:
      foot+=c
    else:
      head+=c
s=head[::-1]+s+foot
if a==1:
  s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
head=''
foot=''

a=0
for i in range(q):
  t=input()
  if t=='1':
    a=a^1
  else:
    t, f, c = map(str, t.split())
    f=int(f)
    if (f^a)&1==0:
      foot+=c
    else:
      head=c+head
s=head+s+foot
if a==1:
  s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    H = True
    A = ""  #
    B = ""
    for _ in range(Q):  # リストにしない
        q = list(input().split())
        if q[0] == "1": H = not H
        elif q[1] == "1" and H:     A = q[2] + A
        elif q[1] == "1" and not H:     B = B + q[2]
        elif q[1] == "2" and H:     B = B + q[2]
        elif q[1] == "2" and not H:     A = q[2] + A

    ans = A + S + B
    if H:   print(ans)
    else:   print(ans[::-1])

main()

Submission status: Accepted

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

dir_changed = False
hd, ta = [], []

for _ in range(Q):
  A = list(input().split())
  if A == ["1"]:
    dir_changed = not dir_changed
    hd, ta = ta, hd
    continue
  F, C = int(A[1]), A[2]
  #if dir_changed:
  #  F = 3-F
  if F == 1:
    hd.append(C)
  else:
    ta.append(C)
    
if dir_changed:
  S = S[::-1]
print("".join(hd[::-1]) + S + "".join(ta))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    S = input().rstrip("\r\n")
    Q = int(input())
    d = deque(S)
    reverse = False
    for _ in range(Q):
        q = input().rstrip("\r\n")
        if q == "1":
            reverse = not reverse
        else:
            _, f, c = q.split()
            if (f == "1" and not reverse) or (f == "2" and reverse):
                d.appendleft(c)
            else:
                d.append(c)
    l = list(d)
    if reverse:l.reverse()
    print("".join(l))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

#import sys
#input = sys.stdin.readline

def main():
    S = input()
    Q = int(input())
    H = True
    A = ''
    B = ''
    for i in range(Q):
        q = list(input().split())
        if q[0] == '1':
            H = not H
        elif q[1]=='1' and H:
            A = q[2] + A
        elif q[1]=='2' and not H:
            A = q[2] + A
        elif q[1]=='1' and not H:
            B = B + q[2]
        elif q[1]=='2' and H:
            B = B + q[2]
    ret = A + S + B
    if H:
        print(ret)
    else:
        print(ret[::-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

dq1 = deque(S)
dq2 = deque(S[::-1])
isReversed = False

for _ in range(Q):
    l = input().split()
    if l[0] == "1":
        isReversed = not isReversed
    else:
        s = l[2]
        if l[1]=="1":
            if isReversed:
                dq1.append(s)
                dq2.appendleft(s)
            else:
                dq1.appendleft(s)
                dq2.append(s)
        else:
            if isReversed:
                dq1.appendleft(s)
                dq2.append(s)
            else:
                dq1.append(s)
                dq2.appendleft(s)

print("".join(dq2 if isReversed else dq1))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
f = []
b = []
h = 0

for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        f, b = b, f
        h = 1 - h
    elif len(a) >= 3:
        if a[1] == "1":
            f.append(a[2])
        elif a[1] == "2":
            b.append(a[2])

if h == 0:
    print("".join(f[::-1]) + s + "".join(b))
else:
    print("".join(f[::-1]) + s[::-1] + "".join(b))

Submission status: Accepted

Solution code:
from collections import deque

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

q = deque(S)

parity = 0
for i in range(Q):
    T = input().split()
    if len(T) == 1:
        parity = (parity + 1) % 2
        #q.reverse()
    else:
        F = int(T[1])
        C = T[2]

        if parity == 0:
            if F == 1:
                q.appendleft(C)
            else:
                q.append(C)
        else:
            if F == 1:
                q.append(C)
            else:
                q.appendleft(C)

if parity == 1:
    q.reverse()

print(''.join(q))

Submission status: Accepted

Solution code:
#
S=input()
Q=int(input())
Query=[]
for i in range(Q):
    Query.append(list(input().split()))

muki=0#0で正方向、１で負方向
ans_right=[]
ans_left=[]
for i in range(Q):
    if Query[i][0]=="1":
        if muki==0:
            muki=1
        elif muki==1:
            muki=0
    else:
        if muki==0:
            if Query[i][1]=="1":
                ans_left.append(Query[i][2])
                #先頭にQuery[i][2]を追加
            elif Query[i][1]=="2":
                ans_right.append(Query[i][2])
                #末尾にQuery[i][2]を追加
        elif muki==1:
            if Query[i][1]=="1":
                ans_right.append(Query[i][2])
                #末尾にQuery[i][2]を追加
            elif Query[i][1]=="2":
                ans_left.append(Query[i][2])
                #先頭にQuery[i][2]を追加
ans_left.reverse()

S=list(S)
ans=ans_left+S+ans_right

if muki==0:
    ans="".join(ans)
    print(ans)
else:
    ans.reverse()
    ans="".join(ans)
    print(ans)

Submission status: Accepted

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

head = []
tail = []
flip = 0

for _ in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = query[1]
		c = query[2]
		if (f == '1' and flip == 0) or (f == '2' and flip == 1):
			head.append(c)
		else:
			tail.append(c)

if flip:
	head, tail = tail, head
	s = s[::-1]

head = ''.join(head)
tail = ''.join(tail)

print(head[::-1] + s + tail)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())

q = int(input())
r = 0
for _ in range(q):
    A = list(input().split())
    if A[0] == '1':
        r = (r + 1)%2
    else:
        if (A[1] == '1' and r % 2 == 0) or (A[1] == '2' and r % 2 == 1):
            s.appendleft(A[2])
        else:
            s.append(A[2])

if r == 1:
    s.reverse()
print("".join(s))

Submission status: Accepted

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

head = []
tail = []
flip = 0

for i in range(q):
	query = input().split()
	if query[0] == '1':
		flip ^= 1
	else:
		f = int(query[1]) - 1
		c = query[2]
		if (f ^ flip) == 0:
			head.append(c)
		else:
			tail.append(c)

if flip:
	head, tail = tail, head
	s = s[::-1]

head = ''.join(head)
tail = ''.join(tail)

print(head[::-1] + s + tail)

Submission status: Accepted

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

flip = False
s_0 = ''
s_n = ''
for q in queries:
  if q[0] == '1':
    flip = not flip
  else:
    if (q[1] == '1' and not flip) or (q[1] == '2' and flip):
      s_0 += q[2]
    else:
      s_n += q[2]
if flip:
  print(s_n[::-1] + S[::-1] + s_0)
else:
  print(s_0[::-1] + S + s_n)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
array = [list(map(str, input().split())) for i in range(q)]
#print(array)
d = deque(s)
reverse = 1
for i in range(q):
    if array[i][0] == "1":
        reverse *= -1
    elif (array[i][1] == "1") and (reverse == 1):
        d.appendleft(array[i][2])
    elif (array[i][1] == "1") and (reverse == -1):
        d.append(array[i][2])
    elif (array[i][1] == "2") and (reverse == 1):
        d.append(array[i][2])
    elif (array[i][1] == "2") and (reverse == -1):
        d.appendleft(array[i][2])

if reverse == -1:
    d.reverse()

d_str = "".join(list(d))
print(d_str)

Submission status: Accepted

Solution code:
from collections import deque

d=deque()
s=input()
for i in s:
  d.append(i)
q=int(input())
flag=1
t = [input().split() for l in range(q)]
for i in range(q):
  if t[i][0]=='1':
    flag=flag*(-1)
  else:
    if t[i][1]=='1' and flag==1:
      d.appendleft(t[i][2])
    elif t[i][1]=='1' and flag==-1:
      d.append(t[i][2])
    elif t[i][1]=='2' and flag==1:
      d.append(t[i][2])
    elif t[i][1]=='2' and flag==-1:
      d.appendleft(t[i][2])
if flag==-1:
  d.reverse()
d="".join(list(d))
print(d)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
q_=[input() for i in range(q)]
l=[]
r=list(s)
reverse=False
for i in q_:
  if i=="1":
    reverse=not reverse
  elif (i.split()[1]=="1" and not reverse) or (i.split()[1]=="2" and reverse):
    l.append(i.split()[2])
  else:
    r.append(i.split()[2])
#print(l)
#print(r)
if reverse:
  print(''.join(r[::-1]+l))
else:print(''.join(l[::-1]+r))

Submission status: Accepted

Solution code:
from collections import deque


S = deque(input())
Q = int(input())
QUERY = [input().split(" ") for i in range(Q)]
reverse_f = 0

for query in QUERY:
    if query[0] == "1":#反転
        reverse_f = (reverse_f+1)%2
    else:#=="2"
        if query[1] == "1":
            if reverse_f:
                S.append(query[2])
            else:
                S.appendleft(query[2])

        else:#=="2"
            if reverse_f:
                S.appendleft(query[2])
            else:
                S.append(query[2])

if reverse_f:
    S.reverse()
print("".join(list(S)))

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
rev = False
fwd =[]

for i in range(q):
    t = input()
    if len(t) == 1:        
        if rev:
            rev = False
        else:
            rev = True
    else:
        q2, f, c = t.split()
        if f == "1":
            if rev:
                s.append(c)
            else:
                fwd.append(c)
        if f == "2":
            if rev:
                fwd.append(c)
            else:
                s.append(c)

s = list(reversed(fwd)) + s

if rev:
    s = "".join(list(reversed(s)))
else:
    s = "".join(s)

print(s)

Submission status: Accepted

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

n = len(s)
rev = False
fwd = []

for i in range(q):
    t = input()
    if len(t) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        q2, f, c = t.split()
        if rev:
            if int(f) == 2:
                fwd.append(c)
                n += 1
            else:
                s.append(c)
                n += 1
        else:
            if int(f) == 1:
                fwd.append(c)
                n += 1
            else:
                s.append(c)
                n += 1

fwd = list(reversed(fwd))

s = fwd + s

if rev:
    s = "".join(list(reversed(s)))
else:
    s = "".join(s)

print(s)

Submission status: Accepted

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

for i in range(q):
    t = input()
    if rev == 0:
        if len(t) == 1:
            rev = 1
        else:
            if t[2] == "1":
                s.appendleft(t[4])
            else:
                s.append(t[4])
    else:
        if len(t) == 1:
            rev = 0
        else:
            if t[2] == "1":
                s.append(t[4])
            else:
                s.appendleft(t[4])

if rev == 1:
    s.reverse()
print(*s, sep="")

Submission status: Accepted

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

x = []
y = []
cnt = 0

for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        x, y = y, x
        cnt += 1
    else:
        if a[1] == "1":
            x.append(a[2])
        else:
            y.append(a[2])

if cnt % 2 == 1:
    s = s[::-1]

s = ''.join(x[::-1]) + s + ''.join(y)

print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a = []
b = []
c = 0
for i in range(q):
    init = list(input().split())
    if init[0] == '1':
        a, b = b, a
        c += 1
    else:
        if init[1] == '1':
            a.append(init[2])
        else:
            b.append(init[2])
if c % 2 == 1:
    s = s[::-1]
print(''.join(a[::-1]) + s + ''.join(b))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
is_reversed = 1
for _ in range(Q):
    query = list(input().split())
    if query[0] == "1":
        is_reversed *= -1
    
    else:
        if is_reversed == 1:
            if query[1] == "1":
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if query[1] == "2":
                S.appendleft(query[2])
            else:
                S.append(query[2])

S = "".join(S)
print(S[::is_reversed])

Submission status: Accepted

Solution code:
import sys
s=input()
q=int(input())
a=[]
b=[]
c=0
for i in sys.stdin:
  if '1' in i.split()[0]:
    a,b=b,a
    c+=1
  else:
    if '1' in i.split()[1]:
      a.append(i.split()[2])
    else:
      b.append(i.split()[2])
if c%2==1:
  s=''.join(list(s)[::-1])
print(''.join(a[::-1])+s+''.join(b))

Submission status: Accepted

Solution code:
from collections import deque

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

def start(S):
    state = {
        "S": S,
        "is_reversed": False,
        "left": deque(),
        "right": deque(),
    }

    return state


def T1(state, Query):
    state['is_reversed'] = not state['is_reversed']


def T2(state, Query):
    F = Query[1]
    C = Query[2]

    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)


def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output

def fetch(Query):
    operations = {
        '1': T1,
        '2': T2,
    }

    index = Query[0]
    operation = operations[index]

    return operation

def operate(state, Query):
    T = fetch(Query)
    T(state, Query)


def execute(state, Queries):
    state = start(S)

    for Query in Queries:
        operate(state, Query)

    result = output(state)

    return result


answer = execute(S, Queries)

print(answer)

Submission status: Accepted

Solution code:
from collections import deque

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

def start(S):
    state = {
        "S": S,
        "is_reversed": False,
        "left": deque(),
        "right": deque(),
    }

    return state


def T1(state, Query):
    state['is_reversed'] = not state['is_reversed']


def T2(state, Query):
    F = Query[1]
    C = Query[2]

    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)


def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output

def fetch(Query):
    operations = {
        '1': T1,
        '2': T2,
    }

    index = Query[0]
    operation = operations[index]

    return operation

def operate(state, Query):
    T = fetch(Query)
    T(state, Query)


def execute(state, Queries):
    for Query in Queries:
        operate(state, Query)

    return state


state = start(S)
execute(state, Queries)

answer = output(state)

print(answer)

Submission status: Accepted

Solution code:
from collections import deque

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

def start(S):
    state = {
        "S": S,
        "is_reversed": False,
        "left": deque(),
        "right": deque(),
    }

    return state


def T1(state, Query):
    state['is_reversed'] = not state['is_reversed']


def T2(state, Query):
    F = Query[1]
    C = Query[2]

    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)


def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output

def fetch(Query):
    operations = {
        '1': T1,
        '2': T2,
    }

    index = Query[0]
    operation = operations[index]

    return operation

def operate(state, Query):
    T = fetch(Query)
    T(state, Query)


state = start(S)
for Query in Queries:
    operate(state, Query)

answer = output(state)

print(answer)

Submission status: Accepted

Solution code:
from collections import deque

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

state = {
    "S": S,
    "is_reversed": False,
    "left": deque(),
    "right": deque(),
}

def T1(state):
    state['is_reversed'] = not state['is_reversed']

def T2(state, F, C):
    if not state['is_reversed']:
        if F == '1':
            state['left'].appendleft(C)
        else:
            state['right'].append(C)
    else:
        if F == '1':
            state['right'].append(C)
        else:
            state['left'].appendleft(C)

def output(state):
    output = ''.join(state['left']) + state['S'] + ''.join(state['right'])
    if state['is_reversed']:
        output = ''.join(list(reversed(output)))

    return output


for Query in Queries:
    if Query[0] == '1':
        T1(state)
    else:
        T2(state, Query[1], Query[2])

answer = output(state)

print(answer)

Submission status: Accepted

Solution code:
from collections import deque

s=input()
q=int(input())
d=deque()
d.append(s)
m=1
for i in range (q):
    a=list(input().split())
    if a[0]=="1":
        m=m*(-1)
    else:
        if m==1:
            if a[1]=="1":
                d.appendleft(a[2])
            else:
                d.append(a[2])
        else:
            if a[1]=="2":
                d.appendleft(a[2])
            else:
                d.append(a[2])
s="".join(d)


print(s[::m])

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

from collections import deque

def main():
  S = deque(input())
  N = int(input())

  d = True
  for i in range(N):
    list = input().split()
    if list[0] == '1':
      d = not d
      continue
    if list[1] == '1':
      S.appendleft(list[2]) if d else S.append(list[2])
      continue
    S.append(list[2]) if d else S.appendleft(list[2])
  return ''.join(S) if d else ''.join(S)[::-1]

if __name__ == '__main__':
  print(main())

Submission status: Accepted

Solution code:
from collections import deque

def main():
    ans = deque(input())
    n = int(input())
    vec = 1
    for i in range(n):
        q = input()
        if q == "1":
            vec *= -1
        else:
            _,o,s = q.split()
            if ((o == "1") and (vec == 1)) or ((o == "2") and (vec == -1)):
                ans.appendleft(s)
            else:
                ans.append(s)
    if vec == -1:
        ans.reverse()
    ans = ''.join(ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

d = deque(input())
Q = int(input())

qs = [list(map(str, input().split())) for _ in range(Q)]

vector = True
for q in qs:
    if q[0] == "1":
        vector = not vector
    else:
        if q[1] == "1":
            if vector:
                d.appendleft(q[2])
            else:
                 d.append(q[2])
        else:
            if vector:
                d.append(q[2])
            else:
                 d.appendleft(q[2])

if not vector:
    d.reverse()

ans = ''.join(d)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import copy
s = input()
q_ = int(input())
query = [input().split() for _ in range(q_)]
l = deque(s)
flag = 0

for q in query:
    if q[0] == '1':
        flag += 1
        continue
    elif q[0] == '2':
        if (int(q[1]) + flag) % 2 == 1:
            l.appendleft(q[2])
        else:
            l.append(q[2])

if flag % 2 == 1:
    l.reverse()

print(''.join(l))