Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Accepted

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

head = []
tail = []

flip = 0
for _ in range(q):
    com, *query = input().split()
    if com == "1":
        flip ^= 1
    else:
        f, c = query
        f = int(f)-1
        if f ^ flip == 0:
            head.append(c)
        else:
            tail.append(c)

if flip:
    head, tail = tail[::-1], head
    s = s[::-1]
else:
    head = head[::-1]
print("".join(head+s+tail))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
flip = 0
for _ in range(q):
    com, *query = input().split()
    if com == "1":
        flip ^= 1
    else:
        f, c = query
        f = int(f)-1
        if f ^ flip == 1:
            s.append(c)
        else:
            s.appendleft(c)
s = "".join(s)
print(s if not flip else s[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
arr = [list(input().split()) for i in range(q)]
mae1 = 0
mae = []
ato = []
for i in arr:
    if i[0] == '1':
        mae1 = 0 if mae1 == 1 else 1
    else:
        if mae1 == 0:
            if i[1] == '1':
                mae.append(i[2])
            else:
                ato.append(i[2])
        else:
            if i[1] == '1':
                ato.append(i[2])
            else:
                mae.append(i[2])
mae = ''.join(mae)
ato = ''.join(ato)
ans = mae[::-1] + s + ato
if mae1 == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
#gcdは3.4.3だとfractionsにある
from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    s = input()
    q = ii()
    qlist = []
    for i in range(q):
        tmp = list(input().split())
        qlist.append(tmp)

    flag = False
    headlist = ''
    hiplist = ''
    for i in qlist:
        if i[0] == '1':
            flag = not flag
            continue
        else:
            ftmp = i[1] == '1'
            ctmp = i[2]
            if flag:
                ftmp = not ftmp
            if ftmp:
                headlist += ctmp
            else:
                hiplist += ctmp
    ansstr = ''    
    headlist = headlist[::-1]
    ansstr+=headlist
    ansstr+=s
    #hiplist = hiplist[::-1]
    ansstr+=hiplist
    if flag:
        ansstr = ansstr[::-1]#reversed(ansstr)
    print(ansstr)
        
            




main()

Submission status: Accepted

Solution code:
from collections import deque
S=input()
Q=int(input())
q=deque(list(S))
directed=True
for i in range(Q):
    command=input().split()
    if command[0]=="1":
        directed=not(directed)
    else:
        if (command[1]=="1")==directed:
            q.appendleft(command[2])
        else:
            q.append(command[2])
ans=""
while(len(q)>0):
    if directed:
        t=q.popleft()
        ans+=t
    else:
        t=q.pop()
        ans+=t
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

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

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

Submission status: Accepted

Solution code:
from collections import deque

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

flag=1
for _ in range(Q):
    q=input().split()
    if q[0]=='1':
        flag*=-1
        continue
    if (q[1]=='1' and flag==-1) or (q[1]=='2' and flag==1):
        Sq.append(q[2])
    else:
        Sq.appendleft(q[2])
if flag==1:
    print(''.join(Sq))
else:
    Sq.reverse()
    print(''.join(Sq))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
 
s = 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:
    ans.reverse()
print(''.join(map(str,ans)))

Submission status: Accepted

Solution code:
from collections import deque

s = 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:
    ans.reverse()
print(''.join(map(str,ans)))

Submission status: Accepted

Solution code:
# 文字列の操作だけでできるか

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

T = 0
l = ""
r = ""
for i in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        # T = T ^ 1 (排他的論理和; 0 XOR 1 = 1, 1 XOR 1 = 0)
        T ^= 1
    elif T == 0:
        s = Query[2]
        if Query[1] == '1':
            #S = s + S
            l = s + l
        else:
            #S = S + s
            r = r + s
    else:
        s = Query[2]
        if Query[1] == '1':
            #S = S + s
            r = r + s
        else:
            #S = s + S
            l = s + l

S = l + S + r

if T == 1:
    S = S[::-1]

print(S)

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
r = 1
top = deque()
top.append(s)
for _ in range(q):
    inp = input()
    if inp[0] == "1":
        r *= -1
    else:
        _, f, c = inp.split()
        if f == "1" and r == -1 or f == "2" and r == 1:
            top.append(c)
        else:
            top.appendleft(c)
ans = "".join(top)
print(ans if r == 1 else ans[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a, b = '', ''
cnt = 0
for _ in range(q):
    que = input().split()
    if que[0] == '1':
        cnt += 1
    else:
        if (cnt+int(que[1])) %2 == 0:
            a += que[2]
        else:
            b += que[2]
if cnt%2 == 1:
    s = a[-1::-1]+s[-1::-1]+b
else:
    s = b[-1::-1]+s+a
print(s)

Submission status: Accepted

Solution code:
from sys import stdout

s = input()
q = int(input())
pref = ''
suff = ''
order = 1
for i in range (q):
  e = input().split()
  if e[0] == '1':
    order *= (-1)
  else:
    if order == 1:
      if e[1] == '1':
        pref = e[2] + pref
      else:
        suff += e[2]
    else:
      if e[1] == '2':
        pref = e[2] + pref
      else:
        suff += e[2]
if order == 1:
    stdout.write(pref)
    stdout.write(s)
    stdout.write(suff)
else:
    stdout.write(suff[::-1])
    stdout.write(s[::-1])
    stdout.write(pref[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
n = int(input())
qs = [list(input().split()) for i in range(n)]
pre = deque()
suf = deque()
frag = True
for q in qs:
    if len(q) == 1:
        frag = not frag
    else:
        if (frag, q[1]) == (True, "1") or (frag, q[1]) == (False, "2"):
            pre.appendleft(q[2])
        else:
            suf.append(q[2])
if frag is True:
    print("".join(list(pre)+list(s)+list(suf)))
else:
    suf.reverse()
    s.reverse()
    pre.reverse()
    print("".join(list(suf)+list(s)+list(pre)))

Submission status: Accepted

Solution code:
# キューを使ったアプローチ

from collections import deque

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

T = 0
for _ in range(Q):
    Query = list(input().split())
    if len(Query) == 1:
        T ^= 1
    elif T == 0:
        m = Query[2]
        if Query[1] == '1':
            S.appendleft(m)
        else:
            S.append(m)
    else:
        m = Query[2]
        if Query[1] == '2':
            S.appendleft(m)
        else:
            S.append(m)

if T==1:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
R = 1
Top = deque()
Top.append(S)
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      Top.append(c)
    else:
      Top.appendleft(c)
S = ''.join(Top)
S = S if R==1 else S[::-1]
print(S)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
head = ""
tail = ""
reversed = False
for _ in range(Q):
    q = input().replace(" ","")
    if len(q) == 3:
        t, f, c = q[0],q[1],q[2]
    else:
        t = q[0]
    if t == "1":
        reversed = not reversed
    elif t == "2":
        if f == "1":
            #元の順のとき
            if not reversed:
                head = c + head
            #元とは逆順になっているとき
            else:
                tail = tail + c
        else:
            #元の順のとき
            if not reversed:
                tail = tail + c
            #元とは逆順になっているとき
            else:
                head = c + head
if reversed:
    print(tail[::-1],S[::-1],head[::-1],sep="")
else:
    print(head,S,tail,sep="")

Submission status: Accepted

Solution code:
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()
S = deque(input())
rev = False
apdlft = S.appendleft
apd = S.append

for _ in range(int(input())):
    q = input().split()
    if q[0] == '1':
        rev = not(rev)
    else:
        if (q[1]=='1' and rev is False) or (q[1]=='2' and rev is True):
            apdlft(q[2])
        else:
            apd(q[2])

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

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverseCount = 0
pre = ""
pro = ""
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            pre = C + pre
        else:
            pro = pro + C
S = pre + S + pro
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

re=False
for i in range(1,q+1):
    q_i=input().split()
    if q_i[0]=="1":
        re= not re
    else:
        if q_i[1]=="1" and re==False:
            s.appendleft(q_i[2])
        elif q_i[1]=="2" and re==True:
            s.appendleft(q_i[2])
        else:
            s.append(q_i[2])

s=list(s)

if re==True:
    s=s[::-1]

s="".join(s)
print(s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverseCount = 0
pre = ""
pro = ""
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            pre = C + pre
        else:
            pro = pro + C
S = pre + S + pro
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
num = int(input())
cnt = 0
for _ in range(num):
    t = input().split()
    if t[0] == "1":
        cnt += 1
    else:
        if (int(t[1])+cnt) % 2 == 1:
            s.appendleft(t[2])
        else:
            s.append(t[2])
if cnt % 2 == 1:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
par = 0
pre = ""
pos = ""

for i in range(q):
  t = input().split()
  if t[0] == "1":
    par += 1
  elif (par + int(t[1]))%2 == 1:
    pre = t[2] + pre
  else:
    pos = pos + t[2]
    
ans = pre + s + pos

if par%2 == 0:
  print(ans)
else:
  print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

t = deque(s)

reverse = False
for _ in range(q):
   query = input()
   if query[0] == '1':
      reverse = not reverse
   elif query[0] == '2':
      _, f, c = query.split()
      if f == '1':
         if reverse:
            t.append(c)
         else:
            t.appendleft(c)
      elif f == '2':
         if reverse:
            t.appendleft(c)
         else:
            t.append(c)

result = ''.join(t)
if reverse:
   result = result[::-1]
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
q_number = int(input())
reverse_flag = 1
head = ""
tail = ""
for i in range(q_number):
  temp = input().split()
  if temp[0] == "1": 
    reverse_flag = reverse_flag * -1
  else:
    headstails = int(temp[1])*reverse_flag 
    if headstails == 1 or headstails == -2:
      head = temp[2] + head
    else:
      tail = tail + temp[2]
      
if reverse_flag == 1:
  print(head+s+tail)
else:
  print(tail[::-1]+s[::-1]+head[::-1])

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
s = 1
for _ in range(Q):
    q = input()
    if q!='1':
        if (q[2]=='1' and s==1) or (q[2]!='1' and s!=1):
            S.extendleft(q[4])
        else:
            S.extend(q[4])
    else:
        s *= -1
S = ''.join(list(S))
if s==1:
    print(S)
else:
    print(S[::-1])

Submission status: Accepted

Solution code:
def main():
    c = 0
    s = input()
    f = ''
    b = ''
    for _ in range(int(input())):
        q = input().split()
        if q[0] == '1':
            c += 1
        else:
            if (q[1] == '1' and c%2 == 0) or (q[1]=='2' and c%2==1):
                f = q[2] + f
            else:
                b += q[2]
    if c % 2 == 0:
        print(f+s+b)
    else:
        print(b[::-1]+s[::-1]+f[::-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:


import collections

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

rev = False
deque = collections.deque()
for c in S:
    deque.append(c)
for _ in range(Q):
    read = input()
    if read == '1':
        rev = not rev
    else:
        _, f, c = read.split()
        if not rev:
            if f == '1':
                deque.appendleft(c)
            else:
                deque.append(c)
        else:
            if f == '1':
                deque.append(c)
            else:
                deque.appendleft(c)

ret = ''.join(deque if not rev else reversed(deque))
print(ret)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
num = int(input())
cnt = 0
for _ in range(num):
    t = input().split()
    if t[0] == "1":
        cnt += 1
    else:
        if (int(t[1])+cnt) % 2 == 1:
            s.appendleft(t[2])
        else:
            s.append(t[2])
if cnt % 2 == 1:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
s1 = ""
s2 = ""
cnt = 1
for i in range(q):
  l1 = list(input().split())
  if len(l1) == 1:
    cnt *= -1
  else:
    if (cnt == 1 and l1[1] == "1") or (cnt == -1 and l1[1] == "2"):
      s1 = l1[2] + s1
    else:
      s2 = s2 + l1[2]
if cnt == 1:
  print(s1+s+s2)
else:
  print(s2[::-1]+s[::-1]+s1[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
num = int(input())
cnt = 0
for i in range(num):
  a = input().split()

  if a[0] == '2':
    if (int(a[1])+cnt)%2==1:
      s.appendleft(a[2])
    else:
      s.append(a[2])
  else:
    cnt += 1
if cnt%2 == 1:    
	s.reverse()
print("".join(s))

Submission status: Accepted

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

S = input()
Q = int(input())
rev_flg = False

output = deque()
output.append(S)

for i in range(Q):
  q = input().split(" ")
  if len(q)==1:
    # T=1のときは文字列を反転する
    rev_flg = not(rev_flg)
    # print(rev_flg)
  if len(q)==3:
    # print("T=2, S=",S)
    # T=2のときは
    # F=1なら先頭に文字列Cを追加
    if int(q[1])==1:
      if rev_flg==False:
        output.appendleft(q[2])
      else:
        output.append(q[2])
    # F=2なら最後に文字列を追加
    if int(q[1])==2:
      if rev_flg==False:
        # S = S + q[2]
        output.append(q[2])
      else:
        # S = q[2] + S
        output.appendleft(q[2])
  
output_s = "".join(list(output))

if rev_flg==True:
  print(output_s[::-1])
  #print(S[::-1])
else:
  #print(S)
  print(output_s)

Submission status: Accepted

Solution code:
# ABC158 D
from collections import deque
s=input()
q=int(input())
l=[]

for i in range(q):
    l.append(list(map(str,input().split())))

cnt=0
front_str=''
end_str=''

t_str=deque(s)
front_str=deque(front_str)
end_str=deque(end_str)

for ll in l:
    if ll[0]=='1':
        cnt+=1
    elif ll[0]=='2':
        if cnt%2==0:
            if ll[1]=='1':
                front_str.appendleft(ll[2])
            elif ll[1]=='2':
                end_str.append(ll[2])
        elif cnt%2==1:
            if ll[1]=='1':
                end_str.append(ll[2])
            elif ll[1]=='2':
                front_str.appendleft(ll[2])
ans=list(front_str)+list(t_str)+list(end_str)

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

Submission status: Accepted

Solution code:
from collections import deque
S=input()
d=deque(S)
rev=False
Q=int(input())
for i in range(Q):
    Que=input().split()
    if(Que[0]=='1'):
        rev=not rev
    else:
        if(Que[1]=='1'):
            if(not rev):
                d.appendleft(Que[2])
            else:
                d.append(Que[2])
        else:
            if(not rev):
                d.append(Que[2])
            else:
                d.appendleft(Que[2])
d=d if not rev else reversed(d)
print(*d,sep='')

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
op = [input().split() for _ in range(q)]


flag = True
ans = deque(s)

for i in op:
    if i[0] == "1":
        flag = not flag
    else:
        if i[1] == "1":
            if flag:
                ans.appendleft(i[2])
            else:
                ans.append(i[2])
        else:
            if flag:
                ans.append(i[2])
            else:
                ans.appendleft(i[2])

if flag:
    while(ans):
        t = ans.popleft()
        print(t, end='')
else:
    while(ans):
        t = ans.pop()
        print(t, end='')

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
li = [list(map(str, input().split())) for _ in range(q)]

frag = True

for i in range(q):
    que = li[i]
    if que[0] == "1":
        if frag:
            frag = False
        else:
            frag = True
    else:
        f = que[1]
        c = que[2]
        if frag:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)

if frag:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
op = [input().split() for _ in range(q)]


flag = True
ans = deque(s)

for i in op:
    if i[0] == "1":
        flag = not flag
    else:
        if i[1] == "1":
            if flag:
                ans.appendleft(i[2])
            else:
                ans.append(i[2])
        else:
            if flag:
                ans.append(i[2])
            else:
                ans.appendleft(i[2])

if flag:
    while(ans):
        t = ans.popleft()
        print(t, end='')
else:
    while(ans):
        t = ans.pop()
        print(t, end='')

Submission status: Accepted

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

hanten = 0
front = ""
end = ""

for i in range(Q):
  query = list(map(str, input().split()))
  if query[0] == "1":
    hanten += 1
    hanten = hanten % 2
  else:
    if query[1] == "1":
      if hanten == 0:
        front = front + query[2]
      else:
        end = end + query[2]
    else:
      if hanten == 0:
        end = end + query[2]
      else:
        front = front + query[2]

front = front[::-1]
ans = front + S + end
if hanten == 1:
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
S = deque(input())
Q = int(input())
# queries = list(list(input().split()) for _ in range(Q))  # 後でroop回し直すのは2度手間

reverse = 0
# for query in queries:
for query in sys.stdin:
    if query[0] == '1':  # len(query)は改行記号の有無で変わるのでダメ
        reverse = 1 - reverse

    else:
        _, f, c = query.split()
        is_append_left = (reverse + int(f)) % 2
        if is_append_left:
            S.appendleft(c)  # listだとO(n)だが，dequeではO(1)
        else:
            S.append(c)

if reverse:  # reverse処理は重い．実際，最後の一回で良い．
    S.reverse()

print(''.join(S))

Submission status: Accepted

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

hanten = 0
front = ""
end = ""

for i in range(Q):
  query = list(map(str, input().split()))
  if query[0] == "1":
    hanten += 1
    hanten = hanten % 2
  else:
    if query[1] == "1":
      if hanten == 0:
        front = query[2] + front
      else:
        end = end + query[2]
    else:
      if hanten == 0:
        end = end + query[2]
      else:
        front = query[2] + front

ans = front + S + end
if hanten == 1:
  ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
def rev_f(string , i):
    if i[2] == '2':
        string = string + i[4]

    if i[2] == '1':
        string = i[4] + string
    return string



def rev_s(string , i):
    if i[2] == '1':
        string = string + i[4]

    if i[2] == '2':
        string = i[4] + string
    return string



front = ''
back = ''
string = input()
num = int(input())
q_list = []
reverse_num = 0
reverse = True
reverese_s = False
for a in range(num):
    i = input()
    if i == '1':
        reverse_num  +=1 
        if reverese_s:
            reverese_s = False
        else:
            reverese_s = True
    else:
        if reverese_s == False:
            if i[2] == '2':
                back += i[4]
            else:
                front = i[4] + front
            
        else :
            if i[2] == '2':
                front = i[4] + front
            else:
                back += i[4]
        
if reverse_num %2 == 0:
    reverse = False
    
string = front + string + back
if reverse == True:
    string = string[::-1]
    
print(string)

Submission status: Accepted

Solution code:
from collections import deque

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

rev_stetus = False
for _ in range(Q):
    ipt = input().split()
    if ipt[0] == "1":
        rev_stetus = not rev_stetus
    else:
        f = int(ipt[1])
        c = ipt[2]
        if rev_stetus:
            if f == 1:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if f == 1:
                S.appendleft(c)
            else:
                S.append(c)

if rev_stetus:
    S.reverse()
    print("".join(S))
else:
    print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque

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

for _ in range(q):
    qs = input().split()
    if qs[0] == '1':
        cnt += 1
    else:
        if qs[1] == '1':
            if cnt%2 == 0:
                s.appendleft(qs[2])
            else:
                s.append(qs[2])
        else:
            if cnt%2 == 0:
                s.append(qs[2])
            else:
                s.appendleft(qs[2])
ans = ''.join(s)
print(ans if cnt%2 == 0 else ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
re = 0
for i in range(Q):
    X = input().split()
    if X[0] == "1":
        re += 1
    else:
        if (int(X[1]) + re)%2 == 1:
            S.appendleft(X[2])
        else:
            S.append(X[2])
while S:
    if re % 2:
        print(S.pop(),end = "")
    else:
        print(S.popleft(),end = "")
print()

Submission status: Accepted

Solution code:
def main():
	s = str(input())
	q = int(input())
	shiji = 1
	rcount = 0
	from collections import deque
	s = deque(s)
	for i in range(q):
		qu = list(map(str,input().split()))
		if qu[0] != '1':
			if shiji == 1:
				if qu[1] == '1':
					s.appendleft(qu[2])
				else:
					s.append(qu[2])
			else:
				if qu[1] == '2':
					s.appendleft(qu[2])
				else:
					s.append(qu[2])
		else:
			shiji *= -1
			rcount += 1
	if rcount % 2 == 0:
		print(''.join(map(str,s)))
	else:
		s.reverse()
		print(''.join(map(str,s)))

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import deque
S = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == "1":
        cnt += 1
    else:
        if (int(q[1]) + cnt) % 2 == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])
ans = "".join(S)
print(ans if cnt % 2 == 0 else ans[::-1])

Submission status: Accepted

Solution code:
flag = True
sen = ""
ushi = ""
S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  if T[0] == "1" :
    flag = not flag
  else:
    if flag:
      if T[1] == "1":
        sen = T[2] + sen
      else:
        ushi = ushi + T[2]
    else:
      if T[1] == "1":
        ushi = ushi + T[2]
      else:
        sen = T[2] + sen
ans = sen + S + ushi
if flag == False:
  print(ans[::-1])
else:
  print(ans)

Submission status: Accepted

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

for i in s:
    d.append(i)

q = int(input())

reverse = 0
for i in range(q):
    query = input().split()
    if query[0] == "1":
        t = int(query[0])
    else:
        t = int(query[0])
        f = int(query[1])
        c = query[2]

    if t == 1:
        reverse = 1 - reverse
    else:
        if reverse == 1:
            f = f - 1

        if f == 1:
            d.appendleft(c)
        else:
            d.append(c)
if reverse == 1:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
query = [input() for _ in range(q)]

s = deque(s)

inv = 0
for q in query :
    if q[0] == '1':
        inv += 1
    else:
        _,f,c = q.split()
        if f == '1':
            if inv % 2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if inv % 2 ==0:
                s.append(c)
            else:
                s.appendleft(c)
s = ''.join(s)
print(s if inv % 2 == 0 else s[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input().split())
f=False
for i in range(int(input())):
  _=input().split()
  if _[0]=='1':
    f=not f
  else:
    if f:
      [s.append(_[2]) if _[1]=='1'
       else s.appendleft(_[2])]
    else:
      [s.appendleft(_[2]) if _[1]=='1'
       else s.append(_[2])]
print("".join(s)[::-1] if f else "".join(s))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
cnt=0
from collections import deque
s=deque(s)
for i in range(q):
    j=tuple(input().split())
    if  len(j)==3 :
        if (cnt+int(j[1]))%2:
            s.appendleft(j[2])
        else:
            s.append(j[2])
    else:
        cnt+=1
if cnt%2==0:
    print("".join(s))
else:
    print("".join(reversed(list(s))))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return input().split()
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    s = S()
    t = 0
    l = []
    r = []
    for _ in range(II()):
        q = LS()
        if q[0] == "1":
            t ^= 1
        else:
            _,f, c = q
            if f == "1":
                if t:
                    r.append(c)
                else:
                    l.append(c)
            else:
                if t:
                    l.append(c)
                else:
                    r.append(c)
    if t:
        print("".join(r[::-1] + s[::-1] + l))
    else:
        print("".join(l[::-1] + s + r))
    return


#main
if __name__ == '__main__':
    solve()