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())
rev=0
for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":
        rev^=1
    else:
        z,f,c=p
        if f=="1":
            if rev:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if rev:
                s.appendleft(c)
            else:
                s.append(c)
if rev:
    s=list(s)[::-1]
else:
    s=list(s)
print("".join(s))

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left = "".join([left,c])
                else:
                    right = "".join([right,c])
            else:
                if not hanten:
                    right =  "".join([right,c])
                else:
                    left = "".join([left,c])
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
import sys

input = lambda: sys.stdin.readline().rstrip()

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

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

result = ''.join(s)
if is_reverse:
    print(result[::-1])
else:
    print(result)

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input());q=int(input());rev=0
for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":rev^=1
    else:
        z,f,c=p
        if f=="1":
            if rev:s.append(c)
            else:s.appendleft(c)
        else:
            if rev:s.appendleft(c)
            else:s.append(c)
s=list(s)[::1-2*rev]
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

rev=0
for i in range(q):
  l=input().split()
  if len(l)==1:
    rev+=1
  else:
    t,f,c=l[0],l[1],l[2]

    if rev%2==0:
      if f=='1':
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f=='1':
        s.append(c)
      else:
        s.appendleft(c)

if rev%2==1:
  s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

rev=0
for i in range(q):
  l=list(input().split())
  if len(l)==1:
    rev+=1
  else:
    t,f,c=l[0],l[1],l[2]

    if rev%2==0:
      if f=='1':
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f=='1':
        s.append(c)
      else:
        s.appendleft(c)

if rev%2==1:
  s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left = c + left
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left = c + left
        #print(query)
        #print(s,left,right)
    #left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  S=input()
  N=int(input())
  cnt=0
  f=''
  b=''
  for i in range(N):
    tmp=[i for i in input().split() ]
    if tmp[0]=='1':
      cnt+=1
    else:
      if (cnt+int(tmp[1]))%2==1:
        f=tmp[2]+f
        
      else:
        b=b+tmp[2]
  ans=f+S+b        
  if cnt%2!=0:
    ans=''.join(list(reversed(ans)))

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

Submission status: Accepted

Solution code:
from collections import deque

S=deque(str((input())))
Q=int(input())
t=0
for _ in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        t+=1
    else:
        if (T[1]=="1" and t%2 == 0) or (T[1]=="2" and t%2 != 0):
            S.appendleft(T[2])
        else:
            S.append(T[2])


if t%2 != 0:
    S.reverse()

print("".join(S))

Submission status: Accepted

Solution code:
import sys
from collections import deque

s = input()
q = int(input())
is_reverse = False
ds = deque(s)

for _ in range(q):
  tf = sys.stdin.readline().split()
  if len(tf) == 1:
    is_reverse = False if is_reverse else True
  else:
    if tf[1] == '1':
      if is_reverse:
        ds.append(tf[2])
      else:
        ds.appendleft(tf[2])
    else:
      if is_reverse:
        ds.appendleft(tf[2])
      else:
        ds.append(tf[2])
if is_reverse:
  ds.reverse()
ans = ''.join(list(ds))
print(ans)

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    left = ""
    right = ""
    hanten = False
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            hanten = not hanten
        else:
            c = query[2]
            if query[1] == "1":
                if not hanten:
                    left += c
                else:
                    right += c
            else:
                if not hanten:
                    right += c
                else:
                    left += c
        #print(query)
        #print(s,left,right)
    left = left[::-1]
    answer = left + s + right

    if not hanten:
        print(answer)
    else:
        print(answer[::-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
S = input().strip()
Q = int(input())
f = 0
add = ['', '']
for q in range(Q):
    query = input().strip()
    if query == '1':
        f ^= 1 
    else:
        _, F, C = query.split(' ')
        add[int(F)-1^f] += C
if f == 0:
    print(''.join([add[0][::-1], S, add[1]]))
else:
    print(''.join([add[1][::-1], S[::-1], add[0]]))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
reverse_cnt = 0
s = deque([i for i in S])

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse_cnt += 1
    else:
        if query[1] == "1":
            if reverse_cnt % 2 == 0:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if reverse_cnt % 2 == 0:
                s.append(query[2])
            else:
                s.appendleft(query[2])

if reverse_cnt % 2 == 0:
    while s:
        print(s.popleft(), end="")
else:
     while s:
        print(s.pop(), end="")

Submission status: Accepted

Solution code:
def main2(s,x,q):
    res = s
    rev = 1 #-1なら反転中
    lstA = []
    lstB = []
    for op in q:
        if int(op[0]) == 1:
            rev = rev * -1
        elif int(op[1])*rev == 1 or int(op[1])*rev == -2:
            lstA.append(op[2])
        elif int(op[1])*rev == -1 or int(op[1])*rev == 2:
            lstB.append(op[2])
    if rev == 1:
        res = "".join(lstA[::-1])+res+"".join(lstB)
    else :
        res = "".join(lstB[::-1])+res[::-1]+"".join(lstA)
    return res

s = input()
x = int(input())
q = [input().split() for _ in range(x)]
print(main2(s,x,q))

Submission status: Accepted

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

reverse = 0
head = []
tail = []
for i in range(Q):
    query = input()
    if len(query) == 1:
        reverse = (reverse + 1) % 2

    else:
        t, F, C = query.split()
        if reverse == 0:
            if F == "1":
                head.append(C)
            else:
                tail.append(C)
        else:
            if F == "1":
                tail.append(C)
            else:
                head.append(C)

if reverse == 0:
    S = ''.join(head[::-1] + list(S) + tail)
else:
    S = ''.join(tail[::-1] + list(S[::-1]) + head)


print(S)

Submission status: Accepted

Solution code:
s = input()
nq = int(input())
flg = 0
front = []
back = []
for i in range(nq):
    q = input()
    if len(q) == 1:
        #flg += 1
        flg = (flg+1)%2
    else:
        if flg == 0:
            if q[2] == '1':
                #front = [q[-1]] + front
                front.append(q[-1])
            else:
                #back = back + [q[-1]]
                back.append(q[-1])
        else:
            if q[2] == '1':
                #back = back + [q[-1]]
                back.append(q[-1])
            else:
                #front = [q[-1]] + front
                front.append(q[-1])
                

if flg % 2 == 1:
    s = ''.join(back)[::-1] + s[::-1] + ''.join(front)
else:
    s = ''.join(front)[::-1] + s + ''.join(back)
print(s)

Submission status: Accepted

Solution code:
from collections import deque

sc = iter(open(0).read().split())
ni = lambda: int(next(sc))
ns = lambda: next(sc)

S = ns()
S = deque(S)

rev = False
Q = ni()
for _ in range(Q):
    op = ni()
    if op == 1:
        rev = not rev
    else:
        F, C = ni(), ns()
        if F == 1:
            if rev:
                S.append(C)
            else:
                S.appendleft(C)
        else:
            if rev:
                S.appendleft(C)
            else:
                S.append(C)

print("".join(reversed(S) if rev else S))

Submission status: Accepted

Solution code:
def main2(s,x,q):
    res = s
    rev = 1 #-1なら反転中
    lstA = []
    lstB = []
    for op in q:
        if int(op[0]) == 1:
            rev = rev * -1
        elif int(op[1])*rev == 1 or int(op[1])*rev == -2:
            lstA.append(op[2])
        elif int(op[1])*rev == -1 or int(op[1])*rev == 2:
            lstB.append(op[2])
    if rev == 1:
        res = "".join(lstA[::-1])+res+"".join(lstB)
    else :
        res = "".join(lstB[::-1])+res[::-1]+"".join(lstA)
    return res
 
s = input()
x = int(input())
q = [input().split() for _ in range(x)]
print(main2(s,x,q))

Submission status: Accepted

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

now_reverse = False

for _ in range(n):
  query = input()

  if query == '1':
    now_reverse = not now_reverse
    continue

  
  _,f,c = query.split()
  if f == '1' and now_reverse:
    d.append(c)
  elif f == '1' and not now_reverse:
    d.appendleft(c)
  elif f == '2' and now_reverse:
    d.appendleft(c)
  else:
    d.append(c)
  
if now_reverse:
  d = reversed(list(d))
else:
  d =list(d)

for c in d:
  print(c,end='')
print()

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque
S = (stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
cnt = 0
ccnt = 0
s = deque(["@"])
for i in range(Q):
    data = [(x) for x in stdin.readline().rstrip().split()]
    if data[0] == "2":
        if data[1] == "1":
            if cnt % 2 == 0:
                s.appendleft(data[2])
            else:
                s.append(data[2])
        else:
            if cnt % 2 == 0:
                s.append(data[2])
            else:
                s.appendleft(data[2])
    else:
        cnt += 1
s = "".join(s)
if cnt % 2 != 0:
    s = s[::-1]
if cnt % 2 != 0:
    S = S[::-1]
ans = s.replace("@",S)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# TLE 乙
from collections import deque


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

for _ in range(q):
    t, *fc = input().split()
    t = int(t)
    if fc:
        fc[0] = int(fc[0])

    if t == 1:
        usd = not usd
        continue
    if t == 2:
        if (fc[0] == 1 and not usd) or (fc[0] == 2 and usd):
            d.appendleft(fc[1])
        else:
            d.append(fc[1])

s = ''.join(list(d))
if usd:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = list(input())
q = I()
front = []
back = []
cnt = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        cnt += 1
    else:
        if query[1] == "1":
            if cnt % 2 == 0:
                front.append(query[2])
            else:
                back.append(query[2])
        else:
            if cnt % 2 == 0:
                back.append(query[2])
            else:
                front.append(query[2])

if cnt % 2 == 0:
    print(''.join(front[::-1])+''.join(s)+''.join(back))
else:
    print(''.join(back[::-1])+''.join(s[::-1])+''.join(front))

Submission status: Accepted

Solution code:
from collections import deque
S=input()
s=deque(S)
Q=int(input())
Que=[list(map(str, input().split())) for _ in range(Q)]
#print(Que)
a=0
for i in range(Q):
    if Que[i][0]=="1":
        a+=1
    elif Que[i][0]=="2":
        if a%2==0:
            if Que[i][1]=="2":
                s.append(Que[i][2])
            elif Que[i][1]=="1":
                s.appendleft(Que[i][2])
        else:
            if Que[i][1]=="1":
                s.append(Que[i][2])
            elif Que[i][1]=="2":
                s.appendleft(Que[i][2])
s=list(s)
if a%2==1:
    s=s[::-1]
ans="".join(s)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque


reverse = 0
S = deque(list(input()))
q = int(input())

for query in sys.stdin:
    if query[0] == '1':
        reverse = 1 - reverse
        continue

    _, f, c = query.split()
    f = int(f)
    action = (reverse + f) % 2
    if action:
        S.appendleft(c)
    else:
        S.append(c)

if reverse:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

s = deque(x for x in stdin.readline().rstrip())
q = int(stdin.readline().rstrip())

direction = 1

for i in range(q):
    query = stdin.readline().rstrip().split()
    if len(query) == 1:
        direction *= -1
    else:
        if ((query[1] == '1') and (direction == 1)) or ((query[1] == '2') and (direction == -1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])

sl = list(s)
if direction == 1:
    print(''.join(sl))
else:
    print(''.join(sl[::-1]))

Submission status: Accepted

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

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

    reverse = False
    header = [''] * Q
    footer = [''] * Q
    for i in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            _, f, c = query.split()
            isHead = f == '1'
            if reverse:
                isHead = not isHead
            if isHead:
                header[i] = c
            else:
                footer[i] = c

    if reverse:
        print("".join(footer[::-1] + list(S)[::-1] + header))
    else:
        print("".join(header[::-1]) + S + "".join(footer))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
init = input()
num = int(input())

_strings = [''] * (num * 2 + len(init))

fi = num - 1
li = num
for c in list(init):
    _strings[li] = c
    li += 1

isReverse = False

i = 0
while i < num:
    query = input().split()
    type = int(query[0])

    if type == 1:
        # _strings.reverse()
        isReverse = not isReverse
    else:
        mode = int(query[1])

        if (mode == 1 and not isReverse) or (mode == 2 and isReverse):
            _strings[fi] = str(query[2])
            fi -= 1
            # _strings.insert(0, str(query[2]))
        else:
            _strings[li] = str(query[2])
            li += 1
            # _strings.append(str(query[2]))
    i += 1

if isReverse:
    _strings.reverse()

print(''.join(_strings))

Submission status: Accepted

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

def abc158d(s_init, n, query):
    head = ""
    tail = ""
    reverse = 0
    for i in range(n):
        tgt = query[i]
        if tgt[0] == "1":
            reverse = (reverse + 1) % 2
        elif tgt[0] == "2":
            if tgt[1] == "1":
                if reverse == 0:
                    head += tgt[2]
                else:
                    tail += tgt[2]
            elif tgt[1] == "2":
                if reverse == 0:
                    tail += tgt[2]
                else:
                    head += tgt[2]
            else:
                print("invalid query!!:" + str(tgt[0]) + str(tgt[1]))
        else:
            print("invalid query!!:" + str(tgt[0]))
    if reverse == 0:
        return head[::-1] + s_init + tail
    else:
        return tail[::-1] + s_init[::-1] + head

###
# main
if(__name__ == '__main__'):
    # input
    s_init = input()
    n = int(input())
    query = []
    for i in range(n):
        query.append(input().split(" "))
    
    accomplishment = abc158d(s_init, n, query)
    
    # output
    print(accomplishment)
# else:
    # do nothing

Submission status: Accepted

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

from collections import deque

ss = deque(s)
flg = True

for i in range(q):
  t = input().split()
  if t[0] == '1':
    flg = not flg
  else:
    if t[1] == '2':
        ss.append(t[2]) if flg else ss.appendleft(t[2])
    else:
        ss.append(t[2]) if not flg else ss.appendleft(t[2])

ss = list(ss)

print("".join(ss)) if flg else print("".join(ss[::-1]))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
cnt = 0
for _ in range(q):
    fc = input().split()
    if fc[0] == '1':
        cnt += 1
    else:
        if cnt == 1:
            fc[1] = '1' if fc[1] == '2' else '2'
        if fc[1] == '1':
            s.appendleft(fc[2])
        else:
            s.append(fc[2])
    cnt %= 2
if cnt == 1:
    s.reverse()
print(*s, sep='')

Submission status: Accepted

Solution code:
from collections import deque

s = deque((str(input())))
loop = int(input())

rev = 0
for i in range(loop):
    queries = input().split()
    if len(queries) == 1:
        rev = (rev+1) % 2
    else:
        type = int(queries[1])
        c = str(queries[2])
        if (type + rev) % 2 == 1:
            s.appendleft(c)
        else:
            s.append(c)
if rev == 1:
    s.reverse()
print("".join(list(s)))

Submission status: Accepted

Solution code:
# coding: utf-8

a = input()
q = int(input())
mode = 1
L = ''
R = ''

for _ in range(q):
    q_list = input().split()

    if q_list[0] == '1':
        mode *= -1
    else:
        if q_list[1] == '1':
            if mode == 1:
                L += q_list[2]
            else:
                R += q_list[2]
        else:
            if mode == -1:
                L += q_list[2]
            else:
                R += q_list[2]
result = L[::-1] + a + R
if mode == -1:
    result = result[::-1]
print(result)

Submission status: Accepted

Solution code:
S = list(input())
S_r = []
Q = int(input())
R = True

for i in range(Q):
  q = list(input().split())
  if int(q[0]) == 1:
    R = not R
  else:
    if int(q[1]) == 1:
      if R:
        S_r.append(q[2])
      else:
        S.append(q[2])
    else:
      if R:
        S.append(q[2])
      else:
        S_r.append(q[2])

if not S_r:
  if R:
    print(''.join(S))
  else:
    print(''.join(S[::-1]))
else:
  if R:
    print(''.join(S_r[::-1]), end="")
    print(''.join(S))
  else:
    print(''.join(S[::-1]), end="")
    print(''.join(S_r))

Submission status: Accepted

Solution code:
import collections
s = input()
_q = int(input())
q = collections.deque(s)

rev = False
for _ in range(_q):
    *query, = map(str, input().split())

    if query[0] == "1":
        rev = not rev
        continue

    f, c = query[1], query[2]

    if rev ^ (f == "1"):
        q.appendleft(c)
    else:
        q.append(c)

if rev:
    q.reverse()

print("".join(q))

Submission status: Accepted

Solution code:
from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
Query = [[] for _ in range(Q)]
for i in range(Q):
    Query[i] = list(map(str,input().split()))

fl = False
for q in Query:
    if q[0]=='1':
        fl = not fl
    else:#q[0]=='2'
        if (q[1]=='1') ^ fl:
            S.appendleft(q[2])
        else:#q[1]=='2'
            S.append(q[2])
T = ''.join(S)
if fl:
    T = T[::-1]
print(T)

Submission status: Accepted

Solution code:
from collections import deque

s = deque((str(input())))
loop = int(input())

rev = 0
for i in range(loop):
    queries = input().split()
    if len(queries) == 1:
        rev = (rev+1) % 2
    else:
        type = int(queries[1])
        c = str(queries[2])
        if (type + rev) % 2 == 1:
            s.appendleft(c)
        else:
            s.append(c)
if rev == 1:
    print("".join(reversed(list(s))))
else:
    print("".join(list(s)))

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input())
q=int(input())
flg=True #正順
for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":flg=not(flg)
    else:
        z,f,c=p
        if f=="1":
            if flg:s.appendleft(c)
            else:s.append(c)
        else:
            if not(flg):s.appendleft(c)
            else:s.append(c)

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

Submission status: Accepted

Solution code:
import collections
s = collections.deque(list(input()))
q = int(input())
cnt = 0
for i in range(q):
    fc = input().split()
    if fc[0] == '1':
        cnt += 1
    else:
        if cnt == 1:
            fc[1] = '1' if fc[1] == '2' else '2'
        if fc[1] == '1':
            s.appendleft(fc[2])
        else:
            s.append(fc[2])
    cnt %= 2
if cnt == 1:
    s.reverse()
print(*s, sep='')

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
q_num = int(input())
d = deque(S)
rev = True
for _ in range(q_num):
  query = list(input().split())
  if(query[0] == "1"):
    rev = not rev
  if(query[0] == "2"):
    if(rev):
      if(query[1] == "1"):
        d.appendleft(query[2])
      else:
        d.append(query[2])
    else:
      if(query[1] == "1"):
        d.append(query[2])
      else:
        d.appendleft(query[2])
if(rev):
  print("".join(d))
else:
  d.reverse()
  print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
x = 1
l=[]
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if (c == "2" and x == -1) or  (c == "1" and x == 1):
            S.appendleft(d)
        else:
            S.append(d)
if x == -1:
    S.reverse()
print(*S, sep="")

Submission status: Accepted

Solution code:
S = input().strip()
Q = int(input())
is_reverse = False
A = ""
B = ""
for i in range(Q):
  query = [j for j in input().strip().split()]
  T = int(query[0])
  if T == 1:
    is_reverse = not is_reverse
  else:
    F, C = int(query[1]), query[2]
    if F == 1:
      if not is_reverse:
        A = C + A
      else:
        B = B + C
    else:
      if not is_reverse:
        B = B + C
      else:
        A = C + A

res = A + S + B
if not is_reverse:
  print(res)
else:
  print(res[::-1])

Submission status: Accepted

Solution code:
from collections import deque
t=input()
s=deque(t)
q=int(input())
flag=True
for i in range(q):
  ques=list(input().split())
  if ques[0]=="1":
    flag=not flag
    continue
  if ques[1]=="1":
    if flag:
      s.appendleft(ques[2])
    else:
      s.append(ques[2])
  else:
    if flag:
      s.append(ques[2])
    else:
      s.appendleft(ques[2])
if flag==False:
    print(''.join(reversed(list(s))))
else:
    print(''.join(list(s)))

Submission status: Accepted

Solution code:
import numpy as np
import math as m
import collections

def main(**kwargs):
    front = collections.deque()
    back = collections.deque()
    now = "f"

    for q in qs:
        if q[0] == "1":
            if now == "b":
                now = "f"
            else:
                now = "b"

    if now == "f":
        now = "f"

        for q in qs:
            if q[0] == "1":
                if now == "b":
                    now = "f"
                else:
                    now = "b"
            else:
                if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                    front.appendleft(q[2])
                else:
                    back.append(q[2])

        res = "".join(front) + s + "".join(back)
    else:
        now = "f"

        for q in qs:
            if q[0] == "1":
                if now == "b":
                    now = "f"
                else:
                    now = "b"
            else:
                if (q[1] == "1" and now == "f") or (q[1] == "2" and now == "b"):
                    front.append(q[2])
                else:
                    back.appendleft(q[2])
        res = "".join(back) + s[::-1] + "".join(front)
        
    return res

if __name__ == "__main__":
    cin = np.array(input().split(" "))
    s, *_ = cin
    cin = np.array(input().split(" ")).astype("int")
    n, *_ = cin

    qs = []

    for i in range(n):
        cin = np.array(input().split(" "))
        qs.append(list(cin))
    
    cout = main(s=s, n=n, qs=qs)
    print(cout)

Submission status: Accepted

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

flg = True

mae = ''
ato = ''

for i in range(q):
    query = input()
    if query[0] == '1':
        flg = not flg
    else:
        if query[2] == '1':
            if flg:
                mae += query[4]
            else:
                ato += query[4]
        else:
            if flg:
                ato += query[4]
            else:
                mae += query[4]

mae = mae[::-1]
ans = mae + s + ato
if not flg:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

_strings = deque(input())
num = int(input())

isReverse = False

for i in range(num):
    query = input().split()
    type = int(query[0])

    if type == 1:
        # _strings.reverse()
        isReverse = not isReverse
    else:
        mode = int(query[1])

        if (mode == 1 and not isReverse) or (mode == 2 and isReverse):
            _strings.appendleft(str(query[2]))
            # _strings.insert(0, str(query[2]))
        else:
            _strings.append(str(query[2]))
            # _strings.append(str(query[2]))

if isReverse:
    _strings.reverse()

print(''.join(_strings))

Submission status: Accepted

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

s = ""
l = ""
h = 1

for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0] == "1":
        h *= -1
        continue
    if q[1] == "1":
        if h == 1:
            s += q[2]
        else:
            l += q[2]
    else:
        if h == 1:
            l += q[2]
        else:
            s += q[2]

ans = s[::-1] + S + l
print(ans[::h])

Submission status: Accepted

Solution code:
def main():
  from collections import deque
  
  s,q,*r = [list(k.split()) for k in open(0)]
  que = deque(s[0])
  p = False
  for x in r:
    if len(x)==1:
      p = not p
      continue
    if (p==False and x[1]=='1') or (p==True and x[1]=='2'):
        que.appendleft(x[2])
    else:
        que.append(x[2])
  print("".join(que) if p==False else "".join(que)[::-1])
  
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
s=input()
n=int(input())
cnt=0
l=[]
r=[]
for i in range(n):
    e=list(map(str,input().split()))
    q=int(e[0])
    if len(e)!=1:
        f=int(e[1])
        c=e[2]
    if q==2:
        if (f==1 and cnt%2==0) or (f==2 and cnt%2!=0):
            l.append(c)
        else:
            r.append(c)
    else:
        cnt+=1
if cnt%2==0:
    print(''.join(l[::-1])+s+''.join(r))
else:
    print(''.join(r[::-1])+s[::-1]+''.join(l))