Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Q_list = list()
for i in range(Q):
    Q_list.append(input().split())

from collections import deque
def operate(i):
    global count, str_list
    if Q_list[i][0] == '1':
        count += 1
    else:
        if count % 2 == 0:
            if Q_list[i][1] == '1':
                str_list.appendleft(Q_list[i][2])
            else:
                str_list.append(Q_list[i][2])
        else:
            if Q_list[i][1] == '1':
                str_list.append(Q_list[i][2])
            else:
                str_list.appendleft(Q_list[i][2])

check = 0
for i in range(Q):
    if Q_list[i][0] == '1':
        check += 1

S_list = [i for i in S]
if check % 2 == 1:
    S_list = list(reversed(S_list))

S_n = ''.join(S_list)

str_list = deque()
str_list.append(S_n)
count = check%2
for i in range(Q):
    operate(i)
print(''.join(str_list))

Submission status: Accepted

Solution code:
from collections import deque

d = deque()
s = input()
d.append(s)
rev = 0
q = int(input())

for i in range(q):
  query = list(input().split())
  if (int(query[0]) == 1):
    rev = (rev + 1) % 2
  else:
    if ((int(query[1]) + rev) % 2 == 0):
      d.append(query[2])
    else:
      d.appendleft(query[2])

ans = ''.join(list(d))
if rev == 1:
  ans = ans[::-1]

print(ans)

Submission status: Accepted

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

rev = False

for _ in range(q):
    t = [x for x in input().split()]
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            s.append(t[2]) if rev else s.appendleft(t[2])
        else:
            s.appendleft(t[2]) if rev else s.append(t[2])

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

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
rev = 0
for i in range(Q):
  query = input()
  if query == "1": rev = 1 - rev 
  elif int(query[2]) + rev == 2: S.append(query[4])
  else:S.appendleft(query[4])
if rev == 1: S.reverse()  
print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque

#inp = list(map(lambda x: int(x), input().split()))
#n,k = map(lambda x: int(x), input().split())
#p_n = list(map(lambda x:int(x),input().split()))


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


cnt = 0
for i in range(q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
    else:
        if inp[1] == "1":
            if cnt % 2 == 0:
                s.appendleft(inp[2])
            else:
                s.append(inp[2])
        else:
            if cnt % 2 == 0:
                s.append(inp[2])
            else:
                s.appendleft(inp[2])

ans = list(s)

if cnt % 2 == 1:
    ans = ans[::-1]

print(''.join(ans))

Submission status: Accepted

Solution code:
def resolve():
    from collections import deque
    d=deque()
    s=list(input())
    d.extend(s)
    #print(d)
    q=int(input())
    Q=[list(map(str,input().split())) for i in range(q)]

    reverse= False
    for i in range(q):
        if int(Q[i][0])==1:
            reverse=not reverse
        elif int(Q[i][0])==2:
            if int(Q[i][1])==1:
                if reverse:
                    d.append(Q[i][2])
                else:
                    d.appendleft(Q[i][2])
                #print(s)
            else:
                if reverse:
                    d.appendleft(Q[i][2])
                else:
                    d.append(Q[i][2])
                #print(s)

    if reverse:
        print(''.join(list(reversed(d))))
    else:
        print(''.join(list(d)))






resolve()

Submission status: Accepted

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

li = [input() for i in range(q)]
k = 0
ans = [s]
ans = deque(ans)

for i in li:
    if i == "1":
        k += 1
    else:
        if (int(i[2]) + k) % 2 == 0:
            ans.append(i[4])
        else:
            ans.appendleft(i[4])

ans = list(ans)
s = "".join(ans)
if k % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()
q = int(input())

r=0

a=""
z=""

for i in range(q):
	t = input()
	
	if t=='1':
		if r==0:
			r=1
		else:
			r=0
#		p("t,r,s",t,r,s)
		continue
	
	_,f,c = t.split()

	if f=='1':
		if r==0: a = a + c
		if r==1: z = z + c
	if f=='2':
		if r==0: z = z + c
		if r==1: a = a + c


#	p("t,r,_,f,c,x,s","[",t,"]",r,_,f,c,x,s)

ans=""
if (r==0):
	ans = a[::-1] + s + z
else:
	ans = z[::-1] + s[::-1] + a

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

s = deque(input().rstrip())

sw = True

nq = int(input().rstrip())
for _ in range(nq):
    query = input().rstrip()
    if query[0] == '1':
        sw = (sw == False)
    else:
        c = query[4]
        if (query[2] == '1') == sw: 
            s.appendleft(c)
        else:
            s.append(c)
if sw:
    print("".join(s))
else:
    print("".join(reversed(s)))

Submission status: Accepted

Solution code:
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = int(q[1])
    if conr % 2 == 0:
      if f == 1:
        D.appendleft(s)
      else:
        D.append(s)
    else:
      if f == 1:
        D.append(s)
      else:
        D.appendleft(s)

D = list(D)
if conr % 2 == 1:
  D = D[::-1]
ans = "".join(D)
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = 1
head = ""
tail = ""
for i in range(Q):
    a = input().split()
    if a[0] == "1":
        reverse *= -1
    elif a[1] == "1":
        if reverse == 1:
            head += a[2]
        else:
            tail += a[2]
    else:
        if reverse == 1:
            tail += a[2]
        else:
            head += a[2]
# for i in range(Q):
#     a = input().split()
#     if a[0] == "1":
#         reverse *= -1
#     elif a[1] == "1":
#         if reverse == 1:
#             S = a[2] + S
#         else:
#             S = S + a[2]
#     else:
#         if reverse == 1:
#             S = S + a[2]
#         else:
#             S = a[2] + S
S = head[::-1] + S + tail
if reverse == -1:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque

S = list(input())
Q = int(input())
Query = []
cnt_re = 0
for i in range(Q):
    tmp = input().split()
    Query.append(tmp)
    if (len(tmp) == 1):
        cnt_re += 1
prev = deque()
after = deque()
if (cnt_re % 2 != 0):
    S = list(reversed(S))
for q in Query:
    if (len(q) == 3):
        if q[1] == '1':
            if (cnt_re % 2 == 0):
                prev.appendleft(q[2])
            else:
                after.append(q[2])
        else:
            if (cnt_re % 2 == 0):
                after.append(q[2])
            else:
                prev.appendleft(q[2])
    else:
        cnt_re -= 1
print(''.join(list(prev)) + ''.join(list(S)) + ''.join(list(after)))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
flag = True
atama = []  # 逆順
osiri = []
for i in range(q):
    inp = input()
    if inp == '1':
        flag = not flag

    else:
        _, f, c = map(str, inp.split())
        if f == '1':
            if flag:
                atama.append(c)
            else:
                osiri.append(c)
        else:
            if flag:
                osiri.append(c)
            else:
                atama.append(c)

if flag:
    for t in atama[::-1]:
        print(t, end='')
    print(s, end='')
    for t in osiri:
        print(t, end='')
else:
    for t in osiri[::-1]:
        print(t, end='')
    print(s[::-1],  end='')
    for t in atama:
        print(t, end='')

Submission status: Accepted

Solution code:


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

def addflont(s,c):
    text = "".join([c,s])
    return text
def addback(s,c):
    text = "".join([s,c])
    return text

reverse = 0
r = "";u = ""
for i in range(q):
    t = input()
    if t[0]=="1":
        reverse = reverse^1
    if t[0]=="2":
        if t[2]=="1":
            if reverse==0:
                r = addflont(r,t[4])
            else:
                u = addback(u,t[4])
        elif t[2]=="2":
            if reverse==0:
                u = addback(u,t[4])
            else:
                r = addflont(r,t[4])

s = r + s + u
if reverse==1:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
from collections import deque
d = deque(input())
q = int(input())
reverse = False
for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    else:
        if query[1] == '1':
            if reverse:
                d.append(query[2])
            else:
                d.appendleft(query[2])
        else:
            if reverse:
                d.appendleft(query[2])
            else:
                d.append(query[2])

if reverse:
    d = list(reversed(d))

print(''.join(map(str, d)))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
reverse_now = 0
first = deque([])
latter = ""
for i in range(q):
    k = list(input().split())
    if k[0] == "1":
        reverse_now = 1 - reverse_now
    else:
        f,c = k[1], k[2]
        if f == "1" and reverse_now == 0:
            first.appendleft(c)
        if f == "1" and reverse_now == 1:
            latter += c
        if f == "2" and reverse_now == 0:
            latter +=  c
        if f == "2" and reverse_now == 1:
            first.appendleft(c)
first = "".join(first)
if reverse_now == 1:
    ans = list(first + s + latter)
    ans.reverse()
    ans = "".join(ans)
else:
    ans = first + s + latter
print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
n = int(input())
flag = True
for _ in range(n):
    q = list(map(str, input().split()))
    if len(q) == 1:
        if flag:
            flag = False
        else:
            flag = True
    if len(q) == 3:
        if (q[1] == "1" and flag == False) or (q[1] == "2" and flag):
            s.append(q[2])
        else:
            s.appendleft(q[2])
if flag == False:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
import sys
from collections import deque

def main():
    input = sys.stdin.readline

    s = input()[:-1]
    q = int(input())
    tmp = [input().split() for _ in range(q)]

    flag = 0
    ans = deque([s])
    for t in tmp:
        if len(t) == 1:
            flag = ~flag 
        else:
            f = int(t[1])
            c = t[2]
            if (f == 1 and flag == 0) or (f ==2 and flag == -1):
                ans.appendleft(c)
            else:
                ans.append(c)
    if flag == 0:
        print(''.join(ans))
    else:
        print(''.join(ans)[::-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left= right = ""
for operation in qs:
    if operation[0] == '1':
        n = not n
    if operation[0] == '2':
        t , f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left=c+left
        elif f == 2:
            right+=c
s = left+s+right
print(s[::-1] if n else s)

Submission status: Accepted

Solution code:
from collections import deque

s = str(input())
q = int(input())
flip = -1
strf = deque()
strb = deque()

for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        flip = flip * -1
    elif query[0] == '2':
        if query[1] == '1':
            if flip == -1:
                strf.appendleft(query[2])
            else:
                strb.append(query[2])
        elif query[1] == '2':
            if flip == -1:
                strb.append(query[2])
            else:
                strf.appendleft(query[2])

# print(strf)
# print(strb)
ans = ''.join(strf) + s + ''.join(strb)
if flip == 1:
    ans = ans[::-1]
print(ans)

# s = str(input())
# q = int(input())
# flip = -1
# strs = '/'
 
# for i in range(q):
#     query = list(map(str, input().split()))
#     if query[0] == '1':
#         flip = flip * -1
#     elif query[0] == '2':
#         if query[1] == '1':
#             if flip == -1:
#                 strs = query[2] + strs
#             else:
#                 strs = strs + query[2]
#         else:
#             if flip == -1:
#                 strs = strs + query[2]
#             else:
#                 strs = query[2] + strs
 
# if flip == -1:
#     ans = strs.replace('/', s)
# else:
#     ans = strs[::-1].replace('/', s[::-1])
 
# print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
reversecount = 0
d = deque(S)
for x in query:
    if int(x[0]) == 1:
        reversecount += 1
    else:
        if (int(x[1]) == 1 and (reversecount % 2 == 0)) or (int(x[1]) == 2 and (reversecount % 2 == 1)):
            d.appendleft(x[2])
        else:
            d.append(x[2])
if reversecount % 2 == 1:
    d.reverse()
print(*d, sep='')

Submission status: Accepted

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

fwd = ""
rev = ""
now = 1

for i in range(Q) :
  Qn = list(map(str, input().split()))
  
  if Qn == ["1"] :
    now *=-1
  else :
    if now == 1 :
      if Qn[1] == "1" :
        fwd = Qn[2] + fwd
      if Qn[1] == "2" :
        rev = rev + Qn[2]
    else : 
      if Qn[1] == "1" :
        rev = rev + Qn[2]
      if Qn[1] == "2" :
        fwd = Qn[2] + fwd

ans = fwd + S + rev
if now == -1 :
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())
head = 0
s = deque(s)

for _ in range(q):
    query = sys.stdin.readline().split()
    if len(query) == 1:
        head = 1 if head == 0 else 0
    elif (query[1] == '1' and head == 1) or (query[1] == '2' and head == 0):
        s.append(query[2])
    else:
        s.appendleft(query[2])

if head == 1:
    s.reverse()

print(''.join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
c = 0
t = ""
u = ""
for i in range(q):
    qy = list(input().split())
    if qy[0] == "1":
        c += 1
    else:
        if (qy[1] == "1" and c % 2 == 0) or (qy[1] == "2" and c % 2 == 1):
            t = qy[2] + t
        else:
            u = u + qy[2]
ans = t + s + u
if c % 2 == 0:
    print(t + s + u)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
s = 0
s1 = []
s2 = []
for i in range(Q):
    q = list(input().split())
    s += int(q[0])
    if q[0]=='2':
        s_i = s + int(q[1]) 
        if s_i%2 == 0:
            s1.append(q[2])
        else:
            s2.append(q[2])
s1 = ''.join(s1)
s2 = ''.join(s2)
s2 = s2[::-1]
S = s2 + S + s1
if s%2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
from queue import deque
s = deque([i for i in input()])
Q = int(input())
 
f = 0
 
for _ in range(Q):
    q = input()
    if int(q[0]) == 1:
        f = abs(f-1)
        continue
    
    _, F, c = q.split()
    if f+int(F) % 2 == 1:
        s.appendleft(c)
    else:
        s.append(c)
        
if f:
    print(''.join(reversed(s)))
else:
    print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

for _ in range(q):
  Q = [x for x in input().split()]
  if Q[0] == '1':
    flg += 1
  else:
    if (Q[1] == '1' and flg%2==0) or (Q[1] == '2' and flg%2==1):
      s.appendleft(Q[2])
    else:
      s.append(Q[2])
if flg%2==1:
  s.reverse()

print('' . join(s))

Submission status: Accepted

Solution code:
# 入力文字列
get_str = list(input())

# 操作回数
N = int(input())

# リバースフラグ
r_flag = 0

left_str = []
right_str = []
# 操作
for i in range(N):
    # 取得操作
    inp_cmd = list(input().split())
    # inp_cmd[0] = 1 ならリバースフラグ更新
    if(inp_cmd[0] == '1'):
        r_flag = (r_flag+1) % 2
    else:
        # inp_cmd[1]=1、かつリバースフラグなし
        # inp_cmd[1]=2、かつリバースフラグあり
        # 先頭に文字追加
        if((inp_cmd[1] == '1' and r_flag == 0) or (inp_cmd[1] == '2' and r_flag == 1)):
            left_str.append(inp_cmd[2])
        # 上記以外は後ろに追加
        else:
            right_str.append(inp_cmd[2])

# 左文字列リストを反転
left_str = left_str[::-1]

# 文字列結合
list_str = []
list_str = left_str + get_str + right_str

# リバースフラグあり
if(r_flag == 1):
    list_str = list_str[::-1]

out_str = ''.join(list_str)
print(out_str)

Submission status: Accepted

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

flip = 0
ad1 = []
ad2 = []
for i in range(Q):
  st = input()
  if(st[0] == '1'):
    flip += 1
  else:
    if((st[2] == '1' and flip % 2 == 0) or (st[2] == '2' and flip % 2 == 1)):
      #nS = st[4] + S
      ad1.append(st[4])
    else:
      #nS = S + st[4]
      ad2.append(st[4])

ft = ''.join(ad1)
ft = ft[::-1]
ed = ''.join(ad2)

S = ft + S + ed

if(flip % 2 != 0):
  S = S[::-1]
print(S)

Submission status: Accepted

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

flag = False
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag = True if flag is False else False
    else:
        if flag is False:
            if q[1] == '1':
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == '1':
                S.append(q[2])
            else:
                S.appendleft(q[2])

if flag is True:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
ans = deque(s)
q = int(input())
check = 1
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        check *= -1
    else:
        if a[1] == "1":
            if check == 1:
                ans.appendleft(a[2])
            else:
                ans.append(a[2])
        else:
            if check == 1:
                ans.append(a[2])
            else:
                ans.appendleft(a[2])
    
ans = list(ans)
if check==1:
    print(*ans,sep="")
else:
    ans = ans[::-1]
    print(*ans,sep="")

Submission status: Accepted

Solution code:
from collections import deque

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

conr = 0

for i in range(Q):
  q = list(input().split())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = int(q[1])
    if conr % 2 == 0:
      if f == 1:
        D.appendleft(s)
      else:
        D.append(s)
    else:
      if f == 1:
        D.append(s)
      else:
        D.appendleft(s)

D = list(D)
if conr % 2 == 1:
  D = D[::-1]
ans = "".join(D)
print(ans)

Submission status: Accepted

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

S = input().strip()
Q = int(input().strip())
Q_list = [input().strip() for _ in range(Q)]
#-----

# order = 1:  左→右
# order =-1:  左←右
order=1

dq = deque()

for word in S:
    dq.append(word)

for line in Q_list:
    if line == "1":
        order *= -1
    else:
        t,f,c = line.split()
        
        if order == 1 and f == "1":
            dq.appendleft(c)
        elif order == 1 and f == "2":
            dq.append(c)
        elif order == -1 and f == "1":
            dq.append(c)
        elif order == -1 and f == "2":
            dq.appendleft(c)

if order == 1:
    print( *list(dq), sep="" )
else:
    print( *list(reversed(dq)), sep="" )

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
r='1'
for _ in range(Q):
    target = input()
    if target == '1':
        if r=='1':
            r='2'
        else:
            r='1'
    else:
        a,b,c=target.split()
        if b=='1' and r=='1':
            S.appendleft(c)
        elif b=='2' and r=='1':
            S.append(c)
        elif b=='1' and r=='2':
            S.append(c)
        else:
            S.appendleft(c)
if r=='2':
    S.reverse()
print(''.join(list(S)))

Submission status: Accepted

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

t = 0
l = ''
r = ''
for i in range(q):
    query = input()
    if query == '1':
        if t == 1:
            t = 0
        else:
            t = 1
    else:
        if query[2] == '1':
            if t == 0:
                l = query[4:]+l
            else:
                r = r+query[4]
        else:
            if t == 0:
                r = r+query[4]
            else:
                l = query[4] + l
s = l+s+r
if t == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
from collections import deque

S = deque([x for x in input()])
Q = int(input())
switch = 0
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        T = int(inp)
        if switch:
            switch = 0
        else:
            switch = 1
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            if switch:
                S.append(C)
            else:
                S.appendleft(C)
        else:
            if switch:
                S.appendleft(C)
            else:
                S.append(C)
if switch:
    print("".join(list(S)[::-1]))
else:
    print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque

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

flg = "order"
for i in range(q):
    t, *f = map(str, input().split())
    if t == "1":
        if flg == "order":
            flg = "reverse"
        else:
            flg = "order"
    else:
        if f[0] == "1":
            if flg == "order":
                s.appendleft(f[1])
            else:
                s.append(f[1])
        else:
            if flg == "order":
                s.append(f[1])
            else:
                s.appendleft(f[1])

if flg == "order":
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
l=""
r=""
flg=True

for i in range(q):
    p=list(map(str,input().split()))
    if p[0]=="1":
        flg=not flg
    else:
        z,f,c=p
        if f=="1":
            if flg:
                l=c+l
            else:
                r=r+c
        else:
            if not flg:
                l=c+l
            else:
                r=r+c
s=l+s+r

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

Submission status: Accepted

Solution code:
from collections import deque

s = input()
a = deque()
for i in s:
  a.append(i)
q = int(input())
rev = 0     # 反転をフラグで持って、実際には反転させない
for i in range(q):
  s = input()
  if s[0] == '1':
    rev ^= 1
  else:
    b = (int(s[2]) - 1) ^ rev
    if b:
      a.append(s[4])
    else:
      a.appendleft(s[4])
if rev:
  a.reverse()
for i in a:
  print(i, end="")

Submission status: Accepted

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                left.append(a[4])
            else:
                right.append(a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                right.append(a[4])
            else:
                left.append(a[4])

right_result="".join(right)
left_result="".join(left)

if flag==0:
    print(str(left_result[::-1]+s+right_result))
else:
    print(str(right_result[::-1]+s[::-1]+left_result))

Submission status: Accepted

Solution code:
I = input
s = I()
q = int(I())
a = [], []
i = 0
for _ in range(q):
  x = I()
  if x[0] == '1':
    i ^= 1
  else:
    f = int(x[2]) - 1
    a[i ^ f].append(x[4])
print(''.join(reversed(a[i])) + (s[::-1] if i else s) + ''.join(a[i^1]))

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
mae = []
ato = []
is_reverse = False
for q in query:
    if q[0] == "1":
        is_reverse = not is_reverse
    else:
        if is_reverse:
            if q[1] == "2":
                mae.append(q[2])
            else:
                ato.append(q[2])
        else:
            if q[1] == "1":
                mae.append(q[2])
            else:
                ato.append(q[2])
mae.reverse()
mae.extend(S)
mae.extend(ato)
if is_reverse:
    mae.reverse()
    print("".join(mae))
else:
    print("".join(mae))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
s = input()
n = int(input())
mae_list = []
usiro_list = []
flag = 1
for i in range(0, n):
    q_list = list(map(str, input().split()))
    if len(q_list) == 1:
        flag *= -1
    else:
        if (q_list[1] == "1" and flag == 1) or (q_list[1] == "2" and flag == -1):
            mae_list.append(q_list[2])
        else:
            usiro_list.append(q_list[2])
if flag == 1:
    if len(mae_list) > 0:
        mae_list.reverse()
        print(''.join(mae_list), end="")
    print(''.join(s), end="")
    if len(usiro_list) > 0:
        print(''.join(usiro_list), end="")
else:
    if len(usiro_list) > 0:
        usiro_list.reverse()
        print(''.join(usiro_list), end="")
    for i in range(0, len(s)):
        print(s[len(s)-i-1], end="")
    if len(mae_list) > 0:
        print(''.join(mae_list), end="")
print()

Submission status: Accepted

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

    s = input()
    s = s[0:len(s)-1]
    Q = int(input())
    head = []
    tail = []

    flg = 0 # 0:sonomama 1:gyakuten
    for _ in range(Q):
        q = input().split()
        if q[0] == "1":
            if flg == 0:
                flg = 1
            else:
                flg = 0
        elif q[0]=="2":
            index,tmp = q[1],q[2]
            if (index == "1" and flg ==0) or (index == "2" and flg ==1):
                head.append(tmp)
            elif (index == "2" and flg == 0) or (index == "1" and flg == 1):
                tail.append(tmp)

    s1 = "".join(head)
    s1 = s1[::-1]
    s2 = "".join(tail)
    ans = s1 + s + s2
    if flg == 1:
        ans = ans[::-1]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
qry = [input().split() for _ in range(Q)]
A = ''
B = ''
oq = 0
even = True
for q in qry:
    if q[0] == '1':
        even = not even
        if oq == '1':
            continue
    else:
        if even:
            if q[1] == '1':
                A = q[2] + A
            else:
                B += q[2]
        else:
            if q[1] == '1':
                B += q[2]
            else:
                A = q[2] + A
    oq = q

if even:
    print(A+S+B)
else:
    print(B[::-1]+S[::-1]+A[::-1])

Submission status: Accepted

Solution code:
def main(s,end,start,perse):
    start.reverse()
    start.append(''.join(s))
    start.append(''.join(end))
    if perse == -1:
        start.reverse()
    print(''.join(start))
def conv(s):
    a = []
    for i in range(len(s)):
        a.append(s[i])
    return a
perse = 1
start = []
end = []
  
s = input()
q = int(input())
for i in range(q):
    query = input().split()
    query[0] = int(query[0])
    if query[0] == 1:
        perse = -perse
    else:
        query[1] = int(query[1])
        if (int(query[1])*2-3)*perse == 1:
            end.append(query[2])
        else:
            start.append(query[2])
s =  conv(s)
if perse == -1:
    s.reverse()
    end.reverse()
main(s,end,start,perse)

Submission status: Accepted

Solution code:
S1=""
S2=""
S=str(input())
Q=int(input())
flag=1
for i in range(Q):
    T=list(map(str,input().split()))
    if T[0]=="1":
        flag*=-1
    else:
        if T[1]=="1":
            if flag==1:
                S1=T[2]+S1
            else: S2=S2+T[2]
        else:
            if flag==1:
                S2=S2+T[2]
            else:S1=T[2]+S1
if flag==1:
    S=S1+S+S2
else:S=S2[::-1]+S[::-1]+S1[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
query = [input().split() for _ in range(Q)]
reverse = False
for q in query:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            S.append(q[2]) if reverse else S.appendleft(q[2])
        else:
            S.appendleft(q[2]) if reverse else S.append(q[2])
if reverse:
    S.reverse()
print(''.join(S))