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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, Q):
    ans = 0
    S = deque(S)
    r = False
    for q in Q:
        if q[0] =='1':
            r = not r
        else:
            f = q[1]
            c = q[2]
            if f == '1':
                if r:
                    S.append(c)
                else:
                    S.appendleft(c)
            else:
                if r:
                    S.appendleft(c)
                else:
                    S.append(c)
    S = list(S)
    if r:
        S.reverse()
    return ''.join(S)


def main():
    S = read_str()
    N = read_int()
    Q = [read_str_n() for _ in range(N)]
    print(slv(S, Q))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    S=input()
    Q=int(input())
    f=False
    ans=deque(list(S))
    for _ in range(Q):
        Query=list(input().split())
        if Query[0]=='1':
            f=not f
        else:
            F,C=Query[1],Query[2]
            if F=='1':
                if not f:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if not f:
                    ans.append(C)
                else:
                    ans.appendleft(C)
    if f:
        ans=list(reversed(ans))
    print(''.join(ans))
        
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

pre = ''
mat = ''
state = 1

for _ in range(q):
    query = input()
    if query[0] == '1':
        state = 3 - state
    else:
        _, f, c = query.split()
        if int(f) == state:
            pre += c
        else:
            mat += c

if state == 1:
    print(pre[::-1] + s + mat)
else:
    print(mat[::-1] + s[::-1] + pre)

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S = input()
    d = deque(S)
    Q = int(input())
    direction = True
    for _ in range(Q):
        x = input()
        if x[0] == '1':
            direction = not direction
        else:
            a,f,c = x.split(' ')
            if (int(f) == 1 and direction) or (int(f) == 2 and not direction):
                d.appendleft(c)
            else:
                d.append(c)
    if direction:
        print(''.join(d))
    else:
        print(''.join(reversed(d)))
                
main()

Submission status: Accepted

Solution code:
from collections import deque

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

s = deque(s)

rev = 0
for _ in range(q):
    que = input()
    if que[0] == "1":
        rev ^= 1

    else:
        t, f, c = que.split()
        if rev:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)

        else:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)

s = list(s)
if rev:
    print(*s[::-1], sep="")
else:
    print(*s, sep="")

Submission status: Accepted

Solution code:
from collections import deque


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

deq = deque(s)

flag = 1
for i in range(q):
    if query[i][0] == "1":
        flag ^= 1
    else:
        _, f, c = query[i]
        f = int(f) % 2
        if flag ^ f:
            deq.append(c)
        else:
            deq.appendleft(c)

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

Submission status: Accepted

Solution code:
from collections import deque

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

D1 = deque()
D2 = deque()

rev = False

for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        if rev == True:
            rev = False
        else:
            rev = True
    else:
        f = int(q[1])
        c = q[2]
        if f == 1:
            if not rev:
                D1.appendleft(c)
            else:
                D2.append(c)
        else:
            if not rev:
                D2.append(c)
            else:
                D1.appendleft(c)


ans = ''.join(D1)+S+''.join(D2)

if not rev:
    print(ans)

else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = input().rstrip()
Q = int(input())


rev = False
out = deque(S)
for i in range(Q):
    args = input().split()
    if args[0] == '1':
        rev = not rev
    else:
        t, f, c = args
        assert t == '2'
        if f == '1':
            if rev:
                out.append(c)
            else:
                out.appendleft(c)
        else:
            assert f == '2'
            if rev:
                out.appendleft(c)
            else:
                out.append(c)

if rev:
    out = reversed(out)
print(''.join(out))

Submission status: Accepted

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

from collections import deque


S = list(input().rstrip())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

q = deque()
gyaku = False
for s in S:
    q.append(s)

for A in Query:
    if A[0] == "1":
        gyaku =  not gyaku
    else:
        if (gyaku and A[1] == "1") or (not gyaku and A[1] == "2"):
            q.append(A[2])
        else:
            q.appendleft(A[2])


S = []
for p in q:
    S.append(p)

if gyaku:
    S = S[::-1]

print("".join(S))

Submission status: Accepted

Solution code:
import sys
from collections import deque
s = list(input())
q = int(input())
h = deque()
h.extend(s)

rev = False
for _ in range(q):
    que = sys.stdin.readline().split()
    if que[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    elif que[1] == '1':
        if rev:
            h.append(que[2])
        else:
            h.appendleft(que[2])
    else:
        if rev:
            h.appendleft(que[2])
        else:
            h.append(que[2])
if rev:
    print(''.join(list(h)[::-1]))
else:
    print(''.join(list(h)))

Submission status: Accepted

Solution code:
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
reverse_flg = False

for i in range(Q):
    try:
        x, f, c = input().split()
        f = int(f)
        if f == 1 and not reverse_flg:
            d.appendleft(c)
        elif f == 1 and reverse_flg:
            d.append(c)
        elif f == 2 and not reverse_flg:
            d.append(c)
        else:
            d.appendleft(c)
    except:
        reverse_flg = not reverse_flg

if reverse_flg:
    d.reverse()

print("".join(d))

Submission status: Accepted

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

rev = False
head = []
tail = []
for _ in range(Q):
    q = input()
    if q[0] == '1':
        rev = not rev

    else:
        _, f, c = q.split()
        if (f == '1') ^ rev:
            head.append(c)
        else:
            tail.append(c)
ans = ''.join(reversed(head)) + S + ''.join(tail)
if rev:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

isNormal = 1

D = deque(list(S))

for i in range(Q):
    t = tuple(input().split())
    if t[0] == '1':
        isNormal ^= 1
    else:
        a = int(t[1]) + isNormal
        # aが奇数ならば末尾
        if a % 2:
            D.append(t[2])
        else:
            D.appendleft(t[2])

SS = ""
if isNormal % 2:
    SS = ''.join(list(D))
else:
    SS = ''.join(reversed(D))

print(SS)

Submission status: Accepted

Solution code:
from collections import deque

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

rev = False

for _ in range(q):
    ls = list(input().split())
    if ls[0] == '1':
        rev = not rev
    elif rev ^ (ls[1] == '1'):
        s.appendleft(ls[2])
    else:
        s.append(ls[2])

if rev:
    s.reverse()

print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque
rev = False
S = deque(list(input().strip()))

Q = int(input())

for _ in range(Q):
    q = input().split()
    if len(q) == 3 and q[1] == '1':
        if not rev:
            S.appendleft(q[2])
        else:
            S.append(q[2])
    elif len(q) == 3 and q[1] == '2':
        if rev:
            S.appendleft(q[2])
        else:
            S.append(q[2])
    else:
        rev = not rev
if not rev:
    print(''.join(list(S)))
else:
    print(''.join(list(S))[::-1])

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:
from collections import deque


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

for _ in range(q):
    query = input()
    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 = list(S)[::-1]

print(''.join(S))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    S = SI()
    if S=="AAA" or S=="BBB":
        print("No")
    else:
        print("Yes")
    return

def examB():
    N, A, B = LI()
    loop = N//(A+B)
    ans = loop*A + min(A,N%(A+B))
    print(ans)
    return

def examC():
    A, B = LI()
    ans = -1
    for i in range(1,20000):
        if i*8//100==A:
            if i*10//100==B:
                ans = i
                break
    print(ans)
    return

def examD():
    S = SI()
    Q = I()
    rev = 0
    L = ""; R = ""
    for _ in range(Q):
        q = LSI()
        if q[0]=="1":
            rev += 1
        else:
            c = int(q[1])
            if (c+rev)%2==1:
                L += q[2]
            else:
                R += q[2]
    ans = ""
    if rev%2==0:
        ans = L[::-1] + S + R
    else:
        ans = R[::-1] + S[::-1] + L
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math,random
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**6)

if __name__ == '__main__':
    examD()

"""

"""

Submission status: Accepted

Solution code:
S = input()
L = []
R = []
rev = 0
for _ in range(int(input())):
    A = input().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

if rev:
    print("".join(R[::-1]) + S[::-1] + "".join(L))
else:
    print("".join(L[::-1]) + S + "".join(R))

Submission status: Accepted

Solution code:
s = input()
u, v = [], []
q = int(input())
cur = 0
for _ in range(q):
    t, *c = input().split()
    t = int(t)
    if t == 1:
        cur ^= 1
    else:
        f = (int(c[0]) - 1) ^ cur
        if f:
            v.append(c[1])
        else:
            u.append(c[1])
if cur:
    print(''.join(reversed(v)) + ''.join(reversed(s)) + ''.join(u))
else:
    print(''.join(reversed(u)) + s + ''.join(v))

Submission status: Accepted

Solution code:
from collections import deque
s=input()
de=deque(list(s))
rev=0
append,appendleft=de.append,de.appendleft
for _ in range(int(input())):
	t=input()
	if t[0]=="1":
		rev^=1
	else:
		_,f,c=t.split()
		if f=="1":
			if rev:
				append(c)
			else:
				appendleft(c)
		else:
			if rev:
				appendleft(c)
			else:
				append(c)
li=list(de)
if rev:
	li=li[::-1]
print("".join(li))

Submission status: Accepted

Solution code:
import sys
from collections import deque as dq
input = sys.stdin.readline
S = dq(list(input())[: -1])
Q = int(input())
rev = 0
ap = [dq([]), dq([])]
for _ in range(Q):
  t = input().split()
  if int(t[0]) == 1: rev = (rev + 1) % 2
  else:
    f, c = int(t[1]) - 1, t[2]
    if (f + rev) % 2: S.append(c)
    else: S.appendleft(c)
if rev: S.reverse()
print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
d = 0
for a in range(Q):
    # print(S,d)
    line = input()
    if line[0] == "1":
        d = 1 - d
    else:
        line = line.split()
        if line[1] == "1":
            if d == 1:
                S.append(line[2])
            else:
                S.appendleft(line[2])
        else:
            if d == 1:
                S.appendleft(line[2])
            else:
                S.append(line[2])
if d == 0:
    print("".join(S))
else:
    print("".join(S)[::-1])

Submission status: Accepted

Solution code:
raw = input()
l = len(raw)
from collections import deque
s = deque()
for i in range(l):
  s.append(raw[i])
#print(s)
n = int(input())
cnt = 0
for _ in range(n):
  tmp = input().split()
  if tmp[0] == '1':
    cnt = 1 - cnt
  else:
    t = int(tmp[1]) - 1
    t ^= cnt
    if t == 1:
      s.append(tmp[2])
    else:
      s.appendleft(tmp[2])
#print(s)
#print(cnt)
ans = ''
if cnt == 1:
  s.reverse()
for x in s:
  ans += x
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    S = input().rstrip('\n')
    ans = deque()
    ans.append(S)
    q = int(input())
    rev = 1
    for _ in range(q):
        query = input().rstrip('\n')
        if query == '1':
            rev *= -1
        else:
            _, f, c = query.split()
            if rev == 1:
                if f == '1':
                    ans.appendleft(c)
                else:
                    ans.append(c)
            else:
                if f == '1':
                    ans.append(c)
                else:
                    ans.appendleft(c)
    ans = ''.join(ans)
    if rev == -1:
        ans = ans[::-1]
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
query = [input() for _ in range(Q)]

S = deque(S)

inv = 0
for q in query:
    if q[0] == "1":
        inv += 1
    else:
        _, f, c = q.split()
        if f == "1":
            if inv % 2 == 0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if inv % 2 == 0:
                S.append(c)
            else:
                S.appendleft(c)
S = "".join(S)
print(S if inv % 2 == 0 else S[::-1])

Submission status: Accepted

Solution code:
s = input()
from collections import deque
d = deque()
for i in s:
    d.append(i)
mode = 0
Q = int(input())
for _ in range(Q):
    q = input()
    if q[0]=='1':
        mode+=1
        mode%=2
    else:
        t,f,c = q.split()
        f = int(f)
        if (f+mode)%2==1:
            d.appendleft(c)
        else:
            d.append(c)
if mode==1:
    while(len(d)):
        print(d.pop(),end='')
else:
    while(len(d)):
        print(d.popleft(),end='')

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    s = deque(list(input()))
    q = int(input())
    query = [input().split() for _ in [0]*q]

    cnt = 0
    for i in range(q):
        if query[i][0] == "1":
            cnt += 1
        else:
            if (cnt+int(query[i][1])) % 2 == 1:
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
    if cnt % 2 == 0:
        print("".join(s))
    else:
        print("".join(list(reversed(s))))


main()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
arr1 = []
arr2 = []
count = 1
for _ in range(Q):
    q = input()
    if q == "1":
        count *= -1
    else:
        _, f, c = map(str, q.split())
        if f == "1":
            arr1.append(c) if count == 1 else arr2.append(c)
        else:
            arr2.append(c) if count == 1 else arr1.append(c)

if count == 1:
    ans = "".join(arr1[::-1]) + S + "".join(arr2)
else:
    ans = "".join(arr2[::-1]) + S[::-1] + "".join(arr1)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
s = input()
q = int(input())
is_reverse = False
res = deque(list(s))
for i in range(q):
    query = [i for i in sys.stdin.readline().split()]
    if query[0] == "1":
        is_reverse = False if is_reverse else True
    else:
        if query[1] == "1":
            if is_reverse:
                res.append(query[2])
            else:
                res.appendleft(query[2])
        else:
            if is_reverse:
                res.appendleft(query[2])
            else:
                res.append(query[2])
if is_reverse:
    res.reverse()
    print("".join(res))
else:
    print("".join(res))

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
rev = False
pre = []
suf = []
for _ in range(n):
    q = input()
    if q == '1':
        rev = not rev
    else:
        t,f,c = q.split()
        if f == '1':
            if rev:
                suf += [c]
            else:
                pre += [c]
        else:
            if rev:
                pre += [c]
            else:
                suf += [c]

if rev:
    print(''.join(reversed(suf)) + ''.join(reversed(s)) + ''.join(pre))
else:
    print(''.join(reversed(pre)) + ''.join(s) + ''.join(suf))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
h, t = [], []
p = 0
for i in range(q):
	c = input().split()
	if c[0] == '1':
		h, t = t, h
		p = 1 - p
	else:
		if c[1] == '1':
			h.append(c[2])
		else:
			t.append(c[2])
if p == 1:
	s = s[::-1]
print("".join(h[::-1]) + s + "".join(t))

Submission status: Accepted

Solution code:
from collections import deque
sl = list(input())
q = int(input())
dl = deque(sl)
r = 0
for _ in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        r += 1
    else:
        if r % 2 == 0:
            if query[1] == "1":
                dl.appendleft(query[2])
            else:
                dl.append(query[2])
        else:
            if query[1] == "1":
                dl.append(query[2])
            else:
                dl.appendleft(query[2])
if r % 2 == 1:
    dl.reverse()
print("".join(dl))

Submission status: Accepted

Solution code:
S = input()
X = [None]*200001 + list(S) + [None]*200001
ifront = 200000
iback = 200001 + len(S)
rev = False

Q = int(input())
for _ in range(Q):
    query = input().split()
    if len(query)==1: rev = not rev
    else:
        F = int(query[1])
        C = query[2]

        if (not rev and F==1) or (rev and F==2):
            X[ifront] = C
            ifront -= 1
        else:
            X[iback] = C
            iback += 1

ans = ''.join(X[ifront+1:iback])
print(ans if not rev else ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = list(input())
Q = int(input())
s = deque(S)
h = 1


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

print("".join(map(str,list(s)[::h])))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
f = 0
for _ in range(q):
  l = input()
  if l == "1":
    f ^= 1
  else:
    a, b, c = l.split()
    if f ^ (b=="1"):
      s.appendleft(c)
    else:
      s.append(c)
if f:
  ans = list(s)[::-1]
else:
  ans = list(s)
print(*ans, sep="")

Submission status: Accepted

Solution code:
from collections import deque

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

for queries in range(Q):
    q=list(input().split())

    if q[0]=="1":
        flag=1-flag
    else:
        x,y=q[1],q[2]

        if (flag+int(x))%2==0:
            S.append(y)
        else:
            S.appendleft(y)

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

Submission status: Accepted

Solution code:
s = input()
pre = ""
suf = ""
rev = False

for i in range(int(input())):
        q = input().split()
        if len(q)==1: rev = not rev
        else:
                t, f, c = q
                f = int(f)
                if rev: f += 1
                f %= 2
                if f: pre += c
                else: suf += c
if rev: print(suf[::-1] + s[::-1] + pre)
else: print(pre[::-1] + s + suf)

Submission status: Accepted

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

head = ""
tail = ""
rev = False
for _ in range(Q):
	query = input().strip().split()
	if query[0] == "1":
		rev = not rev
	else:
		if query[1] == "1":
			if rev:
				tail = tail + query[2]
			else:
				head = query[2] + head
		else:
			if rev:
				head = query[2] + head
			else:
				tail = tail + query[2]

result = head + S + tail

if rev:
	result = "".join(reversed(result))

print(result)

Submission status: Accepted

Solution code:
ans = input()
q = int(input())
r = 0
p = ""
for i in range(q):
    d = input().split()
    if len(d)==1:
        r += 1
    else:
        f = int(d[1])
        c = d[2]
        if (f+r)%2 == 1:
            p += c
        else:
            ans += c
ans = p[::-1] + ans
if r%2:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
Q = int(input())
num = 1
q = deque(S)
for _ in range(Q):
    L = [i for i in input().split()]
    if len(L) == 1:
        num += 1
        num %= 2
    else:
        if int(L[1])%2 == num:
            q.appendleft(L[2])
        else:
            q.append(L[2])

ans = list(q)
if num == 0:
    ans.reverse()
print("".join(ans))

Submission status: Accepted

Solution code:
S = input()

Q = int(input())

mode = True
top = []
bottom = []

for i in range(Q):
    q = input().split()
    if len(q)==1:
        q=int(q[0])
        if q==1:
            if mode:
                mode=False
            else:
                mode=True
    else:
        t,f,c = q[0],q[1],q[2]
        if f=="1":
            if mode:
                top.append(c)
            else:
                bottom.append(c)
        else:
            if mode:
                bottom.append(c)
            else:
                top.append(c)

if mode:
    ans = top[::-1] + [S] + bottom
    print("".join(ans))
else:
    ans = bottom[::-1] + [S[::-1]] + top
    print("".join(ans))

Submission status: Accepted

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


q = int(input())
rev = 0
for _ in range(q):
    query = list(input().split())
    if len(query) == 1:
        rev += 1
    else:
        if query[1] == "1":
            if rev % 2 == 0:
                s.appendleft(query[2])
            else:
                s.append(query[2])

        else:
            if rev % 2 == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])

s = "".join(s)
if rev%2 == 1:
    s = s[::-1]

print(s)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    S = input()
    q = deque(S)
    order = 1
    Q = int(input())
    for _ in range(Q):
        X = input().split()
        if X[0] == "1":
            order = 3 - order
        else:
            f, c = int(X[1]), X[2]
            if f == order:
                q.appendleft(c)
            else:
                q.append(c)
    ans = "".join(q)
    if order == 2:
        ans = ans[::-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

from collections import deque
q = deque(S)
rev = False
for _ in range(Q):
    qr = input()
    if qr == '1':
        rev = not rev
        continue
    _,f,c = qr.split()
    if (f=='2') ^ rev:
        q.append(c)
    else:
        q.appendleft(c)

if rev:
    print(''.join(reversed(list(q))))
else:
    print(''.join(q))

Submission status: Accepted

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

r_addition = ""
l_addition = ""
is_rev = False

for i in range(q):
    qs = input().split()

    if len(qs) == 1:
        is_rev = not is_rev
        # print(is_rev)
    else:
        if int(qs[1]) == 1:
            if is_rev:
                r_addition += qs[2]
            else:
                l_addition += qs[2]
        else:
            if is_rev:
                l_addition += qs[2]
            else:
                r_addition += qs[2]

# print(l_addition)
# print(r_addition)
#
# print(l_addition[::-1])
# print(r_addition[::-1])

if is_rev:
    print(r_addition[::-1]+s[::-1]+l_addition)
else:
    print(l_addition[::-1]+s+r_addition)

Submission status: Accepted

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

from collections import deque
ans = deque(list(s))
flip = 0

for i in range(q):
    # print(ans, flip)
    _query = query[i]
    if len(_query) == 1:
        flip = 1 - flip
    else:
        if _query[1] == '1':
            if flip:
                ans.append(_query[2])
            else:
                ans.appendleft(_query[2])
        else:
            if not flip:
                ans.append(_query[2])
            else:
                ans.appendleft(_query[2])

if not flip:
    print(''.join(ans))
else:
    print(''.join(list(ans)[::-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

s = collections.deque(input())
Q = int(input())
for _ in range(Q):
  I = input()
  if len(I) == 1:
    count += 1
  else:
    F = int(I[2])
    C = I[4]
    k = (count + F ) % 2
    if k == 0:
      s.append(C)
    else:
      s.appendleft(C)
if count % 2:
  s.reverse()
for i in s:
  print(i,end = "")
print()

Submission status: Accepted

Solution code:

S = list(input())

Q = int(input())

state = 0
s = []
e = []

for loop in range(Q):

    T = input()

    if T == "1":
        state ^= 1
    else:

        t,F,C = T.split()
        F = int(F)
        

        if state == 0 and F == 1:
            s.append(C)
        elif state == 0 and F == 2:
            e.append(C)
        elif F == 1:
            e.append(C)
        else:
            s.append(C)

s.reverse()
ans = s+S+e
if state == 1:
    ans.reverse()

print ("".join(ans))

Submission status: Accepted

Solution code:
import sys;input=sys.stdin.readline
S=input().strip()
n=len(S)
Qs = []
for c in S:
    Qs.append((1,1,c))
for q in range(int(input())):
    x = input().strip().split()
    if len(x) == 1:
        Qs.append((0,0,0))
    else:
        n+=1
        Qs.append((1,int(x[1])-1,x[2]))

rs = [0]*n
i,j=0,n-1
f=1
for t, tt, c in Qs[::-1]:
    if t==0:
        f=1-f
    else:
        if (tt+f)%2:
            rs[i] = c
            i+=1
        else:
            rs[j] = c
            j -= 1
print("".join(rs))