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:
s = input()
q = int(input())
st = 0
str1 = ""
str2 = ""
for _ in range(q):
    query = list(input().split())
    if query[0] == "1":
        st = 1 if st == 0 else 0
    else:
        if query[1] == "1":
            if st == 0:
                str1 += query[2]
            else:
                str2 += query[2] 
        else:
            if st == 0:
                str2 += query[2] 
            else:
                str1 += query[2] 

if st == 0:
    ans = str1[::-1] + s + str2
else:
    ans = str2[::-1] + s[::-1] + str1
    
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
info = []
for _ in range(Q):
    info.append(input().split())
# print(info)
rev = 1
ans = deque(list(S))
for i in range(Q):
    if info[i][0] == '1':
        rev *= -1
    else:
        if rev == 1:
            if info[i][1] == '1':
                ans.appendleft(info[i][2])
            else:
                ans.append(info[i][2])
        else:
            if info[i][1] == '1':
                ans.append(info[i][2])
            else:
                ans.appendleft(info[i][2])

if rev == -1:
    ans.reverse()

# print(ans)
print(''.join(ans))

Submission status: Accepted

Solution code:
# li = [[0] * 4 for i in range(3)]

# A = list.map(int, input().split())

S = input()

Q = int(input())
hanten = 0

ato = []
mae = []

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

    one = int(li[0])
    if one == 1:
        hanten += 1
    else:
        F = int(li[1])
        C = li[2]
        if (hanten + F) % 2 == 1:
            mae.append(C)
        else:
            ato.append(C)

if hanten % 2 == 0:
    mae.reverse()
    print("".join(mae) + S + "".join(ato))
else:
    ato.reverse()
    S1 = "".join(list(reversed(S)))

    print("".join(ato) + S1 + "".join(mae))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
QUERY = []
for i in range(Q):
    # q = list(map(int, input().split()))
    q = input().split()
    if q[0] == '1':
        QUERY.append([int(q[0])])
    else:
        QUERY.append([int(q[0]), int(q[1]), q[2]])

st = []
en = []
rev = False
rev_c = 0
for q in QUERY:
    if q[0] == 1:
        if rev:
            rev = False
        else:
            rev = True
        rev_c += 1
    else:
        if q[1] == 1:
            if not rev:
                st.append(q[2])
            else:
                en.append(q[2])
        else:
            if not rev:
                en.append(q[2])
            else:
                st.append(q[2])

# print(st)
# print(en)

st_s = "".join(st)
st_s = st_s[::-1]
en_s = "".join(en)

ans = st_s + S + en_s
if rev_c % 2 == 0:
    pass
else:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
s = deque(s)
n = int(input())
cnt = 0
for i in range(n):
    q = list(map(str,input().split()))
    if q[0] == '1':
        cnt += 1
    else:
        if cnt % 2 == 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])
    #print(q)
if cnt % 2 != 0:
    s.reverse()
print(''.join(list(s)))

Submission status: Accepted

Solution code:
import collections


def resolve():
    s = input()
    n = int(input())
    d = collections.deque()
    d.appendleft(s)

    rev = False
    for _ in range(n):
        q = list(input().split())

        if len(q) == 1:
            rev = not rev

        else:
            if q[1] == '1':
                las = False

            else:
                las = True

            if rev:
                las = not las

            if las:
                d.append(q[2])

            else:
                d.appendleft(q[2])

    dstr = ''.join(d)

    if rev:
        print(dstr[::-1])

    else:
        print(dstr)
        
resolve()

Submission status: Accepted

Solution code:
#input = raw_input

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

inputs = []
n_reverse = 0
for i in range(q):
    input_ = input()
    if input_[0] == "1":
        n_reverse += 1
    inputs.append(input_)

if n_reverse % 2 == 0:
    final_rev = False
else:
    final_rev = True

rev = False
mae = ""
ushiro = ""
for input_ in inputs:
    elms = input_.split(" ")
    t = int(elms[0])
    if elms[0] == "1":
        rev = not rev
    else:
        f = int(elms[1])
        c = elms[2]

        if f == 1:
            if final_rev:
                if rev:
                    mae = c + mae
                else:
                    ushiro = ushiro + c
            else:
                if rev:
                    ushiro = ushiro + c
                else:
                    mae = c + mae
        else:
            if final_rev:
                if rev:
                    ushiro = ushiro + c
                else:
                    mae = c + mae
            else:
                if rev:
                    mae = c + mae
                else:
                    ushiro = ushiro + c

if final_rev:
    s = s[::-1]

out = mae + s + ushiro
print(out)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
count = 0
right = ''
left = ''
for i in range(Q):
    arr = list(input().split())
    if len(arr) == 1:
        count += 1
    else:
        if arr[1] == '1':
            if count%2==0:
                left = arr[2] + left
            else:
                right = right + arr[2]
        else:
            if count%2!=0:
                left = arr[2] + left
            else:
                right = right + arr[2]
S = left + S + right
if count%2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

s=ss()
q=ii()

arr=deque(list(s))
chk=0
for i in range(q):
    x=input()
    if x=="1":
        if chk==1:
            chk=0
        else:
            chk=1
    
    else:
        j,k,l=map(str,x.split())

        if chk==0 and k=="1":
            arr.extendleft(deque(list(l)))
            
        elif chk==0 and k=="2":
            arr.extend(deque(list(l)))
        elif chk==1 and k=="1":
            arr.extend(deque(list(reversed(list(l)))))
        elif chk==1 and k=="2":
            arr.extendleft(deque(list(reversed(list(l)))))

if chk==0:
    print("".join(list(arr)))
else:
    print("".join(reversed(list(arr))))

Submission status: Accepted

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

flag = False
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    flag = not flag
  elif query[0] == '2':
    left = query[1] == '1'
    if flag:
      left = not left
    if left:
      S.appendleft(query[2])
    else:
      S.append(query[2])

if flag:
  print(''.join(reversed(S)))
else:
  print(''.join(S))

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    flag = 0
    cnt = 0
    front = []
    back = []

    for i in range(q):
        t = input()
        if len(t) == 1:
            cnt += 1
            if flag == 0:
                flag = 1
            elif flag == 1:
                flag = 0
        else:
            _, a, b = (str(i) for i in t.split())
            if a == "1":
                if flag == 0:
                    front.append(b)
                elif flag == 1:
                    back.append(b)
            elif a == "2":
                if flag == 0:
                    back.append(b)
                elif flag == 1:
                    front.append(b)

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

    print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
sin = stdin.readline().rstrip()
q = int(stdin.readline().rstrip())
li = [list(stdin.readline().rstrip().split()) for _ in range(q)]
point = 0
omote = ""
ura = ""
for i in li:
    if i[0] == "1":
        point += 1
    elif i[0] == "2":
        if i[1] == "1":
            if point%2 == 0:
                omote += i[2]
            else:
                ura += i[2]
        else:
            if point%2 == 0:
                ura += i[2]
            else:
                omote += i[2]
if point%2 == 0:
    omote = omote[::-1]
    sin = omote + sin + ura
    print(sin)
else:
    sin = sin[::-1]
    ura = ura[::-1]
    sin = ura + sin + omote
    print(sin)

Submission status: Accepted

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

    s_old = list(str(input()))
    q = int(input())
    pl_mi = 1
    pl_mi_2 = 1
    query = [list(input().split()) for i in range(q)]

    s = deque(s_old)
    s.pop()

    for i in range(q):
        if query[i][0] == "1":
            pl_mi = pl_mi * (-1)
            continue
        else:
            if query[i][1] == "1":
                pl_mi_2 = 1
            else:
                pl_mi_2 = -1
            if pl_mi * pl_mi_2 == 1:
                s.appendleft(query[i][2])
            else:
                s.append(query[i][2])
    
    if pl_mi == 1:
        s_new = "".join(list(s))
        print(s_new)
    else:
        s.reverse()
        s_new = "".join(list(s))
        print(s_new)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
d = deque(s)
# d.append(s)
# print(d)

Q = int(input())
dir = 1


def func(f, c, dir):
    # print(f, c, dir)
    # dir = 1のとき
    if dir == 1:
        if f == 1:
            d.appendleft(c)
        else:
            d.append(c)
    # dir = -1のとき
    if dir == -1:
        if f == 1:
            d.append(c)
        else:
            d.appendleft(c)
    # print(d)


for i in range(Q):
    q = input()
    if q[0] == "1":
        dir *= -1
    else:
        z, f, c = q.split()
        func(int(f), c, dir)

if dir == -1:
    d.reverse()
print(*d, sep="")

Submission status: Accepted

Solution code:
se=input()
q=int(input())
h=0
se1=""
se2=""
for i in range(q):
    s=input().split()
    if len(s)==1:
        h+=1
    elif len(s)==3:
        if (h+int(s[1])-1)%2==0:
            se1+=s[2]
        else:
            se2+=s[2]
r=se1[::-1]+se+se2
if h%2==0:
    print(r)
else:
    print(r[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
ud = False
ud_count = 0
sub_s = ""
for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        ud = not(ud)
        ud_count += 1
    else:
        if (int(query[1])==1 and ud==False) or (int(query[1])==2 and ud==True):
            sub_s += query[2]
        else:
            s += query[2]
s = sub_s[::-1] + s
if ud_count%2:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
from collections import deque
s = readline().rstrip().decode('utf-8')
q = int(readline())
flag = 0
lst1 = deque([])
lst2 = deque([])
for i in range(q):
    t = readline().rstrip().decode('utf-8')
    if t[0] == "1":
        #reverse
        flag = 1-flag
    else:
        com,f,c = t.split()
        if f == "1": #先頭にc追加
            if not flag:
                lst1.append(c)
            else: #反転中
                lst2.append(c)
            

        else: #末尾にc追加
            if not flag:
                lst2.append(c)
            else:
                lst1.append(c)

if not flag:
    lst1.reverse()
    s = "".join(lst1) + s + "".join(lst2) 

    
else:
    lst2.reverse()
    s = s[::-1]
    s = "".join(lst2)+ s + "".join(lst1)



print(s)

Submission status: Accepted

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

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

    S_deque = deque()
    for s in S:
        S_deque.append(s)
 
    is_reversed = False
    for q in Query:
        if q[0]=='1':
            is_reversed = not is_reversed
        elif q[0]=='2':
            if (not is_reversed and q[1]=='1') or (is_reversed and q[1]=='2'):
                S_deque.appendleft(q[2])
            else:
                S_deque.append(q[2])

    if is_reversed:
        S_deque.reverse()

    print(*S_deque, sep='')

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
s = input()
Q = int(input())
flag = 0

before = []
after = []

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

ans = ""
if flag == 0:
	for c in reversed(before):
		print(c,end = "")
	print(s,end = "")
	for c in after:
		print(c,end = "")
else:
	for c in reversed(after):
		print(c,end = "")
	for c in reversed(s):
		print(c,end = "")
	for c in before:
		print(c,end = "")

Submission status: Accepted

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

S = readline().strip().decode('utf-8')
Q = int(readline())

is_reversed = False

import collections
answer = collections.deque(S)
for _ in range(Q):
    l = readline().decode('utf-8').split()
    if len(l) == 1:
        is_reversed = not is_reversed
    else:
        _, F, C = map(str, l)
        if F == "1":
            if is_reversed:
                answer.append(C)
            else:
                answer.appendleft(C)
        else:
            if is_reversed:
                answer.appendleft(C)
            else:
                answer.append(C)

if is_reversed:
    answer.reverse()
print("".join(answer))

Submission status: Accepted

Solution code:
from collections import deque
a = input()
q = int(input())
key = [0]*len(a)
for i in range(len(a)):
    key[i] = a[i]

p = deque(key)
rev = False
for i in range(q):
    qinput = [str(x) for x in input().split()]
    if len(qinput) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        k = qinput[1]
        if k == "1":
            if rev:
                p.append(qinput[2])
            else:
                p.appendleft(qinput[2])
        else:
            if not rev:
                p.append(qinput[2])
            else:
                p.appendleft(qinput[2])
if rev:
    p.reverse()
print("".join(p))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
B = []
C = []
t = 1
for i in range(Q):
    A = input()
    if A=="1":
        t*=-1
    else:
        AL = list(A.split())
        if AL[1]=="1":
            if t==1:
                B.append(AL[2])
            else:
                C.append(AL[2])
        elif AL[1]=="2":
            if t==1:
                C.append(AL[2])
            else:
                B.append(AL[2])

B = ''.join(B)
C = ''.join(C)

if t==-1:
    print(C[::-1]+S[::-1]+B)
else:
    print(B[::-1]+S+C)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
li = []
for i in range(Q):
    li.append(list(input().split()))
flag = False
sq = deque(S)
for i in range(Q):
    if li[i][0] == "1":
        flag = not flag
    else:
        if (li[i][1] == "1" and not flag) or (li[i][1] == "2" and flag):
            sq.appendleft(li[i][2])
        else:
            sq.append(li[i][2])
sq = list(sq)
if flag:
    sq.reverse()
for i in range(len(sq)):
    print(sq[i], end="")

Submission status: Accepted

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys
import copy

sys.setrecursionlimit(10**9)
mod = 10**7+9
inf = 10**20

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


sentou = ""
matubi = ""
reverse = False
for i in range(q):
    l = input().split()
    if l[0]=="1":
        reverse = not(reverse)
    else:
        if l[1]=="1":
            if reverse:
                matubi += l[2][::-1]
            else:
                sentou = l[2] + sentou
        else:
            if reverse:
                sentou = l[2][::-1] + sentou
            else:
                matubi += l[2]
    #print(sentou,s,matubi)

if reverse:
    print(matubi[::-1] + s[::-1] + sentou[::-1])
else:
    print(sentou + s + matubi)

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import deque
S=list(input())
Q=int(input())
Sque=deque(S)
re=1
for i in range(Q):
    q=input()
    if q=="1":
        re*=-1
    else:
        two,F,C=q.split()
        if re==1:
            if F=="1":
                Sque.appendleft(C)
            else:
                Sque.append(C)
        else:
            if F=="2":
                Sque.appendleft(C)
            else:
                Sque.append(C)
S=list(Sque)
if re==-1:
    S.reverse()

ans="".join(S)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque


def input():
    return sys.stdin.readline()[:-1]


def main():
    S = input()
    Q = int(input())
    que = deque(S)
    rev = 0
    for i in range(Q):
        q = input()
        if q[0] == '1':
            # que.reverse()
            rev += 1
        else:
            _, f, c = q.split()
            if rev % 2 == 0:
                if f == '1':
                    que.appendleft(c)
                else:
                    que.append(c)
            else:
                if f == '1':
                    que.append(c)
                else:
                    que.appendleft(c)
    if rev % 2 == 1:
        que.reverse()
    print("".join(list(que)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
L = deque()
L.append(S)
Q = int(input())
is_lookforward = True
for i in range(Q):
    query = input()
    if query[0] == '1':
        is_lookforward = not is_lookforward
    else:
        F = int(query[2])
        C = query[4]
        if (F == 1 and is_lookforward) or (F == 2 and not is_lookforward):
            L.appendleft(C)

        else:
            L.append(C)
ans = ''.join(L)
print(ans if is_lookforward else ans[::-1])

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

from collections import deque
from bisect import bisect_right
S = deque(list(str(input())))
Q = I()

one = []
isone = []
F = [0]*Q
C = [0]*Q
for i in range(Q):
    q = input().split()
    if q[0]=='1':
        one.append(i)
        isone.append(True)
    else:
        F[i] = (int(q[1]))
        C[i] = q[2]
        isone.append(False)


num = sum(isone)
if num%2==1:
    S = deque(list(reversed(S)))

bef = deque([])
aft = deque([])
for i,now in enumerate(isone):
    if now==1:
        if num%2==1:
            for b in reversed(bef):
                S.append(b)
            for a in aft:
                S.appendleft(a)
        else:
            for b in reversed(bef):
                S.appendleft(b)
            for a in aft:
                S.append(a)
        bef = deque([])
        aft = deque([])
        num -= 1
    else:
        if F[i]==1:
            bef.appendleft(C[i])
        else:
            aft.append(C[i])
for b in reversed(bef):
    S.appendleft(b)
for a in aft:
    S.append(a)
print(*S,sep='')

Submission status: Accepted

Solution code:
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = readline().decode().rstrip()
q = int(readline())

head = ""
tail = ""
flip = 0
for _ in range(q):
    que = readline().decode().split()
    if len(que) == 1:
        flip += 1

    else:
        if que[1] == '1':
            if flip % 2 == 0:

                head = ''.join([que[2], head])
            else:
                tail = ''.join([tail, que[2]])

        else:
            if flip % 2 == 0:

                tail = ''.join([tail, que[2]])
            else:
                head = ''.join([que[2], head])

if flip % 2 == 0:
    print(''.join([head, S, tail]))
else:
    print(''.join([tail[::-1], S[::-1], head[::-1]]))

Submission status: Accepted

Solution code:
import collections
from collections import deque

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

s=deque(s)
flag=True
for i in range(n):
    if q[i][0]==str(1):
        if flag:
            flag=False
        else:
            flag=True
    else:
        if flag:
            if q[i][1]==str(1):
                s.appendleft(q[i][2])
            else:
                s.append(q[i][2])
        else:
            if q[i][1]==str(1):
                s.append(q[i][2])
            else:
                s.appendleft(q[i][2])

if not flag:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

Query = [[0] * 3 for _ in range(Q)]
for i in range(Q):
    tmp = input().split()
    if tmp[0] == "1":
        Query[i][0] = 1
    elif tmp[0] == "2":
        Query[i][0] = 2
        Query[i][1] = int(tmp[1])
        Query[i][2] = tmp[2]

q = deque()
q.extend(S)
rev_flag = 0


for i in range(Q):
    if Query[i][0] == 1:
        rev_flag = (1 - rev_flag)
    else:
        if Query[i][1] == 1 and rev_flag == 0:
            q.appendleft(Query[i][2])
        elif Query[i][1] == 1 and rev_flag == 1:
            q.append(Query[i][2])
        elif Query[i][1] == 2 and rev_flag == 0:
            q.append(Query[i][2])
        elif Query[i][1] == 2 and rev_flag == 1:
            q.appendleft(Query[i][2])

if rev_flag == 1:
    q.reverse()
print("".join(list(q)))

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy
import bisect


def dfs(s, prev, pre):
  val[s] += Ope[s] + prev
  for i in G[s]:
    if(i == pre):
      continue
    dfs(i, val[s], s)

def bfs(sx, sy, n):
  que = deque([[sy,sx]])
  while que:
    y,x = que.popleft()
    for i,j in D:
      if(x+i<0 or y+j<0 or x+i>W-1 or y+j>H-1):
        continue
      dist = G[y+j][x+i]
      if(G[y+j][x+i] != "X"):
        if(type(G[y+j][x+i]) is str):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        elif(dist>G[y][x]+1):
          G[y+j][x+i] = G[y][x]+1
          que.append([y+j,x+i])
        

#X = str(input()).split()
#a = [int(x) for x in input().split()]
#sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]

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

S2 = deque(S)

#print(S2)

for i in range(Q):
  Que = str(input()).split()
  if(int(Que[0])==1):
    flag = 1 - flag
  else:
    if(flag == 1):
      if(int(Que[1])==1):
        S2.append(Que[2])
      else:
        S2.appendleft(Que[2])
    else:
      if(int(Que[1])==1):
        S2.appendleft(Que[2])
      else:
        S2.append(Que[2])

S = "".join(list(S2))

if(flag == 1):
  S = S[::-1]

print(S)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
W = 0
ans = deque(list(S.rstrip()))

for _ in range(Q):
#    print(W,ans)
    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans.append(C)
    else:
        ans.appendleft(C)
ans = "".join(map(str,ans))
if W == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
reverse = False
flont = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        reverse = not reverse
    else:
        F = int(tmp[1])
        C = tmp[2]
        if F == 1:
            if reverse:
                S.append(C)
            else:
                flont.append(C)
        else:
            if reverse:
                flont.append(C)
            else:
                S.append(C)
flont.reverse()
flont.extend(S)
if reverse:
    flont.reverse()
print(''.join(flont))

Submission status: Accepted

Solution code:
from collections import deque


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

    d = deque(S)

    r = False
    for _ in range(Q):
        q = input()
        if q == '1':
            r = not r
        else:
            _, b, c = q.split()
            if r:
                if b == '1':
                    d.append(c)
                else:
                    d.appendleft(c)
            else:
                if b == '1':
                    d.appendleft(c)
                else:
                    d.append(c)

    if r:
        d.reverse()
    print("".join(d))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()
s1=""
s2=""
q=int(input())
Q=[input().split() for i in range(q)]

flg=1
for i in range(q):
    if Q[i][0]=="1":flg*=-1
    if Q[i][0]=="2":
        if Q[i][1]=="1" and flg==1: s1=Q[i][2]+s1
        if Q[i][1]=="2" and flg==-1:s1=Q[i][2]+s1
        if Q[i][1]=="1" and flg==-1:s2=s2+Q[i][2]
        if Q[i][1]=="2" and flg==1:s2=s2+Q[i][2]
s=s1+s+s2
if flg==-1:s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
queries = [input() for i in range(q)]

ans = deque(s)
flag_reverse = False

for i in range(q):
    query = queries[i].split()
    if query[0] == "1":
        flag_reverse = not flag_reverse
    else:
        if query[1] == "1" and not flag_reverse or query[1] == "2" and flag_reverse:
            ans.appendleft(query[2])
        else:
            ans.append(query[2])

if flag_reverse:
    ans.reverse()

print("".join(ans))

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
t1,t2=[],[]
cnt=0
for _ in range(q):
  t=list(input().split())
  if len(t)==1:
    cnt+=1
    t1,t2=t2,t1
  else:
    if int(t[1])==1:
      t1+=[t[2]]
    else:
      t2+=[t[2]]
if cnt%2==1:
  s=s[::-1]
u=t1[::-1]+s+t2
print(''.join(u))