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 = list(input())
ans = deque(s)
Q = int(input())
is_reverse = False
for _ in range(Q):
    q = list(map(str,input().split()))
    if q[0] == '1':
        is_reverse = True ^ is_reverse # 反転
        #ans = ans[::-1]
    else:
        if is_reverse: # 反転してた場合
            if q[1] == '1':
                #ans = ans + q[2]
                ans.append(q[2])
            else:
                #ans = q[2] + ans
                ans.appendleft(q[2])
        else:
            if q[1] == '1':
                #ans = q[2] + ans
                ans.appendleft(q[2])
            else:
                #ans = ans + q[2]
                ans.append(q[2])
ans_s = ''.join(ans)
if is_reverse:
    print(ans_s[::-1])
else:
    print(ans_s)

#    print(ans)
#print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
cnt = 0
l = ''
r = ''
for i in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        cnt += 1
    else:
        if cnt % 2 == 0:
            if tmp[1] == '1':
                l = l + tmp[2]
            else:
                r = r + tmp[2]
        else:
            if tmp[1] == '1':
                r = r + tmp[2]
            else:
                l = l + tmp[2]
if cnt % 2 == 0:
    ans = l[::-1] + S + r
else:
    ans = r[::-1] + S[::-1] + l
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
q = int(input().strip())


rev_count = 0
for i in range(q):
  query = input()
  if query[0] == "1":
    rev_count += 1
  elif query[0] == "2":
    query = query.strip().split(" ")
    if rev_count & 1 == 1:
      if query[1] == "1":
        S.append(query[2])
      else:
        S.appendleft(query[2])
    else:
      if query[1] == "1":
        S.appendleft(query[2])
      else:
        S.append(query[2])

if rev_count & 1 == 1:
  S.reverse()

print("".join(S))

Submission status: Accepted

Solution code:
import sys, math
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left # bisect_left(list, value)
#from fractions import gcd
enu = enumerate

sys.setrecursionlimit(10**7)
S = input()
Q = int(input())
Que = [input().split() for _ in range(Q)]

d = deque([s for s in S])

oneisleft = True
for q in Que:
    if q[0] == '1':
        oneisleft = not(oneisleft)
    else:
        fi, ci = q[1], q[2]
        if oneisleft:
            if fi == '1':
                d.appendleft(ci)
            else:
                d.append(ci)
        else:
            if fi == '1':
                d.append(ci)
            else:
                d.appendleft(ci)
if oneisleft:
    res = ''.join(list(d))
else:
    res = ''.join(reversed(list(d)))
print(res)

Submission status: Accepted

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

revs = 0
front = []
rear = []

for i in range(q):
    ins = input().split()
    if(ins[0] == "1"):
        revs += 1
    else:
        if (ins[1] == "1" and revs%2 == 0 or ins[1] =="2" and revs%2 == 1):
            front.extend(ins[2])
        else:
            rear.extend(ins[2])

if (revs % 2 == 0):
    for j in reversed(front):
        print(j, end="")
    for j in s:
        print(j, end="")
    for j in rear:
        print(j, end="")
else:
    for j in reversed(rear):
        print(j, end="")
    for j in reversed(s):
        print(j, end="")
    for j in front:
        print(j, end="")
print()

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    count = 0
    A = []
    B = []
    for _ in range(Q):
        U = input()
        if len(U) == 1:
            count += 1
        else:
            _, F, C = U.split()
            F = int(F)
            if F == 1:
                if count % 2 == 0:
                    A.append(C)
                else:
                    B.append(C)
            else:
                if count % 2 == 0:
                    B.append(C)
                else:
                    A.append(C)

    if count % 2 == 0:
        A = A[::-1]
        return "".join(A) + S + "".join(B)
    else:
        B = B[::-1]
        return "".join(B) + S[::-1] + "".join(A)
        
if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

reverse_flag = 0
head = ""
back = ""
for _ in range(q):
    query = input()
    if query[0] == "1":
        reverse_flag ^= 1
    else:
        t,f,c = query.split()
        f = int(f) + reverse_flag
        if f%2 == 1:
            head = c + head
        else:
            back = back + c
s = head + s + back
print(s if reverse_flag%2 == 0 else s[::-1])

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
d = deque()
for s in S:
    d.append(s)
cnt = 0
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        cnt += 1
    else:
        if q[1] == '1':
            if cnt & 1:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if cnt & 1:
                d.appendleft(q[2])
            else:
                d.append(q[2])
if cnt & 1:
    ans = reversed(list(d))
else:
    ans = list(d)
print(''.join(ans))

Submission status: Accepted

Solution code:
from collections import deque

def answer():
    s = deque(input())
    q = int(input())
    rev = False
    for i in range(q):
        query = input().split()
        if int(query[0]) == 2:
            f, c = int(query[1]), query[2]
            if (not rev and f == 1) or (rev and f == 2):
                s.appendleft(c)
            else:
                s.append(c)
        else:
            rev = not rev
    if rev:
        s = list(s)
        s = s[::-1]
    print(''.join(s))
    return

answer()

Submission status: Accepted

Solution code:
from collections import deque

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

reverse_flag = -1 # -1なら反転しない

for i in range(q):
    query = list(input().split())
    if query[0] == '1':
        reverse_flag *= -1
    else:
        if query[1] == '1':
            if reverse_flag == -1:
                deque.appendleft(s, query[2])
            else:
                deque.append(s, query[2])
        else:
            if reverse_flag == -1:
                deque.append(s, query[2])
            else:
                deque.appendleft(s, query[2])
                
if reverse_flag == 1:
    s.reverse()
    
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

mae = deque()
usiro=deque()

s=input()
kai=int(input())

siji=[]*kai

for i in range(kai):
    siji.append(input())


state=1
for i in range(kai):
    si=siji[i]
    if(si[0]=='1'):
        state*=-1
    else:
        _,f,c=si.split()
        f=int(f)
        if(state==-1):
            f=3-f
        if(f==1):
            mae. appendleft(c)
        else:
            usiro.append(c)

ans=deque()
for m in mae:
    if(state==1):
        ans.append(m)
    else:
        ans.appendleft(m)
for smoji in s:
    if(state==1):
        ans.append(smoji)
    else:
        ans.appendleft(smoji)
for u in usiro:
    if(state==1):
        ans.append(u)
    else:
        ans.appendleft(u)

kotae="".join(ans)
print(kotae)

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
left = deque()
right = deque()
rotate = 1
for i in range(Q):
    n = [str(i) for i in input().split()]
    if n[0] == "1":
        rotate *= -1
    else:
        if n[1] == "1":
            if rotate == 1:
                left.appendleft(n[2])
            else:
                right.append(n[2])
        else:
            if rotate == 1:
                right.append(n[2])
            else:
                left.appendleft(n[2])
left = list(left)
right = list(right)
ans = "".join(left)
ans += S
ans += "".join(right)
if rotate == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
s=input()
q=int(input())
dp=[' ']*(len(s))
for i in range(len(s)):
    dp[i]=s[i]
index=[0]*(len(s))
for i in range(len(s)):
    index[i]=i
flag=0
head=-1
tail=len(s)
for i in range(q):
    t=input()
    if t[0]=='1':
        flag=1-flag
    else:
        _,f,c=map(str,t.split())
        dp.append(c)
        if f=='1':
            if flag==1:
                index.append(tail)
                tail+=1
            else:
                index.append(head)
                head-=1
        else:
            if flag==0:
                index.append(tail)
                tail+=1
            else:
                index.append(head)
                head-=1
index2=[]
for i in range(len(dp)):
    index2.append((index[i],dp[i]))
index2.sort(reverse=flag)
#print(index2)
for i in range(len(index)):
    print(index2[i][1],end='')

Submission status: Accepted

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

rev=0

sento=[]
matsubi=[]

for _ in range(q):
    temp=input().split(" ")
    
    if temp[0]=="1":
        if rev==0:
            rev=1
        else:
            rev=0
    else:
        if rev==0:
            if temp[1]=="1":
                sento.append(temp[2])
            else:
                matsubi.append(temp[2])
        else:
            if temp[1]=="2":
                sento.append(temp[2])
            else:
                matsubi.append(temp[2])

sento_s="".join(sento)
sento_s=sento_s[::-1]
matsubi_s="".join(matsubi)

s=sento_s+s+matsubi_s

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

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
bgn = ''
end = ''
is_inversion = False

for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        if is_inversion:
            is_inversion = False
        else:
            is_inversion = True
    elif query[0] == '2':
        if query[1] == '1':
            if is_inversion:
                end += query[2]
            else:
                bgn += query[2]
        elif query[1] == '2':
            if is_inversion:
                bgn += query[2]
            else:
                end += query[2]
    


if is_inversion:
    ans = end[::-1] + S[::-1] + bgn
else:
    ans = bgn[::-1] + S + end

print(ans)

Submission status: Accepted

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

conv = 0
pre = []
pos = []
for i in range(q):
    query = input().split()
    if query[0] == '1':
        conv += 1
        continue
    if (int(query[1])+conv) %2 == 1:
        pre.append(query[2])
        continue
    pos.append(query[2])
if conv % 2 == 0:
    pre = ''.join(pre[::-1])
    pos = ''.join(pos[::1])
    s = pre + s + pos
else:
    pre = ''.join(pre[::1])
    pos = ''.join(pos[::-1])
    s = s[::-1]
    s = pos + s + pre
print(s)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

reverse = 0
pre = []
after = []
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == '1':
        reverse = 1 - reverse
    else:
        T, F, C = q.split()
        if (F == '1') ^ (reverse == 1):
            pre.append(C)
        else:
            after.append(C)
if reverse:
    after.reverse()
    ans = ''.join(after) + S[::-1] + ''.join(pre)
else:
    pre.reverse()
    ans = ''.join(pre) + S + ''.join(after)
print(ans)

Submission status: Accepted

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

tmp = 1
a = ''
b = ''
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        tmp *= -1
    else:
        if tmp > 0:
            if query[1] == '1':
                a = ''.join((query[2], a))
            else:
                b = ''.join((b, query[2]))
        else:
            if query[1] == '1':
                b = ''.join((b, query[2]))
            else:
                a = ''.join((query[2], a))
res = ''.join((a, s, b))
if tmp > 0:
    print(res)
else:
    print(res[::-1])

Submission status: Accepted

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

s = input()
q = int(input())
qs = [list(input().split()) for i in range(q)]

pls_vec = []
pls_append = pls_vec.append

mns_vec = []
mns_append = mns_vec.append

q1_vec = 1
for qi in qs:
    if qi[0]=="1":
        q1_vec*=(-1)
    else:
        if q1_vec==1:
            if qi[1]=="1":
                mns_append(qi[2])
            else:
                pls_append(qi[2])
        else:
            if qi[1]=="1":
                pls_append(qi[2])
            else:
                mns_append(qi[2])

ret = "".join(mns_vec[::-1]) + s + "".join(pls_vec)
if q1_vec==-1:
    ret=ret[::-1]
print(ret)

Submission status: Accepted

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

    counter = 0
    for _ in range(q):
        query = [t for t in input().split()]
        if len(query)==1:
            counter +=1

        else:
            f = (counter+int(query[1]))%2
            if f==1:
                s.appendleft(query[2])
            else:
                s.append(query[2])

    s = list(s)
    if counter%2==1:
        s = s[::-1]

    for c in s:
        print(c,end='')

    print()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

def main():
    S = input()
    Q = int(input())
    head = ""
    tail = ""
    is_vert_normal = True
    for _ in range(Q):
        query = input().split()
        T = int(query[0])
        if T == 1:
            is_vert_normal = not is_vert_normal
        else:
            F = int(query[1])
            C = query[2]
            if F == 1:
                if is_vert_normal:
                    head += C
                else:
                    tail += C
            else:
                if not is_vert_normal:
                    head += C
                else:
                    tail += C
    if is_vert_normal:
        print(head[::-1] + S + tail)
    else:
        print(tail[::-1] + S[::-1] + head)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
isRight = True
for i in range(q):
    query = input()
    if query[0] == '1':
        isRight = not(isRight)
    else:
        _, f, c = query.split()
        if f == '1':
            if isRight:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if isRight:
                s.append(c)
            else:
                s.appendleft(c)
t = ''.join(list(s))
if not isRight:
    reversed(t)
print(t if isRight else t[::-1])

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  s = input().strip()
  q = int(input())
  state = 1
  swap = [0, 2, 1]
  ss = []
  es = []
  for _ in range(q):
    x = input().strip()
    if x == '1':
      state *= -1
    else:
      _, f, c = x.split(' ')
      f = int(f)
      if state == -1:
        f = swap[int(f)]
      if f == 1:
        ss.append(c)
      else:
        es.append(c)
  s = ''.join(ss[::-1]) + s + ''.join(es)
  if state == -1:
    s = s[::-1]
  print(s)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
 
rev = False
fore = []
end = []
 
for i in range(q):
    l = input().split()
    if l[0] == "1":
        rev ^= True
    else:
        if l[1] == "1":
            if rev == False:
                fore.append(l[2])
            else:
                end.append(l[2])
        else:
            if rev == False:
                end.append(l[2])
            else:
                fore.append(l[2])
 
if rev == False:
    print("".join(reversed(fore)) + s + "".join(end))
else:
    print("".join(reversed(end)) + "".join(reversed(s)) + "".join(fore))

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter, namedtuple, deque
from bisect import bisect_left
from functools import reduce
import math

NN = 2020202
MOD = 10**9+7
INF = float("inf")


S = list(input().split())
Q = int(input())
query = [list(input().split()) for i in range(Q)]
que = deque(S)
rev = 0

for q in query:
    if len(q) == 1:
        rev ^= 1
    else:
        if q[1] == "1":
            if rev:
                que.append(q[2])
            else:
                que.appendleft(q[2])
        else:
            if rev:
                que.appendleft(q[2])
            else:
                que.append(q[2])
    # print(que)

if rev:
    print("".join(que)[::-1])
else:
    print("".join(que))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
rev = 0
head = []
tail = []
is_rev = False

for _ in range(q):
    qq = input().split()
    t = int(qq[0])
    if t == 1:
        rev += 1
        is_rev = not is_rev
    else:
        f = int(qq[1])
        c = qq[2]
        if f == 1:
            if is_rev:
                tail.append(c)
            else:
                head.append(c)
        else:
            if is_rev:
                head.append(c)
            else:
                tail.append(c)

if is_rev:
    ls = list(reversed(tail))
    ls.extend(reversed(s))
    ls.extend(head)
else:
    ls = list(reversed(head))
    ls.extend(s)
    ls.extend(tail)
print(''.join(ls))

Submission status: Accepted

Solution code:
import sys
fastinput = sys.stdin.readline
from collections import deque
S = input()
Q = int(input())
ans = deque([S])
Reverse = False
for q in range(Q):
    query = fastinput().split()
    if query[0]=="1":
        Reverse = not Reverse
    elif (query[1]=="1") ^ Reverse:#左側にくっつく
        ans.appendleft(query[2])
    else:
        ans.append(query[2])
SS = "".join(list(ans))
if Reverse:
    SS = "".join(reversed(SS))
print(SS)

Submission status: Accepted

Solution code:
S = input()
N = int(input())
Q = [input().split() for _ in range(N)]
start = ''
end = ''

# Sの向きを定義1:前から0:後ろから
direct = 0
for i in range(N):
  if Q[i][0] =='1':
    direct^=1
  else:
    if direct == int(Q[i][1])-1:
      start += Q[i][2]
    else:
      end += Q[i][2]
      
ans = start[::-1]+S+end
print(ans) if direct == 0 else print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

right=True
rev=False
for i in range(q):
    t=input().split()
    if t[0]=="1":
        right=not right
    if t[0]=="2":
        f=int(t[1])
        c=t[2]
        if f==1:rev=True
        else:rev=False

        if right^rev:
            s.append(c)
        else:
            s.appendleft(c)

if not right:
    s.reverse()
ans="".join(list(s))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())
count = 0
for q in range(Q):
    Query = input().split()
    if Query == ['1']:
        count += 1
    else:
        if count % 2 == 0:
            if Query[1] == "1":
                S.appendleft(Query[2])
            else:
                S.append(Query[2])
        else:
            if Query[1] == "1":
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
if count % 2 == 1:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
inversion_flag = False
for _ in range(Q):
    query_list = list(input().split())
    if query_list[0] == "1":
        inversion_flag = not inversion_flag
        continue
    C = query_list[2]
    if query_list[1] == "2":
        if inversion_flag:
            S.appendleft(C)
        else:
            S.append(C)
    else:
        if inversion_flag:
            S.append(C)
        else:
            S.appendleft(C)

S = list(S)
if inversion_flag:
    print(*S[::-1], sep="")
else:
    print(*S, sep="")

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
rev = False

Q = int(input())

for _ in range(Q):
    i_q = list(input().split())
    if i_q[0] == "1":
        rev = not rev
    else:
        appendtop = False
        if not rev and i_q[1] == "1":
            appendtop = True
        elif rev and i_q[1] == "2":
            appendtop = True

        if appendtop:
            S.appendleft(i_q[2])
        else:
            S.append(i_q[2])

if rev:
    while len(S) > 0:
        print(S.pop(), end="")
else:
    while len(S) > 0:
        print(S.popleft(), end="")

print()

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    reverse = False
    front = []
    back = []
    for _ in range(Q):
        TFC = list(input().split())
        if len(TFC) == 1:
            reverse = not(reverse)
        else:
            t,f,c = TFC
            if reverse:
                if f == "2":
                    front.append(c)
                else:
                    back.append(c)

            else:
                if f == "1":
                    front.append(c)
                else:
                    back.append(c)
    if reverse:
        print("".join(back[::-1]) + S[::-1] + "".join(front))
    else:
        print("".join(front[::-1]) + S + "".join(back))
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a=input();b='';l='';f=0
for _ in range(int(input())):
    k=list(input().split())
    if k[0]=='1':
        f=(f+1)%2
    else:
        if (k[1]=='1' and f==0) or (k[1]=='2' and f==1):
            b+=k[2]
        else:
            l+=k[2]
p=b[::-1]+a+l
print(p if f==0 else p[::-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from collections import deque


def resolve():
    S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    Q = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    # N, D = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    # h_list = [int(x) for x in sys.stdin.readline().split()]  # 複数int

    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    # v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)]
    # grid = [[int(x) for x in sys.stdin.readline().split()]
    #         for _ in range(N)]  # int grid

    logger.debug('{}'.format([]))

    reverse = False

    s_list = deque(S)

    for _ in range(Q):
        q = input().strip()
        if q == '1':
            reverse = not reverse
        else:
            t, f, c = q.split(' ')
            if (f == '1' and not reverse) or (f == '2' and reverse):
                s_list.appendleft(c)
            else:
                s_list.append(c)

    if reverse:
        print(''.join(list(s_list)[::-1]))
    else:
        print(''.join(list(s_list)))


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(input, output)

Submission status: Accepted

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline

S = list(readline().rstrip())
Q = int(readline())
a = deque([])
b = deque([])
s = 1

for _ in range(Q):
    query = readline().rstrip()
    if query == '1':
        s = -s
    else:
        _, F, C = map(str, query.split())
        if s == 1:
            if F == '1':
                a.appendleft(C)
            else:
                b.append(C)
        else:
            if F == '1':
                b.append(C)
            else:
                a.appendleft(C)

answer = list(a) + S + list(b)
answer = ''.join(answer)
if s == -1:
    answer = answer[::-1]
print(answer)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
flag = True
atama = deque()
osiri = deque()
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.appendleft(c)
            else:
                osiri.append(c)
        else:
            if flag:
                osiri.append(c)
            else:
                atama.appendleft(c)

if flag:
    while(atama):
        tmp = atama.popleft()
        print(tmp, end='')
    print(s, end='')
    while(osiri):
        tmp = osiri.popleft()
        print(tmp, end='')
else:
    while(osiri):
        tmp = osiri.pop()
        print(tmp, end='')
    print(s[::-1], end='')
    while(atama):
        tmp = atama.pop()
        print(tmp, end='')

Submission status: Accepted

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

reverse = False # 反転しているか

list_first =  []
list_last = []

for i in range(Q):
  query = input().split()
  if query[0] == '1':
    reverse = not reverse
  elif (query[1]=='1') == reverse: # query[0]==2 and ...
    list_last.append(query[2])
  else: # query[0]==2 and (query[1]==1) != reverse
    list_first.append(query[2])

if not reverse:
  for c in list_first[::-1] + list(S) + list_last:
    print(c, end='')
else:
  for c in list_last[::-1] + list(S)[::-1] + list_first:
    print(c, end='')

print('')

Submission status: Accepted

Solution code:
from collections import deque
s=deque(list(input()))
p=0
for i in range(int(input())):
    a=list(map(str,input().split()))
    if len(a)==1:
        p^=1
    else:
        if p%2:
            if a[1]=="2":
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if a[1]=="1":
                s.appendleft(a[2])
            else:
                s.append(a[2])
ans=""
if p%2==1:
    while s:
        ans+=s.pop()
else:
    while s:
        ans+=s.popleft()
print(ans)

Submission status: Accepted

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

#qs = []
hanten = 0
mae = ''
ushiro = ''
for i in range(q):
    x = input()
    if x[0] == '1':
        #qs.append(x)
        hanten += 1
    else:
        xx = x.split(' ')
        #qs.append(x.split(' '))
        if xx[1] == '1':
            if hanten % 2 == 0:
                mae = xx[2] + mae
            else:
                ushiro = ushiro + xx[2]
        else:
            if hanten % 2 == 0:
                ushiro = ushiro + xx[2]
            else:
                mae = xx[2] + mae           

if hanten % 2 == 0:
    s = mae + s + ushiro
else:
    s = ushiro[::-1] + s[::-1] + mae[::-1]

print(s)

Submission status: Accepted

Solution code:
s = input()
front = []
back = []
isflip = False

# flipがFalseの時は、F=1ならfront[0]に、f=2ならback[-1]に来るように付ける
# flipがtrueの時は、F=1ならback[-1]に、F=2ならfront[0]に来るように付ける
# ∴常にappendすればOK.最後にfrontにreversedを掛けて出力.
a = int(input())
for i in range(a):
    query = input()
    if(int(query[0]) == 1):
        isflip = not isflip
    else:
        t, f, c = query.split()
        if(int(f) == 2):
            if(isflip):
                front.append(c)
            else:
                back.append(c)
        else:
            if(not isflip):
                front.append(c)
            else:
                back.append(c)

if(isflip):
    print("".join(str(e) for e in reversed(back)) + "".join(str(e) for e in reversed(s)) + "".join(str(e) for e in front))
else:
    print("".join(str(e) for e in reversed(front)) + s + "".join(str(e) for e in back))

Submission status: Accepted

Solution code:
from collections import deque
def inputs(): return [int(x) for x in input().split()]
s = deque(input())
q = int(input())
flag = 1
for i in range(q):
    q1 = input()
    if len(q1)==1:
      flag *=-1 
    else:
        l,f,c = q1.split()
        if flag ==1:
            if int(f)==1:
                s.appendleft(c)
            else:
                s.append(c)
        elif flag==-1:
            if int(f)==1:
                s.append(c)
            else:
                s.appendleft(c)
if flag==1:
    print("".join(list(s)))
else:
    s = list(s)
    s = s[::-1]
    print("".join(list(s)))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
head=[]
end=[]
rev = False
for i in range(n):
    q = list(map(str,input().split()))
    if q[0]=='1':
        head,end = end, head
        rev = not rev
    elif q[0]=='2':
        if q[1]=='1':
            head.append(q[2])
        else:
            end.append(q[2])

head.reverse()
if rev:
    ans = list(s)
    ans.reverse()
else:
    ans = list(s)
print("".join(head+ans+end))