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

def msf(s,queries):
    queue = deque(s)
    addl = queue.appendleft
    addr = queue.append
    is_reversed = 0

    for query in queries:
        if query[0] == '1':
            is_reversed ^= 1
            addl,addr = addr,addl
        elif query[0] == '2':
            f = query[1]
            c = query[2]
            if f == '1':
                addl(c)
            elif f == '2':
                addr(c)
    return ''.join(queue) if not is_reversed else ''.join(reversed(queue))

if __name__ == '__main__':
    s = list(input())
    q = int(input())
    ql = [input().split() for i in range(q)]
    print(msf(s,ql))

Submission status: Accepted

Solution code:
# import bisect
# import heapq
# from copy import deepcopy
from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np
# import math

# n, k = map(int, input().split())
# a = list(map(int, input().split()))
s = deque(input())
q = int(input())
rev = False
for i in range(q):
    que = input()
    if que[0] == '1':
        rev = not rev
    else:
        que = list(que.split())
        if (int(que[1])==1 and not rev) or (int(que[1])==2 and rev):
            s.appendleft(que[2])
        else:
            s.append(que[2])

if rev: ans = ''.join(reversed(s))
else: ans = ''.join(s)
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = not reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front = front+C
        else:
            end = end+C
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    S = list(input().strip())
    Q = int(input())

    d = deque(S)
    mode = True

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

        if query[0] == "1":
            mode = not mode
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                if mode:
                    d.appendleft(C)
                else:
                    d.append(C)
            elif F == "2":
                if mode:
                    d.append(C)
                else:
                    d.appendleft(C)

    s = list(d)
    if not mode:
        s = s[::-1]

    print(''.join(s))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''
#最高480msぐらい
from collections import deque
letter = deque(input())
Q= int(input())
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: #まえに追加
            if reversed == False:
                letter.appendleft(query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.appendleft(query[2])
if reversed==True:
    letter.reverse()

output = ''.join(letter)
print(output)
'''

from collections import deque
true_letter = input()
Q= int(input())
letter = deque(['*'])
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: #まえに追加
            if reversed == False:
                letter.appendleft(query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.appendleft(query[2])
if reversed==True:
    letter.reverse()
    true_letter = true_letter[::-1]

output = ''.join(letter)
output = output.replace('*',true_letter)
print(output)

Submission status: Accepted

Solution code:
from collections import deque
letter = deque(input())
Q= int(input())
reversed = False
for i in range(Q):
    query = input().split()
    if len(query)==1:
        reversed = not bool(reversed)
    else:
        if int(query[1])==1: #まえに追加
            if reversed == False:
                letter.appendleft(query[2])
            else:
                letter.append(query[2])
        else:
            if reversed == False:
                letter.append(query[2])
            else:
                letter.appendleft(query[2])
if reversed==True:
    letter.reverse()

output = ''.join(letter)
print(output)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
line = True
first, last = '',''

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        line = True if line==False else False
    else:
        if line == True:
            if q[1] == '1':
                first = q[2] + first
            else:
                last = last + q[2]
        elif line==False:
            if q[1] == '1':
                last = last + q[2]
            else:
                first = q[2] + first
S = first+S+last
print(S if line else S[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
Query = [input().split() for _ in range(Q)]

reverse = False
for query in Query:
    if query[0] == '1':
        reverse = not reverse
    else:
        if query[1] == ('2' if reverse else '1'):
            S.appendleft(query[2])
        else:
            S.append(query[2])
print(''.join(reversed(S) if reverse else S))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
flag = False
l = ''
r = ''
for _ in range(q):
  Q = input().split()
  if Q[0] == '1':
    flag = not flag
  else:
    if flag:
      if Q[1] == '1':
        r += Q[2]
      else:
        l += Q[2]
    else:
      if Q[1] == '1':
        l += Q[2]
      else:
        r += Q[2]
if flag:
  print(''.join(reversed(r)), ''.join(reversed(s)), l, sep='')
else:
  print(''.join(reversed(l)), s, r, sep='')

Submission status: Accepted

Solution code:
from collections import deque


s = deque(list(input()))
n = int(input())
flag = True
for _ in range(n):
    _input = input().split()
    if len(_input) == 1:
        if flag == True:
            flag = False
        else:
            flag = True
    else:
        if (_input[1] == '1' and flag == True) or (_input[1] == '2' and flag == False):
            s.appendleft(_input[2])
        else:
            s.append(_input[2])

if flag == True:
    print("".join(s))
else:
    print("".join([x for x in reversed(s)]))

Submission status: Accepted

Solution code:
from collections import deque

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

queries = [input().strip().split() for _ in range(q)]
is_reversed = False

for query in queries:
    t = query[0]
    if t == '1':
        is_reversed = not is_reversed
    else:
        f = query[1]
        c = query[2]
        if f == '1':
            if is_reversed:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if is_reversed:
                s.appendleft(c)
            else:
                s.append(c)

str_list = list(s)
if is_reversed:
    str_list = reversed(str_list)

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

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(list(s))
for _ in range(q):
    qi = input()
    if len(qi) == 1:
        # query 1
        is_reverse = not is_reverse
    else:
        # query 2
        _, f, c = qi.split()
        f1 = f == '1'
        if f1 == is_reverse:
            d.append(c)
        else:
            d.appendleft(c)

d = list(d)
if is_reverse:
    d = d[::-1]
ans = ''.join(d)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
rev = False
ans = deque(S)
for i in range(Q):
    tfc = input()
    if tfc == '1':
        rev = not rev
    else:
        t,f,c = tfc.split()
        if f == '1':
            if rev: ans.append(c)
            else: ans.appendleft(c)
        elif f == '2':
            if rev: ans.appendleft(c)
            else: ans.append(c)

if rev: ans.reverse()

print(''.join(ans))

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

# not AC
# def get_result(data):
#     S, Q = data[0][0], int(data[1][0])
#     for i in range(Q):
#         if data[i+2][0] == '1':
#             S = S[::-1]
#         else:
#             F, C = data[i+2][1], data[i+2][2]
#             if F == '1':
#                 S = C + S
#             else:
#                 S = S + C
#     return S


def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    # 文字列と反転状況を別で判断する
    ans = deque(S)
    cnt = 0
    for i in range(Q):
        if data[i+2][0] == '1':
            cnt += 1
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                if cnt % 2 == 0:
                    # 前に追加
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if cnt % 2 == 0:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    ans = ''.join(ans)
    if cnt % 2 == 1:
        ans = ans[::-1]

    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

# not AC
# def get_result(data):
#     S, Q = data[0][0], int(data[1][0])
#     for i in range(Q):
#         if data[i+2][0] == '1':
#             S = S[::-1]
#         else:
#             F, C = data[i+2][1], data[i+2][2]
#             if F == '1':
#                 S = C + S
#             else:
#                 S = S + C
#     return S


def get_result(data):
    S, Q = data[0][0], int(data[1][0])
    # 文字列と反転状況を別で判断する
    ans = deque(S)
    cnt = 0
    for i in range(Q):
        if data[i+2][0] == '1':
            cnt += 1
        else:
            F, C = data[i+2][1], data[i+2][2]
            if F == '1':
                if cnt % 2 == 0:
                    ans.appendleft(C)
                else:
                    ans.append(C)
            else:
                if cnt % 2 == 0:
                    ans.append(C)
                else:
                    ans.appendleft(C)

    if cnt % 2 == 1:
        ans = ''.join(reversed(ans))
    else:
        ans = ''.join(ans)

    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip('\n') for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(q):
    cmd = input().split()
    if cmd[0] == '1':
        rev_f = (False if rev_f else True)
    else:
        f = cmd[1]
        c = cmd[2]
        if (rev_f and f == '1') or (not rev_f and f != '1'):
            s.appendleft(c)
        else:
            s.append(c)

print(''.join(s if rev_f else reversed(s)))

Submission status: Accepted

Solution code:
S,f=__import__('collections').deque(input()),0
for _ in range(int(input())):
	T=input()
	if'2'>T:f=1-f
	else:(S.appendleft if(T[2]<'2')^f else S.append)(T[4])
if f:S.reverse()
print(*S,sep='')

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = list(input())
    Q = int(input())
    l = [list(input().split(' ')) for _ in range(Q)]

    d = deque(S)

    FLAG = False

    for li in l:
        if int(li[0]) == 1: # 前後反転
            if FLAG == True:
                FLAG = False
            else:
                FLAG = True
        
        elif int(li[0]) == 2: # 追加, li[1] == 1 => li[2]を先頭に，li[1] == 2 => li[2]を末尾
            if int(li[1]) == 1:
                if FLAG == True:
                    d.append(li[2])
                else:
                    d.appendleft(li[2])
            elif int(li[1]) == 2:
                if FLAG == True:
                    d.appendleft(li[2])
                else:
                    d.append(li[2])

    S = ''.join(d)
    if FLAG:
        S = S[::-1]
    print(S)


main()

Submission status: Accepted

Solution code:
S,f=__import__('collections').deque(input()),0
for _ in range(int(input())):
	T=input()
	if T<'2':f=1-f
	else:(S.appendleft if (T[2]<'2')^f else S.append)(T[4])
if f:S.reverse()
print(*S,sep='')

Submission status: Accepted

Solution code:
S=str(input())
S_list=list(S)

Q=int(input())

q=[]
right=[]
left=[]

rev=1

for i in range(Q):
    q.append(input().split())
    if q[i][0]=="1":
        rev=rev*(-1)
    elif q[i][0]=="2":
        if q[i][1]=="1":
            if rev==1:
                right.append(q[i][2])
            else:
                left.append(q[i][2])
        elif q[i][1]=="2":
            if rev==1:
                left.append(q[i][2])
            else:
                right.append(q[i][2])
right.reverse()
if rev==1:
    S_list=right+S_list+left
else:
    S_list=right+S_list+left
    S_list.reverse()

S=''.join(S_list)
print(S)

Submission status: Accepted

Solution code:
S = input()
l1 = []
l2 = []
rev = False
for _ in range(int(input())):
    l = [s for s in input().split()]
    if l[0] == "1":
        rev = not rev
    elif l[1] == "1":
        if rev:
            l2.append(l[2])
        else:
            l1.append(l[2])
    else:
        if rev:
            l1.append(l[2])
        else:
            l2.append(l[2])

if rev:
    ret = "".join(l2)[::-1] + S[::-1] + "".join(l1)
else:
    ret = "".join(l1)[::-1] + S + "".join(l2)

print(ret)

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
q=int(input())
rev=False
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    rev=not rev
  elif c[1]=='1' and rev==False:
    S.appendleft(c[2])
  elif c[1]=='2' and rev==True:
    S.appendleft(c[2])
  else:
    S.append(c[2])
if rev:
  S.reverse()
ans=''.join(S)
print(ans)

Submission status: Accepted

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

pres = []
posts = []
mode = 0
for _ in range(q):
    query = input().split()
#     print(s, query, mode, pres, posts)
    if len(query) == 1:
        mode = 1 - mode
    else:
        if mode == 0:
            if query[1] == "1":
                pres.append(query[2])
            else:
                posts.append(query[2])
        else:
            if query[1] == "1":
                posts.append(query[2])
            else:
                pres.append(query[2])
s = "".join(pres[::-1]) + s + "".join(posts)
if mode == 1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
S = input()
T = ''
Q = int(input())
flag = True
for i in range(Q):
    que = input()
    if len(que) == 1:
        if flag:
            flag = False
        else:
            flag = True
    else:
        if que[2] == '1':
            if flag:
                T = T+que[4]
            else:
                S = S+que[4]
        else:
            if flag:
                S = S+que[4]
            else:
                T = T+que[4]
if flag:
    U = ''
    for i in range(len(T)):
        U += T[len(T)-i-1]
    print(U+S)
else:
    U = ''
    for i in range(len(S)):
        U += S[len(S)-i-1]
    print(U+T)

Submission status: Accepted

Solution code:
import sys, math
from collections import deque

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

count_reverse = 0
for i in range(Q):
  q = [j for j in input().split()]
  if len(q) == 1:
    count_reverse += 1
  else:
    if q[1] == '1':
      if count_reverse % 2 == 0:
        S.appendleft(q[-1])
      else:
        S.append(q[-1])
    else:
      if count_reverse % 2 == 0:
        S.append(q[-1])
      else:
        S.appendleft(q[-1])
        
ret = ''
if count_reverse % 2:
  for s in list(S)[::-1]:
    ret = ret + s
else:
  for s in S:
    ret = ret + s
    
print(ret)

Submission status: Accepted

Solution code:
from collections import deque

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

rev = False
for i in range(Q):
	query = input().split()
	if query[0] == '1':
		rev = not rev
	else:
		if (query[1] == '1' and rev == False) \
		or (query[1] == '2' and rev == True):
			S.appendleft(query[2])
		else:
			S.append(query[2])

if rev:
	S.reverse()

print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
cnt = 0
for i in range(q):
  query = input()
  if query == '1':
    cnt += 1
  else:
    t, f, c = query.split()
    if (f == '1' and cnt % 2 == 0) or (f == '2' and cnt % 2 == 1):
      s.appendleft(c)
    else:
      s.append(c)
if cnt % 2 == 0:
  print(''.join(s))
else:
  s.reverse()
  print(''.join(s))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
front=""
back=s
reverse=False

for i in range(n):
  t=input().split()
  if t[0]=="1":
    reverse=not reverse
  else:
    if (t[1]=="1")^reverse:
      front=front+t[2]
    else:
      back=back+t[2]
if reverse:
  print(back[::-1]+front)
else:
  print(front[::-1]+back)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
flip = False # False: left side is the head
for i in range(Q):
  Query_i = list(map(str, input().split()))
  if Query_i[0]=='1':
    flip = not flip
  elif Query_i[1] == '1' and not flip:
    S.appendleft(Query_i[2])
  elif Query_i[1] == '1' and flip:
    S.append(Query_i[2])
  elif Query_i[1] == '2' and not flip:
    S.append(Query_i[2])
  elif Query_i[1] == '2' and flip:
    S.appendleft(Query_i[2])
if flip:
  S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = S

for _ in range(Q):
    I = input().split()
    if I[0] == "1":
        reverse = not reverse
    else:
        if (I[1] == "1") ^ reverse:
            front =front + I[2]
        else:
            end = end + I[2]


if reverse:
    print(end[::-1]+front)
    #print("".join(list(reversed(end)))+front)
else:
    print(front[::-1]+end)
    #print("".join(list(reversed(front)))+end)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = not reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front = front+C
        else:
            end = end+C
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""
 
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        reverse = not reverse
    else:
        if (inp[1] == "1") ^ reverse:
            front = front+inp[2]
        else:
            end = end+inp[2]
 
if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
 
A = ""
B = ""
count = 0

Que = []
ope = []

def rev(s):
    re = ""
    for i in range(len(s)):
        re = re + s[len(s) - 1 - i]
    return re
 
for i in range(Q):
    k = input()
    if k == "1":
        count += 1
        Que.append("1")
        ope.append("0")
    else:
        Que.append("2")
        ope.append([k[2],k[4]])
 
cou = 0
 
if count % 2 == 0:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = ope[i][1] + A
            else:
                B = B + ope[i][1]
else:
    for i in range(Q):
        if Que[i] == "1":
            cou += 1
        else:
            if (int(ope[i][0]) + cou) % 2 == 1:
                A = A + ope[i][1]
            else:
                B = ope[i][1] + B
 
if count % 2 == 0:
    S = A + S + B
else:
    S = rev(S)
    S = B + S + A
 
print(S)

Submission status: Accepted

Solution code:
import sys
# import numpy as np

# S = input()
# N, M = map(int, input().split())
# As = [int(input()) for _ in range(N)]
# nums = list(map(int, input().split()))
# nums_2d = [list(map(int, input().split())) for _ in range(N)]


def main():
    S = input()
    Q = int(input())
    print(solve(S, Q))


def solve(S, Q):
    input = sys.stdin.readline

    prefix = ''
    suffix = ''

    switch1 = 1
    switch2 = 1

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

        q0 = int(query[0])

        if q0 == 1:
            switch1 *= -1
            continue

        F = int(query[1])
        C = query[2]

        if F == 2:
            F = -1

        switch2 = switch1 * F

        if switch2 == 1:
            prefix = C + prefix

        else:
            suffix = suffix + C

    ans = prefix + S + suffix

    if switch1 == -1:
        ans = ans[::-1]

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
ans = deque(S)
cnt = 0
for i in range(Q):
    qlst = list(map(str, input().split()))
    if qlst[0] == '1':
        cnt += 1
        continue
    else:
        if qlst[1] == '1':
            if cnt % 2 == 0:
                ans.appendleft(qlst[2])
            else:
                ans.append(qlst[2])
        else:
            if cnt % 2 == 0:
                ans.append(qlst[2])
            else:
                ans.appendleft(qlst[2])

if cnt % 2 == 1:
    print(''.join(reversed(ans)))
else:
    print(''.join(ans))

Submission status: Accepted

Solution code:

s = input()
q = int(input())
t = []
 
def F(f,fr,ba,c):
  if f == '1':
     return c + fr, ba
  if f == '2':
    return fr,ba + c

def R(r):
  if r == '1':return '2'
  if r == '2':return '1'

ans = s
fr = ''
ba = ''
r = '1'
for n in range(q):
  t = input().split()
  if t[0] == '1':
    r = R(r)
  else:
    if r =='2':
      t[1] = R(t[1])
    fr, ba = F(t[1],fr,ba,t[2])
 
    
if r == '1': 
   print(fr + ans + ba)
else:
  print(ba[::-1] + ans[::-1]+ fr[::-1])

Submission status: Accepted

Solution code:

S = input()

ql = []
for i in range(int(input())):
    ql.append(input())

rev = False
add_head = ""
add_tail = ""

for query in ql:
    if query == "1":
        rev = not rev
    else:
        _, tp, s2 = query.split(" ")

        if tp == "1":
            if not rev:
                add_head = s2 + add_head
            if rev:
                add_tail = add_tail + s2[::-1]
        else:
            if not rev:
                add_tail = add_tail + s2
            if rev:
                add_head = s2[::-1] + add_head

S = add_head + S + add_tail
if rev:
    S = S[::-1]

print(S)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import deque

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
# print(d)
flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        # print(len(d[0]))
        if len(d) == 1 and len(d[0]) == 1:
            pass
        else:
            flag = ~flag

        #     if len(d[0])!=:
        #         flag = ~flag
        # if len(d[0]) != 1:
        #     flag = ~flag
            # print(flag)
        # flag = ~flag
        # S=S[::-1]
        # d.reverse()

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.append(s[2])
                # print(d)
            # d.appendleft(s[2])
            # S.insert(0,s[2])

        else:
            if flag == 0:
                d.append(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.appendleft(s[2])
            # d.append(s[2])
            # S.append(s[2])
# print(flag)
ans = "".join(d)
# print(ans)
if flag == -1:
    ans = ans[::-1]
    # d.reverse()
    # print(d)

# print("".join(d))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
q = int(input())
rv = False
fr = deque()
re = []
for i in range(q):
    t = input().split(' ')
    if t[0] == '1':
        rv = True if not rv else False
    else:
        if t[1] == '1' and not rv or t[1] == '2' and rv:
            fr.appendleft(t[2])
        else:
            if t[1] == '1' and rv or t[1] == '2' and not rv:
                re.append(t[2])
if rv:
    re.reverse()
    s.reverse()
    fr.reverse()
    print(''.join(re + s) + ''.join(fr))
else:
    print(''.join(fr) + ''.join(s + re))

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(['S'])

#d = ['S']

rev = False

for i in range(q):
    if query[i][0] == '1':
        rev = not rev
        continue
    
    if not rev:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    else:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])

#ans = deque()
if rev:
    #d = ''.join(list(reversed(d)))
    s = s[::-1]



while len(d):
    if rev:
        tmp = d.pop()
    else:
        tmp = d.popleft()
    if tmp == 'S':
        #ans.append(s)
        print(s,end='')
    else:
        print(tmp,end='')
        #ans.append(tmp)

#print(*ans,sep='')

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == '__main__':
    s = input()
    q = int(input())
    ans = s
    rev_count = 0
    leftList = deque()
    rightList =[]
    alist =[]

    for i in range(q):
        a = [i for i in input().split()]
        alist.append(a)
        if len(a) == 1:
            rev_count = rev_count +1

    if rev_count % 2 ==1:
        ans = ans[::-1]

    for i in alist:
        if len(i) == 1:
            rev_count = rev_count -1
        else:
            if int(i[1]) == 1:
                if rev_count % 2 == 1:
                    rightList.append(i[2])
                else:
                    leftList.appendleft(i[2])

            else:
                if rev_count % 2 == 1:
                    leftList.appendleft(i[2])
                else:
                    rightList.append(i[2])

    ans = ''.join(leftList)+ ans+''.join(rightList)

    print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
muki = True
sento = []
matubi = []
for _ in range(Q):
    L = input()
    if L[0] == '1':
        muki = not muki
    else:
        _, F, C = L.split()
        F = int(F)
        if (muki and F == 1) or ((not muki) and F == 2):
            sento.append(C)
        else:
            matubi.append(C)          

if muki:
    print(''.join(reversed(sento)), end='')
    print(S, end='')
    print(''.join(matubi))
else:
    print(''.join(reversed(matubi)), end='')
    print(''.join(list(reversed(S))), end='')
    print(''.join(sento))

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(n):
	com = input().split()
	if com[0] == '1':
		status = not status
	elif int(com[1])+status == 2:
		s.appendleft(com[2])
	else:
		s.append(com[2])
        
if not status:
	s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = str(input())
q = int(input())  
st = s
sth = s
sth2 = ""
stm = ""
stm2 = ""
count = 0
flagh = 0
flagm = 0
for i in range(q):
  k = input().split()
  if(k[0] == '1'):
    count = (count + 1) % 2
  else:
    c = count - 2
    d = int(k[1]) 
    e = k[2]
    if(c + d != 0):
      sth = e + sth
      if(len(sth) > 5000 and flagh == 0):
        flagh = 1
        sth2 = sth
        sth = ""
    else:
      stm = stm + e
      if(len(stm) > 5000 and flagm == 0):
        flagm = 1
        stm2 = stm
        stm = ""
      

st = sth + sth2 + stm2 + stm
if(count == 1):
  st = st[::-1]
  
print(st)

Submission status: Accepted

Solution code:
from collections import deque

reverse = False

S = deque(list(input()))

N = int(input())

for _ in range(N):
    cmd = input()
    if cmd[0] == '1':
        reverse = not reverse
    else:
        cmd = cmd.split()
        if cmd[1] == '1':
            if reverse:
                S.append(cmd[2])
            else:
                S.appendleft(cmd[2])
        else:
            if reverse:
                S.appendleft(cmd[2])
            else:
                S.append(cmd[2])

if reverse:
    S.reverse()

print(''.join(S))