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())
reverse = False
top = []
bottom = []
for _ in range(Q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        reverse = not reverse
    else:
        if (reverse and query[1] == "1") or (not reverse and query[1] == "2"):
            bottom.append(query[2])
        else:
            top.append(query[2])

ans = "".join(reversed(top)) + s + "".join(bottom)
if reverse:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
d = deque()
for i in S:
    d.append(i)
flag = True
for _ in range(Q):
    q = [i for i in input().split()]
    if int(q[0]) == 1:
        flag = not flag
    else:
        if (int(q[1]) == 1 and flag) or (int(q[1]) == 2 and not flag):
            d.appendleft(q[2])
        else:
            d.append(q[2])
S = ""
while(len(d)>0):
    if flag:
        S += d.popleft()
    else:
        S += d.pop()
print(S)

Submission status: Accepted

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

count_rev = 0
state = True
front_s = []
for i in range(q):
    input_list = input().split()
    t = int(input_list[0])
    if t == 1:
        state = not state
        count_rev += 1
    elif t == 2:
        f = int(input_list[1])
        c = input_list[2]
        if f == 1 and state:
            front_s.append(c)
        elif f == 1 and not state:
            s.append(c)
        elif f == 2 and state:
            s.append(c)
        elif f == 2 and not state:
            front_s.append(c)

front_s.reverse()
new_s = front_s + s
if not state:
    new_s.reverse()
print(''.join(new_s))

Submission status: Accepted

Solution code:
import sys
import math
import copy
import queue
input = sys.stdin.readline
def LI(): return [int(x) for x in input().split()]
INF = 10**15
DD = ((1,0),(0,1),(-1,0),(0,-1))

S = [x for x in input().rstrip()]
n = int(input())

a = [[],[]]
d = 0
for _ in range(n):
    q = list(input().split())
    if q[0] == '1':
        d = 1-d
    else:
        if q[1] == '1':
            a[d].append(q[2])
        else:
            a[1-d].append(q[2])

if d == 1:
    a[1].reverse()
    S.reverse()
    S = ''.join(a[1]) + ''.join(S) + ''.join(a[0])
else:
    a[0].reverse()
    S = ''.join(a[0]) + ''.join(S) + ''.join(a[1])
print (S)

Submission status: Accepted

Solution code:
from collections import deque

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

dq = deque(s)
rev = False
fcond = {"1": True, "2": False}
rcond = {True: False, False: True}
for i in range(q):
    t = input()
    if t.startswith("1"):
        rev = rcond[rev]
    elif t.startswith("2"):
        _, f, c = t.split()
        if fcond[f] is not rev:
            dq.appendleft(c)
        else:
            dq.append(c)
if rev:
    print("".join(reversed(dq)))
else:
    print("".join(dq))

Submission status: Accepted

Solution code:
s = [_ for _ in input()]
qn = int(input())
rev = False
prevs = ['']*200000
nexts = ['']*200000
previ = 0
nexti = 0

for _ in range(qn):
  q = [_ for _ in input().split()]
  if q[0] == '1':
    rev = not rev
  else:
    if (rev and q[1] == '2') or (not rev and q[1] == '1'):
      prevs[previ] = q[2]
      previ += 1
    else:
      nexts[nexti] = q[2]
      nexti += 1

prevs.reverse()
s = prevs + s + nexts
if rev:
  s.reverse()
print("".join(s))

Submission status: Accepted

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

forward = []
backend = []
reverse = 0

for i in range(Q):
        s = input()
        s1 = list(s)

        if s1[0] == '1':
            reverse += 1
        else:
            if s1[2] == '1':
                if reverse % 2 == 0:
                    forward.append(s1[4])
                else:
                    backend.append(s1[4])
            else:
                if reverse % 2 == 0:
                    backend.append(s1[4])
                else:
                    forward.append(s1[4])

if reverse % 2 == 0:
    for i in forward[::-1]:
        print(i, end="")
    
    print(S,end="")

    for i in backend:
        print(i,end="")

    print()
else:
    for i in backend[::-1]:
        print(i,end="")
    
    for i in list(S)[::-1]:
        print(i,end="")

    for i in forward:
        print(i,end="")

    print()

Submission status: Accepted

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

rev = 0
head = ""
foot = ""
for i in range(Q) :
    query = input()
    if query == "1" :
        rev += 1
    else :
        a, F, C = query.split()
        if F == "1" and rev%2 == 0:
            head = C + head
        elif F == "1" and rev%2 == 1:
            foot = foot + C
        elif F == "2" and rev%2 == 0:
            foot = foot + C
        else :
            head = C + head

if rev%2 == 0 :
    print("".join(list(head + S + foot)))
else :
    print("".join(list(reversed(head + S + foot))))

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
snt=1
for i in range(Q):
    inp=input().split()
    if inp[0]=="1":
        snt*=(-1)
    else:
        F=int(inp[1])
        C=inp[2]
        if (2*F-3)*snt>0:
            #S=S+C
            S.append(C)
        else:
            #S=C+S
            S.appendleft(C)
if snt==1:
    #print(S)
    for i in range(len(S)):
        print(S.popleft(),end="")
    print()
else:
    for i in range(len(S)):
        #print(S[len(S)-1-i],end="")
        print(S.pop(),end="")
    print()

Submission status: Accepted

Solution code:
from collections import deque
s = input()
que = deque(list(s))
q = int(input())
flag = True
for _ in range(q):
    l = input()
    if flag:
        if l[0] == "1":
            flag = False
        else:
            if l[2] == "1":
                que.appendleft(l[4])
            else:
                que.append(l[4])
    else:
        if l[0] == "1":
            flag = True
        else:
            if l[2] == "1":
                que.append(l[4])
            else:
                que.appendleft(l[4])
if flag:
    ans = list(que)
    print("".join(ans))
else:
    ans = list(que)[::-1]
    print("".join(ans))

Submission status: Accepted

Solution code:
S = str(input())
Q = int(input())
st = [0] * Q
n = [0] * Q
p = 0
s_h = 0
for i in range(Q):
  q = str(input())
  if int(q[0]) == 1:#Ti = 1
    s_h += 1
  else:
    if int(q[2]) == 1:#先頭
      st[p] = q[4]
      n[p] = s_h
      p += 1
    else:
      st[p] = q[4]
      n[p] = s_h + 1
      p += 1

odd = ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 1) and (str(st[a]) != "0")]
odd = "".join(odd)
even =  ["".join(st[a]) for a in range(Q) if ((n[a] + s_h) % 2 == 0) and (str(st[a]) != "0")]
even = "".join(even)[::-1]
if s_h % 2 == 1:
  S = S[::-1]

b = [even, S, odd]
#print(S,odd,even)
print("".join(b))

Submission status: Accepted

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

rev = False

forward = ''
back = ''

for i in range(q):
    tfc = input()
    if len(tfc) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        t, f, c = tfc.split()
        if f == '1':
            if rev:
                back += c
            else:
                forward += c
        else:
            if rev:
                forward += c
            else:
                back += c

s = forward[::-1] + s + back
if rev:
    s = s[::-1]

print(s)

Submission status: Accepted

Solution code:
import sys
import math
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
q = I()
que = [S() for i in range(q)]
#print(que)

st = []
en = []
f = 0
for i in range(q):
  n = que[i]
  if n == "1":
    f += 1
  else:
    t,ff,c = list(n.split())
    if (f+int(ff))%2 == 0:
      st.append(c)
    else:
      en.append(c)

#print(st)
#print(en)

if f%2 == 0:
  print("".join(en[::-1]) + s + "".join(st))
else:
  print("".join(st[::-1]) + s[::-1] + "".join(en))

Submission status: Accepted

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial,hypot
#import sys
#from fractions import gcd
setrecursionlimit(10**6+1)
input=stdin.readline

s=input().rstrip()
q=int(input().rstrip())
qu=deque(s)
rev=0
for _ in range(q):
    que,*C=input().split()
    que=int(que)
    if que==2:
        f,c=C
        f=int(f)
        if f==1:
            if rev%2==0:
                qu.appendleft(c)
            else:
                qu.append(c)
        else:
            if rev%2==0:
                qu.append(c)
            else:
                qu.appendleft(c)
    else:
        rev+=1
print("".join(qu) if rev%2==0 else "".join(qu)[::-1])

Submission status: Accepted

Solution code:
import bisect

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

S = {i: s for i, s in enumerate(S)}


head = -1
tail = len(S)

is_filp = False
for i in range(Q):
    q = input()

    if q[0] == '1':
        if is_filp:
            is_filp = False
        else:
            is_filp = True
    elif q[0] == '2':
        _, f, c = q.split(' ')
        if f == '1':
            if is_filp:
                S[tail] = c
                tail += 1
            else:
                S[head] = c
                head -= 1
        else:
            if is_filp:
                S[head] = c
                head -= 1
            else:
                S[tail] = c
                tail += 1

if is_filp:
    print(''.join([S[i] for i in range(tail - 1, head, -1)]))
else:
    print(''.join([S[i] for i in range(head + 1, tail)]))

Submission status: Accepted

Solution code:
from collections import deque

s = input()
Q = int(input())
query = [list(map(str, input().split())) for _ in range(Q)]

d = deque(s)

cnt = 0
for q in query:
    if q[0] == '1':
        cnt += 1
    else:
        if q[1] == '1':
            if cnt % 2:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if cnt % 2:
                d.appendleft(q[2])
            else:
                d.append(q[2])
        
d_list = list(d)
s = ''.join(d_list)
if cnt % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
# from fractions import gcd
# mod = 10 ** 9 + 7
# N = int(input())
# a = list(map(int,input().split()))
# a,b,c = map(int,input().split())
# ans = [0] * N
# math.ceilで切り上げ
# dp = [[0] * 4 for i in range(3)] #2次元配列初期化
# dp = [[[0] * 2 for i in range(3)] for j in range(5)]
# (ord('A'))でASCII出力 Aは65
import math
import statistics
import string
import sys
from array import array
from collections import deque

def intinput():
    return int(input())
def listintinput():
    return list(map(int, input().split()))
def splitintinput():
    return map(int, input().split())
def factorialsurplus(x, y, z):  # xからyまでの階乗をzで割ったあまりを求める
    ret = 1
    for i in range(x, y + 1):
        ret = (ret * i) % z
    return (ret)
def isprime(x):
    if x == 1:
        return False
    if x == 2:
        return True
    sq = int(math.sqrt(x))
    for i in range(2,sq+1):
        if x % i == 0:
            return False
    return True
def hcfnaive(a,b):#最大公約数 math.gcdが使えない時の手段として入れとく。pythonが更新されたらいらなくなるはず
    if(b==0):
        return a
    else:
        return hcfnaive(b,a%b)

s = deque()
s.append(input())
Q = int(input())
rev_count = 0

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

if rev_count % 2 == 0:
    print(''.join(s))
else:
    a = ''.join(s)
    print(a[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
front = ''
end = ''
cnt = 0
for _ in range(Q):
    T = input().split()
    if int(T[0]) == 1:
        cnt += 1
    else:
        F = int(T[1])
        C = T[2]
        if F == 1:
            if cnt%2==0:
                front = C+front
            else:
                end = end+C
        else:
            if cnt%2==0:
                end = end+C
            else:
                front=C+front
if cnt % 2 == 0:
    S = front+S+end
    print(S)
else:
    S = list(reversed(S))
    end = list(reversed(end))
    front = list(reversed(front))
    S = end+S+front
    print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S=input()
    Q=int(input())
    d = deque(S)
    is_forward = True
    for _ in range(Q):
        q=input()
        if q[0] == '1':
            is_forward = not is_forward
        else:
            _, F, C = q.split()
            if F == '1':
                if is_forward:
                    d.appendleft(C)
                else:
                    d.append(C)
            else:
                if is_forward:
                    d.append(C)
                else:
                    d.appendleft(C)
    if is_forward:
        for s in d:
            print(s, sep="", end="")
    else:
        for s in reversed(d):
            print(s, sep="", end="")
    print()
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

reverse_flag = False

top = []
last = []
for i in range(q):
  q_list = list(input().split())
  
  if len(q_list) == 1:
    reverse_flag = not reverse_flag
    
  elif q_list[1] == '1' and (not reverse_flag):
    top.append(q_list[2])
    
  elif q_list[1] == '2' and (not reverse_flag):
    last.append(q_list[2])
    
  elif q_list[1] == '1' and reverse_flag:
    last.append(q_list[2])
    
  elif q_list[1] == '2' and reverse_flag:
    top.append(q_list[2])
    
if reverse_flag:
  print(''.join(reversed(last)) + ''.join(reversed(s)) + ''.join(top))
else:
  print(''.join(reversed(top)) + ''.join(s) + ''.join(last))

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]



def main():
    s=input()
    q=int(input())
    even=1
    right=[]
    left=[]
    for i in range(q):
        temp=list(input().split())
        temp[0]=int(temp[0])
        if temp[0]==2:
            a=int(temp[1])
            b=temp[2]
            if a==1:
                left.append(b)
            else:
                right.append(b)
        else:
            even=-(even-1)
            left,right=right,left
#    print(left,s,right,even)
    if even==0:
        s=s[::-1]
 #       right=right[::-1]
    left=left[::-1]
    ans="".join(left)+s+"".join(right)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

former = ""
latter = ""
is_rev = False

for _ in range(0, num_query):
    query = input()
    if query[0] == "1":
        is_rev = not is_rev
    else:
        pos = query[2]
        character = query[4]
        if pos == "1":
            if is_rev:
                latter += character
            else:
                former = character + former
        else:
            if is_rev:
                former = character + former
            else:
                latter += character

if is_rev:
    ans = latter[::-1] + s[::-1] + former[::-1]
else:
    ans = former + s + latter
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	S = IS()
	Q = II()
	c = 0
	head=[]
	tail=[]
	for _  in range(Q):
		query = IS()
		if(len(query)==1):
			c = (c + 1)%2
		else:
			T,F,C = query.split()
			T = int(T)
			F = int(F)
			if F==1:
				if(c==0):
					head.append(C)
				else:
					tail.append(C)
			else:
				if(c==1):
					head.append(C)
				else:
					tail.append(C)
	if c == 0:
		head = ''.join(head[::-1])
		tail = ''.join(tail)
		ans = head+S+tail
	else:
		head = ''.join(head)
		tail = "".join(tail[::-1])
		ns = S[::-1]
		ans = tail+ns+head
	print(ans)




if __name__ == '__main__':
	main()

Submission status: Accepted

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

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

a = deque(s)
reverse = False

for i in range(q):
    query = input()
    if query == "1":
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        x, q2_1, q2_2 = query.split()
        if q2_1 == "1":
            if reverse:
                a.append(q2_2)
            else:
                a.appendleft(q2_2)
        else:
            if reverse:
                a.appendleft(q2_2)
            else:
                a.append(q2_2)

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

Submission status: Accepted

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

lim = 10**5 *2
buf = [None for _ in range(lim)] + S + [None for _ in range(lim)]
forward_i, back_i = lim-1, lim + len(S) + 1
is_reversed = False
for _ in range(Q):
    token = input().split(' ')
    if len(token) == 1:
        is_reversed = ~is_reversed

    else:
        _, f, c = token
        f = int(f)
        if (f == 1 and is_reversed) or (f == 2 and not is_reversed):
            buf[back_i] = c
            back_i += 1
            # S.append(c)

        else:
        # elif (f == 1 and not is_reversed) or (f == 2 and is_reversed):
            buf[forward_i] = c
            forward_i -= 1
            # S.insert(0, c)

S = ''.join([b for b in buf if b is not None])
if is_reversed:
    print(''.join(reversed(list(S))))
    # S.reverse()
    # print(''.join(S))

else:
    print(S)
    # print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
count = 0
for i in range(Q):
    input_list = input().split()
    if input_list[0] == "1":
        count += 1
        continue

    if count % 2 == 0:  # even
        if input_list[1] == "1":
            S.appendleft(input_list[2])
        else:
            S.append(input_list[2])
    else:
        if input_list[1] == "2":
            S.appendleft(input_list[2])
        else:
            S.append(input_list[2])

if count % 2 == 0:
    ans = ''.join(S)
    print(ans)
else:
    S.reverse()
    ans = ''.join(S)
    print(ans)

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
sc=0
lhs=[]
rhs=[]
for _ in range(q):
  query=input()
  if query=="1":
    sc+=1
  else:
    t,f,c=query.split()
    if f=="1":
      if sc%2==0:
        lhs.append(c)
      else:
        rhs.append(c)
    else:
      if sc%2==0:
        rhs.append(c)
      else:
        lhs.append(c)

lhs.reverse()
s=lhs+s+rhs
if sc%2!=0:
  s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

# print('input >>')
S = deque(input())
Q = int(input())
queries = []
for _ in range(Q):
    queries.append(input())

is_reverse = False

for q in queries:
    ql = list(q.split())
    if len(ql) == 1:
        is_reverse = not is_reverse
    else:
        if ql[1] == '1': # 先頭に加える
            if not is_reverse:
                S.appendleft(ql[2])
            else:
                S.append(ql[2])
        else: # 末尾に加える
            if not is_reverse:
                S.append(ql[2])
            else:
                S.appendleft(ql[2])

# print('-----output-----')
if not is_reverse:
    print(''.join(list(S)))
else:
    print(''.join(list(S)[::-1]))

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
ans=0
mae=[]
ato=[]
for i in range(q):
    query=input().split()
    if query==['1']:
        ans+=1
    else:
        if query[1]=='1' and ans%2==0:
            mae.append(query[2])
        elif query[1]=='1' and ans%2==1:
            ato.append(query[2])
        elif query[1]=='2' and ans%2==0:
            ato.append(query[2])
        else:
            mae.append(query[2])
if ans%2==1:
    s.reverse()
    ato.reverse()
    s=ato+s+mae
else:
    mae.reverse()
    s=mae+s+ato
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

S = list(input())
d = deque(S)
Q = int(input())
r_flag = 0

def top(d,C,r_flag):
    if r_flag:
        d.append(C)
    else:
        d.appendleft(C)

def bottom(d,C,r_flag):
    if r_flag:
        d.appendleft(C)
    else:
        d.append(C)

for i in range(Q):
    tmp = list(input().split())
    if tmp[0] == "1":
        if r_flag == 0:
            r_flag = 1
        else:
            r_flag = 0
    else:
        if tmp[1] == "1":
            top(d,tmp[2],r_flag)
        else:
            bottom(d,tmp[2],r_flag)

d = list(d)
if r_flag:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
aaa = list(S)
aaa.reverse()
sss = "".join(aaa)

Q = int(input())
d = deque()
d.append(S)
rev = 1
cnt = 0

for i in range(Q):
  List = list(input().split())
  a = int(List[0])
  if a == 1:
    rev = rev * -1
  else:
    b = int(List[1])
    if (b == 1 and rev == 1) or (b == 2 and rev == -1):
      d.appendleft(List[2])
      cnt = cnt + 1
    else:
      d.append(List[2])

if rev == -1:
  d[cnt] = sss
  d.reverse()
string = "".join(list(d))
print(string)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
is_turned = False
head = []
tail = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        is_turned = not is_turned
        continue
    f, c = int(tmp[1]), tmp[2]
    # print('f c ', f, c)
    if not is_turned:
        if f == 1:
            head.append(c)
        else:
            tail.append(c)
    else:
        if f == 1:
            tail.append(c)
        else:
            head.append(c)

if not is_turned:
    # S = ''.join(S)
    head = ''.join(reversed(head))
    tail = ''.join(tail)
    ans = head + S + tail
else:
    S = ''.join(reversed(S))
    head = ''.join(head)
    tail = ''.join(reversed(tail))
    ans = tail + S + head
# print('head tail', head, tail, is_turned, S)
print(ans)

Submission status: Accepted

Solution code:
s=list(input())
import collections 
D=collections.deque(s)
counter=0
lastindex=0
q=int(input())
for i in range(q):
    a=list(input().split())
    if int(a[0])==1:
        counter+=1

    else:
        if (counter+int(a[1]))%2==0:
            D.append(a[2])
        else:
            D.appendleft(a[2])
if counter%2==0:
    D=list(D)
    print("".join(D))
else:
    D=list(D)[::-1]
    print("".join(D))

Submission status: Accepted

Solution code:
import collections

def read():
    S = input().strip()
    Q = int(input().strip())
    queries = list()
    for i in range(Q):
        query = input().strip().split()
        t = int(query[0])
        if t == 1:
            queries.append((t, -1, "*"))
        if t == 2:
            f = int(query[1])
            c = query[2]
            queries.append((t, f, c))
    return S, Q, queries

def solve(S, Q, queries):
    head = collections.deque()
    tail = collections.deque()
    is_fliped = False
    for t, f, c in queries:
        if t == 1:
            is_fliped = not is_fliped
        if t == 2:
            is_head = (f == 1) ^ is_fliped
            if is_head:
                head.appendleft(c)
            else:
                tail.append(c)
    s = ''.join(head) + S + ''.join(tail)
    if is_fliped:
        s = ''.join(reversed(s))
    return s


if __name__ == '__main__':
    inputs = read()
    print("{}".format(solve(*inputs)))

Submission status: Accepted

Solution code:
from collections import deque
def solve(S, Q):
    S = deque(S)
    flg = 0
    for _ in range(Q):
        que = input().split()
        if len(que) == 1:
            flg += 1
        else:
            if flg % 2 == 0:
                if que[1] == '1':
                    S.extendleft(que[2])
                elif que[1] == '2':
                    S.extend(que[2])
            elif flg % 2 == 1:
                if que[1] == '1':                 
                    S.extend(que[2])
                elif que[1] == '2':                
                    S.extendleft(que[2])
    S = ''.join(S)
    if flg % 2 == 1:
        S = S[::-1]
    print(S)

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
inv=False
head=""
last=""
for i in range(Q):
    qu=input().split(" ")
    if int(qu[0])==1:
        inv=not inv
    else:
        F=int(qu[1])
        if inv:
            F=3-F
        if F==1:
            head+=qu[2]
        else:
            last+=qu[2]

if inv:
    print(last[::-1]+S[::-1]+head)
else:
    print(head[::-1]+S+last)

Submission status: Accepted

Solution code:
from collections import deque

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

#pop_flag = True => 正常
#pop_flag = False => 反転している
pop_flag = True
S_deque = deque(S)
for i in range(Q):
  Querie = [str(j) for j in input().split()]
# 反転させるやつ
  if Querie[0] == "1":
    if pop_flag == True:
      pop_flag = False
    elif pop_flag == False:
      pop_flag = True

# 追加するやつ
  elif Querie[0] == "2":
    if Querie[1] == "1":
      if pop_flag == True:
        S_deque.appendleft(Querie[2])
      elif pop_flag == False:
        S_deque.append(Querie[2])
    elif Querie[1] == "2":
      if pop_flag == True:
        S_deque.append(Querie[2])
      elif pop_flag == False:
        S_deque.appendleft(Querie[2])

if pop_flag == False:
  S_deque.reverse()

ans = ''.join(S_deque)

print(ans)

Submission status: Accepted

Solution code:
import collections
S = collections.deque(list(input()))
N = int(input())
parity = True
for i in range(N):
  query = input().split()
  if query[0] == "1":
    if parity == True:
      parity = False
    else:
      parity = True
  if query[0] == "2":
    if parity:
      if query[1] == "1":
        S.appendleft(query[2])
      else:
        S.append(query[2])
    else:
      if query[1] == "1":
        S.append(query[2])
      else:
        S.appendleft(query[2])

Sstr = ""
if parity:
  for i in range(len(S)):
    Sstr = Sstr + S.popleft()
else:
  for i in range(len(S)):
    Sstr = Sstr + S.pop()
print(Sstr)

Submission status: Accepted

Solution code:
s=input()
t=int(input())
u=[input().split()for i in range(t)]
a=0
b=''
c=''
for i in range(t):
  if u[i][0]=='1':
    a+=1
  else:
    if u[i][1]=='1':
      if a%2==0:
        b=b+u[i][2]
      else:
        c=c+u[i][2]
    else:
      if a%2==0:
        c=c+u[i][2]
      else:
        b=b+u[i][2]
if a%2==0:
  b=b[::-1]
  s=b+s+c
  print(s)
else:
  s=s[::-1]
  c=c[::-1]
  s=c+s+b
  print(s)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

doTest=0

def yn(n):
    if n==1:
        print("Yes")
    else:
        print("No")
    return

def resolve():
    readline=sys.stdin.readline


    s=readline().strip()
    q=int(readline().strip())
    hed=""
    btm=""
    rev=0
    for i in range(q):
        x=list(map(str, readline().strip().split()))
        if x[0]=="1": #rev
            if rev==0:
                rev=1
            else:
                rev=0
        elif x[0]=="2":
            if x[1]=="1": #head
                if rev==0:
                    hed = x[2] + hed
                else:
                    btm = btm + x[2]
            else: #bottom
                if rev==0:
                    btm = btm + x[2]
                else:
                    hed = x[2] + hed
    if rev==0:
        print(hed,end="")
        print(s,end="")
        print(btm)
    else:
        print(''.join(list(reversed(btm))),end="")
        print(''.join(list(reversed(s))),end="")
        print(''.join(list(reversed(hed))))
    return

if doTest==0:
    resolve()
    sys.exit()