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 = input()

d = deque(list(s))

rev = False

for i in range(int(input())):
    tp = input().split()

    if tp[0] == '1':
        rev = not rev
    else:
        f, c = tp[1:]

        if rev:
            f = '12'[f == '1']

        if f == '1':
            d.appendleft(c)
        else:
            d.append(c)

if rev:
    print(*list(d)[::-1], sep='')
else:
    print(*list(d), sep='')

Submission status: Accepted

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

flug=0
ans=deque(list(s))

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

    if t[0]=="1":
        flug+=1
    else:
        if (flug%2==0 and t[1]=="2") or (flug%2==1 and t[1]=="1"):
            ans.append(t[2])
        else:
            ans.appendleft(t[2])

ans="".join(map(str,ans))

if flug%2==1:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def main():

    S = deque(list(input()))

    r = False
    for i in range(int(input())):
        Query = input().split()
        if Query[0] == "1":
            r = not r
        else:
            if (r==False and Query[1] == "1") or (r==True and Query[1] == "2"):
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

    if r == True:
        S.reverse()

    print("".join(list(S)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

s = collections.deque(list(input()))
q = int(input())
rev = 0 #反転するなら1そうでないなら0
for i in range(q):
    tfc = list(input().split())
    if tfc[0]=='1':
        rev = abs(rev-1) #revが入力されるたびに1か0に変更される
    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
input=lambda:sys.stdin.readline().rstrip()
from collections import deque

r=False
d=deque(list(input()))
for i in range(int(input())):
  Q=input().split()
  if Q[0]=='1':
    r=not r
  else:
    if (Q[1]=='1' and not r) or (Q[1]=='2' and r):
      d.appendleft(Q[2])
    else:
      d.append(Q[2])

if r:
  d.reverse()

print("".join(list(d)))

Submission status: Accepted

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

r=False
d=deque(list(input()))
for i in range(int(input())):
  Q=input().split()
  if Q[0]=='1':
    r=not r
  else:
    if (Q[1]=='1' and not r) or (Q[1]=='2' and r):
      d.appendleft(Q[2])
    else:
      d.append(Q[2])

if r:
  d.reverse()

print("".join(list(d)))

Submission status: Accepted

Solution code:

class Solution:

    def solve(self, S: str, Q: int, Query) -> int:

        S_list = list(S)
        R = []
        L = []

        num_reverse = 0
        for q in Query:
            if int(q[0]) == 1:
                num_reverse += 1
            else:
                if (num_reverse + int(q[1])) % 2 == 0:
                    R.append(q[2])
                else:
                    L.append(q[2])

        if num_reverse % 2 == 1:
            return ''.join(R[::-1] + S_list[::-1] + L)
        else:
            return ''.join(L[::-1] + S_list + R)


if __name__ == '__main__':

    # standard input
    S = input()
    Q = int(input())
    Query = []
    for _ in range(Q):
        Query.append(tuple(input().split()))

    # solve
    solution = Solution()
    print(solution.solve(S, Q, Query))

Submission status: Accepted

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

L = []
R = []

rev = False

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if q[0] == '1':
        rev = (not rev)

    else:
        if (rev and q[1] == '1') or (not rev and q[1] == '2'):
            R.append(q[2])
        else:
            L.append(q[2])

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

Submission status: Accepted

Solution code:
# standard input
S = input()
Q = int(input())
Query = []
num_reverse = 0

L = ''
R = ''

rev = False

for _ in range(Q):

    q = tuple(map(str, input().split()))

    if q[0] == '1':
        rev = (not rev)

    else:
        if (rev and q[1] == '1') or (not rev and q[1] == '2'):
            R = R + q[2]
        else:
            L = q[2] + L 

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

Submission status: Accepted

Solution code:
from collections import deque
S=input()
Q=int(input())
r_count=0
top=[1,2]
tail=[2,1]
S=deque(list(S))

for i in range(Q):
    q=list(input().split())
    if q[0]=="1":
        r_count+=1
    else:
        q[1]=int(q[1])
        if q[1]==1:
            q[1]=top[r_count%2]
        else:
            q[1]=tail[r_count%2]
        
        if q[1]==1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
S=list(S)
if r_count%2==1:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
fb=True
forward=""
back=""

for i in range(Q):
    query=input()
    if(query=="1"):
        fb=False if(fb) else True
    else:
        query, F, C =query.split()
        if((F=="1")==fb):
            forward=C+forward
        else:
            back=back+C
print((forward+S+back)[::1 if(fb) else -1])

Submission status: Accepted

Solution code:
import sys

s = sys.stdin.readline().replace('\n','')
q = sys.stdin.readline().replace('\n','')
q = int(q)

flag = 0
prev=""
end=""
for i in range(0,q):
	str = sys.stdin.readline().replace('\n','')
        if( str[0] == '1'):
          flag += 1
        if( str[0] == '2'):
          if( str[2] == '1'):
	    if( flag % 2 == 0):
	      prev = str[4] + prev
	    else:
	      end = end + str[4]
          if( str[2] == '2'):
	    if( flag % 2 == 0):
	      end = end + str[4]
	    else:
	      prev = str[4] + prev

ss = prev + s + end
if( flag % 2 == 1):
  ss = ss[::-1]

print ss
sys.stdout.flush()

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
 
me = 1#1:sei  -1:gyaku
for i in range(q):
    t = input().split()
    if t[0] == '1':
        me *= -1
            
    else:# 0=2
        if t[1]=='1':#1=1
            s.appendleft(t[2]) if me == 1 else s.append(t[2])
        else:#1=2
            s.append(t[2]) if me == 1 else s.appendleft(t[2])
s= list(s) 
if me != 1:
    s = s[::-1]
print(''.join(s))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = S

for _ in range(Q):
    I = input().split()
    if I[0] == "1":
        reverse = not reverse
    else:
        if (I[1] == "1") ^ reverse:
            front =front + I[2]
        else:
            end = end + I[2]


if reverse:
    print("".join(list(reversed(end)))+front)
else:
    print("".join(list(reversed(front)))+end)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
l = ''
r = ''
countReverse = 0
for operation in range(q):
    t = input().split()
    if int(t[0]) == 1:
        countReverse += 1
    if int(t[0]) == 2:
        if int(t[1]) == 1:
            if countReverse % 2 == 0:
                l = t[2] + l
            else:
                r = r + t[2]
        else:
            if countReverse % 2 == 0:
                r = r + t[2]
            else:
                l = t[2] + l
if countReverse % 2 == 0:
    s = l + s + r
else:
    s = s[::-1]
    l = l[::-1]
    r = r[::-1]
    s = r + s + l
print(s)

Submission status: Accepted

Solution code:
import sys
from collections import deque
S=[input()]
S=deque(S)
Q=int(sys.stdin.readline())
flag=0
for i in range(Q):
	qu=tuple(input().split())
	if qu[0]=='1':
		if flag==0:
			flag+=1
		else:
			flag=0
	else:
		if qu[1]=='1' and flag==0:
			S.appendleft(qu[2])
		elif qu[1]=='1' and flag==1:
			S.append(qu[2])
		elif qu[1]=='2' and flag==0:
			S.append(qu[2])
		elif qu[1]=='2' and flag==1:
			S.appendleft(qu[2])
if flag==1:
	S=''.join(S)
	S=S[::-1]
	print(S)
	exit()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input())
q=int(input())
turn=True
 
for i in range(q):
    qq=input().split()
    if qq[0]=='1':
        turn=not turn
    else:
        if turn==bool(int(qq[1])-2):
            s.appendleft(qq[2])
        else:
            s.append(qq[2])
 

text="".join(s)
print(text if turn else text[::-1])

Submission status: Accepted

Solution code:
S = str(input())
n = int(input())
Q = [list(map(str, input().split())) for _ in range(n)]
cnt = 0
s_pre = ""
s_pro = ""
for q in Q:
    if q[0] == "1":
        cnt += 1
    else:
        if (int(q[1])+cnt)%2 == 1:
#             S = q[2] + S
            s_pre = q[2] + s_pre
        else:
            s_pro = s_pro + q[2]
#             S = S + q[2]

S = s_pre + S + s_pro
if cnt%2==1:
    S = list(S)
    S.reverse()
print("".join(S))

Submission status: Accepted

Solution code:
s=[input()]
q=int(input())
turn=True
l=""
for i in range(q):
    qq=input().split()
    if qq[0]=='1':
        turn=not turn
    else:
        if turn==bool(int(qq[1])-2):
            l+=qq[2]
        else:
            s.append(qq[2])

text=l[::-1]+"".join(s)
print(text if turn else text[::-1])

Submission status: Accepted

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

f = ''
r = ''

flag = True

for i in range(q):
    li = list(input().split())
    if li[0] == '1':
        flag = not flag
    else:
        if li[1] == '1':
            if flag:
                f = li[2] + f
            else:
                r += li[2]
        else:
            if flag:
                r += li[2]
            else:
                f = li[2] + f

if flag:
    print(f + s + r)
else:
    print((f + s + r)[::-1])

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline

def main():
    s = input()
    q = int(input())
    is_rev = False
    l = ''
    r = ''
    for _ in range(0, q):
        query = input()
        if len(query) == 1:
            is_rev = not is_rev
        else:
            query = query.split(' ')
            query = [int(query[0]), int(query[1]), query[2]]
            if (query[1] == 1 and is_rev) or (query[1] == 2 and not is_rev):
#                s = s + query[2]
                r += query[2]
            else:
#                s = query[2] + s
                l += query[2]
    if is_rev:
        print(r[::-1] + s[::-1] + l)
    else:
        print(l[::-1] + s + r)

main()

Submission status: Accepted

Solution code:
s = input();

v1 = [];
v2 = [];

rev = 0;

q = int(input());

for i in range(q):
    t = [i for i in input().split()];
    if t[0] == "1":
        rev = 1 - rev;
    else:
        if rev == 0:
            if t[1] == "1":
                v2.append(t[2]);
            else:
                v1.append(t[2]);
        else:
            if t[1] == "1":
                v1.append(t[2]);
            else:
                v2.append(t[2]);

res = "";

if rev == 0:
    v2.reverse();
    for i in v2:
        res += i;
    res += s;
    for i in v1:
        res += i;
else:
    v1.reverse();
    for i in v1:
        res += i;
    res += s[::-1];
    for i in v2:
        res += i;

print(res);

Submission status: Accepted

Solution code:
s = input()
n = int(input())
top = 0
llist = ""
rlist = ""
for i in range(n):
    x = input()
    if x == "1":
        top += 1
        top = top%2
    else:
        if x.split()[1] == '1':
            if top == 0:
                llist = x.split()[2] + llist
            else:
                rlist += x.split()[2]
        else:
            if top == 1:
                llist = x.split()[2] + llist
            else:
                rlist += x.split()[2]
if top == 0:
    print(llist + s + rlist)
else:
    print(rlist[::-1] + s[::-1] + llist[::-1])

Submission status: Accepted

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

tcount = 0
rlist = ""
llist = ""

for i in range(q):
    qret = input()
    if qret[0] == "1":
        tcount += 1
        # ans = ans[::-1]
    else:
        f = qret[2]
        c = qret[4]
        if f == "1":
            if tcount % 2 == 0:
                llist = c + llist
            else:
                rlist += c
        else:
            if tcount % 2 == 0:
                rlist += c
            else:
                llist = c + llist

if tcount % 2 == 0:
    print(llist + s + rlist)
else:
    print(rlist[::-1] + s[::-1] + llist[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
Queries = [input().split() for i in range(Q)]
dq = deque(S)

f = 0

for a in Queries:
    if a[0] == '1':
        f ^= 1
    else:
        if f == (int(a[1])-1):
            dq.appendleft(a[2])
        else:
            dq.append(a[2])

if f:
    dq.reverse()

print(''.join(dq))

Submission status: Accepted

Solution code:
#パラメータの取得
S = input()
Q = int(input())
cnt = 0   #反転回数カウンタ
head = ''
tail = ''

#処理実体処理
for i in range(Q):
  tmp = str(input())
  #Q各行の値を、T, F, Cに基づいて処理
  if tmp[0] == '1':
    cnt += 1
  else:
    T, F, C = tmp.split(' ')
    if cnt % 2 == 1:
      if F == '1':
        tail = tail + C
      else:
        head = C + head
    else:
      if F == '1':
        head = C + head
      else:
        tail = tail + C
    #cnt = 0
  #print('head=',head)
  #print('tail=',tail)

#print('cnt=',cnt)
if cnt % 2 == 0:
  ans = head + S + tail
else:
  ans = tail[::-1] + S[::-1] + head[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
a = 0
mem = []
for i in query:
    if i[0] == '1':
        if a == 0:
            a = 1
        else:
            a = 0
    elif i[0] == '2':
        if i[1] == '1':
            if a == 1:
                S.append(i[2])
            else:
                #S.insert(0, i[2])
                mem.append(i[2])
        elif i[1] == '2':
            if a == 1:
                #S.insert(0, i[2])
                mem.append(i[2])
            else:
                S.append(i[2])
mem.reverse()
S[0:0] = mem
if a == 1:
    S.reverse()
# print(S)
print(''.join(S))

Submission status: Accepted

Solution code:
s=list(input())
from collections import deque
l=deque(s)
q=int(input())
m=1
for _ in range(q):
    x=list(input().split())
    if x[0]=="1":
        m*=-1
    else:
        f,c=x[1],x[2]
        f=int(f)
        if f==1:
            if m==1:
                l.appendleft(c)
            else:
                l.append(c)
        else:
            if m==1:
                l.append(c)
            else:
                l.appendleft(c)
if m==1:
    print("".join(l))
else:
    l.reverse()
    print("".join(l))

Submission status: Accepted

Solution code:
s = input()
t = ""
Q = int(input())

flag = 0
for _ in range(Q):
  A = list(map(str, input().split()))
  if A[0] == "1":
    flag += 1
  else:
    if (int(A[1]) + flag) %2:
      t += A[2]
    else:
      s += A[2]
ans = t[::-1]+s

if flag%2:
  print(ans[::-1])
else:
  print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

t = deque(s)
flg = False
for _ in range(q):
    query = input()
    if query[0] == "1":
        flg = not flg
    elif query[0] == "2":
        _, f, c = query.split()
        if f == "1":
            if flg:
                t.append(c)
            else:
                t.appendleft(c)
        elif f == "2":
            if flg:
                t.appendleft(c)
            else:
                t.append(c)
result = "".join(t)
if flg:
    result = result[::-1]
print(result)

Submission status: Accepted

Solution code:
from collections import deque

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

deq = deque(s)
rev = False

for _ in range(q):
    query = input().split()
    t = query[0]
    if t == "1":
        rev = not rev
        continue
    f = query[1]
    c = query[2]
    if not rev and f == "2" or rev and f == "1":
        deq.append(c)
    else:
        deq.appendleft(c)
if rev:
    deq = reversed(deq)
print("".join(deq))

Submission status: Accepted

Solution code:
from collections import deque

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

flag = 0
for _ in range(Q):
    q = input()
    if q == "1":
        flag += 1
    else:
        _, t, c = q.split()
        if flag % 2 == 0:
            if t == "1":
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if t == "2":
                d.appendleft(c)
            else:
                d.append(c)

if flag % 2 == 1:
    d.reverse()
print("".join(list(d)))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
query = [input().split() for _ in range(Q)]

ans = deque(list(S))
flag = 0

for q in range(Q):
    if len(query[q]) == 1:
        flag = 1 - flag
    else:
        if query[q][1] == '1':
            if flag:
                ans.append(query[q][2])
            else:
                ans.appendleft(query[q][2])
        else:
            if not flag:
                ans.append(query[q][2])
            else:
                ans.appendleft(query[q][2])

if not flag:
    print(''.join(ans))
else:
    print(''.join(ans)[::-1])

Submission status: Accepted

Solution code:
Sr = list(input())
Sl = []
Q = int(input())


for i in range(Q):
    s = list(input().split())
    if len(s) == 1:
        Sr,Sl = Sl,Sr
    elif s[1] == '1':
        Sl.append(s[2])
    else:
        Sr.append(s[2])
Sl.reverse()
print('%s' % (''.join(Sl) + ''.join(Sr)))

Submission status: Accepted

Solution code:
from collections import deque

d = deque(input())

q = int(input())

headflag = 0

for i in range(q):
    s = input()
    if len(s) == 1:
        if headflag == 0:
            headflag = 1
        else:
            headflag = 0
    else:
        l = list(s)
        if headflag == 0:
            if l[2] == '1':
                d.appendleft(l[4])
            else:
                d.append(l[4])
        else:
            if l[2] == '2':
                d.appendleft(l[4])
            else:
                d.append(l[4])

result = ''.join(d)

if headflag == 0:
    print(result)
else:
    result = result[::-1]
    print(result)

print()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S =input()

input_line = int(input())

flag0 = ""
flag1 = ""
flag = 0


for i in range(input_line):
    action = list(input())
    # print(flag0,flag1)
    # print(action)
    if action[0]=="1":
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        # print("check")
        if action[2] == "2":
            
            if flag == 0:
                flag1 = flag1 + action[4]
            elif flag ==1:
                flag0 = action[4] + flag0
        else:
            if flag == 0:
                flag0 = action[4] + flag0
            
            elif flag == 1:
                flag1 += action[4]
                
S = flag0 + S + flag1
if flag == 1:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
head=[]
tail=[]
rev=False
for _ in range(q):
  tmp=list(input().split())
  if tmp[0]=='1':
    if rev==False:
      rev=True
    else:
      rev=False
  if tmp[0]=='2':
    if rev==False:
      if tmp[1]=='1':
        head.append(tmp[2])
      if tmp[1]=='2':
        tail.append(tmp[2])
    if rev==True:
      if tmp[1]=='1':
        tail.append(tmp[2])
      if tmp[1]=='2':
        head.append(tmp[2])
head=''.join(map(str,head))
tail=''.join(map(str,tail))
ans=head[::-1]+s+tail
if rev==True:
  ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
# TLE
s = input()
loop = int(input())
is_head = True
to_head = []
to_tail = []
for i in range(loop):
    q_li = input().split()
    t = int(q_li[0])
    if t == 1:
        is_head = not is_head
    else:
        f = int(q_li[1])
        c = q_li[2]
        if f == 1:
            # 先頭に追加
            to_head.append(c) if is_head else to_tail.append(c)
        else:
            # 末尾に追加
            to_tail.append(c) if is_head else to_head.append(c)

if is_head:
    print("".join(to_head[::-1]) + s + "".join(to_tail))
else:
    print("".join(to_tail[::-1]) + s[::-1] + "".join(to_head))

Submission status: Accepted

Solution code:
#２つのリストを使う場合
rL=list(input())#右のリストに入力
lL=[]
Q=int(input())
for i in range(Q):
  N=list(input().split())
  if N[0]=="1":#反転させる場合は右と左を入れ替える
    lL,rL=rL,lL
  elif N[1]=="1":#先頭の場合は左のリストに
    lL.append(N[2])
  else:#末尾の場合は右のリストに
    rL.append(N[2])
lL.reverse()#左のリストの向きを入れ替える
print("".join(lL)+"".join(rL))

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
F=0#フラグ
for i in range(Q):
  N=list(input().split())
  if F==0:#反転していない場合
    if N[0]=="1":#反転させる
      F=1
    elif N[1]=="1":#先頭に追加
      S.appendleft(N[2])
    else:#末尾に追加
      S.append(N[2])
  elif F==1:#反転している場合
    if N[0]=="1":#反転を戻す
      F=0
    elif N[1]=="1":#末尾に追加
      S.append(N[2])
    else:#先頭に追加
      S.appendleft(N[2])
if F==1:#反転するならsを反転させる
  S.reverse()
  print("".join(S))
else:#そうでないならそのまま
  print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
Q = int(input())
count = 0
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        f, c = int(q[1]), q[2]
        if (count%2==0 and f==1) or (count%2==1 and f==2):
            s.appendleft(c)
        else:
            s.append(c)
if count%2==1:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

c = deque(input())
n = int(input())
head = 1

for _ in range(n):
  now = list(input().split())
  if now[0] == "1":
    head *= -1
  else:
    if now[1] == "1":
      if head == 1:
        c.appendleft(now[2])
      else:
        c.append(now[2])
    else:
      if head == 1:
        c.append(now[2])
      else:
        c.appendleft(now[2])
print("".join(c) if head == 1 else "".join([c.pop() for _ in range(len(c))]))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
queryNum = input()
flip = False
for i in range(int(queryNum)):
	order = input()
	if order == '1':
		if flip:
			flip = False
		else:
			flip = True
	else:
		if order[2] == '1':
			if not flip:
				S.appendleft(order[4])
			elif flip:
				S.append(order[4])
		else:
			if not flip:
				S.append(order[4])
			elif flip:
				S.appendleft(order[4])
S = ''.join(S)
if flip:
	print(S[::-1])
else: 
	print(S)

Submission status: Accepted

Solution code:
from collections import deque

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

for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        if Flag == False:
            Flag = True
        else:Flag = False
    else:
        if Query[1] == '1':
            if Flag == False:S.appendleft(Query[2])
            else:S.append(Query[2])
        else:
            if Flag == False:S.append(Query[2])
            else:S.appendleft(Query[2])
l = list(S)
if Flag:print(''.join(l[::-1]))
else:print(''.join(l))

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input())
f=False
for _ in range(int(input())):
  t=list(input().split())
  if t[0]=='1':
    if f is False:
      f = True
    else:
      f = False
  else:
    if t[1]=='1':
      if f is False:
      	s.appendleft(t[2])
      else:
        s.append(t[2])
    else:
      if f is False:
      	s.append(t[2])
      else:
        s.appendleft(t[2])
x=''.join(map(str,s))
if f is False:
	print(x)
else:
  print(x[::-1])

Submission status: Accepted

Solution code:
from collections import deque  
d = deque(input())
Q = int(input())
Query = [input() for i in range(Q)]

# 先頭から読むか，末尾から読むかを判定する
# 偶数 -> 先頭から，奇数 -> 末尾から
isReadOrientation = 0

for i in range(Q):
    if len(Query[i]) == 1:
        isReadOrientation += 1
    else:
        #先頭に追加
        if Query[i][2] == "1":
            #先頭に追加
            if isReadOrientation % 2 == 0:
                d.appendleft(Query[i][4])
            #末尾に追加
            else:
                d.append(Query[i][4])
        #末尾に追加
        else:
            #末尾に追加
            if isReadOrientation % 2 == 0:
                d.append(Query[i][4])
            #先頭に追加
            else:
                d.appendleft(Query[i][4])

#先頭から文字列に記録
if isReadOrientation % 2 != 0:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
import collections

s=input()
t=collections.deque()
t.append(s)
q=int(input())
flag=False
for _ in range(q):
  query=list(map(str,input().split()))
  if query[0]=='1':
    if flag==False:
      flag=True
    else:
      flag=False
  elif query[0]=='2':
    if query[1]=='1':
      if flag==False:
        t.appendleft(query[2])
      else:
        t.append(query[2])
    else:
      if flag==False:
        t.append(query[2])
      else:
        t.appendleft(query[2])
t=''.join(map(str,t))
if flag==False:
  print(t)
else:
  print(t[::-1])

Submission status: Accepted

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

for _ in range(q):
  query = list(input().split())
  if len(query) == 1:
    num += 1
  else:
    if int(query[1]) == 1:
      if num%2 == 0:        
        # 先頭
        ans.appendleft(query[2])
      else:
        ans.append(query[2])
    else:
      if num%2 == 0:
        # 後方
        ans.append(query[2])
      else:
        ans.appendleft(query[2])     

if num%2 == 0:
  print(''.join(ans))
else:
  ans.reverse()
  print(''.join(ans))

Submission status: Accepted

Solution code:
s = str(input())
q = int(input())
b = 1
x=''
y=''
for i in range(q):
  la = input()
  if la == "1":
    b ^= 1
  else:
    a,f,c=la.split()
    if f == "1":
      if b==1:x=c+x
      else:y=y+c
    else:
      if b==1:y=y+c
      else:x=c+x
s = x+s+y      
print(s if b==1 else s[::-1])

Submission status: Accepted

Solution code:
import collections
S = input()
Q = int(input())
q = collections.deque()
q.append(S)
inversion = True
for i in range(Q):
    Query = list(map(str, input().split()))
    if Query[0] == "1":
        inversion = not(inversion)
    elif Query[1] == "1":
        if inversion:
            q.appendleft(Query[2])
        else:
            q.append(Query[2])
    else:
        if inversion:
            q.append(Query[2])
        else:
            q.appendleft(Query[2])

if inversion:
    print("".join(q))
else:
    print("".join(q)[::-1])

Submission status: Accepted

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

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

if not flag:
    s.reverse()

print("".join(s))

Submission status: Accepted

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

for i in range(q):
    query=input().split()
    if query[0] == "1":
        flag = 3 - flag
    elif int(query[1]) - flag :
         s.appendleft(query[2])
    else:
        s.append(query[2])

if flag-2:
    s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
s = input()
Q = int(input())
count = 0
right = []
left = []
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        count += 1
    else:
        if (count%2 == 0 and int(q[1]) == 1) or (count%2 == 1 and int(q[1]) == 2):
            left.append(q[2])
        else:
            right.append(q[2])

left = "".join(left)
right = "".join(right)
if count%2 == 0:
    left = left[::-1]
    print(left+s+right)
else:
    s = s[::-1]
    right = right[::-1]
    print(right+s+left)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
q = deque(S)
fg = False
for i in range(Q):
    que = list(map(str , input().split()))
    if que[0] == "1":
        fg = not fg
    elif (que[1] == "1" and fg == False) or (que[1] == "2" and fg == True) :
        q.appendleft(que[2])
    else:
        q.append(que[2])
if fg:
    q.reverse()
print("".join(q))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
Qr = [list(input().split()) for i in range(Q)]
isForward = True
for i in range(Q):
    if Qr[i][0] == "1":
        isForward = not(isForward)
    elif (isForward and  Qr[i][1] == "1") or (not(isForward) and Qr[i][1] == "2"):
        S.appendleft(Qr[i][2])
    else:
        S.append(Qr[i][2])
S = list(S)
if not(isForward):
    S.reverse()
print("".join(S))