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 = input()
Q = int(input())

d = deque(S)
flg = 1 #Trueが前から,
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = 1 - flg
    else:
        if flg:
            if a[0] == "1":
                d.appendleft(a[1])
            else:
                d.append(a[1])
        else:
            if a[0] == "2":
                d.appendleft(a[1])
            else:
                d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(S)
flg = True #Trueが前から,
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = not flg
    else:
        if flg:
            if a[0] == "1":
                d.appendleft(a[1])
            else:
                d.append(a[1])
        else:
            if a[0] == "2":
                d.appendleft(a[1])
            else:
                d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


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

    que = deque(S)

    rev = False
    for _ in range(Q):
        T = input()

        if len(T) == 1:
            rev = not rev
        else:
            t, F, C = T.split()
            # 先頭
            if int(F) == 1:
                if not rev:
                    que.appendleft(C)
                else:
                    que.append(C)
            # 末尾
            else:
                if not rev:
                    que.append(C)
                else:
                    que.appendleft(C)

    if rev:
        que = reversed(que)
    print(*que, sep="")


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = str(input())
Q = int(input())
forward = 1
front = ""
back = ""

for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        forward *= -1
    else:
        if q[1] == "1":
            if forward == 1:
                front = q[2] + front
            else:
                back = back + q[2]        
        else:
            if forward == 1:
                back = back + q[2]
            else:
                front = q[2] + front

s = front + s + back
if forward == -1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
d = deque([S[i] for i in range(len(S))])
Q = int(input())
bol = 0
for i in range(Q):
    a = input().split()
    if a[0] == "1":
        bol = 0 if bol else 1
    elif a[1] == "1":
        if not bol:
            d.appendleft(a[2])
        else:
            d.append(a[2])
    else:
        if not bol:
            d.append(a[2])
        else:
            d.appendleft(a[2])
print("".join(d) if not bol else "".join(reversed(d)))

Submission status: Accepted

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

rev = 0
sh = ''
for i in range(n):
    q = input().split()
    if q[0] == '1':
        rev = (rev + 1) % 2
    else:
        head = int(q[1]) % 2
        if head ^ rev:
            sh += q[2]
            # print('sh', sh)
        else:
            s += q[2]
            # print('s', s)
ans = sh[::-1] + s
print(ans[::-1]) if rev else print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
#import numpy as np
#import scipy
from collections import Counter,defaultdict,deque
# from itertools import permutations, combinations
# from heapq import heappop, heappush
# from fractions import gcd
# input = sys.stdin.readline
# sys.setrecursionlimit(10**8)
# mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

s = input()
q = int(input())
ql = [input().split() for i in range(q)]

ans = deque(s)
isrev=False
for i in range(q):
  if ql[i][0] == '1':
    isrev = not isrev
    continue
  if (ql[i][1] == '2') == isrev:
    ans.appendleft(ql[i][2])
  else:
    ans.append(ql[i][2])

if isrev:
  ans.reverse()

s = ''.join(ans)

print(s)

Submission status: Accepted

Solution code:
S = input()
N = int(input())
line = [''] * (N+1) * 2
rev = False
pos = N
line[pos] = S
leftPos = pos - 1
rightPos = pos + 1

for i in range(N):
    A = input().split()
    if len(A) == 1:
        rev = not rev
    else:
        if A[1] == '1':
            if rev == False:
                pos = leftPos
                leftPos -= 1
            else:
                pos = rightPos
                rightPos += 1
        else:
            if rev == False:
                pos = rightPos
                rightPos += 1
            else:
                pos = leftPos
                leftPos -= 1
        line[pos] = A[2]

ans = line[leftPos+1:rightPos]
ans = "".join(ans)
if rev == False:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
queries = [input().split() for i in range(Q)]

head = []
tail = []
forward = True
for query in queries:
    if query[0] == '1':
        forward = not forward
    else:
        if query[1] == '1' and forward or query[1] == '2' and not forward:
            head.append(query[2])
        else:
            tail.append(query[2])

if forward:
    ans = ''.join(head[::-1]) + S + ''.join(tail)
else:
    ans = ''.join(tail[::-1]) + S[::-1] + ''.join(head)
print(ans)

Submission status: Accepted

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

Q=int(input())
append_flag=False
Q_=[]

for q in range(Q):
    T = list(input().split())
    if T[0]=='2':
        append_flag=True
    if append_flag:
        Q_.append(T)
        
rev=1
rev_num=0
for q in Q_:
    if q[0]=='1':
        rev = -1*rev
        rev_num+=1
    elif rev == 1:
        if q[1]=='1':
            s.appendleft(q[2])
        elif q[1]=='2':
            s.append(q[2])
    elif rev == -1:
        if q[1]=='2':
            s.appendleft(q[2])
        elif q[1]=='1':
            s.append(q[2])

s=list(s)
if rev_num%2 != 0:
    s.reverse()
    
print(''.join(s))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
Q = int(input())
q = [input().split() for _ in range(Q)]
reverse = 0
start = ""
end = ""
for i in q:
    if i[0] == "1":
        reverse ^= 1
    else:
        if (int(i[1]) + reverse) % 2:
            start += i[2]
        else:
            end += i[2]
s = start[::-1] + s + end
print(s[::1 - reverse * 2])

Submission status: Accepted

Solution code:
s=input()
q=int(input())
flag=0
t=['' for _ in range(2)]
cnt=0
for _ in range(q):
  a=list(input().split())
  if a[0]=='1':
    flag=1-flag
    cnt+=1
  else:
    if a[1]=='1':t[flag]+=a[2]
    else:t[1-flag]+=a[2]
ans=t[0][::-1]+s+t[1]
if cnt%2:ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
before = []
after = []
for i in range(Q):
    q = input().split()
    if len(q) == 1:
        reverse = not reverse
    else:
        T, F, C = q
        if (F == '1' and not reverse) or (F == '2' and reverse):
            before.append(C)
        else:
            after.append(C)

if not reverse:
    answer = ''.join(before[::-1]) + S + ''.join(after)
else:
    answer = ''.join(after[::-1]) + S[::-1] + ''.join(before)
print(answer)

Submission status: Accepted

Solution code:
from collections import deque

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

deq = deque([s])
flag = 1
for i in range(q):
    query = input().split()
    if query[0]==str(1):
        flag*=-1 
    if query[0]==str(2):
        x = 1 if query[1]==str(1) else -1
        if flag*x==1:
            deq.appendleft(query[2])
        else:
            deq.append(query[2])

if flag==-1:
    print(''.join(deq)[::-1])
else:
    print(''.join(deq))

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque

s = deque(stdin.readline().rstrip())
n = int(stdin.readline().rstrip())

flag = 0

for i in range(n):
    q = [x for x in stdin.readline().rstrip().split()]

    if len(q) == 1:
        flag = 1 - flag
    else:
        if (q[1] == "1" and flag == 0) or (q[1] == "2" and flag == 1):
            s.appendleft(q[2])
        else:
            s.append(q[2])

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

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque()
for v in s:
    d.append(v)
r = False
for _ in range(q):
    xs = input().split()
    if xs[0] == "1":
        r = not r
    else:
        if (not r and xs[1] == "1") or (r and xs[1] == "2"):
            d.appendleft(xs[2])
        else:
            d.append(xs[2])
s = "".join(d)
print(s[::-1] if r else s)

Submission status: Accepted

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


def main():
  S = input()[:-1]
  Q = int(input())

  q = deque([S])
  reverse = False
  for i in range(Q):
    line = input().split()
    if line[0] == '1':
      reverse = not(reverse)
    else:
      if reverse ^ (int(line[1])-1):
        # S = S + line[2]
        q.append(line[2])
      else:
        # S = line[2] + S
        q.appendleft(line[2])

  print(''.join(list(reversed(''.join(q)))) if reverse else ''.join(q))


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

Submission status: Accepted

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

s = deque(s)
t = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": t = 1 - t
  else:
    if t == 0:
      if query[1] == "1": s.appendleft(query[2])
      elif query[1] == "2": s.append(query[2])
    elif t == 1:
      if query[1] == "1": s.append(query[2])
      elif query[1] == "2": s.appendleft(query[2])
if t == 1:print("".join(s)[::-1])
else: print("".join(s))

Submission status: Accepted

Solution code:
# ABC158
# D - String Formation

from collections import deque

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True

S=deque(input().split())
Q=int(input())
Is_normal=True

for _ in range(Q):
    q = input()
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[2]=='1':
            if Is_normal==True:
                # S=q[4]+S
                S.appendleft(q[4])
            else :
                # S=S+q[4]
                S.append(q[4])

        else :
            if Is_normal==True:
                # S=S+q[4]
                S.append(q[4])

            else:
                # S=q[4]+S
                S.appendleft(q[4])

    # print(Is_normal, S)
S=''.join(S)
if Is_normal==False:
    S=S[::-1]
print(S)

Submission status: Accepted

Solution code:
# ABC158
# D - String Formation

def ChangeState(Is_normal):
    if Is_normal==True:
        return False
    else:
        return True



S=input()
Q=int(input())
Is_normal=True

h=''
t=[]

for _ in range(Q):
    q = input()
    if len(q)== 1 :
        Is_normal = ChangeState(Is_normal)
    else:
        if q[2]=='1':
            if Is_normal==True:
                h=h+q[4]
            else :
                S=S+q[4]
        else :
            if Is_normal==True:
                S=S+q[4]
            else:
                h=h+q[4]
    # print(Is_normal, S)
h=h[::-1]
S=h+S
if Is_normal==False:
    S=S[::-1]
print(S)

Submission status: Accepted

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

dir = 0
mae = []
usi = []

for _ in range(p):
  Q = input().split()
  if Q[0] == "1":
    dir = not dir
  else:
    if (int(Q[1]) - 1) == dir:
      mae.append(Q[2])
    else:
      usi.append(Q[2])
      
ans = "".join(mae[::-1]) + s + "".join(usi)
      
if not dir:
  print(ans)
else:
  print(ans[::-1])

Submission status: Accepted

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

S = input()
Q = int(input())
query = list()
for i in range(Q):
  query.append(list(map(str, input().split())))


d = deque()
for s in S:
  d.append(s) # 右からsを追加
is_reverse = False

for q in query:
  if len(q) == 1:
    # Tiが1の場合
    
    is_reverse = not is_reverse
    
  else:
    # Tiが2の場合
    
    if q[1] == '1':
      # Fiが1の場合
      
      if is_reverse == False:
        d.appendleft(q[2])
      else:
        d.append(q[2])
      
    else:
      # Fiが2の場合
      
      if is_reverse == False:
        d.append(q[2])
      else:
        d.appendleft(q[2])

ans = ''
if is_reverse == True:
  d.reverse()
ans = ''.join(d)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
n = int(input())
now: bool = True

for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        now = not now
    else:
        if (q[1] == '1' and now == True) or (q[1] == '2' and now == False):
            s.appendleft(q[2])
        elif (q[1] == '1' and now == False) or (q[1] == '2' and now == True):
            s.append(q[2])
if now == True:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    s=STR()
    q=INT()
    l=''
    r=''
    ct=0
    for i in range(q):
        fc=LISTS()
        if fc[0]=="1":
            ct+=1
        else:
            if fc[1]=="1":
                if ct%2==0:
                    l=fc[2]+l
                else:
                    r=r+fc[2]
            else:
                if ct%2==1:
                    l=fc[2]+l
                else:
                    r=r+fc[2]
    
    sums=l+s+r
    if ct%2==1:
        ans=""
        for i in range(len(sums)-1,-1,-1):
            ans=ans+sums[i]
    else:
        ans=sums
    
    print(ans)
                
if __name__ == '__main__':
    do()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from functools import reduce, lru_cache
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def S(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    s = S()
    q = I()
    Q = LR(q)

    turned = False
    q = deque()
    for c in s:
        q.append(c)

    for query in Q:
        if query[0] == "1":
            turned = not turned
        else:
            _, f, c = query
            if (not turned and f == "1") or (turned and f == "2"):
                q.appendleft(c)
            else:
                q.append(c)


    if turned:
        q.reverse()
    ans = "".join(q)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


def order_manegement():
    while True:
        yield 1
        yield 0


def main():
    s = input()
    q = int(input())
    s = deque(s)
    gen = order_manegement()
    state = 0
    for i in range(q):
        order = list(input().split())
        if order[0] == '1':
            state = next(gen)
        elif order[0] == '2':
            if (state + int(order[1])) % 2 == 0:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    s = list(s)
    if state == 1:
        s = s[::-1]

    print(''.join(s))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
n = int(input())

memo = 0
for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        if  memo == 0: memo = 1
        else: memo = 0
    else:
        if memo == 0:
            if q[1] == '1': s.appendleft(q[2])
            else: s.append(q[2])
        else:
            if q[1] == '1': s.append(q[2])
            else: s.appendleft(q[2])

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

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
n = int(input())

#x.append(y)
#x.appendleft(y)

memo = 0
for i in range(n):
    q = list(input().split())
    if q[0] == '1':
        if  memo == 0: memo = 1
        else: memo = 0
    else:
        if memo == 0:
            if q[1] == '1': s.appendleft(q[2])
            else: s.append(q[2])
        else:
            if q[1] == '1': s.append(q[2])
            else: s.appendleft(q[2])

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

Submission status: Accepted

Solution code:
MAX_Q = 500_001
OFFSET = 200_000
C = [None] * MAX_Q
S = input()
N = len(S)
for i in range(N):
    C[OFFSET + i] = S[i]
Q = int(input())
left = OFFSET - 1
right = N + OFFSET
flg = False
for _ in range(Q):
    T = input()
    if len(T) == 1:
        flg = not(flg)
    else:
        f, c = T.split()[1:]
        if ((not flg and f == '1') or (flg and f == '2')):
            C[left] = c
            left -= 1
        else:
            C[right] = c
            right += 1
if flg:
    print(''.join(C[right-1:left:-1]))
else:
    print(''.join(C[left+1:right]))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
cnt=0
left=[]
right=[]
for _ in range(n):
    tmp=input()
    if tmp[0]=="1":
        cnt+=1
    else:
        num,side,st=tmp.split()
        if side=="1":
            if cnt%2==0:
                left.append(st)
            else:
                right.append(st)
        else:
            if cnt%2==0:
                right.append(st)
            else:
                left.append(st)
left="".join(left)
left=left[::-1]
right="".join(right)
s=left+s+right
if cnt%2==1:
    s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    s = input()
    d = deque(s)
    q = int(input())
    reverse = False
    for _ in range(q):
        line = input().split()
        if line[0] == "1":
            reverse = reverse ^ True
        else:
            if (not reverse and line[1] == "1") or (reverse and line[1] == "2"):
                d.appendleft(line[2])
            else:
                d.append(line[2])
    print("".join(list(d)) if not reverse else "".join(list(reversed(d))))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
inversion = 1 #1 は反転していない、 -1 は反転している。
s = input()
s1 = ""
s2 = ""
n_query = int(input())
for i in range(n_query):
    q = input().split()
    
    if q[0] == '1':
        inversion *= -1
    elif q[0] == '2':
        if inversion == 1:
            if q[1] == '1':
                s1 = s1 + q[2]
            elif q[1] == '2':
                s2 = s2 + q[2]
        elif inversion == -1:
            if q[1] == '1':
                s2 = s2 + q[2]
            elif q[1] == '2':
                s1 = s1 + q[2]

if inversion == 1:
    s = s1[::-1] + s + s2
elif inversion == -1:
    s = s2[::-1] + s[::-1] + s1

print(s)

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()

def main():
    s = deque(input())
    q = int(input())
    reverse = False
    
    for _ in range(q):
        query = list(input().split())
        if query[0] == '1':
            reverse ^= True
        else:
            if reverse:
                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])
                    
    s = ''.join(s)
    if reverse:
        s = s[::-1]
    print(s)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()

from collections import deque
S = deque(S)

Q = int(input())
rev = False

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

S = "".join(list(S))
if rev:
  S = S[::-1]
  
print(S)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
cnt = 1
for _ in range(q):
    query = input()
    if len(query)==1:
        cnt = 1-cnt
    else:
        f = query[2]
        c = query[-1]
        if cnt:
            if f =='1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
print(''.join(s) if cnt else ''.join(s)[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

flip = 0
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        flip += 1
        flip %= 2
    else:
        if (int(q[1]) - 1 + flip)%2 == 0:
            S.appendleft(q[2])
        else:
            S.append(q[2])

S = list(S)
if flip == 0:
    print(*S, sep="")
else:
    print(*S[::-1], sep="")

Submission status: Accepted

Solution code:
from collections import deque

s = deque(list(input()))

q = int(input())

reverse = 0

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

Submission status: Accepted

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

forward = True
que0 = []
que1 = []

for _ in range(Q):
    query = input().split()

    if query[0] == '1':
        forward = not forward

    else:
        head = (query[1] == '1')
        if head ^ forward:
            que1.append(query[2])
        else:
            que0.append(query[2])

S = ''.join(que0[::-1]) + S + ''.join(que1)

if forward:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s,q=deque([*input()]),int(input())
judge=False
for i in range(q):
  t=input().split()
  if len(t)==1:
    judge = not judge
  else:
    t,f,c=t
    if not judge and f=='1': s.appendleft(c)
    elif not judge and f=='2': s.append(c)
    elif judge and f=='1': s.append(c)
    else: s.appendleft(c)
if judge: s.reverse()
print(*s,sep='')

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

S = readline().rstrip()

from collections import deque
S = deque(S)

Q = int(readline())
turn = False
for i in range(Q):
  query = readline().split()
  if query[0] == "1":
    turn ^= True
  elif query[0] == "2":
    if query[1] == "1":
      if turn:
        S.append(query[2])
      else:
        S.appendleft(query[2])
    elif query[1] == "2":
      if turn:
        S.appendleft(query[2])
      else:
        S.append(query[2])

S = "".join(S)
if turn:
  S = S[::-1]
  
print(S)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(list(input()))
n = int(input())
q = [list(input().split()) for _ in range(n)]
rev = False

for tfc in q:
    if tfc[0] == "1" and rev is False:
        rev = True
    elif tfc[0] == "1" and rev is True:
        rev = False
    elif rev is False:
        if tfc[1] == "1":
            s.appendleft(tfc[2])
        else:
            s.append(tfc[2])
    elif rev is True:
        if tfc[1] == "1":
            s.append(tfc[2])
        else:
            s.appendleft(tfc[2])

if rev:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:

from collections import deque

S = input()
Q = int(input())
q = deque(list(S))
rev = 0
for _ in range(Q):
    s, *x = input().split()
    if s == "1":
        rev += 1
    else:
        if ((x[0] == "1" and rev % 2 == 0)
                or (x[0] == "2" and rev % 2 == 1)):
            q.appendleft(x[1])
        else:
            q.append(x[1])

if rev % 2 == 0:
    print("".join(q))
else:
    print("".join(q)[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = input()

heads = deque([])
tails = deque([])
reverse = False

Q = int(input())
for _ in range(Q):
    cmds = input().split()
    if cmds[0] == '1':
        reverse = not reverse
        heads, tails = tails, heads
    else:
        f, c = cmds[1], cmds[2]
        if f == '1':
            if reverse:
                heads.append(c)
            else:
                heads.appendleft(c)
        else:
            if reverse:
                tails.appendleft(c)
            else:
                tails.append(c)
    
    # if reverse:
    #     print(list(heads)[::-1], S[::-1], list(tails)[::-1])
    # else:
    #     print(list(heads), S, list(tails))
    # print('-' * 10)

if reverse:
    heads = list(heads)[::-1]
    tails = list(tails)[::-1]
    S = S[::-1]

heads = ''.join(map(str, heads))
tails = ''.join(map(str, tails))
ans = ''.join([heads, S, tails])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections as cl


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    S = input()
    Q = II()
    
    direction = 1
    pre = []
    sur = []

    for i in range(Q):
        query = input()
        if query == '1':
            direction *= -1
        else:
            _, f, c = query.split()
            if direction == 1:
                if f == '1':
                    pre.append(c)
                else:
                    sur.append(c)
            else:
                if f == '1':
                    sur.append(c)
                else:
                    pre.append(c)

    if direction == 1:
        ans = "".join(pre[::-1]) + S + "".join(sur)
    else:
        ans = "".join(sur[::-1]) + "".join(S[::-1]) + "".join(pre)
    print(ans)

main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[14]:


from collections import deque


# In[21]:


S = deque(input())
Q = int(input())
rev_cnt = 0
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        rev_cnt += 1
    else:
        if (rev_cnt+int(q[1]))%2 == 0:
            S.append(q[2])
        else:
            S.appendleft(q[2])
if rev_cnt%2 == 0:
    print("".join(S))
else:
    print("".join(list(reversed(S))))    


# In[ ]:

Submission status: Accepted

Solution code:
s=list(input())
top=bool()
t=[]
u=[]
n=int(input())
for i in range(n):
  q=list(map(str,input().split()))
  if q[0]=='1':
    top=not top
  else:
    if (q[1]=="1") ^ top:
      t.append(q[2])
    else:
      u.append(q[2])
s=list(reversed(t))+s+u
if top:
  print(*list(reversed(s)),sep='')
else:
  print(*s,sep='')

Submission status: Accepted

Solution code:
from collections import deque
s = input()
n = int(input())
q_list = [input().split() for _ in range(n)]
d = deque(list(s))
r_flag = False
for q in q_list:
    if q[0] == "1":
        r_flag = not r_flag
    elif q[0] == "2":
        if q[1] == "1":
            if r_flag:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        elif q[1] == "2":
            if r_flag:
                d.appendleft(q[2])
            else:
                d.append(q[2])
if r_flag:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
s = input()
sgn = 1
Q = int(input())
L = '' #sgn=-1
R = '' #sgn=1
for _ in range(Q):
  i = input()
  if i[0] == '1':
    sgn*=-1
  else:
    if i[2] == '1':
      if sgn == 1:
        L += i[4]
      else:
        R += i[4]
    else:
      if sgn == 1:
        R += i[4]
      else:
        L += i[4]

if sgn == -1:
  s = s[::-1]
  R = R[::-1]
  print(R + s +L)
  
else:
  L = L[::-1]
  print(L + s + R)