Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Accepted

Solution code:
from collections import deque
S = deque([input()])
Q=int(input())
query=[[i for i in input().split()] for _ in range(Q)]

rf=0
for q in query:
    if q[0] == "1":
        rf ^= 1
    else:
        if q[1] == '1':
            if rf:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if rf:
                S.appendleft(q[2])
            else: 
                S.append(q[2])
print(''.join(S)[::-1] if rf else ''.join(S))

Submission status: Accepted

Solution code:
def op2(f, c, r):
  if r == 0:
    if f == "1":
      left.append(c)
    elif f == "2":
      right.append(c)

  if r == 1:
    if f == "1":
      right.append(c)
    elif f == "2":
      left.append(c)

S = input()
Q = int(input())
v = [list(input().split()) for i in range(Q)]
rev = 0
left = []
right = []

for val in v:
  if val[0] == "1":
    if rev == 0:
      rev = 1
    elif rev == 1:
      rev = 0
  elif val[0] == "2":
    op2(val[1], val[2], rev)

nleft = left[::-1]
ans = "".join(nleft + [S] + right)

if rev == 0:
  print(ans)
else:
  print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
q = int(input())
query = [list(input().split()) for i in range(q)]
now = 0
a = deque(s)
for i in query:
  if i[0] == "1":
    now ^= 1
  elif i[0] == "2":
    f,c = int(i[1]),i[2]
    if (f+now) % 2:
      a.appendleft(c)
    else:
      a.append(c)
if now:
  a.reverse()
ans = "".join(a)
print(ans)

Submission status: Accepted

Solution code:
S = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]
hanten = -1
atama = ""
shiri = ""
for row in range(quantity):
    each = L[row]
    if each[0] == "1":
        hanten = hanten * -1
    else:
        if each[1] == "1":
            if hanten == -1:
                atama = atama + each[2]
            else:
                shiri = shiri + each[2]
        else:
            if hanten == -1:
                shiri = shiri + each[2]
            else:
                atama = atama + each[2]

atama = atama[::-1]
ans = atama + S + shiri
if hanten == 1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
t  ={}
for i in range(q):
    t[i]=input().split()
rv_count = 0
x = "X"
for d in t.values():
	if d[0] == "1":
		rv_count += 1
	else:
		if rv_count % 2 ==0:
			if d[1] == "1":
				x2 = d[2]+x
			else:
				x2 = x+d[2]
			x = x2
		else:
			red = d[2][::-1]
			if d[1] == "1":
				x2 = x + red
			else:
				x2 = red+x
			x = x2

if rv_count%2 == 0:
    xf = x.replace("X", s)
else:
	xf = x.replace("X", s)[::-1]
    
print(xf)

Submission status: Accepted

Solution code:
S = input('')
Q = int(input(''))
head, tail, rev_flag = '', '', 1
for i in range(Q):
    char = input()
    if char == '1':
        rev_flag *= -1
    else:
        t, f, c = char.split()
        if (f == '1' and rev_flag == 1) or (f == '2' and rev_flag == -1):
            head = c + head
        elif (f == '2' and rev_flag == 1) or (f == '1' and rev_flag == -1):
            tail = tail + c
if rev_flag == 1:
    print(head, S, tail, sep='')
else:
    print(tail[::-1], S[::-1], head[::-1], sep='')

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(Q):
    v = list(input().split())
    if v[0] == '1':
        SWITCH = not SWITCH

    else:
        if v[1] == '1':
            if SWITCH==False:
                S.appendleft(v[2])
            else:
                S.append(v[2])
        else:
            if SWITCH==False:
                S.append(v[2])
            else:
                S.appendleft(v[2])

S = ''.join(S)

if SWITCH:
    print(S[::-1])
else:
    print(S)

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
lst=[]
for _ in range(Q):
    lst.append(input().split())

import collections

str=list(S)
d=collections.deque(str)

right=True
for operation in lst:
    if operation[0]=='1':
       right=not right

    else:
        if right==True and operation[1]=='1' or right==False and operation[1]=='2':
            d.appendleft(operation[2])
        else:
            d.append(operation[2])
str=list(d)
if right==True:
    print(''.join(str))
else:
    print(''.join(str[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

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

S = 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:
                S.append(C)
            else:
                S.appendleft(C)
        elif F == '2':
            if reverse:
                S.appendleft(C)
            else:
                S.append(C)
            
result = ''.join(S)
if reverse:
    result = result[::-1]
print(result)

Submission status: Accepted

Solution code:
def resolve():
    import collections
    S = input()
    Q = int(input())

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

    T_cnt_list = collections.deque([0])
    left = ''
    right = ''

    for i, query in enumerate(query_list):
        if query[0] == '1':
            T_cnt_list.append(1+T_cnt_list[-1])

        else:
            T_cnt_list.append(T_cnt_list[-1])

            if query[1] == '1' and T_cnt_list[i+1]%2 == 0:
                left = query[2] + left

            elif query[1] == '1' and T_cnt_list[i+1]%2 == 1:
                right = right + query[2]

            elif query[1] == '2' and T_cnt_list[i+1]%2 == 0:
                right = right + query[2]

            elif query[1] == '2' and T_cnt_list[i+1]%2 == 1:
                left = query[2] + left

    if T_cnt_list[-1]%2 == 0:
        print(left + S + right)
    else:
        print(right[::-1] + S[::-1] + left[::-1])

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]
key = 0  #0 start
former = []
latter = []

for i in range(kai):
    if(data[i][0] == '1'):
        key = (key + 1) % 2
    elif((int(data[i][1]) + key) % 2 == 1):
        former.append(data[i][2])
    else:
        latter.append(data[i][2])

string = ''.join(former[::-1]) + string + ''.join(latter)
if(key == 0):
    print(string)
else:
    print(string[::-1])

Submission status: Accepted

Solution code:

s = input()
q = int(input())
turn = 0
# ともにSに近い方から並べる
before = "" 
after = ""
for _ in range(q):
    tmp = input()
    if tmp=="1":
        turn += 1
    else:
        _,f,c = tmp.split()
        f = int(f)
        if (turn + f) % 2 == 1:
            before += c
        else:
            after += c
if turn%2==0:
    out = before[::-1] + s + after
else:
    out = after[::-1] + s[::-1] + before
print(out)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
n = int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s.appendleft(l[i][2])
            else:
                s.append(l[i][2])
        else:
            if l[i][1]=='1':
                s.append(l[i][2])
            else:
                s.appendleft(l[i][2])

if x == 0:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Accepted

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

front = ""
back = ""#前と後ろに分ける最後につなげてリバース

now_swap = 0
q=int(input())
for i in range(q):
    a = input().strip()
    a = list(a)
    
    if a[0] == "1":
        if now_swap == 0:
           now_swap = 1
        else: #now_step == 1:
            now_swap = 0
    else:
        if now_swap == 1:
            if a[2] == "1":
               # print("back")
                back+=a[4]

            else: #a[1]==2:
               ## print("f 2")
                front = a[4] + front

        else: #now_step == 0:
            if a[2] == "1":
                front = a[4] + front

            else: #a[1]==2:
                #print("hj")
                back+=a[4]
ans= front + s + back
#print(front,a,back)
if now_swap == 1:#合計数が奇数なら
    
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

flag = 1
for i in range(Q):
    qry = input().split()

    if qry[0] == '1':
        if flag:
            flag = 0
        else:
            flag = 1
    
    elif qry[0] == '2':
        if flag:
            if qry[1] == '1':
                S.appendleft(qry[2])
            else:
                S.append(qry[2])
        else:
            if qry[1] == '1':
                S.append(qry[2])
            else:
                S.appendleft(qry[2])

S = ''.join(S)

if flag:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
class Node:
    def __init__(self, char: str):
        self.before = None
        self.char = char
        self.next = None

class Linked_List:
    def __init__(self):
        self.head = None
        self.tail = None
    
    def add_head(self, node):
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            node.next = self.head
            self.head.before = node
            self.head = node

    def add_tail(self, node):
        if self.tail is None:
            self.head = node
            self.tail = node
        else:
            node.before = self.tail
            self.tail.next = node
            self.tail = node

    def show(self, is_reversed: bool):
        ret = []
        node = self.tail if is_reversed else self.head
        while not node is None:
            c = ''.join(list(node.char)[::-1]) if is_reversed else node.char
            ret.append(c)
            node = node.before if is_reversed else node.next
        print(''.join(ret))

S = input()
Q = int(input())
ll = Linked_List()
ll.add_head(Node(S))
is_reversed = False

for _ in range(Q):
    query = input()
    if query[0] == '1':
        is_reversed = False if is_reversed else True
    else:
        t, f, c = query.split()
        if is_reversed:
            c = ''.join(list(c)[::-1])
        if f == '1':
            if is_reversed:
                ll.add_tail(Node(c))
            else:
                ll.add_head(Node(c))
        else:
            if is_reversed:
                ll.add_head(Node(c))
            else:
                ll.add_tail(Node(c))

ll.show(is_reversed)

Submission status: Accepted

Solution code:
from collections import deque

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

t=True

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

S=''.join(S)

if not t:
  S=S[::-1]

print(S)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
Q = int(input())
    
t = True
    
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        t = not t
    else:
        if(t and query[1] == '1') or (not t and query[1] == '2'):
            s.appendleft(query[2])
        else:
            s.append(query[2])
            
s = ''.join(s)
    
if not t:
    s = s[::-1]
        
print(s)

Submission status: Accepted

Solution code:
from collections import deque

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

t=deque(S)
flag_r=0
for _ in range(Q):
    Query=input().split()
    if int(Query[0])==1:
        flag_r=(flag_r+1)%2
    elif int(Query[1])==1+flag_r:
        t.appendleft(Query[2])
    else:
        t.append(Query[2])
result=''.join(t)
if flag_r:# reverseは最後にやる
    result=result[::-1]
print(result)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
d = deque(list(S))
tf = 1

for i in range(Q):
    Queryi = list(map(str, input().split()))
    if Queryi[0] == '1':
        tf *= -1
    else:   #Queryi[0] == '2'
        if tf == 1:
            if Queryi[1] == '1':
                d.appendleft(Queryi[2])
            else: #Queryi[1] == '0'
                d.append(Queryi[2])
        else: #tf = -1
            if Queryi[1] == '1':
                d.append(Queryi[2])
            else: #Queryi[1] == '1'
                d.appendleft(Queryi[2])

if tf == -1:
    d.reverse()

print(''.join(d))

Submission status: Accepted

Solution code:
from collections import deque

L = deque([])
S = str(input())
q = int(input())
Q = [[] for _ in range(q)] 

for i in range(len(S)):
    L.append(S[i])

for i in range(q):
    A = input().split()
    Q[i] = A

num = 0

for i in range(q):
    if Q[i][0] == '1':
        num ^= 1
    if Q[i][0] == '2':
        if num == 0:
            if Q[i][1] == '1':
                L.appendleft(Q[i][2])
            else:
                L.append(Q[i][2])
        else:
            if Q[i][1] == '2':
                L.appendleft(Q[i][2])
            else:
                L.append(Q[i][2])

if num:
    L.reverse()

print(''.join(L))

Submission status: Accepted

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

X=deque(list(S))
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    else:
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            else:
                X.append(A[2])
        else:
            if f==-1:
                X.appendleft(A[2])
            else:
                X.append(A[2])

if f==-1:
    X.reverse()

print("".join(X))

Submission status: Accepted

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

X=deque(S)
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            if f==-1:
                X.append(A[2])
        if A[1]=="2":
            if f==-1:
                X.appendleft(A[2])
            if f==1:
                X.append(A[2])

if f==-1:
    print("".join(reversed(X)))
else:
    print("".join(X))

Submission status: Accepted

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

X=deque(S)
f=1
for _ in range(Q):
    A=list(input().split())
    if A[0]=="1":
        f*=-1
    if A[0]=="2":
        if A[1]=="1":
            if f==1:
                X.appendleft(A[2])
            if f==-1:
                X.append(A[2])
        if A[1]=="2":
            if f==-1:
                X.appendleft(A[2])
            if f==1:
                X.append(A[2])

if f==-1:
    print("".join(list(reversed(X))))
else:
    print("".join(list(X)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
S = deque(str(input()))
Q = int(input())
count = 0
for i in range(Q):
    tmp = list(input().split())
    if tmp[0]=="1":
        count += 1
    else:
        if tmp[1]=="1" and count%2==0 or tmp[1]=="2" and count%2==1:
            S.appendleft(tmp[2])
        elif tmp[1]=="2" and count%2==0 or tmp[1]=="1" and count%2==1:
            S.append(tmp[2])
if count%2==0:
    ans = "".join(S)
    print(ans)
else: 
    S.reverse()
    ans = "".join(S)
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
r=1
for i in range(Q):
    T=input().split()
    if T[0]=="1":
        r*=-1
        continue
    t=T[2]
    if T[1]=="1":
        if r==1:
            S.appendleft(t)
        else:
            S.append(t)
        continue
    if r==1:
        S.append(t)
    else:
        S.appendleft(t)
if r==1:
    print("".join(list(S)))
else:
    S.reverse()
    print("".join(list(S)))

Submission status: Accepted

Solution code:
import copy

S=input()
Q=int(input())
Query=[]
temp=[]

revflag=0
sbef=[]
saft=[]

for i in range(Q):
    temp=input().split()
    if len(temp) ==1:
        revflag += 1
    else:
        if (revflag + int(temp[1])-1 )%2 == 0:
            sbef.append(temp[2])
        else:
            saft.append(temp[2])

ans=""

for i in range(len(sbef)):
    ans+=sbef[len(sbef)-i-1]

ans += S
for i in range(len(saft)):
    ans+=saft[i]

tmp=""
tmp2 = copy.copy(ans)


if revflag%2 ==1:
    for i in range(len(ans)):
        tmp+=tmp2[len(ans)-i-1]
    ans=tmp


print(ans)

Submission status: Accepted

Solution code:
"""
    Author: _YL_
"""
s = input()
n = int(input())

flip = 0
left = 0
right = 1
strs = ["", ""]

i = 0
while i < n:
    lst = input().split()

    if lst[0] == "1":
        flip += 1
        left = 0 if left == 1 else 1
        right = 0 if right == 1 else 1
    elif lst[1] == "1":
        strs[left] += lst[2]
    else:
        strs[right] += lst[2]
    i += 1

if flip % 2 == 1:
    s = s[::-1]
    
s = strs[left][::-1] + s + strs[right]

print(s)

Submission status: Accepted

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

t = ''
for i in range(n):
    q = input().split()
    if len(q) == 1:
        t, s = s, t
    elif len(q) == 3:
        if q[1] == '1':
            t += q[2]
        elif q[1] == '2':
            s += q[2]

t = t[::-1]
print(t + s)

Submission status: Accepted

Solution code:
s = list(str(input()))
q = int(input())
w = [list(map(str,input().split())) for i in range(q)]

reverse = False
front_list = []
end_list = []

for data in w:
    if int(data[0]) == 1:
        reverse = not reverse
    else:
        if (int(data[1]) == 1 and not reverse) or (int(data[1]) == 2 and reverse):
            front_list.append(data[2])
        else:
            end_list.append(data[2])
if not reverse:
    front_list.reverse()
    print("".join(front_list + s + end_list))
else:
    end_list.reverse()
    s.reverse()
    print("".join(end_list + s + front_list))

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(Q):
    T, *fc = input().split()
    if T == "1":
        is_reversed = not is_reversed
        continue
    # 以下 T == "2"
    F, C = fc
    if F == "1":
        # 先頭に追加
        if is_reversed:
            S.append(C)
        else:
            S.appendleft(C)
    elif F == "2":
        # 末尾
        if is_reversed:
            S.appendleft(C)
        else:
            S.append(C)

ans = "".join(S) if not is_reversed else "".join(S)[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
que=deque(input())
q=int(input())
rev=False
for i in range(q):
    t=input()
    if t=='1':
        rev=not rev
    else:
        tt,f,c=t.split()
        if (rev==False and f=='1')or(rev and f=='2'):
            que.appendleft(c)
        else:
            que.append(c)
if rev:
    que.reverse()
print(''.join(que))

Submission status: Accepted

Solution code:
s = input()
n = int(input())
t = ''
for i in range(n):
    q = input().split()
    if len(q) == 1:
        t, s = s, t
    elif len(q) == 3:
        if q[1] == '1':
            t += q[2]
        elif q[1] == '2':
            s += q[2]

t = t[::-1]
print(t + s)

Submission status: Accepted

Solution code:
str = input()
Q = int(input())
left = []
right = []
flag = 1
for _ in range(Q):
    line = input().split()
    op1 = int(line[0])
    if op1 == 1:
        flag = -flag
    else:
        op2 = int(line[1])
        char = line[2]
        if (op2 == 1 and flag == 1) or (op2 == 2 and flag == -1) :
            left.append(char)
        else:
            right.append(char)

if flag == -1:
    right.reverse()
    str = str[::-1]
    str = "".join(right) + str
    str += "".join(left)
else:
    left.reverse()
    str = "".join(left) + str
    str += "".join(right)

print(str)

Submission status: Accepted

Solution code:
from collections import deque
a = input()
a = deque(a)
n = int(input())
tmp = 0
for i in range(n):
    q = input().split()
    if len(q) == 1:
        tmp += 1
    elif len(q) == 3:
        if q[1] == '1':
            if tmp % 2 == 0:
                a.appendleft(q[2])
            else:
                a.append(q[2])
        elif q[1] == '2':
            if tmp % 2 == 0:
                a.append(q[2])
            else:
                a.appendleft(q[2])

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
n = len(s)
flag = 0
mae = 0
ushiro = 0
b1 = ["a" for i in range(q)]
b2 = ["a" for i in range(q)]
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        flag += 1
    elif (flag+int(a[1])) % 2 == 1:
        b1[mae] = a[2]
        mae += 1
    else:
        b2[ushiro] = a[2]
        ushiro += 1

if flag % 2 == 0:
    for i in range(mae):
        print(b1[mae-1-i], end="")
    print(s, end="")
    for i in range(ushiro):
        print(b2[i], end="")
else:
    for i in range(ushiro):
        print(b2[ushiro-1-i], end="")
    print(s[::-1], end="")
    for i in range(mae):
        print(b1[i], end="")

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(len(ss)) :
  s.append(ss[i])

r = 0
for i in range(q) :
  t = list(map(str,input().split()))
  if t[0] == '1' :
    r += 1
  if t[0] == '2' :
    f = t[1] ; c = t[2]
    if r%2 == 0 :
      if f == '1' :
        s.appendleft(c)
      else :
        s.append(c)
    else :
      if f == '2' :
        s.appendleft(c)
      else :
        s.append(c)
        
if r%2 == 1 :
  s.reverse()
  
print(*s,sep='')

Submission status: Accepted

Solution code:
from collections import deque

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

query = []
rev=1

for i in range(q):
    query.append(input().split())

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

result="".join(list(s))
if rev==-1:
    result = result[::-1]
print(result)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
sento = 0
for i in range(q):
    line = list(input().split(" "))
    if line[0] == "1":
        sento += 1
    elif line[1] == "1":
        if sento % 2 == 0:
            s.appendleft(line[2])
        else:
            s.append(line[2])
    else:
        if sento % 2 == 0:
            s.append(line[2])
        else:
            s.appendleft(line[2])
if sento % 2 ==1:
    s.reverse()
for i in range(len(s)):
    print(s[i],end ="")
print()

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
Q_l = []
for i in range(Q):
    Q_l.append(input().split())
rev_flg = 0
head_list =[]
tail_list = []
for i in range(Q):
    if int(Q_l[i][0]) == 1:
        rev_flg += 1
    else:
        if rev_flg % 2 == 0 and int(Q_l[i][1]) == 1:
            head_list.append(Q_l[i][2])
        elif rev_flg % 2 == 1 and int(Q_l[i][1]) == 1:
            tail_list.append(Q_l[i][2])
        elif rev_flg % 2 == 0 and int(Q_l[i][1]) == 2:
            tail_list.append(Q_l[i][2])
        else:
            head_list.append(Q_l[i][2])
head_list.reverse()
head_list.extend(S)
head_list.extend(tail_list)
if rev_flg %2 ==1:
    head_list.reverse()
print("".join(head_list))

Submission status: Accepted

Solution code:

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=deque(input())
n=int(input())
r=False
for i in range(n):
    q=input()
    if q[0]=="1":
        r= not r
    else:
        t,f,c=map(str,q.split())
        if f=="1":
            if r==False:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if r==True:
                s.appendleft(c)
            else:
                s.append(c)
s=list(s)
if r==True:
    s=s[::-1]
print(*s,sep="")

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())

Q = int(input())

c = False
for i in range(Q):
  *T, = map(str,input().split())
  if len(T) == 1:
    c = not c  
  elif (int(T[1]) == 1 and not c) or (int(T[1]) == 2 and c):
    S.appendleft(T[2])
  else:
    S.append(T[2])

print("".join(S) if not c else "".join(list(reversed(S))))

Submission status: Accepted

Solution code:
s = input()
count = int(input())
flg = False
top = ""
for i in range(count):
    tfc = input().split()
    if tfc[0] == "1":
        flg = not flg
    elif tfc[0] == "2":
        if (not flg and tfc[1] == "1") or (flg and tfc[1] == "2"):
            top = top + tfc[2]
        elif (flg and tfc[1] == "1") or (not flg and tfc[1] == "2"):
            s = s + tfc[2]

if flg:
    s = s[::-1] + top
else:
    s = top[::-1] + s 
print(s)

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
q = [list(input().split()) for _ in range(Q)]
head = []
tail = []
flip = 0
for i in range(len(q)):

    if q[i][0] == "1":
        flip ^= 1
    else:
        if flip == 0 and q[i][1] == "1":
            head.append(q[i][2])
        elif flip == 0 and q[i][1] == "2":
            tail.append(q[i][2])
        elif flip == 1 and q[i][1] == "1":
            tail.append(q[i][2])
        else:
            head.append(q[i][2])

if flip == 0:
    ans = "".join(head[::-1] + S + tail)
    # print(head)
    # print(S)
    # print(tail)
    print( ans )
else:
    ans = "".join(tail[::-1] + S[::-1] + head)
    print( ans )

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(list(S))

rev = False

for q in range(Q):
    query = list(map(str, input().split()))
    if int(query[0]) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if int(query[1]) == 1:
            if not rev:
                d.appendleft(query[2])
            else:
                d.append(query[2])
        else:
            if not rev:
                d.append(query[2])
            else:
                d.appendleft(query[2])

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

Submission status: Accepted

Solution code:
a = input()
flag = 0
first = []
last = []
for i in range(int(input())):
    query = [j for j in input().split()]
    t = int(query[0])
    if t == 1:
        flag = 1 - flag
    else:
        x = int(query[1])
        if x == 1:
            if flag == 0:
                first.append(query[2])
            else:
                last.append(query[2])
        else:
            if flag == 0:
                last.append(query[2])
            else:
                first.append(query[2])

if flag == 0:
    print("".join(first)[::-1] + a + "".join(last))
else:
    print("".join(last)[::-1] + a[::-1] + ''.join(first))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
queries = []
for i in range(Q):
    query = [x for x in input().split()]
    query[0] = int(query[0])
    if query[0] == 2:
        query[1] = int(query[1])
    queries.append(query)

former = []
latter = []
is_normal = True
for query in queries:
    if query[0] == 1:
        is_normal = not is_normal
    
    else:
        if is_normal and query[1] == 1:
            former.append(query[2])
        elif not is_normal and query[1] == 1:
            latter.append(query[2])
        elif is_normal and query[1] == 2:
            latter.append(query[2])
        elif not is_normal and query[1] == 2:
            former.append(query[2])

if is_normal:
    ans = "".join(former[::-1]) + S + "".join(latter)
else:
    ans = "".join(latter[::-1]) + S[::-1] + "".join(former)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
ref=0
for i in range(Q):
    T=input().split()
    if T[0] == '2':
        if (T[1]=='1' and ref%2==0):
            S.appendleft(T[2])
        elif (T[1]=='2' and ref%2==0):
            S.append(T[2])
        elif (T[1]=='1' and ref%2==1):
            S.append(T[2])
        elif (T[1]=='2' and ref%2==1):
            S.appendleft(T[2])
    else:
        ref+=1
for i in range(len(S)):
    if ref%2==0:
        char=S.popleft()
        print(char,end='')
    else:
        char=S.pop()
        print(char,end='')

Submission status: Accepted

Solution code:
# 分岐を改良
from collections import deque

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

# flag = 0
flag = False  # フラグをブール値で。 Trueのとき反転
for i in range(Q):
    query = input().split()

    if query[0] == '1':
        # flag = flag ^ 1
        flag = not flag
        continue

    if query[0] == '2':
        if (query[1] == '1' and flag is False) or (query[1] == '2' and flag is True):
            S.appendleft(query[2])
        else:
            S.append(query[2])

if flag is True:
    # deque型はスライスで取り出せない print(''.join(S[::-1]))
    S.reverse()
print(''.join(S))