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:
# D - String Formation
# 2020-05-21 08:17
from collections import deque


def main():
    chars = deque(input())
    chars_append = chars.append
    chars_appendleft = chars.appendleft
    n = int(input())
    direction = 1
    for _ in range(n):
        query = input().split()
        if query[0] == '1':
            direction *= (-1)  # reverse
        else:
            if query[1] == '1':
                # if head-adding: 1, tail: -1
                head = direction
            else:
                head = (-direction)
            if head == 1:
                chars_appendleft(query[2])
            else:
                chars_append(query[2])

    if direction == -1:
        chars.reverse()
    return ''.join(chars)


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

Submission status: Accepted

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

for _ in range(q):
    buf = input()
    if buf[0]=='1':
        if r:
            r=False
        else :
            r=True
    elif buf[0]=='2':
        c=buf.split()
        if (c[1]=='1' and r) or (c[1]=='2' and r==False):
            d.appendleft(c[2])
        else:
            d.append(c[2])
if r==False:
    d.reverse()
for i in d:
    print(i,end='')

Submission status: Accepted

Solution code:
from collections import deque
 
s = input()
q = int(input())
 
query = [input().split() for i in range(q)]
 
a = deque([s])
 
flag = True
 
for i in range(q):
    if query[i][0] == "1":
        flag = not(flag)
    else:
        if (query[i][1] == "1" and flag) or (query[i][1] != "1" and not(flag)):
            a.appendleft(query[i][2])
        else:
            a.append(query[i][2])
 
b = "".join(a)
if not(flag):
    b = b[::-1]
 
print(b)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])

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

print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input().replace('\n', ''))
q = int(input())
flag = True
for i in range(q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        if flag: flag = False
        else: flag = True
    else:
        
        if (not flag and query[1]=='1') or flag and query[1]=='2':
            s.append(query[2])
        else:
            s.appendleft(query[2])
s = ''.join(s)
print(s if flag else s[::-1])

Submission status: Accepted

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

for i in range(Q):
    query = input().split()
    if len(query) == 1:
        parity += 1
    else:
        if int(query[1]) == 1 + (parity % 2):
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = ''.join([c for c in S])
if parity % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Accepted

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

tmp = 1
for i in range(q):
    x = input()
    if x[0]==str(1):
        tmp*=-1
    else:
        t,f,c = x.split()
        t,f = int(t),int(f)
        if tmp==-1:
            if f==1:
                queue.append(c)
            if f==2:
                queue.appendleft(c)
        if tmp==1:
            if f==1:
                queue.appendleft(c)
            if f==2:
                queue.append(c)

print(''.join(queue)[::tmp])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = [list(map(str, input().split())) for i in range(Q)]

reverse = 0
b = []
a = []

for i in range(Q):
  if len(Query[i]) == 1:
    reverse += 1
  else:
    if Query[i][1] == '1':
      if reverse % 2 == 0:
        b.append(Query[i][2])
      else:
        a.append(Query[i][2])
    else:
      if reverse % 2 == 0:
        a.append(Query[i][2])
      else:
        b.append(Query[i][2])
        
ans = ''
s = (''.join(b))[::-1] + S + ''.join(a)
if reverse % 2 == 0:
  ans = s
else:
  ans = s[::-1]
  
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
N = int(input())

Q = [list(input().split()) for _ in range(N)]

count = 0
c = 0

ans_append = []
ans_insert = []

for q in Q:
    if q[0] == "1":
        count += 1
        c = count % 2
    else:
        if (c == 0 and q[1] == "1") or (c == 1 and q[1] == "2"):
            ans_insert.append(q[2])
        elif (c == 0 and q[1] == "2") or (c == 1 and q[1] == "1"):
            ans_append.append(q[2])


if count % 2  == 1:
    ans = ans_append[::-1] + S[::-1] + ans_insert
    print("".join(ans))
else:
    ans = ans_insert[::-1] + S + ans_append
    print("".join(ans))

Submission status: Accepted

Solution code:
txt = input()
n = (int)(input())
count = 0
sentou = ""
saigo = ""
for i in range(n):
  temp = input()
  if temp[0] == '1':
    count += 1
  else:
    sec, moji = (int)(temp.split(" ")[1]), temp.split(" ")[2]
    if (sec + count) % 2 == 1:
      sentou = moji + sentou
    else:
      saigo += moji
if count % 2 == 1:
  txt = saigo[::-1] + txt[::-1] + sentou[::-1]
else:
  txt = sentou + txt + saigo
print(txt)

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    rev = False # rev=1で反転した状態
    tail = ""
    head = ""
    for i in range(Q):
        query = input()
        if len(query)==1:
            rev = not rev
        else:
            tmp = list(query.split())
            f = int(tmp[1])
            c = tmp[2]
            if rev:
                if f==1:
                    tail+=c
                else:
                    head=c+head
            else:
                if f==1:
                    head=c+head
                else:
                    tail+=c

    ans = tail[::-1]+S[::-1]+head[::-1] if rev else head+S+tail
    print(ans)

main()

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
rev = False
for _ in range(q):
    inp = [x for x in input().split()]
    if inp[0] == "1":
        rev = not rev
    else:
        if inp[1] == "1":
            if rev:
                s.append(inp[2])
            else:
                s.appendleft(inp[2])
        else:
            if rev:
                s.appendleft(inp[2])
            else:
                s.append(inp[2])

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

Submission status: Accepted

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

from collections import deque

deq=deque(s)

cnt=0
for _ in range(q):
    query=input().split()
    if int(query[0])==1:
        cnt+=1
    elif int(query[0])==2:
        if int(query[1])==1:
            if cnt%2==0:
                deq.appendleft(query[2])
            else:
                deq.append(query[2])
        else:
            if cnt%2==0:
                deq.append(query[2])
            else:
                deq.appendleft(query[2])

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque([input()])
Q = int(input())
is_reverse = False

for _ in range(Q):
  inp = input().split()
  if len(inp) == 1:
    if is_reverse:
      is_reverse = False
    else:
      is_reverse = True
  else:
    _, f, c = inp
    if f == '1':
      if is_reverse:
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if is_reverse:
        S.appendleft(c)
      else:
        S.append(c)
if is_reverse:
  ans = "".join(S)
  print(ans[::-1])
else:
  print("".join(S))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
turn = 1
start = []
end = []
for i in range(q):
    q_list = list(input().split())
    if q_list[0] == "1":
        turn *= -1
    else:
        if q_list[1] == "1":
            if turn == 1:
                start.append(q_list[2])
            else:
                end.append(q_list[2])
        else:
            if turn == -1:
                start.append(q_list[2])
            else:
                end.append(q_list[2])

dum1 = "".join(start)
dum2 = "".join(end)
if turn == 1:
    print(dum1[::-1]+s+dum2)
else:
    print(dum2[::-1] + s[::-1] + dum1)

Submission status: Accepted

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

state = 1 # 反転していたら-1
Stock = deque([S])
for i in range(Q):
    q = input()
    if q == '1':
        state *= -1
    else:
        Y, f, c = q.split()
        if (state == 1 and f == '1') or (state == -1 and f == '2'):
            Stock.appendleft(c)
        elif (state == 1 and f == '2') or (state == -1 and f == '1'):
            Stock.append(c)
S = ''.join(Stock)
if state == -1:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
s = input()
qn = int(input())
left = ''
right = s
for i in range(qn):
    q = input()
    if q[0] == '1':
        left, right = right, left
    else:
        _, f, c = q.split()
        if f == '1':
            left += c
        else:
            right += c
print(left[::-1] + right)

Submission status: Accepted

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

n = int(input())
l = []

base = 1

for i in range(n):
  l=list(input().split())
  if l[0] == '1':
    base = base *-1
  else:
    if l[1] =='1' and base == 1 :
      s.appendleft(l[2])
    elif l[1] =='2' and base == 1 :
      s.append(l[2])
    elif l[1] =='1' and base == -1 :
      s.append(l[2])
    elif l[1] =='2' and base == -1 :
      s.appendleft(l[2])

if base ==-1:
  s=''.join(list(reversed(s)))
      
print("".join(s))

Submission status: Accepted

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

a = 0
for i in range(Q):
    t = input()
    if t == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        t,f,c = t.split()
        if f == "1":
            if a == 0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if a == 0:
                S.append(c)
            else:
                S.appendleft(c)
            
if a == 1:
    S = reversed(S)
    
S = ''.join(S)
print(S)

Submission status: Accepted

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

a = []
b = []
rev = 0

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        a, b = b, a
        rev += 1
    else:
        if q[1] == '1':
            a.append(q[2])
        else:
            b.append(q[2])

#print(a)
#print(b)
if rev % 2 == 1:
    S = S[::-1]
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(IN())
mod=1000000007

#+++++

def main():
	#a = int(input())
	#b , c = tin()
	s = input()
	q = int(input())
	ff = []
	bb = []
	frip = False
	for _ in range(q):
		ss = input()
		if ss[0] == '1':
			frip = not frip
		else:
			_a, f, c = ss.split()
			is_ff = ((1 if frip else 0) + (1 if f == '1' else 0 )) % 2 == 1
			if is_ff:
				ff.append(c)
			else:
				bb.append(c)
	
	ret = ''.join(ff[::-1]) + s + ''.join(bb)
	if frip:
		ret = ret[::-1]
	print(ret)
			
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
# ABC151 D String Formation
from collections import deque

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

b_flag = False
for i in range(q):
    _list = [x for x in input().split()]
    if _list[0] == "1":
        if b_flag:
            b_flag = False
        else:
            b_flag = True
    else:
        if _list[1] == "1":
            if b_flag:
                s.append(_list[2])
            else:
                s.appendleft(_list[2])
        else:
            if b_flag:
                s.appendleft(_list[2])
            else:
                s.append(_list[2])

if b_flag:
    s.reverse()

print(*s,sep="")

Submission status: Accepted

Solution code:
from collections import deque

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

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

a = deque([s])

flag = True

for i in range(q):
    if query[i][0] == "1":
        flag = not(flag)
    else:
        if (query[i][1] == "1" and flag) or (query[i][1] != "1" and not(flag)):
            a.appendleft(query[i][2])
        else:
            a.append(query[i][2])

b = "".join(a)
if not(flag):
    b = b[::-1]

print(b)

Submission status: Accepted

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

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

u = ''
t = ''

r = False
for _ in range(Q):
    q = readline().split()
    if q[0] == '1':
        u, t = t, u
        r = not r
    else:
        if q[1] == '1':
            t += q[2]
        else:
            u += q[2]
            
if r:
    print(t[::-1]+S[::-1]+u)
else:
    print(t[::-1]+S+u)

Submission status: Accepted

Solution code:
s = input()
top = ''
q = int(input())
isReverse = False
for i in range(q):
    que = input().split()
    if(que[0] == '1'):
        isReverse = not isReverse
    else:
        if que[1] == '1':
            if isReverse:
                s += que[2]
            else:
                top += que[2]
        else:
            if isReverse:
                top += que[2]
            else:
                s += que[2]
if isReverse:
    s = s[::-1] + top
else:
    s = top[::-1] + s

print(s)

Submission status: Accepted

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

f = ""
l = ""
count = 0
for i in range(Q):
    if q[i][0] == "1":
        count += 1
    elif q[i][0] == "2":
        if count % 2 == 0:
            if q[i][1] == "1":
                f = q[i][2] + f
            else:
                l = l + q[i][2]
        else:
            if q[i][1] == "1":
                l = l + q[i][2]
            else:
                f = q[i][2] + f
        
if count % 2 == 0:
    print(f+S+l)
else:
    print((f+S+l)[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
results_forward = str("")
results_back = str("")
qlist = []
for i in range(q):
  qlist.extend(input().split(" "))

amount = 0
for i in range(len(qlist)):
  if qlist[i] == "1" or qlist[i] == "2":
    amount += int(qlist[i])
  elif amount%2 == 1:
    results_forward = qlist[i] + results_forward
    amount -= int(qlist[i-1])
  elif amount%2 == 0:
    results_back = results_back + qlist[i]
    amount -= int(qlist[i-1])

if q%2 == (len(results_forward)+len(results_back))%2:
  print(results_forward+s+results_back)
else:
  print((results_forward+s+results_back)[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input())
q=int(input())
flag=0
for _ in range(q):
  query=input().split()
  if len(query)==1:
    flag=(flag+1)%2
  else:
    if (int(query[1])+flag)%2:
      s.appendleft(query[2])
    else:
      s.append(query[2])
print(''.join(s) if flag==0 else ''.join(reversed(s)))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
sque = deque(s)
q = int(input())
que = [0] * q
flg = False
for i in range(q):
    que[i] = (list(map(str,input().split())))
    if(len(que[i])==3):
        if que[i][1] == '1':
            if flg:
                sque.append(que[i][2])
            else:
                sque.appendleft(que[i][2])
        else:
            if flg:
                sque.appendleft(que[i][2])
            else:
                sque.append(que[i][2])
    else:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    sque.reverse()
    print("".join(list(sque)))
else:
    print("".join(list(sque)))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
rev = 0
top = []
bot = []

for _ in range(Q):
  a = list(input().split())
  if a[0] == "1":
    rev += 1
  else:
    if rev%2 == 0:
      if a[1] == "1":
        top.append(a[2])
      else:
        bot.append(a[2])
    else:
      if a[1] == "2":
        top.append(a[2])
      else:
        bot.append(a[2])
      
t = "".join(top)[::-1]
b = "".join(bot)
S = "".join([t,S,b])
if rev%2 == 1:
  S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
# abc158_d.py
import sys
import time
import unittest
from io import StringIO


def resolve():
    S = input()
    Q = int(input())
    reverseFlg = False
    preR = ""
    post = ""
    for i in range(Q):
        QQ = input().split()
        if QQ[0] == "1":
            reverseFlg = not reverseFlg
        else:
            if reverseFlg:
                if QQ[1] == "1":
                    post += QQ[2]
                else:
                    preR += QQ[2]
            else:
                if QQ[1] == "1":
                    preR += QQ[2]
                else:
                    post += QQ[2]

    if reverseFlg:
        print(post[::-1] + S[::-1] + preR)
    else:
        print(preR[::-1] + S + post)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def checkTLE(self, input):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin

    def debugIO(self, input):
        stdin = sys.stdin
        sys.stdin = StringIO(input)
        resolve()
        sys.stdin = stdin

    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print('%s: %.3f' % (self.id(), t), flush=True)

    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)


if __name__ == "__main__":
    if "IS_LOCAL" in locals():
        suite = unittest.TestLoader().loadTestsFromTestCase(TestClass)
        unittest.TextTestRunner(verbosity=0).run(suite)
    else:
        resolve()

Submission status: Accepted

Solution code:
from collections import deque

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

rev_cnt = 0
rev = 0
for i in range(Q):
  query = input().split()
  if query[0]=="1":
    rev_cnt += 1
    rev = rev_cnt % 2
  elif query[0]=="2":
    #print(query)
    s=query[2]
    if query[1] == "1" and rev == 0:
      queue.appendleft(s)
    elif query[1] == "1" and rev == 1:
      queue.append(s)
    elif query[1] == "2" and rev == 0:
      queue.append(s)
    elif query[1] == "2" and rev == 1:
      queue.appendleft(s)
if rev == 0:
  print(''.join(list(queue)))
else:
  print(''.join(reversed(list(queue))))

Submission status: Accepted

Solution code:
s=input()
q_=int(input())
q=[input() for _ in range(q_)]
reverse=False
pre=[]
fin=[]
for qi in q:
    if qi[0]=='1':
        reverse=not reverse
    elif qi[2]=='1' and reverse==False:
        pre.append(qi[4])
    elif qi[2]=='1' and reverse==True:
        fin.append(qi[4])
    elif qi[2]=='2' and reverse==False:
        fin.append(qi[4])
    elif qi[2]=='2' and reverse==True:
        pre.append(qi[4])
if reverse==False:
    print(''.join([p for p in pre[::-1]])+s+''.join(fin))
else:
    print(''.join([f for f in fin[::-1]])+''.join([si for si in list(s)[::-1]])+''.join(pre))

Submission status: Accepted

Solution code:
from sys import stdin

S = list(stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())

b = [""]
f = [""]

t = 0

for i in range(Q):
    T = stdin.readline().rstrip().split()
    if(len(T) == 1):
        t += 1
        continue
    else :
        F,C = int(T[1]),T[2]

    if t%2 == 0: #反転なし
        if F == 1:
            b.append(C)
        else :
            f.append(C)
    else: #反転あり
        if F == 1:
            f.append(C)
        else :
            b.append(C)



if(t%2 == 0):
    print(''.join(b[::-1] + S + f))
else:
    print(''.join(f[::-1] + S[::-1] + b))

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.readline
    s = list(input()[:-1])
    Q = int(input())
    reverse = False
    before = []
    after = []
    for _ in range(Q):
        q = list(map(str, input().split()))
        t = q[0]
        if t == '1':
            reverse = not (reverse)
        else:
            if reverse ^ (q[1] == '1'):
                before.append(q[2])
            else:
                after.append(q[2])
    if reverse:
        after.reverse()
        s.reverse()
        print(''.join(after + s + before))
    else:
        before.reverse()
        print(''.join(before + s + after))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
def main():
    s=input()
    q=int(input())
    queryy=[]
    sent="a"
    back="a"
    for i in range(q):
        queryy.append(tuple(input().split()))
    check=-1
    for query in queryy:
    #   print(query)
        if query[0]=="1":
            check*=-1
        else:
            if query[1]=="1":
                if check==-1:
                    sent+=str(query[2])
                   # s=str(query[2])+s
                else:
                    back+=str(query[2])
                   # s+=str(query[2])
                    
            else:
                if check==-1:
                    back+=str(query[2])
                    #s+=str(query[2])
                else:
                    sent+=str(query[2])
                    #s=str(query[2])+s
   # print(type(sent))
    #if sent[0]=="1":
     #   sent=sent[1:]
    sent=sent[1:]
    sent=sent[::-1]   
    back=back[1:]

    if check==1:
        s=sent+s+back        
        print(s[::-1]) 
    else:
        s=sent+s+back
        print(s)

if __name__=="__main__":
    main()

Submission status: Accepted

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

front_s = ''
back_s = ''
rev_flg = False

for i in range(q):
    query = list(map(str, input().split())) 
    if query[0] == '1':
        rev_flg = not rev_flg
    else:
        if not rev_flg:
            if query[1] == '1':
                front_s += query[2]
            else:
                back_s += query[2]
        else:
            if query[1] == '1':
                back_s += query[2]
            else:
                front_s += query[2]
    
res = front_s[::-1] + s + back_s
if rev_flg: res = res[::-1]
print(res)

Submission status: Accepted

Solution code:
s=input()
l=len(s)
q=int(input())
cnt=1
l=str()
r=str()

for _ in range(q):
	a=list(input().split())
	if a[0]=="1":
		cnt*=(-1)
	else:
		if int(a[1])*cnt==1 or int(a[1])*cnt==-2:
			l+=a[2]
		else:
			r+=a[2]				

s=l[::-1]+s+r
if cnt==-1:
	s=s[::-1] 	

print(s)

Submission status: Accepted

Solution code:
# ABC158D

s = input()
q = int(input())
ht = 1
head = ''
tail = ''
count = 0
for i in range(q):
    txt3 = input().split()
    if txt3[0] == '1':
        ht = 3 - ht
        count += 1
    else:
        if int(txt3[1]) == ht:
            head = txt3[2] + head
        else:
            tail = tail + txt3[2]
s = head + s + tail
if count % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque

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

t = deque(S)
reverse = False
for _ in range(Q):
    query = input()
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        _, F, C = query.split()
        if F == '1':
            if reverse:
                t.append(C)
            else:
                t.appendleft(C)
        elif F == '2':
            if reverse:
                t.appendleft(C)
            else:
                t.append(C)
result = ''.join(t)
if reverse:
    result = result[::-1]
print(result)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = -1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
    if i == q-1 and isR == 1:
        s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
query = [[1, 0, ''] for i in range(q)]
for i in range(q):
    tmp = input().split()
    if tmp[0] == '2':
        query[i][0] = int(tmp[0])
        query[i][1] = int(tmp[1])
        query[i][2] = tmp[2]

dire = 1
for i in range(q):
    if query[i][0] == 1:
        dire *= -1
    else:
        if (2*query[i][1]-3) * dire == 1:
            s.append(query[i][2])
        else:
            s.appendleft(query[i][2])

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

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
re=True
cnt=0
for i in range(Q):
    q=list(map(str,input().split()))
    if q[0]=='1':
        cnt+=1
        if re==True:
            re=False
        else:
            re=True
    else:
        if re==True:
            if q[1]=='1':
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1]=='2':
                S.appendleft(q[2])
            else:
                S.append(q[2])
if cnt%2==1:
    S.reverse()
print(*S,sep='')

Submission status: Accepted

Solution code:

s = list(input())
n = int(input())

flag = 0
s1 = []
s2 = s
for i in range(n):
    Q = input()
    if Q[0] == "2":
        if Q[2] == "1":
            if flag == 1:
                s2.append(Q[4])
            else:
                s1.append(Q[4])
        else:
            if flag == 0:
                s2.append(Q[4])
            else:
                s1.append(Q[4])
    else:
        flag ^= 1
s2 = s1[::-1] + s2
print("".join(s2) if flag == 0 else "".join(s2[::-1]))

Submission status: Accepted

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

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

T2query = []
begin = []
end = []
cnt=0
for i in range(q-1,-1,-1):
    if(query[i][0]=='1'):
        cnt+=1
        continue
    if(cnt%2==1):
        if(query[i][1]=='1'):
            end.append(query[i][2])
        else:
            begin.append(query[i][2])
    else:
        if(query[i][1]=='1'):
            begin.append(query[i][2])
        else:
            end.append(query[i][2])
wb = ''.join(begin)
we = ''.join(end)
we = we[::-1] 

if(cnt%2!=0):
    s = s[::-1]

s = wb + s + we

print(s)

Submission status: Accepted

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

direction = 1
head = []
tail = []
for i in range(Q):
    T = input()
    if T == '1':
        direction = direction * -1
    else:
        T = T.split()
        if T[1] == '1':
            if direction == 1:
                head.append(T[2])
            else:
                tail.append(T[2])
        else:
            if direction == 1:
                tail.append(T[2])
            else:
                head.append(T[2])

head = head[::(-1)]
head.append(S)
head.extend(tail)
S = "".join(head)[::direction]

print(S)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
que=[]
count=0
left=[]
right=[]
for i in range(q):
  qu=list(map(str,input().split()))
  if qu[0]=="1":
    count+=1
  que.append(qu)
han=0
for i in range(q):
  if que[i][0]=="1":
    han+=1
  else:
    if que[i][1]=="1":  
      if (count-han)%2==0:
        left.append(que[i][2])
      else:
        right.append(que[i][2])
    elif que[i][1]=="2":  
      if (count-han)%2==0:
        right.append(que[i][2])
      else:
        left.append(que[i][2])
ls="".join(left[::-1])
rs="".join(right)
if count%2==0:
  print(ls+s+rs)
else:
  print(ls+s[::-1]+rs)

Submission status: Accepted

Solution code:
from collections import deque
queue = deque(input())
q = int(input())
li = [list(input().split()) for _ in range(q)]

flag = 1
for i in range(q):
    if int(li[i][0]) == 1:
        flag *= -1
        continue

    if int(li[i][1]) == 1:
        if flag == 1:
            queue.appendleft(li[i][2])
        else:
            queue.append(li[i][2])
    else:
        if flag == 1:
            queue.append(li[i][2])
        else:
            queue.appendleft(li[i][2])

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