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 = input()
Q = [input() for i in range(int(input()))]
rev = 0
l,r = -1,1
res ={0:S}
for q in Q:
  if q == "1":
    rev = (rev+1)%2
  else:
    t,f,c = q.split()
    if int(f) + rev == 2 :
      res[r] = c
      r += 1
    else:
      res[l] = c
      l -= 1

if rev == 0:
  for i in range(l+1,r):
    print(res[i],end="")
else:
  for i in range(r-1,l,-1):
    print("".join(list(reversed(res[i]))),end="")

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flag = 0
H = ""
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag += 1
    else:
        if flag%2 == 0:
            if q[1] == "1":
                H = H+q[2]
            else:
                S = S+q[2]
        else:
            if q[1] == "1":
                S = S+q[2]
            else:
                H = H+q[2]
H = H[::-1]
H = H+S
if flag%2 == 0:
    print(H)
else:
    H = H[::-1]
    print(H)

Submission status: Accepted

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


def main():
    s = input().strip()
    q = int(input().strip())

    rev_count = 0
    s_prev = ""
    s_suffix = ""
    for i in range(q):
        query = input().strip()
        if query[0] == "1":
            rev_count += 1
        else:
            _, f, c = query.split()
            if (f == "1" and rev_count % 2 == 0) or (f == "2" and rev_count % 2 == 1):
                s_prev += c
            else:
                s_suffix += c

    if rev_count % 2 == 0:
        print(s_prev[::-1] + s + s_suffix)
    else:
        print(s_suffix[::-1] + s[::-1] + s_prev)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
A = input()
B = int(input())
C = ""
for i in range(B):
    D = input().split()
    if D[0] == "1":
        C,A = A,C
    else:
        if D[1] == "1":
            C += D[2]
        else:
            A += D[2]
C = C[::-1]
print(C+A)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
rev = False
h = []
t = []
for i in range(n):
    q = readline()
    if q.startswith("1"):
        rev = not rev
    if q.startswith("2 1"):
        if not rev:
            h.append(q[-2])
        else:
            t.append(q[-2])
    if q.startswith("2 2"):
        if not rev:
            t.append(q[-2])
        else:
            h.append(q[-2])
s = "".join(h[::-1]) + s + "".join(t)
if rev:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
T=""
for i in range(Q):
    q=input().split()
    if q[0]=="1":
        T,S=S,T
    else:
        if q[1]=="1":
            T+=q[2]
        else:
            S+=q[2]
print(T[::-1]+S)

Submission status: Accepted

Solution code:
from collections import deque

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

S_list = list(S)

r = 1
a = []
b = []
d = deque(a)

for i in range(Q):
    q = input().split()
    if q[0] == '1':
        r *= -1
        continue
    elif len(q) == 3:
        if q[1] == '1':
            if r == -1:
                b.append(q[2])
            else:
                d.appendleft(q[2])
        else:
            if r == -1:
                d.appendleft(q[2])
            else:
                b.append(q[2])
                
answer = list(d) + S_list + b

if r == -1:
    answer = answer[::-1]
    
print(''.join(answer))

Submission status: Accepted

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
from functools import lru_cache

INF = 1 << 60

# ここから書き始める
s = input()
q = int(input())
cnt = 0
left = []
right = []
for i in range(q):
    query = input().split()
    if len(query) == 1:
        cnt += 1
    else:
        f = query[1]
        c = query[2]
        if cnt % 2 == 0:
            if f == "1":
                # s = c + s
                left.append(c)
            else:
                # s += c
                right.append(c)
        else:
            if f == "1":
                # s += c
                right.append(c)
            else:
                # s = query[2] + s
                left.append(c)

left = left[::-1]
right = "".join(right)
left = "".join(left)
ans = "".join([left, s, right])
# print(right, s, left)
if cnt % 2 != 0:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

rev = True

for i in range(q):
    q = list(input().split())
    if(int(q[0]) == 1):
        rev = rev ^ True
    else:
        n = int(q[1])
        if((n == 1 and rev) or (n == 2 and not rev)):
            s.appendleft(q[2])
        else:
            s.append(q[2])


txt = ''
if rev:
    txt = ''.join(s)
else:
    while not(not s):
        txt += s.pop()

print(txt)

Submission status: Accepted

Solution code:
start = []
end = []

def query1(b):
    return not b

def query2(F, C, string, b):
    if (F == '1' and b) or (F == '2' and not b):
        start.append(C)
        return C + string
    else:
        end.append(C)

S = input()
Q = int(input())
direction = True

for i in range(Q):
    Query = input().split()
    if len(Query) == 1:
        direction = query1(direction)
    else:
        query2(Query[1], Query[2], S, direction)

if direction:
    print(''.join(start[::-1]) + S + ''.join(end))
else:
    print(''.join(end[::-1]) + S[::-1] + ''.join(start))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
n = int(input())
q = []
for i in range(n):
  query = list(input().split())
  q.append(query)
  
a = 1
for i in range(n):
  if int(q[i][0]) == 1:
    a *= -1
  else:
    if int(q[i][1]) == 1:
      if a > 0:
        s.appendleft(q[i][2])
      else:
        s.append(q[i][2])
    else:
      if a > 0:
        s.append(q[i][2])
      else:
        s.appendleft(q[i][2])
s = list(s)
s = "".join(s)
if a > 0:
  print(s)
else:
  s = s[::-1]
  print(s)

Submission status: Accepted

Solution code:
s=input()
n=int(input())
t=""
u=""
bo=True
for _ in range(n):
  q=input().split()
  if q[0]=="1":
    t,u=u,t
    if bo==True:
      bo=False
    else:
      bo=True
  else:
    if q[1]=="1":
      t+=q[2]
    else:
      u+=q[2]
t=t[::-1]
if bo==False:
  s=s[::-1]
print(t+s+u)

Submission status: Accepted

Solution code:
s=input()
n=int(input())
t=""
for _ in range(n):
  q=input().split()
  if q[0]=="1":
    t,s=s,t
  else:
    if q[1]=="1":
      t+=q[2]
    else:
      s+=q[2]
t=t[::-1]
print(t+s)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc158/tasks/abc158_d

S = input()
Q = int(input())
T = ""

for _ in range(Q):
    query = input()
    ty = query[0]
    if ty == '1':
        S, T = T, S
    else:
        if query[2] == '1':
            T += query[4]
        else:
            S += query[4]

T = T[::-1]
print(T + S)

Submission status: Accepted

Solution code:
t=list(input())
q=int(input())
s=[]

for i in range(q):
  query=list(input().split())
  if query[0]=='1':
    s,t=t,s
  else:
    if query[1]=='1':
      s.extend(query[2])
    else:
      t.extend(query[2])
if s==[]:
  s=t
else:
  s.reverse()
  s.extend(t)

moji=''.join(s)
print(moji)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
flag = True
forward = ""
backward = ""
for i in range(Q):
    if Query[i][0] == "1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if Query[i][2] == '1':
            if flag:
                forward = Query[i][4] + forward
            else:
                backward = backward + Query[i][4]
        else:
            if flag:
                backward = backward + Query[i][4]
            else:
                forward = Query[i][4] + forward

if flag:
    ans = forward + S + backward
else:
    forward = forward[::-1]
    backward = backward[::-1]
    S = S[::-1]
    ans = backward + S + forward
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

flag = 1

for _ in range(q):
    tfc = input().split()
    if tfc[0] == "1":
        flag *= -1

    elif tfc[0] == "2":
        if (tfc[1] == "1" and flag == 1) or (tfc[1] == "2" and flag == -1):
            s.appendleft(tfc[2])
        elif (tfc[1] == "1" and flag == -1) or (tfc[1] == "2" and flag == 1):
            s.append(tfc[2])
else:

    if flag == -1:
        s = list(s)[::-1]

print("".join(s))

Submission status: Accepted

Solution code:
s = [_ for _ in input()]

rev = False

qn = int(input())

ins = []

for i in range(qn):
    q = input().split()
    if int(q[0])==1:
        rev = not rev
        continue

    if rev:
        if int(q[1])==1:
            s.append(q[2])
        else:
            ins.append(q[2])
    else:
        if int(q[1])==1:
            ins.append(q[2])
        else:
            s.append(q[2])

ins = ins[::-1]
ins.extend(s)

if rev:
    print("".join(ins[::-1]))
    exit()
print("".join(ins))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
flag = True
s = "!"
for i in range(Q):
    if Query[i][0] == "1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if Query[i][2] == '1':
            if flag:
                s = Query[i][4] + s
            else:
                s = s + Query[i][4]
        else:
            if flag:
                s = s + Query[i][4]
            else:
                s = Query[i][4] + s
ans = s.replace("!", S)
if flag:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

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

cnt = 0
S2 = []

for i in range(q):
    if procedure[i][0] == '1':
        cnt += 1
    if procedure[i][0] == '2':
        if (procedure[i][1] == '1' and cnt%2 == 0) or (procedure[i][1] == '2' and cnt%2 == 1):
            S2.append(procedure[i][2])
        else:
            S.append(procedure[i][2])

S2.reverse()
S = S2 + S
if cnt%2 == 1:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input().split())
n = int(input())
query_li = (input().split() for i in range(n))

flag = 1

for query in query_li:
    if int(query[0]) == 1:
        flag = flag * -1
    else:
        if int(query[1]) == 1:
            if flag == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if flag == 1:
                s.append(query[2])
            else:
                s.appendleft(query[2])

s = ''.join(s)
if flag == -1:
    s = s[::-1]

print(s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
is_norm = True  # Sが元の順番かどうか
s_head = ""     # 元のSに対して先頭側につく文字列
s_tail = ""     # 元のSに対して末尾側につく文字列

for i in range(0, Q):
    query = input().split()
    if int(query[0]) == 1:
        is_norm = not is_norm
    else:
        if is_norm:
            if int(query[1]) == 1:
                s_head = query[2] + s_head
            else:
                s_tail = s_tail + query[2]
        else:
            if int(query[1]) == 1:
                s_tail = s_tail + query[2]
            else:
                s_head = query[2] + s_head

if is_norm:
    print(s_head + S + s_tail)
else:
    print(s_tail[::-1] + S[::-1] + s_head[::-1])

Submission status: Accepted

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

def main():
    S = list(input().strip())
    Q = int(input())

    d = deque(S)
    mode = True

    for _ in range(Q):
        query = input().split()

        if query[0] == "1":
            mode = not mode
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                if mode:
                    d.appendleft(C)
                else:
                    d.append(C)
            elif F == "2":
                if mode:
                    d.append(C)
                else:
                    d.appendleft(C)

    s = list(d)
    if not mode:
        s = s[::-1]

    print(''.join(s))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = list(input().strip())
    Q = int(input())

    d = deque(S)
    mode = True

    for _ in range(Q):
        query = input().split()

        if query[0] == "1":
            mode = not mode
        elif query[0] == "2":
            F, C = query[1], query[2]
            if F == "1":
                if mode:
                    d.appendleft(C)
                else:
                    d.append(C)
            elif F == "2":
                if mode:
                    d.append(C)
                else:
                    d.appendleft(C)

    s = list(d)
    if not mode:
        s = s[::-1]

    print(''.join(s))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()
n=int(input())
r=-1
f=[]
b=[]
for i in range(n):
    q=input().split()
    if q[0]=='1':
        r*=-1
    else:
        t=int(q[1])
        if t*r==-1 or t*r==2 :
            f.append(q[2])
        else:
            b.append(q[2])
f=''.join(f)
b=''.join(b)
s=f[::-1]+s+b
if r==1:
    s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc158/tasks/abc158_d
from collections import deque

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

is_reverse = False
text = deque(S)
for _ in range(Q):
    query = input()
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[2] == '1':
            if is_reverse:
                text.append(query[4])
            else:
                text.appendleft(query[4])
        else:
            if is_reverse:
                text.appendleft(query[4])
            else:
                text.append(query[4])

if is_reverse:
    print(''.join(text)[::-1])
else:
    print(''.join(text))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input().strip())
flag = 1
for i in range(Q):
  line = input().strip()
  if len(line) == 1:
    flag = 1 - flag
  else:
    _, F, C = line.strip().split()
    if flag:
      if int(F) == 1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if int(F) == 1:
        S.append(C)
      else:
        S.appendleft(C)

if flag:
  print(''.join(S))
else:
  print(''.join(list(reversed(S))))

Submission status: Accepted

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

Inv = False #反転しているか
front_str = [] #末尾側
back_str = [] #先頭側

for query_list in query:
    if query_list[0] == '1':
        Inv = not Inv
    else:
        if query_list[1] == '1': #先頭に追加
            if Inv:
                front_str.append(query_list[2])
            else:
                back_str.append(query_list[2])
        else:
            if Inv:
                back_str.append(query_list[2])
            else:
                front_str.append(query_list[2])

answer = "".join(back_str[::-1] + [s] + front_str)

if Inv:
    answer = answer[::-1]

print(answer)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
n = int(input())

point = 0
for i in range(n):
  q = input().split()
  if q.pop(0)=='1':
    point = (point+1)%2
  else:
    _q = int(q.pop(0))-1
    if (point+_q)%2 == 0:
      s.appendleft(q.pop(0))
    else:
      s.append(q.pop(0))

if point == 1:
  s.reverse()

print(''.join(s))

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
reverse=0
back=""
front=""
for i in range(Q):
    T=input()
    if T[0]=="1":
        reverse=1-reverse
    else:
        q,F,C=T.split()
        F=int(F)
        if F-reverse==1:
            front=C+front
        else:
            back+=C

S=front+S+back
if reverse==0:
    print(S)
else:
    S=S[::-1]
    print(S)

Submission status: Accepted

Solution code:
s = input()
Q = int(input())
r = -1
head = ''
tail = ''
for i in range(Q):
    t = input()
    if t[0] == '1':
        r *= -1
    else:
        if t[2] == '1':
            if r == 1:
                tail += t[4]
            else:
                head = t[4] + head
        else:
            if r == 1:
                head = t[4] + head
            else:
                tail += t[4]
if r == 1:
    print(tail[::-1] + s[::-1] + head[::-1])
else:
    print(head + s + tail)

Submission status: Accepted

Solution code:
from collections import deque
import sys

S = input()
Q = int(input())
t = deque(S)
rev = False

for _ in range(Q):
  Query = input().split()
  if Query[0] == '1':
    rev = not rev
    
  else:
    F = Query[1]
    C = Query[2]
    if F == '1':
      if rev:
        t.append(C)
      else:
        t.appendleft(C)
    else:
      if rev:
        t.appendleft(C)
      else:
        t.append(C)

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

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
b = True
left = []
right = []
for _ in range(q):
  query = input()
  if query == "1":
    b = not(b)
  else:
    t, f, c = query.split()
    if int(f) + b == 2:
      left.append(c)
    else:
      right.append(c)
s = "".join(left)[::-1] + s + "".join(right)
print(s if b else s[::-1])

Submission status: Accepted

Solution code:
S = input()
lQ = list(range(int(input())))
def main(S):
  T = True
  L = ''
  R = ''
  for i in lQ:
    q = list(input().split())
    if q[0]=='1':
      T = not(T)
    else:
      if (T and q[1]=='1') or (not(T) and q[1]=='2'):
        L += q[2]
      elif (T and q[1]=='2') or (not(T) and q[1]=='1'):
        R += q[2]
  SS = L[::-1] + S + R
  if T:
    print(SS)
  else:
    print(SS[::-1])
main(S)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
mode = True
l = ''
r = ''
for _ in range(q):
    tfc = input().split()
    if int(tfc[0]) == 1:
        mode = not mode
    else:
        if mode:
            if int(tfc[1]) == 1:
                l += tfc[2]
            else:
                r += tfc[2]
        else:
            if int(tfc[1]) == 1:
                r += tfc[2]
            else:
                l += tfc[2]
print(l[::-1]+s+r if mode else r[::-1]+s[::-1]+l)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
head, tail = "", ""
reverse = False
for _ in range(Q):
    cmd = input().split()
    if cmd[0] == "1":
        reverse = not reverse
    else:
        top = (cmd[1] == "1") ^ reverse
        if top:
            head += cmd[2]
        else:
            tail += cmd[2]
print(tail[::-1] + S[::-1] + head) if reverse else print(head[::-1] + S + tail)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
n = int(input())
query = []
for i in range(n):
    query.append(input().split())
cond = 1
word = deque(s)
for i in range(n):
    if query[i] == ['1']:
        cond *= -1
    else:
        if cond == 1:
            if query[i][1] == '1':
                word.appendleft(query[i][2])
            else:
                word.append(query[i][2])
        else:
            if query[i][1] == '2':
                word.appendleft(query[i][2])
            else:
                word.append(query[i][2])

result = ''.join(word)
if cond == -1:
    result = result[::-1]
print(result)

Submission status: Accepted

Solution code:
string = input()
N=int(input())
Que = []
for i in range(N):
	Que.append(input())

init = ""
end = ""
rev = 2
for Q in Que:	
	if int(Q[0]) == 1:
		if rev != 1:
			rev=1
		else:
			rev=2
	elif int(Q[0])==2:
		T,F,C = Q.split(' ')
		if rev == 1:
			if int(F) == 1:
				F =2
			else:
				F = 1
		if int(F) == 1:
			init = init+C
		elif int(F) == 2:
			end = end + C
final = init[-1::-1]+string+end
if rev == 1:
	print(final[-1::-1])
else:
	print(init[-1::-1]+string+end)

Submission status: Accepted

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

F, B = [], []
reverse = False
for i in range(Q):
	Query = input().split()
	if Query[0] == '1':
		reverse = not reverse
	else:
		if not reverse:
			if Query[1] == '1':
				F.append(Query[2])
			else:
				B.append(Query[2])
		else:
			if Query[1] == '1':
				B.append(Query[2])
			else:
				F.append(Query[2])

if not reverse:
	ans = "".join(reversed(F))
	ans += "".join(S)
	ans += "".join(B)
	print(ans)
else:
	ans = "".join(reversed(B))
	ans += "".join(reversed(S))
	ans += "".join(F)
	print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
N = int(input())
is_inversed = False
for i in range(N):
    Q = input()
    if Q[0] == '1':
        is_inversed = not (is_inversed)
    else:
        if (Q[2] == '1' and is_inversed) or (Q[2] == '2' and not (is_inversed)):
            S.append(Q[4])
        else:
            S.appendleft(Q[4])
if is_inversed:
    print(''.join(list(S)[::-1]))
else:
    print(''.join(S))

Submission status: Accepted

Solution code:
S = input()

flag = 1
S1 = ""
S2 = ""
N = int(input())
for i in range(N):
  line = input().split()
  if line[0] == "1":
    if flag == 1:
      flag = 2
    else:
      flag = 1
  else:
    if flag == 1:
      if line[1] == "1":
        S1 = line[2] + S1
      else:
        S2 = S2 + line[2]
    else:
      if line[1] == "1":
        S2 = S2 + line[2]
      else:
        S1 = line[2] + S1

S = S1 + S + S2
if flag == 2: S = S[::-1]
print(S)

Submission status: Accepted

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

top_list = []
end_list = []

flag = 1

for q in q_list:
    if q[0] == '1':
        flag *= -1
    else:
        if flag == 1:
            if q[1] == '1':
                top_list.append(q[2])
            else:
                end_list.append(q[2])
        else:
            if q[1] == '2':
                top_list.append(q[2])
            else:
                end_list.append(q[2])
if flag == 1:            
    print(''.join(top_list[::-1]) + s + ''.join(end_list))
else:
    print(''.join(end_list[::-1]) + s[::-1] + ''.join(top_list))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
turnover = False
suffix = ""
prefix = ""

for i in range(Q):
  TFC = input().split()
  T = int(TFC[0])
  if T == 1:
    # turn over
    turnover = not turnover
  elif T == 2:
    F = int(TFC[1])
    C = TFC[2]
    if turnover:
      if F == 1:
        F = 2
      else:
        F = 1
    if F == 1:
      prefix = prefix + C
      # put C before S
    elif F == 2:
      suffix = suffix + C
      # put C after S
S = prefix[-1::-1]+S+suffix
if turnover:
  S = S[-1::-1]

print(S)

Submission status: Accepted

Solution code:
from collections import deque

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

flag = False

for i in range(1, q+1):
    a = input().split()
    if len(a) == 1:
        flag = not flag
    else:
        _, f, c = a
        if (f == "1" and flag == False) or (f == "2" and flag == True):
            ans.extendleft(c)
        elif (f == "1" and flag == True) or (f == "2" and flag == False):
            ans.extend(c)
        else:
            print("fac")
            exit(1)

if flag:
    print("".join(list(ans)[::-1]))
else:
    print("".join(ans))

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
d.extend(list(input()))
q = int(input())
z = 0
F = ""
R = ""
for i in range(q):
    a = tuple(input().split())
    if a[0] == "1":
        z += 1
        z %= 2
    else:
        if (int(a[1]) + z) % 2 == 0:
            d.append(a[2])
        else:
            d.appendleft(a[2])

ans = "".join(d)
if z % 2 == 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque

def solve():
    S = input()
    Q = int(input())
    length = len(S)
    reverse = False
    first,last = deque([]),deque([])
    
    for _ in range(Q):
        query = input().split()
        if len(query) == 1:
            reverse = reverse^1
            first,last = last,first
        else:
            T,F,C = query
            T = int(T)
            F = int(F)
            if F == 1:
                if reverse:
                    first.append(C)
                else:
                    first.appendleft(C)
            else:
                if reverse:
                    last.appendleft(C)
                else:
                    last.append(C)
                
            length += 1
    
    if length == 1:
        print(S)
    else:
        first = list(first)
        last = list(last)
        first = first[::-1] if reverse else first
        mid = list(S)[::-1] if reverse else list(S)
        last = last[::-1] if reverse else last
        # print(reverse,first,mid,last)
        print(''.join(first+mid+last))
        
solve()

Submission status: Accepted

Solution code:
from collections import deque
S=input()
Q=int(input())
words=[]
for a in range(Q):
    A=input().split()
    words.append(A)
T=[a for a in S]
answers=deque(T)
number=0
for a in words:
    if a[0]=='1':
        number+=1
    else:
        if number%2==0:
            if a[1]=='1':
                answers.appendleft(a[2])
            else:
                answers.append(a[2])
        else:
            if a[1]=='1':
                answers.append(a[2])
            else:
                answers.appendleft(a[2])
if number%2==0:
    kotae=list(answers)
    moji=''
    for a in kotae:
        moji+=a
    print(moji)
else:
    kotae = list(answers)
    kotae.reverse()
    moji = ''
    for a in kotae:
        moji += a
    print(moji)

Submission status: Accepted

Solution code:
from collections import deque
ans=deque()
ans.append(str(input()))
q=int(input())
l=[list(map(str,input().split())) for i in range(q)]
#print(l)
c=0
for i in range(q):
	if l[i][0]=='1':
		c+=1
	else:
		if c%2==0:
			if l[i][1]=='1':
				ans.appendleft(l[i][2])
			else:
				ans.append(l[i][2])
		else:
			if l[i][1]=='2':
				ans.appendleft(l[i][2])
			else:
				ans.append(l[i][2])
A="".join(map(str,list(ans)))
if c%2==1:
	print(A[::-1])
else:
	print(A)

Submission status: Accepted

Solution code:
from collections import deque

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

rev = False

for _ in range(Q):
    T, F, C, *_ = input().split() + [None, None]

    if int(T) == 1:
        rev = not rev
    else:
        F = int(F)
        if rev:
            F = F % 2 + 1

    if F == 1:
        S.appendleft(C)
    else:
        S.append(C)

if rev:
    S.reverse()

print(*[x for x in S if x], sep='')

Submission status: Accepted

Solution code:
from collections import deque

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

rev = False

for _ in range(Q):
    T, F, C, *D = input().split() + [None, None]
    T = int(T)

    if T == 1:
        rev = not rev
    else:
        F = int(F)
        if rev:
            if F == 1:
                F = 2
            else:
                F = 1

    if F == 1:
        S.appendleft(C)
    else:
        S.append(C)

if rev:
    S.reverse()

print(*[x for x in list(S) if x], sep='')

Submission status: Accepted

Solution code:
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front
S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Submission status: Accepted

Solution code:
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front

S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Submission status: Accepted

Solution code:
S=str(input())
Q=int(input())
which=0
front=""
back=""
for i in range(Q):
    a=list(map(str,input().split()))
    if a==["1"]:
        which+=1
        which%=2
        continue
    else:
        if which==0:
            if a[1]=="1":
                front=a[2]+front
            if a[1]=="2":
                back+=a[2]
        if which==1:
            if a[1]=="1":
                back+=a[2]
            if a[1]=="2":
                front=a[2]+front

S=front+S+back
if which==1:
    print(S[::-1])
if which==0:
    print(S)

Submission status: Accepted

Solution code:
import math
import string
import itertools
import fractions
import heapq
import collections
import re
import array
import bisect
import sys
import random
import time
from collections import deque
inf = 10**9


def main():
    o = 1
    s = deque(input())
    q = int(input())
    for i in range(q):
        qu = input().split()
        if qu[0] == '1':
            o = int(not o)
        else:
            addo = 1 if qu[1] == '1' else 0
            if o == addo:
                s.appendleft(qu[2])
            else:
                s.append(qu[2])
        # print(s, o)
    
    if o:
        print(''.join(s))
    else:
        s.reverse()
        print(''.join(s))




if __name__ == '__main__':
    main()