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:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key



__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':
#     S = input()
#     Q = intinput()
#     queries = []
#     for q in range(Q):
#         query = list(input().split())
#         queries.append(query)
#         if int(query[0]) == 1:
#             S = S[::-1]
#         else:
#             if int(query[1]) == 1:
#                 S = query[2] + S
#             else:
#                 S = S + query[2]
# 
#     # 出力
#     print(S)

    S = input()
    Q = intinput()
    queries = []
    prefix = []
    suffix = []
    direction = 0
    for q in range(Q):
        query = list(input().split())
        queries.append(query)
        if int(query[0]) == 1:
            direction += 1
        else:
            dl = int(query[1])
            dm = direction % 2
            if dl == 1 and dm == 0:
                # head
                prefix.append(query[2])
            elif dl == 1 and dm == 1:
                # tail
                suffix.append(query[2])
            elif dl == 2 and dm == 0:
                # tail
                suffix.append(query[2])
            else:
                # head
                prefix.append(query[2])

    if direction % 2 == 1:
        S = S[::-1]
        prefix, suffix = suffix, prefix

    p = "".join(prefix[::-1])
    s = "".join(suffix)
    print(p+S+s)

Submission status: Accepted

Solution code:

from sys import stdin
import sys

def getInputs():
	inputs=[]
	for line in stdin:
		line=line.split()
		inputs.append(line)
	return inputs

def rev(s):
	return s[-1::-1]

# def main(inputs):
# 	s=inputs[0][0]
# 	q=int(inputs[1][0])
# 
# 	for qi in range(q):
# 		ti=int(inputs[qi+2][0])
# 		if ti==1:
# 			s=rev(s)
# 		else:
# 			f=int(inputs[qi+2][1])
# 			c=inputs[qi+2][2]
# 			if f==1:
# 				s=c+s
# 			else:
# 				s=s+c
# 	print(s)


class E:
	def __init__(self, c, pre, nex):
		self.c=c
		self.pre=pre
		self.nex=nex
		

	
def main(inputs):
	s=inputs[0][0]
	q=int(inputs[1][0])
	
	prev=None
	for ci,c in enumerate(s):
		e=E(c, prev, None)
		if prev is not None: prev.nex=e
		prev=e
		if ci==0:
			head=e
	tail=e
	
	size=len(s)
	order=0 #0: forward, 1: backward
	for qi in range(q):
		ti=int(inputs[qi+2][0])
		if ti==1:
			order=1-order
		else:
			f=int(inputs[qi+2][1])
			c=inputs[qi+2][2]
			e=E(c, None, None)
			size+=1
			if f==1:
				if order==0:
					e.nex=head
					head.pre=e
					head=e
				else:
					e.pre=tail
					tail.nex=e
					tail=e
			else:
				if order==1:
					e.nex=head
					head.pre=e
					head=e
				else:
					e.pre=tail
					tail.nex=e
					tail=e
	
	cList=[None]*size
	if order==0:
		e=head
		for i in range(size):
			cList[i]=e.c
			e=e.nex
	else:
		e=tail
		for i in range(size):
			cList[i]=e.c
			e=e.pre
	
	print("".join(cList))
	

if __name__=="__main__":
	inputs=getInputs()
# 	inputs=simInputs()
	main(inputs)

Submission status: Accepted

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

rev_flag = 1
head_str = ""
tail_str = ""
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[1] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str = query[2] + head_str
        else:
            tail_str += query[2]

ans = head_str + S + tail_str
print(ans if rev_flag == 1 else ans[::-1])

Submission status: Accepted

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

    isReversed = False
    for i in range(Q):
        query = list(input().split())
        T = int(query[0])
        if T == 1:
            if isReversed:
                isReversed = False
            else:
                isReversed = True
        if T == 2:
            F = int(query[1])
            C = query[2]
            # 先頭
            if (isReversed and F == 2) or (not isReversed and F == 1):
                S.appendleft(C)
            # 末尾
            else:
                S.append(C)
        
    ans = ""
    if isReversed:
        ans = list(S)[::-1]
    else:
        ans = list(S)
        
    print("".join(ans))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
S = [S[i] for i in range(len(S))]
S = deque(S)                         
Q = int(input())
rev = 1
for i in range(Q):
  comm = input()
  if comm == "1":
    rev *= -1
  else:
    T, F, C = comm.split()
    F = int(F) * 2 - 3
    if rev * F == 1:
      S.append(C)
    else:
      S.appendleft(C)
S = list(S)
ans = ""
for i in range(len(S)):
  ans += S[i * rev + (rev-1) // 2]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
s = deque(s)
q = int(input())
flag = ''
count = 0
for i in range(q):
    que = list(map(str,input().split()))
    if que[0] == "1":
        count += 1
        if flag == 'one':
          flag = ''
        else:
          flag = 'one'
    else:
        if que[1] == '1':
            if flag == 'one':
                s.append(que[2])
            else:
                s.appendleft(que[2])
        elif que[1] == '2':
            if flag == 'one':
                s.appendleft(que[2])
            else:
                s.append(que[2])
if count % 2 == 0:
  for i in s:
    print(i,end="")
else:
  for i in reversed(s):
    print(i,end="")

Submission status: Accepted

Solution code:
from collections import deque
S=list(input())
Q=int(input())
A=deque()
O=deque()
flag=0
for i in range(Q):
    q=input()
    if q=='1':
        if flag==0:
            flag=1
        else:
            flag=0
    else:
        t,f,c=q.split()
        if flag==0:
            if f=='1':
                A.appendleft(c)
            else:
                O.append(c)
        else:
            if f=='1':
                O.append(c)
            else:
                A.appendleft(c)
if flag==0:
    R=list(A)+S+list(O)
else:
    R=list(O)[::-1]+S[::-1]+list(A)[::-1]  
print(''.join(R))

Submission status: Accepted

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

prev = ""
aft = ""
rev = 0
for i in range(q):
	g = input()
	if len(g) == 1:
		rev += 1
	else:
		f = int(g[2])
		c = g[4]
		if f == 2:
			if rev%2 == 1:
				prev = c + prev
			else:
				aft = aft + c
		else:
			if rev%2 == 0:
				prev = c + prev
			else:
				aft = aft + c
if rev%2 == 1:
	print(aft[::-1]+s[::-1]+prev[::-1])
else:
	print(prev+s+aft)

Submission status: Accepted

Solution code:
from collections import deque

def switch(flag):
    if flag==1:
        return 2
    else:
        return 1

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


s = deque(s)

flag = 0
hanten = 1
counter = 0
for i in range(n):
    # 入力
    tmp = list(input().split())
    t = int(tmp[0])
    if len(tmp) == 3:
        f = int(tmp[1])
        c = tmp[2]
    #######################

    if t == 1:
        hanten = switch(hanten)
        counter += 1
    if t == 2:
        if (f == 1 and hanten == 1) or (f==2 and hanten==2):
            s.appendleft(c)
        if (f == 2 and hanten == 1) or (f==1 and hanten ==2):
            s.append(c)
    
if counter%2 !=0:
    s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = deque(input())
    Q = int(input())
    revFlag = False
    for i in range(Q):
        tmp = list(input().split())
        if tmp[0] == "1":
            if revFlag:
                revFlag = False
            else:
                revFlag = True
        else:
            if tmp[1] == "1":
                if revFlag:
                    S.append(tmp[2])
                else:
                    S.appendleft(tmp[2])
            else:
                if revFlag:
                    S.appendleft(tmp[2])
                else:
                    S.append(tmp[2])
    if revFlag:
        S = list(S)
        S.reverse()
        print("".join(S))
    else:
        S = list(S)
        print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
t = 0
form = ""
late = ""
for i in range(q):
  l = list(input().split())
  if l[0] == "1":
    t += 1
  else:
    if t % 2 == 0:
      if l[1] == "1":
        form = l[2] + form
      else:
        late += l[2]
    else:
      if l[1] == "1":
        late += l[2]
      else:
        form = l[2] + form
if t % 2 == 0:
  ans = form + s + late
else:
  ans = late[::-1] + s[::-1] + form[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys

input = lambda: sys.stdin.readline().rstrip()

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

rev_cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '2':
        if (query[1] == '1' and rev_cnt % 2 == 0) or ((query[1] == '2' and rev_cnt % 2 == 1)):
            s.appendleft(query[2])
        else:
            s.append(query[2])
    else:
        rev_cnt += 1

if rev_cnt % 2 == 1:
    print(''.join([s[len(s) - i - 1] for i in range(len(s))]))
else:
    print(''.join([c for c in s]))

Submission status: Accepted

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

sg = 1
fr = []
ba = []
for x in range(q):
  query = [s for s in input().split()]
  if query[0] == '1':
    sg *= -1
  else:
    if int(query[1])*2 + sg == 3:
      fr.append(query[2])
    else:
      ba.append(query[2])

if sg == 1:
  fr.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(frn + s + ban)
else:
  ba.reverse()
  frn = ''.join(fr)
  ban = ''.join(ba)
  print(ban + ''.join(list(reversed(s))) + frn)

Submission status: Accepted

Solution code:
from collections import deque

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

deq = deque(S)

direction = 1

for _ in range(Q):
  q = input()
  if q == '1':
    direction *= -1
  else:
    _, f, c = q.split()
    if direction > 0:
      if f == '1':
        deq.appendleft(c)
      else:
        deq.append(c)
    else:
      if f == '1':
        deq.append(c)
      else:
        deq.appendleft(c)

if direction < 0:
  deq.reverse()
        
print(''.join(deq))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
mae_usiro = [[],[]]
rev = 0
for _ in range(Q):
    temp = input().split()
    T = temp[0]
    if T =="1":
        rev += 1
    else:
        F = temp[1]
        C = temp[2]
        if F == "1": #前
            mae_usiro[rev % 2].append(C)
        else: # 後ろに付ける
            mae_usiro[(rev + 1) % 2].append(C)

# print(mae_usiro)
mae =  mae_usiro[0]
usiro= mae_usiro[1]
if rev % 2 == 1: #奇数回の反転なら
    S = S[::-1] #元の文字列は反転
    usiro.reverse()
    ans = "".join(usiro) + S + "".join(mae)
else:
    mae.reverse()
    ans = "".join(mae) + S + "".join(usiro)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    Q = int(input())

    s = deque(S)
    left, right = s.appendleft, s.append
    rev = 0
    for _ in [0] * Q:
        T = input().split()
        if T[0] == '1':
            left, right = right, left
            rev += 1
        else:
            f, c = T[1], T[2]
            if f == '1':
                left(c)
            else:
                right(c)

    if rev % 2:
        s.reverse()
    print(''.join(s))


main()

Submission status: Accepted

Solution code:
#
import sys
import math
import numpy as np
import itertools

# ひとつ入力
s = list(input())
# ひとつ入力
q = int(input())

t=0
a=[]
b=[]

for i in range(q):
    query = input().split()
#    print(query)
    if query[0]=='1':
        t = (t+1)%2
    else:
        if (t+int(query[1]))%2==0:
            b.append(query[2])
        else:
            a.append(query[2])
p=[]
#print(a,s,b)
if t == 0:
    a.reverse()
    p.extend(a)
    p.extend(s)
    p.extend(b)
else:
    b.reverse()
    s.reverse()
    p.extend(b)
    p.extend(s)
    p.extend(a)

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

Submission status: Accepted

Solution code:
s = input()
n = int(input())
rev = 0
rev_check = False
first = list()
last = list()
for i in range(n):
    q = list(input().split())
    if q[0] == "1":
        rev += 1
        rev_check = not rev_check
    else:
        if rev_check ==  True:
            if q[1] == "1":
                last.append(q[2])
            else:
                first.append(q[2])
        else:
            if q[1] == "1":
                first.append(q[2])
            else:
                last.append(q[2])
if rev_check == False:
    print("".join(reversed(first)) + s + "".join(last))
else:
    print("".join(reversed(last)) + s[::-1] + "".join(first))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
r = False
F = []
B = []
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    if r:
      r = False
    else:
      r = True
  else:
    if (q[1] == '1' and not r) or (q[1] == '2' and r):
      F.append(q[2])
    else:
      B.append(q[2])

if r:
  print(''.join(B[::-1]+list(reversed(S))+F))
else:
  print(''.join(F[::-1]+list(S)+B))

Submission status: Accepted

Solution code:
from collections import deque

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

qs = [list(input()) for _ in range(q)]

d = deque([])

for c in s:
    d.append(c)

flag_r = False

for qq in qs:
    if qq[0] == '1':
        flag_r = not flag_r
    else:
        if flag_r:
            if qq[2] == '1':
                d.append(qq[4])
            else:
                d.appendleft(qq[4])
        else:
            if qq[2] == '1':
                d.appendleft(qq[4])
            else:
                d.append(qq[4])

if flag_r:
    d.reverse()

print(''.join(d))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def main():
    S = input()
    Q = int(input())
    flag = False
    head = []
    tail = []
    for n in range(Q):
        query = input()
        if query[0] == '1':
            flag = not flag
        elif query[0] == '2':
            command = query.split()
            F = int(command[1])
            C = command[2]
            if (not flag and F == 1) or (flag and F == 2):
                head.append(C)
            else:
                tail.append(C)
    result = ''.join(reversed(head)) + S + ''.join(tail)
    if flag:
        result = result[::-1]
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
REVERSE=False
for _ in range(q):
  Input=input().split()
  if len(Input)==1:
    REVERSE = not REVERSE
  else:
    f=int(Input[1])
    c=Input[2]
    if not REVERSE:
      if f==1:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f==1:
        s.append(c)
      else:
        s.appendleft(c)
if not REVERSE:
  print("".join(s))
else:
  print("".join(reversed(s)))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(list(str(input())))
q = int(input())
que = [list(map(str, input().split())) for _ in range(q)]

r_cnt=0
for query in que:
    if len(query) == 1:
        r_cnt+=1
    else:
        if int(query[1]) == 1:
            if r_cnt % 2 == 1:
                s.append(query[2])
            else:
                s.appendleft(query[2])
        else:
            if r_cnt % 2 == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])

for _ in range(r_cnt % 2):
    s.reverse()

print(*s,sep='')

Submission status: Accepted

Solution code:
from collections import deque
flag = 1
s = deque(input())
q = int(input())
for i in range(q):
         z = list(map(str,input().split()))
         if z[0] == "1":
                  flag = flag*(-1)
         else:
                  if (flag == 1 and z[1] == "1")  or (flag == (-1) and z[1] == "2"):
                           s.appendleft(z[2])
                  else:
                           s.append(z[2])
if flag == (-1):
         s.reverse()
ans = "".join(s)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(q):
    query = input()
    if query == "1":
        reversed_flag = not reversed_flag
    else:
        t, f, c = query.split()
        if f == "1":
            if reversed_flag:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reversed_flag:
                s.appendleft(c)
            else:
                s.append(c)

if reversed_flag:
    s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
re_flag = False 
start = deque()
finish = deque()
for i in range(Q):
    l = input()
    if l == "1":
        re_flag = not bool(re_flag)
    else:
        t, f, c = l.split()
        if f == "1":
            if re_flag:
                finish.append(c)
            else:
                start.append(c)
        else:
            if re_flag:
                start.append(c)
            else:
                finish.append(c)

S.extend(finish)
S.extendleft(start)

ans = deque()
if re_flag:
    ans.extendleft(S)
else:
    ans = S

print("".join(list(ans)))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = deque(input())
    Q = int(input())
    rev_flag = False
    for _ in range(Q):
        query = input().split()
        if query[0] == "1":
            rev_flag = not(rev_flag)
            continue
        if query[1] == "1":
            if rev_flag:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        else:
            if rev_flag:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    
    if rev_flag:
        S.reverse()

    print("".join(list(S)))
        
if __name__ == "__main__":
    # print(timeit.timeit(lambda: main(), number=1))
    main()

Submission status: Accepted

Solution code:
import sys
from pprint import pprint
from collections import deque

S = list(sys.stdin.readline().strip())
Q = int(sys.stdin.readline().strip())

q = deque(S)

n_rev = 0
for _ in range(Q):
    tmp = sys.stdin.readline().strip().split()
    if tmp[0] == "1":
        n_rev += 1
    else:
        f, c = tmp[1], tmp[2]
        if f == "1":
            if n_rev % 2 == 0:
                #S = c + S
                q.appendleft(c)
            else:
                #S = S + c
                q.append(c)
        else:
            if n_rev % 2 == 0:
                #S = S + c
                q.append(c)
            else:
                #S = c + S
                q.appendleft(c)

S = "".join(list(q))
if n_rev % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
s=str(input())
q=int(input())
rev=1
rg=[]
lf=[]
for i in range(q):
  l=list(map(str,input().split()))
  if l[0]=='1':
    rev*=-1
  else:
    if l[1]=='1' and rev==1:
      lf.append(l[2])
    elif l[1]=='1':
      rg.append(l[2])
    if l[1]=='2' and rev==1:
      rg.append(l[2])
    elif l[1]=='2':
      lf.append(l[2])
res=[]
if rev==1:
  lf.reverse()
  res=lf+[s]+rg
else:
  rg.reverse()
  ns=list(reversed(s))
  res=rg+ns+lf
print(''.join(res))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
swc = 0
top = ""
bot = ""
for i in range(q):
	a = list(map(str, input().split()))
	if a[0] == "1":
		if swc == 0:
			swc = 1
		else:
			swc = 0
	else:
		if swc == 0:
			if a[1] == "1":
				top += a[2]
			else:
				bot += a[2]
		else:
			if a[1] == "1":
				bot += a[2]
			else:
				top += a[2]
if swc == 0:
	top = ''.join(list(reversed(top)))
	print (top + s + bot)
else:
	bot = ''.join(list(reversed(bot)))
	s = ''.join(list(reversed(s)))
	print (bot + s + top)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
Q = int(input())
forward = 0
for _ in range(Q):
    q = input()
    if q=='1':
        forward ^= 1
    else:
        _, f, c = q.split()
        if (int(f)-1)^forward==0:
            s.appendleft(c)
        else:
            s.append(c)
s = list(s)
if forward:
    for i in reversed(s):
        print(i, end='')
else:
    for i in s:
        print(i, end='')
print()

Submission status: Accepted

Solution code:

def main():
  S = input()
  Q = int(input())
  Queries = [input().split() for i in range(Q)]
  #print(Queries)
  is_reversed = False
  prefix = ''
  suffix = ''
  for Query in Queries:
    if Query[0] == '1':
      is_reversed = not is_reversed
    else:
      if (Query[1] == '1') ^ is_reversed:
        prefix = Query[2] + prefix
      else:
        suffix += Query[2]
  ans = prefix + S + suffix
  if not is_reversed:
    print(ans)
  else:
    print(''.join(list(reversed(ans))))

main()

Submission status: Accepted

Solution code:
from collections import deque
S = str(input())
q = int(input())
que =deque()
order = 1
for i in range (len(S)):
  que.append(S[i])
for j in range(q):
  Q = list(map(str, input().split()))
  T = Q[0]
  if T == "1":
        order *= -1
  elif T == "2":
    F = Q[1]
    C = Q[2]
    if F == "1":
      if order == 1:
        que.appendleft(C)
      elif order == -1:
        que.append(C)
    elif F == "2":
      if order == 1:
        que.append(C)
      elif order == -1:
        que.appendleft(C)
if order == 1:
  que_string = "".join(list(que))
  print(que_string)
else:
  que.reverse()
  que_string = "".join(list(que))
  print(que_string)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
qu = [input().split() for _ in range(q)]
le = deque()
ri = deque()
for i in range(len(s)):
    le.append(s[i])
    ri.appendleft(s[i])
x = 1
for i in range(q):
    if qu[i] == ['1']:
        x *= -1
    elif x == 1:
        ci = qu[i][2]
        if qu[i][1] == '1':
            le.appendleft(ci)
            ri.append(ci)
        else: 
            le.append(ci)
            ri.appendleft(ci)
    else: 
        ci = qu[i][2]
        if qu[i][1] == '1':
            le.append(ci)
            ri.appendleft(ci)
        else: 
            le.appendleft(ci)
            ri.append(ci)
y = ''
if x == 1:
    for i in le:
        y += i
else: 
    for i in ri:
        y += i
print(y)

Submission status: Accepted

Solution code:
from collections import deque

left = deque()
right = deque()

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

reverse = False
for i in range(q):
    t_inp = input().split()
    if t_inp[0] == "1":
        reverse = not reverse
    else:
        if t_inp[1] == "1" and reverse:
            right.append(t_inp[2])
        if t_inp[1] == "1" and not reverse:
            left.appendleft(t_inp[2])
        if t_inp[1] == "2" and reverse:
            left.appendleft(t_inp[2])
        if t_inp[1] == "2" and not reverse:
            right.append(t_inp[2])

l_li = list(left)
r_li = list(right)

# print(left)
# print(right)
ans = "".join(l_li)+s+"".join(r_li)

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

Submission status: Accepted

Solution code:
# D - String Formation

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

# 1の数
c = 0
l = ''
r = ''

for _ in range(Q):
    q = [str(x) for x in input().split()]
    if q[0] == '1':
        c += 1
        l, r = r, l
    elif q[1]=='1':
        l = l + q[2]
    else:
        r = r + q[2]

if c%2==1:
    S = ''.join(reversed(S))

l = ''.join(reversed(l))

print(l+S+r)

Submission status: Accepted

Solution code:
from collections import deque

if __name__ == "__main__":
    s = input()
    ans = deque()
    for ss in s:
        ans.append(ss)
    q = int(input())
    revflag = False

    for i in range(q):
        tmp = input().split()
        if tmp[0] == '1':
            if revflag:
                revflag = False
            else:
                revflag = True
        else:
            if tmp[1] == '1' and revflag:
                ans.append(tmp[2])
            elif tmp[1] == '1' and not revflag:
                ans.appendleft(tmp[2])
            elif tmp[1] == '2' and revflag:
                ans.appendleft(tmp[2])
            else:
                ans.append(tmp[2])

    if revflag:
        print(''.join(reversed(ans)))
    else:
        print(''.join(ans))

Submission status: Accepted

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

rev_flag = False

heads = []
tails = []

for i in range(Q):
  in_ = input()
  if len(in_) > 1:
    _, f, c = in_.split()
    f = int(f)
    
    if rev_flag:
      if f==1:
        #S = S + c
        tails.append(c)
      elif f==2:
        #S = c+S
        heads.append(c)
    else:
      if f==2:
        #S = S + c
        tails.append(c)
      elif f==1:
        #S = c + S
        heads.append(c)
    
  else:
    # reverse    
    rev_flag = not rev_flag

if rev_flag:
  ans = "".join(tails[-1::-1]) + S[-1::-1] + "".join(heads)
else:
  ans = "".join(heads[-1::-1]) + S + "".join(tails)

print(ans)

Submission status: Accepted

Solution code:
import sys
s=sys.stdin.readline().strip()

q=int(input())

p=1
front=''
back=''

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

    if len(slst)==1:
        p*=-1
    else:
        t,f,c=slst

        f=int(f)

        if (f*2-3)*p==1:
            back=back+c
        else:
            front=c+front

s=front+s+back

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

print(s)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
is_reversed = False
for i in range(q):
    query = input().split()
    if query[0] == '1':
        is_reversed = not is_reversed
    elif (query[1] == '2') ^ is_reversed:
        s.append(query[2])
    else:
        s.appendleft(query[2])
if is_reversed:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

d = 1
c = 1
for i in range(Q):
    que = list(input().split())
    if que[0] == str(1):
        d += c
        c *= -1
    else:
        if (int(que[1]) ^ d) == 0:
            S.appendleft(que[2])
        else:
            S.append(que[2])

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
x = 1
f, b = '', ''
for i in range(q):
  a = input().split()
  if a[0] == '1':
    x = -x
  elif (1.5-int(a[1]))*x > 0:
    f = a[2] + f
  else:
    b = b + a[2]
s = f + s + b
if x == -1:
  s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
hanten=0
s_1 = ""
s_2 = ""

for i in range(q):
    s1=input()
    if len(s1)==1:
        hanten+=1
    else:
        a,f,c=s1.split()
        f=int(f)
        if (f+hanten)%2==0:
            s_2 += c
        else:
            s_1 = c + s_1     

if hanten%2==0:
    print(s_1 + s + s_2)
else:
    s_3 = s_1 + s + s_2
    print(s_3[::-1])