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 = deque(list(input()))
Q = int(input())
q = [list(input().split()) for _ in range(Q)]
r = 0


for i in range(Q):
    if q[i][0] == "1":
        r += 1
    else:
        if (q[i][1] == "1" and r%2==0)or (q[i][1] == "2" and r%2==1 ):
            S.appendleft(q[i][2])
        else:
            S.append(q[i][2])

S = "".join(S)
if r%2==1:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
reverse=0
for q in range(Q):
    Query=list(input().split())
    T=Query[0]
    if T=="1":
        reverse+=1
    if T=="2":
        F,C=Query[1],Query[2]
        if F=="1" and reverse%2==0 or F=="2" and reverse%2==1:
            S.appendleft(C)
        else:
            S.append(C)
S="".join(S)
print(S if reverse%2==0 else S[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
isInversed = False
front = deque()
back = deque()
for _ in range(q):
    inp =  list(input().split())
    if inp[0] == '1':
        isInversed = not isInversed
    else:
        if inp[1] == '1':
            if not isInversed:
                front.appendleft(inp[2])
            else:
                back.append(inp[2])
        else:
            if not isInversed:
                back.append(inp[2])
            else:
                front.appendleft(inp[2])

front_str = ''.join(front)
back_str = ''.join(back)
ans = front_str + s + back_str

if not isInversed:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
before = []
after = []
isReversed = 0


for _ in range(q):
    tmp = input()
    if len(tmp) == 1:
        if isReversed:
            isReversed = 0
        else:
            isReversed = 1
    else:
        if tmp[2] == str(2):
            if isReversed:
                before.append(tmp[-1])
            else:
                after.append(tmp[-1])
        else:
            if isReversed:
                after.append(tmp[-1])
            else:
                before.append(tmp[-1])


if isReversed:
    ans = "".join(after[::-1]) + s[::-1] + "".join(before)
    print(ans)

else:
    ans = "".join(before[::-1]) + s + "".join(after)
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
 
S = input().rstrip()
S = deque(S)
Q = int(input())
Que = (input().rstrip() for _ in range(Q))
 
flag = 0 #0:そのまま 1:反転
for elem in Que:
  if elem[0] == '1':
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S.appendleft(elem[4])
    elif s == 2 and flag == 0:
      S.append(elem[4])
    elif s == 1 and flag == 1:
      S.append(elem[4])
    else:
      S.appendleft(elem[4])
 
if flag == 1:
  S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S=input()
N=int(input())
q = deque()
A = [input().split()]
for n in range(N-1):
  A.append(input().split())
for s in S:
  q.append(s)
top = True
for n in range(N):
  if A[n][0]=="1":
    top = not top
  elif A[n][1]=="1" and top or A[n][1]=="2" and not top:
    q.appendleft(A[n][2])
  else:
    q.append(A[n][2])
T = ""
while len(q)>0:
  if top:
    T+=q.popleft()
  else:
    T+=q.pop()
print(T)

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())
  query[0] = int(query[0])
  if query[0] == 1: t = 1 - (t)
  else:
    if t == 0:
      if int(query[1]) == 1: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if int(query[1]) == 1: s.append(query[2])
      else: s.appendleft(query[2])
if t == 0: print(*s, sep = '')
else: print(*reversed(s), sep = '')

Submission status: Accepted

Solution code:
from collections import deque
s = input()
qq = int(input())
q = []
for _ in range(qq):
  q.append(input())
h = 1
for i in q:
  if i[0] == '1':
    h*=-1
if h == -1:
  ss = ''
  for i in reversed(s):
    ss += i
  s = ss
d = deque()
d.append(s)
h = 1
for i in q:
  if i[0] == '1':
    h*=-1
  else:
    if (h == 1 and i[2] == '1') or (h == -1 and i[2] == '2'):
      d.appendleft(i[4])
    else:
      d.append(i[4])
p = ''
if h == -1:
  for i in reversed(d):
    p += i
else:
  for i in d:
    p += i
print(p)

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    d = deque([s for s in input()])
    Q = int(input())
    reverse = False

    for i in range(Q):
        q = input().split()
        if q[0] == "1":
            reverse = not reverse
        else:
            if (q[1] == "1" and not reverse) or (q[1] == "2" and reverse):
                d.appendleft(q[2])
            else:
                d.append(q[2])
    
    if reverse:
        d.reverse()
    
    print("".join(d))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
list1 = []
list2 = []
flag = 1
for i in range(q):
    in_line = input().split()
    if in_line[0] == "1":
        flag *= -1
    else:
        if in_line[1] == "1":
            if flag == 1:
                list1.append(in_line[2])
            else:
                list2.append(in_line[2])
        else:
            if flag == 1:
                list2.append(in_line[2])
            else:
                list1.append(in_line[2])
if flag == 1:
    print("".join(list1[::-1]) + s + "".join(list2))
else:
    print("".join(list2[::-1]) + s[::-1] + "".join(list1))

Submission status: Accepted

Solution code:
S = list(input())
mae = []
ato = []
rev = False
Q = int(input())
for i in range(Q):
    A = list(input().rstrip().split(" "))
    if int(A[0]) == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if (rev and int(A[1]) == 2 or not rev and int(A[1]) == 1):
            mae.append(A[2])
        else:
            ato.append(A[2])
if rev:
    ato.reverse()
    S.reverse()
    print("".join(ato) + "".join(S) + "".join(mae))
else:
    mae.reverse()
    print("".join(mae) + "".join(S) + "".join(ato))

Submission status: Accepted

Solution code:
from collections import deque
import sys
s = deque(list(input()))
Q = int(input())
cnt = 0

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

if cnt == 1:
  s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque
S=deque(list(input()))
Q=int(input())
count=0
for i in range(Q):
  A=list(input().split())
  if A[0]=="1":
    count=(count+1)%2
  else:
    if A[1]=="1":
      if count==0:
        S.appendleft(A[2])
      else:
        S.append(A[2])
    else:
      if count==0:
        S.append(A[2])
      else:
        S.appendleft(A[2])
if count==1:
  S.reverse()
ans="".join(S)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s = list(input())
n = int(input())
q = deque(s)
Reverse = 0

for i in range(n):
    Input = input().split()
    if int(Input[0]) == 1:
        Reverse = abs(Reverse-1)
        continue
    else:
        if int(Input[1]) == 1:
            if Reverse == 0:
                q.appendleft(Input[2])
            else:
                q.append(Input[2])
        else:
            if Reverse == 0:
                q.append(Input[2])
            else:
                q.appendleft(Input[2])
if Reverse==1:
    q.reverse()
print(''.join(q))

Submission status: Accepted

Solution code:
# deque で高速に文字を先頭でもたせる

#初期入力
from collections import deque
import sys
input = sys.stdin.readline
S = input().strip()
Q = int(input())
FQ_dic={}
for i in range(Q):
    FQ_dic[i] =input().split()

rev =0 #反転Flag
dq =deque(S)
for i in range(Q):
    if FQ_dic[i][0] =="1":
        rev +=1
    else:
        a,b,c =FQ_dic[i]
        if rev %2 ==0 and b =="1"  :
            dq.appendleft(c)
        elif rev %2 ==0 and b =="2":
            dq.append(c)
        elif rev %2 ==1 and b =="1":
            dq.append(c)
        elif rev %2 ==1 and b =="2":
            dq.appendleft(c)
if rev %2 ==1:
    dq.reverse()
ans ="".join(dq)
print(ans)

Submission status: Accepted

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

rev = 0

for i in range(N):
  Q = input().split()
  if Q[0] == "1":
    rev += 1
    rev %= 2
  else:
    if (rev + int(Q[1])) % 2 == 1:
      S.appendleft(Q[2])
    else:
      S.append(Q[2])
S = "".join(S)
if rev == 1:
  S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque
H=deque(input())
Q=int(input())
reverse=False

A,B="",""
for _ in range(Q):
    T=input()
    if T[0]=="1":
        reverse=not reverse
    else:
        _,F,C=T.split()

        if ((not reverse) and F=="1") or (reverse and F=="2"):
            H.appendleft(C)
        else:
            H.append(C)

if reverse:
    H.reverse()

for s in H:
    print(s,end="")
print()

Submission status: Accepted

Solution code:
S = input()
N = int(input())
Q = tuple([_ for _ in input().split()] for _ in range(N))
C = 0
F = ""
R = ""
for q in Q:
    if q[0] == "1":
        C = (C+1)%2
    else:
        if (int(q[1])+C)%2 != 0:
            F = q[2] + F
        else:
            R = R + q[2]
S = F + S + R
if C != 0:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
S = input()
p = 0
head = ''
end = ''
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        end += Qr[2]
      else:
        head = Qr[2]+head
    else:
      if p:
        head = Qr[2]+head
      else:
        end += Qr[2]
S = head + S + end
if p:
  print(S[::-1])
else:
  print(S)

Submission status: Accepted

Solution code:
from collections import deque

s=list(input())
q=int(input())
d = deque(s)
reverse=False

for i in range(q):
  qs=list(input().split())
  if qs[0]=='1':
    reverse=not reverse
  else:
    if qs[1]=='1':
      d.appendleft(qs[2]) if not reverse else d.append(qs[2])
    else:
      d.append(qs[2]) if not reverse else d.appendleft(qs[2])
#  print(d)
if reverse:
  d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
B = deque()
res = 0
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    A = list(map(str, input().split()))
    if int(A[0]) == 1:
        if res == 0:
            res = 1
        else:
            res = 0
    else:
        if int(A[1]) == 1:
            if res == 0:
                B.appendleft(A[2])
            else:
                B.append(A[2])
        else:
            if res == 0:
                B.append(A[2])
            else:
                B.appendleft(A[2])
if res == 0:
    print(''.join(B))
else:
    B.reverse()
    print(''.join(B))

Submission status: Accepted

Solution code:
from collections import deque
s=deque(list(input()))
Q=int(input())
count = 0
for i in range(Q):
    q=input()
    if q[0]=='1':
        count += 1
    elif (q[2]=='1' and count%2==0) or (q[2]=='2' and count%2==1):
        s.appendleft(q[4])
    elif (q[2]=='2' and count%2==0) or (q[2]=='1' and count%2==1):
        s.append(q[4])

if count%2==0:
    print(''.join(s))
else:
    print(''.join(reversed(s)))

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
rev=False
for i in range(Q):
    A=input().split()
    if A[0]=="1":
        if rev==False:rev=True
        else:rev=False
    else:
        if rev==False:
            if A[1]=="1":
                S.appendleft(A[2])
            else:
                S.append(A[2])
        else:
            if A[1]=="1":
                S.append(A[2])
            else:
                S.appendleft(A[2])
if rev:
    S.reverse()
print(*S,sep="")

Submission status: Accepted

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

S = input().rstrip()
S = deque(S)
Q = int(input())
Que = (input().rstrip() for _ in range(Q))

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if elem[0] == '1':
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S.appendleft(elem[4])
    elif s == 2 and flag == 0:
      S.append(elem[4])
    elif s == 1 and flag == 1:
      S.append(elem[4])
    else:
      S.appendleft(elem[4])

if flag == 1:
  S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
## coding: UTF-8
from collections import deque

S = input()
ls = deque()
for i in range(len(S)):
    ls.append(S[i])


Q = int(input())

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


#print(S, Q, lq, ls)

mode = 'jun'

for i in range(Q):
    query = lq[i]
    ti = query[0]
    if(ti == '1'):
        if(mode == 'jun'):
            mode = 'gyaku'
        else:
            mode = 'jun'
    else:
        fi = query[1]
        ci = query[2]
        if(fi == '1'):
            if(mode == 'jun'):
                ls.appendleft(ci)
            else:
                ls.append(ci)
        else:
            if(mode == 'jun'):
                ls.append(ci)
            else:
                ls.appendleft(ci)

a = []

if(mode == 'jun'):
    for i in range(len(ls)):
        a.append(ls[i])
else:
    for i in range(len(ls)):
        j = (-1) * (i+1)
        a.append(ls[j])


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

Submission status: Accepted

Solution code:
from collections import deque

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

rev = False

for i in range(q):
    inp = input()
    if inp == '1':
        rev = True if rev == False else False
    else:
        n = inp.split()[1]
        t = inp.split()[2]
        if n == '1':
            d.append(t) if rev == True else d.appendleft(t)
        else:
            d.appendleft(t) if rev == True else d.append(t)

if rev == True:
    d.reverse()

print(''.join(d))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a = [list(map(str, input().split())) for i in range(q)]

head = 1
ss = ''

for i in range(q):
    if(a[i][0] == '1'):
        head *= -1
    else:
        if(a[i][1] == '2' and head == 1):
            s = s + a[i][2]
        elif(a[i][1] == '1' and head == -1):
            s = s + a[i][2]    
        else:
            ss = ss + a[i][2]
if(head == 1):
    print(ss[::-1] + s)
else:
    print(s[::-1] + ss)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
rev = False
for i in range(Q):
    query = input()
    if query[0] == '1':
        rev = not rev
    else:
        qtype = query[2]
        qstr = query[4]
        if qtype == "1":#先頭
            if not rev:
                S.appendleft(qstr)
            else:
                S.append(qstr)
        else:
            if not rev:
                S.append(qstr)
            else:
                S.appendleft(qstr)
if rev:
    print(*list(S)[::-1],sep="")
else:
    print(*S,sep="")

Submission status: Accepted

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

rev = False
before = []
after = []
for i in range(q):
  inp = list(input().split())
  if inp[0] == '1':
    rev = not rev
  else:
    f = inp[1]
    c = inp[2]
    if rev:
      if f == '1':
        f = '2'
      else:
        f = '1'
    if f == '1':
      before.append(c)
    else:
      after.append(c)
ans = ''
if rev:
  after.reverse()
  ans = ''.join(after) + s[::-1] + ''.join(before)
else:
  before.reverse()
  ans = ''.join(before) + s + ''.join(after)
print(ans)

Submission status: Accepted

Solution code:
#D
s_list=list(input())
q=int(input())
r=0
sl_list=[]
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            sl_list.append(ope[2])
        else:
            s_list.append(ope[2])
sl_list.reverse()
s_list=sl_list + s_list
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))

Submission status: Accepted

Solution code:
s = str(input())
q = int(input())
ls = [list(map(str,input().split())) for _ in range(q)]
a = -1
f = []
b = []
for i in range(q):
    if ls[i] == ["1"]:
        if a == -1:
            a = -2
        else:
            a = -1
    else:
        if a+int(ls[i][1]) == 0:
            f.append(ls[i][2])
        else:
            b.append(ls[i][2])
if a == -1:
    f = f[::-1]
    print("".join(f)+s+"".join(b))
else:
    b = b[::-1]
    print("".join(b)+s[::-1]+"".join(f))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
left = ""
right = ""
flag_swap = False
for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    flag_swap = not flag_swap
  else:
    if (query[1] == "1" and not flag_swap) or (query[1] == "2" and flag_swap):
      left += query[2]
    else:
      right += query[2]

if flag_swap:
  print(right[::-1]+s[::-1]+left)
else:
  print(left[::-1]+s+right)

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
c=0
left=[]
right=[]
for i in range(q):
    s1=input()
    if s1[0]=='1':
        c+=1
    else:
        if s1[2] == '1':
            if c % 2 == 0:
                left.append(s1[4])
            else:
                right.append(s1[4])
        else:
            if c % 2 == 0:
                right.append(s1[4])
            else:
                left.append(s1[4])
left=left[::-1]
left.extend(s)
left.extend(right)
if c%2==0:
    print(''.join(left))
else:
    left=left[::-1]
    print(''.join(left))

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
c=0
left=[]
right=[]
for i in range(q):
    s1=input()
    if s1[0]=='1':
        c+=1
    else:
        if s1[2] == '1':
            if c % 2 == 0:
                left.append(s1[4])
            else:
                right.append(s1[4])
        else:
            if c % 2 == 0:
                right.append(s1[4])
            else:
                left.append(s1[4])
left=left[::-1]
left.extend(s)
left.extend(right)
if c%2==0:
    print(''.join(left))
else:
    left=left[::-1]
    print(''.join(left))

Submission status: Accepted

Solution code:
from _collections import deque

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

for i in range(q):
    query = list(input().split())
    if query[0] == '1':
        reverse_flg = not reverse_flg
    else:
        if query[1] == '1':
            if reverse_flg:
                s.append(query[2])
            else:
                s.appendleft(query[2])
        else:
            if reverse_flg:
                s.appendleft(query[2])
            else:
                s.append(query[2])
if reverse_flg:
    s.reverse()
print("".join(s))

Submission status: Accepted

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

front = ''
back = ''
rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = not rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            back += ci
        else:
            front += ci

if rv:
    s = back[::-1] + s[::-1] + front
else:
    s = front[::-1] + s + back
print(s)

Submission status: Accepted

Solution code:
import sys
s = sys.stdin.readline()
s = s[:-1]
q = int(input())

front = ''
back = ''
rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = not rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            back += ci
        else:
            front += ci

if rv:
    s = back[::-1] + s[::-1] + front
else:
    s = front[::-1] + s + back
print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
r = False
top = ""
end = ""

for i in range(q):
    t = input().split()
    if t[0] == "1":
        r = not r
    else:
        if t[1] == "1":
            if r == False:
                top += t[2]
            else:
                end += t[2]
        else:
            if r == True:
                top += t[2]
            else:
                end += t[2]
if r == False:
    print(top[::-1] + s + end)
else:
    print(end[::-1] + s[::-1] + top)

Submission status: Accepted

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = col.deque(sinput().split())
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt: #反転の状態
        if f == "1": s.append(c)
        if f == "2": s.appendleft(c)
    else: #ノーマル状態
        if f == "1": s.appendleft(c)
        if f == "2": s.append(c)
    #print(s)

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
r = False
top = []
end = []


for i in range(q):
    t = list(input().split())
    if t[0] == "1":
        r = not r
    else:
        if t[1] == "1":
            if r == False:
                top.append(t[2])
            else:
                end.append(t[2])
        else:
            if r == True:
                top.append(t[2])
            else:
                end.append(t[2])

if r == False:
    print("".join(top)[::-1] + s + "".join(end))
else:
    print("".join(end)[::-1] + s[::-1] + "".join(top))

Submission status: Accepted

Solution code:
from collections import deque
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
D=deque()
for i in range(len(S)):
  D.append(S[i])  
a=1
for i in range(Q):
  if ls[i][0]=="1":
    a*=(-1)
  else:
    if (ls[i][1]=="1" and a==1) or (ls[i][1]=="2" and a==-1):
      D.appendleft(ls[i][2])
    else:
      D.append(ls[i][2])
if a==1:
  while D:
    print(D.popleft(), end="")
else:    
  while D:
    print(D.pop(), end="")

Submission status: Accepted

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

d = 1
S1 = []
S2 = []
for i in range(n):
    Q = list(input().split())
    if Q[0] == '1':
        d *= -1
    else:
        f = Q[1]
        c = Q[2]
        if f == '1':
            if d == 1:
                S1.append(c)
            else:
                S2.append(c)
        else:
            if d == 1:
                S2.append(c)
            else:
                S1.append(c)
if d == 1:
    ans = ''.join(S1[::-1]) + S + ''.join(S2)
else:
    ans = ''.join(S2[::-1]) + S[::-1] + ''.join(S1)

print(ans)

Submission status: Accepted

Solution code:
#import time
#t1=time.time()
s=input()
q=int(input())
trans=0
head=''
#t2=time.time()
for i in range(q):
  query = input().split()
  if query[0]=='1':
    trans += 1
  elif (int(query[1]) & 1) ^ (trans & 1):
    head += query[2]
  else:
    s += query[2]
res = ''
#t3=time.time()
if trans % 2 == 0:
  res = ''.join([head[len(head)-i-1] for i in range(len(head))]) + s
else:
  res = ''.join([s[len(s)-i-1] for i in range(len(s))]) + head
print(res)
#t4=time.time()
#print(int((t2-t1)*1000),int((t3-t2)*1000),int((t4-t3)*1000),int((t4-t1)*1000))

Submission status: Accepted

Solution code:
from collections import deque

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

ANS = deque(S)

rev = 0

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

if rev:
    ANS.reverse()

print(''.join(ANS))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
querys = [list(input().split()) for i in range(q)]

rev = 0
moji = []
for query in querys:
    if query[0] == '1':
        rev += 1
    else:
        query[1] = int(query[1])
        p = True if query[1] == 1 else False
        moji.append([query[2], rev, p])

if rev%2 != 0:
    s = s[::-1]
head = ""
end = ""
for m in moji:
    if (rev - m[1]) % 2 != 0:
        m[2] = not(m[2])
    
    if m[2]:
        head = m[0] + head
    else:
        end = end + m[0]

s = head + s + end
print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
s_s = []
s_e = []
sw = 0
for i in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        sw += 1
    else:
        if (int(Q[1]) + sw)%2 == 0:
            s_s.append(Q[2])
        else:
            s_e.append(Q[2])
if sw%2 != 0:
    ans = s_s[::-1] + list(s[::-1]) + s_e
    print("".join(ans))
else:
    ans = s_e[::-1] + list(s) + s_s
    print("".join(ans))

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().strip()
q = int(readline())
is_reversed = False
qs = read().decode().split("\n")
left = []
right = [] 
for i in range(q):
    query = qs[i].split()
    t = int(query[0])
    if t == 1:
        is_reversed = not is_reversed
    elif t == 2:
        f = int(query[1])
        c = query[2]
        if f == 1:
            if is_reversed:
                right.append(c)
            else:
                left.append(c)
        elif f == 2:
            if is_reversed:
                left.append(c)
            else:
                right.append(c)

s = "".join(reversed(left)) + s + "".join(right)
if is_reversed:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
S=list(input())
#print(S)
q=int(input())
x=0
appends=[[] for i in range(2)]
for i in range(q):
    s=list(input().split())
    if int(s[0])==1:
        x+=1
    else:
        appends[(x+int(s[1])+1)%2].append(s[2])
        #print(appends)
if  x%2==1:
    S=S[::-1]
S=appends[x%2][::-1]+S+appends[(x+1)%2]
#print(S)
for k in S:
    print(k,end='')

Submission status: Accepted

Solution code:
from collections import deque

S=input()
N=int(input())
inilen=len(S)
d=deque()
for i in range(inilen):
    d.append(S[i])
revflag=False
for i in range(N):
    L=list(map(str,input().split()))
    if L[0]=="1":
        if revflag==False:
            revflag=True
        else:
            revflag=False
    elif L[1]=="1":
        if revflag==False:
            d.appendleft(L[2])
        else:
            d.append(L[2])
        
    elif L[1]=="2":
        if revflag==False:
            d.append(L[2])
        else:
            d.appendleft(L[2])

if revflag==True:
    d.reverse()

Z=list(d)
Z=[str(a) for a in Z]
Z="".join(Z)
print(Z)

Submission status: Accepted

Solution code:
ans = input()
q = int(input())
top = 0
first = str()
last = str()

for i in range(q):
    li =input().split()
    if li[0] =="1":
        top = (top+1)%2
    else:
        if li[1] == "1":
            if top ==0:
                first += li[2]
            else:
                last += li[2]
        else:
            if top ==1:
                first += li[2]
            else:
                last += li[2]

if top == 0:
    ans = first[::-1] + ans + last
else:
    ans = last[::-1] + ans[::-1] + first

print(ans)

Submission status: Accepted

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

top =0
first = str()
last = str()

for i in range(times):
    Q =input().split()
    if Q[0] =="1":
        top = (top+1)%2
    else:
        if Q[1] == "1":
            #先頭に追加する 0のとき先頭
            if top ==0:
                first +=Q[2]
            else:
                last += Q[2]
        else:
            if top ==1:
                first +=Q[2]
            else:
                last += Q[2]
if top == 0:
    S = first[::-1] + S + last
else:
    S = last[::-1] + S[::-1] + first


print(S)

Submission status: Accepted

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

#print(List)

#S='a'
#Q=4
#List=[['2', '1', 'p'], ['1'], ['2', '2', 'c'], ['1']]
han=0
b=''
for a in List:
    if a[0]=='1':
        han+=1
    if a[0]=='2':
        if (a[1]=='1' and han%2==0) or (a[1]=='2' and han%2==1):
            b+=a[2]
        if (a[1]=='2' and han%2==0) or (a[1]=='1' and han%2==1):
            S=S+a[2]
#print(S)
#print(b)
if han%2==0:
    print(b[::-1]+S)
else:
    print(S[::-1]+b)

Submission status: Accepted

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

for i in range(q):
  a = list(input().split())
  if a[0] == '1':
    count += 1
    continue
    
  if count%2 == 0:
    if a[0] == '2':
      if a[1] == '1':
        s.appendleft(a[2])
      elif a[1] == '2':
        s.append(a[2])
        
  if count%2 == 1:
    if a[0] == '2':
      if a[1] == '1':
        s.append(a[2])
      elif a[1] == '2':
        s.appendleft(a[2])
      
ans = ''.join(s)
print(ans if count%2 == 0 else ''.join(list(reversed(ans))))

Submission status: Accepted

Solution code:

s=input()
q=int(input())
p=0
a=""
b=""
for i in range(q):
    l=list(map(str,input().split()))
    if l[0]=="1":
        p=(p+1)%2
    else:
        if p==0:
            if l[1]=="1":
                a+=l[2]
            else:
                b+=l[2]
        else:
            if l[1]=="1":
                b+=l[2]
            else:
                a+=l[2]
if p==1:
    print(b[::-1]+s[::-1]+a)
else:
    print(a[::-1]+s+b)