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:
import sys
S = sys.stdin.readline().rstrip()
L = []
R = []
rev = 0
for _ in range(int(sys.stdin.readline().rstrip())):
    A = sys.stdin.readline().rstrip().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

if rev:
    print("".join(R[::-1]) + S[::-1] + "".join(L))
else:
    print("".join(L[::-1]) + S + "".join(R))

Submission status: Accepted

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

from collections import deque
count = 0
 
S = list(S)
S = deque(S)
 
for i in range(Q):
    if Que[i][0] == '1':
        #S.reverse()
        count += 1
    elif Que[i][0] == '2':
        if Que[i][1] == '1' and count%2 == 0:
            S.appendleft(Que[i][2])
        elif Que[i][1] == '1' and count%2 == 1:
            S.append(Que[i][2])
        elif Que[i][1] == '2' and count%2 == 0:
            S.append(Que[i][2])
        elif Que[i][1] == '2' and count%2 == 1:
            S.appendleft(Que[i][2])
            
if count%2 == 0:
    print("".join(S))
else:
    S.reverse()
    print("".join(S))

Submission status: Accepted

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

cnt = 1
for i in range(q):
    t_f_c = list(input().split())
    if t_f_c[0] == "1":
        cnt *= -1
    elif t_f_c[0] == "2":
        if cnt == 1: #反転-
            if t_f_c[1] == "1":
                s.appendleft(t_f_c[2])
            elif t_f_c[1] == "2":
                s.append(t_f_c[2])
        elif cnt == -1:  #反転+
             if t_f_c[1] == "2":
                s.appendleft(t_f_c[2])
             elif t_f_c[1] == "1":
                s.append(t_f_c[2])
if cnt == 1:
    print("".join(s))
elif cnt == -1:
    s.reverse()
    print("".join(s))

Submission status: Accepted

Solution code:
s,_,*q=open(0)
a=['','']
p=1
for q in q:
  p^=q[0]<'2'
  a[(q<'2 2')^p]+=q[-2]*(q[0]>'1')
print(a[p^1][::-1]+s[:-1][::p or-1]+a[p])

Submission status: Accepted

Solution code:
S = [c for c in input()]
N = int(input())
r = 0

B = []

for i in range(N):
	values = input().split()
	if values[0] == '1':
		r ^= 1
	else:
		f = int(values[1]) - 1
		if f == r:
			B.append(values[2])
		else:
			S.append(values[2])

if r == 0:
	print(''.join(B[::-1] + S))
else:
	print(''.join(S[::-1] + B))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
pre, suf = '', ''
rev = False
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        rev ^= 1
    else:
        f, c = query[1:]
        if (f == '1' and not rev) or (f == '2' and rev): pre = c + pre
        else: suf = suf + c
print(pre + S + suf if not rev else (pre + S + suf)[::-1])

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

S = input()
L = []
R = []
rev = 0
for _ in range(int(input())):
    A = input().split()
    if A[0] == "1":
        rev ^= 1
    else:
        if (int(A[1]) ^ rev) & 1:
            L.append(A[2])
        else:
            R.append(A[2])

if rev:
    print("".join(R[::-1]) + S[::-1] + "".join(L))
else:
    print("".join(L[::-1]) + S + "".join(R))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
cnt = 2
for _ in range(Q):
    L = input().split()
    if L[0] == "1":
        cnt += 1
    elif L[0] == "2":
        if L[1] == "1" and (cnt)%2 == 0:
            S.appendleft(str(L[2]))
        elif L[1] == "1" and (cnt)%2 != 0:
            S.append(str(L[2]))
        elif L[1] == "2" and (cnt)%2 == 0:
            S.append(str(L[2]))
        elif L[1] == "2" and (cnt)%2 != 0:
            S.appendleft(str(L[2]))
S ="".join(S)
if cnt%2 != 0:
        print(S[::-1])
else:
        print(S)

Submission status: Accepted

Solution code:
import collections

s = collections.deque(list(input()))
q = int(input())
rev = 0
for i in range(q):
    tfc = list(input().split())
    if tfc[0]=='1':
        rev = abs(rev-1)
    else:
        t,f,c = tfc
        if rev:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
if rev:
    t = []
    for i in range(len(s)):
        t.append(s.pop())
    print(''.join(t))
else:
    print(''.join(s))

Submission status: Accepted

Solution code:
import sys
s = input()
q = int(input())
a = ['', '']
r = 0
for l in sys.stdin:
    if l[0] == '1':
        r ^= 1
    else:
        a[0 if l[2] == '21'[r] else 1] += l[4]
if r:
    print(a[0][::-1] + s[::-1] + a[1])
else:
    print(a[1][::-1] + s + a[0])

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
N = int(input())
rev = False
se = False
S = deque(S)
for x in range(N):
    Q = list(input().split())
    if Q[0] == "1":
        if rev:
            rev = False
        elif se:
            rev = True
    else:
        se =True
        if Q[1] == "1":
            if rev:
                S.append(Q[2])
            else:
                S.appendleft(Q[2])
        else:
            if rev:
                S.appendleft(Q[2])
            else:
                S.append(Q[2])

S = list(S)
if rev:
    S = reversed(S)
    
ans = "".join(S)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

query_list = []
for _ in range(q):
    query_list += [input()]

deq = deque(s)
true_position = True

for i in range(q):
    if len(query_list[i])==1:
        true_position = not true_position
        # print(s)
    else:
        if query_list[i][2]=="1":
            if true_position:
                deq.appendleft(query_list[i][4])
            else:
                deq.append(query_list[i][4])
        elif query_list[i][2]=="2":
            if true_position:
                deq.append(query_list[i][4])
            else:
                deq.appendleft(query_list[i][4])

if true_position:
    ans = "".join(deq)
else:
    deq.reverse()
    ans = "".join(deq)

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    d = input()
    n = int(input())
    qs = [input() for e in range(n)]
    s = []
    e = []
    r = False
    for q in qs:
        if q == '1':
            r = not(r)
        elif q.startswith("2 1"):
            e.append(q[-1]) if r else s.append(q[-1])
        else:
            s.append(q[-1]) if r else e.append(q[-1])
    print("".join(e[::-1]) + d[::-1] + "".join(s) if r else "".join(s[::-1]) + d + "".join(e))

Submission status: Accepted

Solution code:
import sys
from collections import deque

S=raw_input()
Q=input()
Query=[ sys.stdin.readline().split() for _ in range(Q) ]

s1=deque()
s2=deque()
judge={}
judge["front"]="s1"
judge["end"]="s2"

cnt=0
for x in Query:
    if x[0]=="1":
        n=x[0]
        cnt+=1
    elif x[0]=="2":
        n,f,c=x[0],x[1],x[2]
    if n=="1":
        judge["front"],judge["end"]=judge["end"],judge["front"]
    elif n=="2":
        if f=="1":
            if judge["front"]=="s1":
                s1.appendleft(c)
            elif judge["front"]=="s2":
                s2.append(c)
        elif f=="2":
            if judge["end"]=="s1":
                s1.appendleft(c)
            if judge["end"]=="s2":
                s2.append(c)

str1=""
for x in s1:
    str1+=x
str2=""
for x in s2:
    str2+=x

if judge["front"]=="s1" and judge["end"]=="s2":
    print str1+S+str2
elif judge["front"]=="s2" and judge["end"]=="s1":
    print str2[::-1]+S[::-1]+str1[::-1]

Submission status: Accepted

Solution code:
import sys
s = input()
q = int(input())
a = ['', '']
r = 0
for l in sys.stdin:
    if l[0] == '1':
        r += 1
    else:
        a[0 if l[2] == '21'[r % 2] else 1] += l[4]
if r % 2 == 0:
    print(''.join(reversed(a[1])) + s + ''.join(a[0]))
else:
    print(''.join(reversed(a[0])) + ''.join(reversed(s)) + ''.join(a[1]))

Submission status: Accepted

Solution code:
from collections import*
s,_,*q=open(0)
d=deque(s[:-1])
p=1
for q in q:
  try:
    q,f,c=q.split()
    if(f<'2')^p:d+=c,
    else:d.appendleft(c)
  except:p^=1
print(''.join(d)[::p or-1])

Submission status: Accepted

Solution code:
def d():
    from collections import deque
    S = deque(str(input()))
    Q = int(input())
    rvs = False

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

        if q[0] == '1':
            rvs = not rvs
        elif q[0] == '2':
            if q[1] == '1':
                if rvs:
                    S.append(q[2])
                else:
                    S.appendleft(q[2])
            elif q[1] == '2':
                if rvs:
                    S.appendleft(q[2])
                else:
                    S.append(q[2])
            else:
                raise ValueError(
                    'F({}) is {}, which is invalid!'.format(i, q[1]))
        else:
            raise ValueError('T({}) is {}, which is invalid!'.format(i, q[0]))

    if rvs:
        ans = list(S)[::-1]
    else:
        ans = list(S)
    print(''.join(ans))


##########
if __name__ == "__main__":
    d()

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
turn=0
ans=deque(s)
for i in range(q):
    query=input().split()
    if query[0]=="1":
        turn+=1
    if query[0]=="2":
        if query[1]=="1":
            if turn%2==0:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        elif query[1]=="2":
            if turn%2==0:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
ans="".join(ans)
if turn%2==1:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
#D
from collections import deque
s=input()
q=int(input())
l=[]
turn=0
ans=deque()
ans.append(s)
for i in range(q):
    query=input().split()
    if query[0]=="1":
        turn+=1
    if query[0]=="2":
        if query[1]=="1":#先頭
            if turn%2==0:#正規
                ans.appendleft(query[2])
            else:#逆
                ans.append(query[2])
        elif query[1]=="2":#末尾
            if turn%2==0:#正規
                ans.append(query[2])
            else:#逆
                ans.appendleft(query[2])
ans="".join(ans)
if turn%2==1:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = int(input())
l = [list(input().split()) for i in range(n)]
start = []
end = []
d = True
for i in range(n):
    if l[i][0] == "1":
        d = not (d)
    else:
        if l[i][1] == "1":
            if d:
                start.append(l[i][2])
            else:
                end.append(l[i][2])
        else:
            if d:
                end.append(l[i][2])
            else:
                start.append(l[i][2])
if d:
    print("".join(start[::-1]) + s + "".join(end))
else:
    print("".join(end[::-1]) +
          ''.join(list(reversed(s))) + "".join(start))

Submission status: Accepted

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

hanten = 0
saki = ""
ato = ""

for i in range(Q):
    q = input().split()
    
    if q[0] == "1":
        hanten += 1
    else:
        if hanten % 2 == 0:
            if q[1] == "1":
                saki += q[2]
            else:
                ato += q[2]
        else:
            if q[1] == "1":
                ato += q[2]
            else:
                saki += q[2]

if hanten % 2 == 0:
    for i in range(len(saki)):
        print(saki[-i-1], end="")
    print(S, end="")
    print(ato)
else:
    for i in range(len(ato)):
        print(ato[-i-1], end="")
    for i in range(len(S)):
        print(S[-i-1], end="")
    print(saki)

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
ans_str=deque(s)
cnt=0
for _ in range(q):
    Q=list(input().split())
    if Q[0]=='1':
        cnt=1 if cnt==0 else 0

    else:
        if Q[1]=='1':
            if cnt==0 or len(ans_str)==1:
                ans_str.appendleft(Q[-1])
            else:
                ans_str.append(Q[-1])

        else:
            if cnt==0 or len(ans_str)==1:
                ans_str.append(Q[-1])
            else:
                ans_str.appendleft(Q[-1])
    #print(ans_str,cnt,Q)

ans=list(ans_str) if cnt==0 else list(ans_str)[::-1]
print(*ans,sep='')

Submission status: Accepted

Solution code:
from collections import deque

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

sArry = deque(S)
rFlag = False
for i in range(Q):
    if Query[i][0] == '1':
        rFlag = (rFlag == False)
        
    else:
        if rFlag:
            if Query[i][1] == '1':
                sArry.append(Query[i][2])
            else:
                sArry.appendleft(Query[i][2])
        else:
            if Query[i][1] == '1':
                sArry.appendleft(Query[i][2])
            else:
                sArry.append(Query[i][2])
            
#    print("  ", i, rFlag, "".join(sArry), Query[i])



if rFlag:
    print("".join(reversed(sArry)))
else:
    print("".join(sArry))

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 = input() # contains spaces
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[2] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str += query[4]
        else:
            tail_str += query[4]

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

Submission status: Accepted

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

cnt_reverse = False
left = ''
for i in range(q):
  query = input()
  
  if query[0] == '1':
    cnt_reverse = not cnt_reverse
  elif (query[2] == '1' and not cnt_reverse) or (query[2] == '2' and cnt_reverse):
    left = left + query[4]

  else:
    s  = s + query[4]
s = left[::-1] + s
if cnt_reverse:
  s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(input())
cnt=0
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        cnt+=1
    if len(que)!=1 and cnt%2==0:
        if int(que[1])==1:
            s.appendleft(que[2])
        elif int(que[1])==2:
            s.append(que[2])
    if len(que)!=1 and cnt%2!=0:
        if int(que[1])==1:
            s.append(que[2])
        elif int(que[1])==2:
            s.appendleft(que[2])
if cnt%2!=0:
    s.reverse()
print(''.join(s))

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 = str(input()) # contains spaces
    if query[0] == "1":
        rev_flag *= -1
    else:
        h_or_t_flag = 1 if query[2] == "1" else -1
        h_or_t_flag *= rev_flag
        
        if h_or_t_flag == 1:
            head_str += query[4]
        else:
            tail_str += query[4]

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

Submission status: Accepted

Solution code:
from collections import deque

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

ans = deque()
ans.append(S)
reverse = False

for i in range(Q):
    x = list(input().split())
    if int(x[0]) == 1:
        reverse = not reverse
        continue
    if int(x[1]) == 1 and not reverse:
        ans.appendleft(x[2])
    elif int(x[1]) == 2 and reverse:
        ans.appendleft(x[2])
    else:
        ans.append(x[2])

ans = ''.join(ans)

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
query_list = [input().split() for _ in range(q)]

front = []
back = []
is_reverse = False

for query in query_list:
    if query[0] == '1':
        is_reverse = not is_reverse
    elif query[0] == '2':
        key = int(query[1])
        value = query[2]
        if (key == 1 and not is_reverse) or (key == 2 and is_reverse):
            front.append(value)
        else:
            back.append(value)

ans = ''
if is_reverse:
    ans = back[::-1]+list(s)[::-1]+front
else:
    ans = front[::-1]+list(s)+back
print(''.join(ans))

Submission status: Accepted

Solution code:
from collections import deque
 
S = deque(input())
Q = int(input())
 
Q_list = [list(map(str, input().split())) for i in range(Q)]
 
rev = False
 
for i in range(Q):
  if int(Q_list[i][0]) == 1:
    rev = not rev
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if rev ^ (int(F) == 1): # 排他的論理和片方真片方ぎの時真になる
      S.appendleft(C) # 反転している場合は、末尾、先頭追加を逆にする
    else:
      S.append(C)
      
if rev: # 最終的に反転している場合は反転させる
  S.reverse()
  
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

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

t = 0
for i in range(q):
    o = list(map(str, input().split()))
    if o[0] == "1":
        t += 1
    else:
        if o[1] == "1" and t%2 == 0 or o[1] == "2" and t%2 == 1:
            s.appendleft(o[2])
        else:
            s.append(o[2])

if t%2 == 1:
    s.reverse()

print("".join(s))

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]
        else:
            tail_str += query[2]

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

Submission status: Accepted

Solution code:
from collections import deque
 
S = deque(input())
Q = int(input())
 
Q_list = [list(map(str, input().split())) for i in range(Q)]
 
rev = False
 
for i in range(Q):
  if int(Q_list[i][0]) == 1:
    rev = not rev
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if rev ^ (int(F) == 1): # 排他的論理和片方真片方ぎの時真になる
      S.appendleft(C)
    else:
      S.append(C)
      
if rev:
  S.reverse()
  
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
query_num = int(input())
query_li = list()
for _ in range(query_num):
    query_li.append(input().split())

rev_flag = False
for query in query_li:
    if query[0] == '1':
        rev_flag = not rev_flag
    elif (query[1] == '1' and rev_flag) or (query[1] == '2' and not rev_flag):
        s.append(query[2])
    else:
        s.appendleft(query[2])

if rev_flag:
    print(''.join(deque(reversed(s))))
else:
    print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
q = int(input())
ans = deque(s)
cnt = 0
for i in range(q):
    f = [i for i in input().split()]
    if f[0] == '1':
        cnt += 1
    else:
        if f[1] == '1':
            if cnt % 2 == 0:
                ans.appendleft(f[2])
            else:
                ans.append(f[2])
        else:
            if cnt % 2 == 0:
                ans.append(f[2])
            else:
                ans.appendleft(f[2])
if cnt % 2 == 0:
    print(''.join(map(str,ans)))
else:
    ans2 = []
    for i in (reversed(ans)):
        ans2.append(i)
    print(''.join(map(str,ans2)))

Submission status: Accepted

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

ans = deque()
ans.append(s)
reverse = False
for i in range(q):
    x = list(input().split())
    if int(x[0]) == 1:
        reverse = not reverse
        continue
    if int(x[1]) == 1 and not reverse:
        ans.appendleft(x[2])
    elif int(x[1]) == 2 and reverse:
        ans.appendleft(x[2])
    else:
        ans.append(x[2])

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

Submission status: Accepted

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

topFlg = True

leftStr = ""
rightStr = ""

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            if topFlg:
                leftStr = query[2] + leftStr
            else:
                rightStr += query[2]
        else:
            if topFlg:
                rightStr += query[2]
            else:
                leftStr = query[2] + leftStr

print(leftStr + S + rightStr if topFlg else (leftStr + S + rightStr)[::-1])

Submission status: Accepted

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

topFlg = True

leftStr = ""
rightStr = ""

for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == "1":
        topFlg = not (topFlg)
    else:
        if query[1] == "1":
            if topFlg:
                leftStr = query[2] + leftStr
            else:
                rightStr += query[2]
        else:
            if topFlg:
                rightStr += query[2]
            else:
                leftStr = query[2] + leftStr

print(leftStr + S +
      rightStr if topFlg else rightStr[::-1] + S[::-1] + leftStr[::-1])

Submission status: Accepted

Solution code:
import sys
s = input()
flag = 0
s1 = ""
s2 = ""

for i in range(int(input())):
	X = sys.stdin.readline().split()
	if int(X[0]) == 1:
		flag = flag^1	
	elif (int(X[1])-1)^flag == 0:
		s1 += X[2]
	else:
		s2 += X[2]
s3 = s1[::-1]+s+s2
if flag:
	print(s3[::-1])
else:
	print(s3)

Submission status: Accepted

Solution code:
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip()
d = deque(input())

reverse = -1
n = int(input())
for i in range(n):
    query = input().split()
    if len(query) == 1:
        reverse *= (-1)
    else:
        f = int(query[1])
        c = query[2]
        if reverse == -1:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if reverse == 1:
    d.reverse()
# print(d)
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
d = deque(s)
forward = True
q = int(input())
for i in range(q):
    l = input().split()
    if len(l) == 1:
        forward = not forward
    else:
        pos = int(l[1])
        c = l[2]
        if pos == 1:
            if forward:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if forward:
                d.append(c)
            else:
                d.appendleft(c)

if forward == False:
    d.reverse()

print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque


L = list(input().strip())
que = deque(L)
Q = int(input())
flip = False
for i in range(Q):
    q = list(map(str, input().split()))
    if len(q) == 1:
        flip = not flip
    else:
        t, f, c = q
        if flip is False and f == '1':
            que.appendleft(c)
        if flip is False and f == '2':
            que.append(c)
        if flip is True and f == '1':
            que.append(c)
        if flip is True and f == '2':
            que.appendleft(c)

if flip:
    que.reverse()

print(''.join(que))

Submission status: Accepted

Solution code:
from collections import deque

S = input().rstrip('\r\n')
S_list = deque(S)
is_reverse = False

Q = int(input())
for _ in range(Q):
    query = input().rstrip('\r\n')
    t, f, c = '', '', ''
    if len(query) == 1:
        t = query
    else:
        t, f, c = query.split(' ')
    if t == '1':
        is_reverse = not is_reverse
    else:
        if f == '1':
            if is_reverse:
                S_list.append(c)
            else:
                S_list.appendleft(c)
        else:
            if is_reverse:
                S_list.appendleft(c)
            else:
                S_list.append(c)
if is_reverse:
    S_list.reverse()
print(''.join(S_list))

Submission status: Accepted

Solution code:
from collections import deque

def resolve():
    s = input()
    s = deque(s)
    q = int(input())
    flag = False
    for i in range(q):
        list = input().split()
        t = list[0]
        if t == "1":
            if len(s) > 1:
                flag = not flag
        elif (list[1] == "1") ^ flag:
            s.appendleft(list[2])
        else:
            s.append(list[2])
    s = ''.join(s)
    if flag:
        s = ''.join(reversed(s))
    print(s)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from collections import deque
Q=deque(list(input()))
rev=0

for _ in range(int(input())):
  query=input()
  if query[0]=='1':
    rev^=1
  else:
    q,f,c=query.split()
    if (f=='1')^(rev):
      Q.appendleft(c)
    else:
      Q.append(c)

if rev:
  Q=reversed(Q)
print(''.join(Q))

Submission status: Accepted

Solution code:

from collections import deque
s = input()
q = int(input())
s = deque(s)
ans = []
ans_s = ''
one_count = 0
key = 0
for i in range(q):
    ans.append(list(input().split()))
for i in ans:
    if int(i[0])==1:
        one_count += 1
    else:
        if one_count%2==0:
            if int(i[1]) == 1:
                s.appendleft(i[2])
            else:
                s.append(i[2])
        else:
            if int(i[1]) == 1:
                s.append(i[2])
            else:
                s.appendleft(i[2])

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

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
sd=deque()
for i in range(len(s)):
    sd.append(s[i])
count=0
for i in range(q):
    que=list(map(str,input().split()))
    if int(que[0])==1:
        count+=1
    else:
        if int(que[1])==1:
            if count%2==0:
                sd.appendleft(str(que[2]))
            else:
                sd.append(str(que[2]))
        else:
            if count%2==0:
                sd.append(str(que[2]))
            else:
                sd.appendleft(str(que[2]))
if count%2!=0:
    sd.reverse()

ans=''.join(sd)
print(ans)

Submission status: Accepted

Solution code:
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    ret = sys.stdin.readline().rstrip()
    Q = int(sys.stdin.readline().rstrip())
    m = ""
    u = ""
    r = 0
    for q in range(Q):
        line = sys.stdin.readline().split()
        if line[0] == '1':
            m, u = u, m
            r ^= 1
        elif line[0] == '2' and line[1] == '1':
            m += line[2]
        else:
            u += line[2]

    if r == 0:
        print(m[::-1] + ret[::1] + u)
    else:
        print(m[::-1] + ret[::-1] + u)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

fl = 0
for q in Query:
    if q[0]=='1':
        fl += 1
    else:#q[0]=='2'
        # 前に追加
        if (q[1]=='1'):
            # 反転あり
            if fl&1:
                S.append(q[2])
            # 反転なし
            else:
                S.appendleft(q[2])
        # 後に追加
        else:#q[1]=='2'
            # 反転あり
            if fl&1:
                S.appendleft(q[2])
            # 反転なし
            else:
                S.append(q[2])

T = ''.join(S)
if fl&1:
    T = T[::-1]
print(T)

Submission status: Accepted

Solution code:
import sys
from collections import deque

S = input()
Q = int(input())
QUERY = sys.stdin.readlines()

state = 0
q = deque()
for s in S:
    q.append(s)

for i in range(Q):
    query = QUERY[i]
    if query[0] == '1':
        state ^= 1
    else:
        _, F, C = query.split()
        F = int(F) - 1
        if state ^ F:
            q.append(C)
        else:
            q.appendleft(C)

ans = []
if state == 0:
    while q:
        ans.append(q.popleft())
else:
    while q:
        ans.append(q.pop())

print(''.join(ans))

Submission status: Accepted

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

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

    flag = False
    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1':
            flag = not flag
        else:
            if (f == '1') ^ flag:
                S.appendleft(c)
            else:
                S.append(c)
    if flag:
        S.reverse()
    print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

    flag = False
    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1':
            flag = not flag
        else:
            if (f == '1') ^ flag:
                S.appendleft(c)
            else:
                S.append(c)
    prS(S, flag)
    # prS(S, flag)


def prS(S, flag):
    if flag:
        S.reverse()
    print("".join(S))
    if flag:
        S.reverse()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    ans = deque(list(S))
    ordered = True  # ordered -> False のときは「先頭」と「末尾」が替わっている
    for q in Query:
        if q[0] == '1':
            ordered = not ordered
            continue

        _, f, c = q
        if (f == '1' and ordered) or (f == '2' and not ordered):
            operation = ans.appendleft
        else:
            operation = ans.append
        operation(c)
    return ''.join(ans) if ordered else ''.join(reversed(ans))

print(d_string_formation())