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(input())
Q = int(input())
rev = False
for _ in range(Q):
    s = input().split()
    if len(s) == 1:
        rev = not rev
        continue
    if not rev:
        if s[1] == '1':
            S.appendleft(s[2])
        else:
            S.append(s[2])
    else:
        if s[1] == '1':
            S.append(s[2])
        else:
            S.appendleft(s[2])
if rev:
    print(''.join(reversed(S)))
else:
    print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
S = input()
d.append(S)
N = int(input())
direct = 1
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct *= -1
  else:
    if (X[1] == "1" and direct == 1) or (X[1] == "2" and direct == -1):
      d.appendleft(X[2])
    else:
      d.append(X[2])
      
S = "".join(list(d))
print(S) if direct == 1 else print(S[::-1])

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
S = input()
d.append(S)
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      d.appendleft(X[2])
    else:
      d.append(X[2])
      
S = "".join(list(d))
print(S) if direct == 0 else print(S[::-1])

Submission status: Accepted

Solution code:
S = input()
f = ""
N = int(input())
direct = 0
for i in range(N):
  X = input().split()
  if len(X) == 1:
    direct = 1 - direct
  else:
    if (X[1] == "1" and direct == 0) or (X[1] == "2" and direct == 1):
      f += X[2]
    else:
      S += X[2]
print(f[::-1]+S) if direct == 0 else print(S[::-1]+f)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
d = deque(s)
q = int(input())
flag = 0
for i in range(q):
    tfc = input().split()
    if tfc[0] == '1':
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if flag == 0:
            if int(tfc[1]) == 1:
                d.appendleft(tfc[2])
#                 print('appendleft:', d)
            else:
                d.append(tfc[2])
#                 print('append:', d)
        else:
            if int(tfc[1]) == 1:
                d.append(tfc[2])
#                 print('append:', d)
            else:
                d.appendleft(tfc[2])
#                 print('appendleft:', d)
if flag == 0:
    print(''.join(d))
else:
    print(''.join(reversed(d)))

Submission status: Accepted

Solution code:
sf=""
sb=input()
q=int(input())

rev=1

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    a,b=n[1],n[2]
    if (a=="1" and rev==1) or (a=="2" and rev==-1):
      sf+=b
    else:
      sb+=b

s=sf[::-1]+sb

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

Submission status: Accepted

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

rev=1

sf=""
sb=s

for _ in range(0,q):
  n=input().split()
  if n[0]=="1":
    rev*=-1
  else:
    if (n[1]=="1" and rev==1) or (n[1]=="2" and rev==-1):
      sf+=n[2]
    else:
      sb+=n[2]

s=sf[::-1]+sb

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

Submission status: Accepted

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

d=deque(S)
reverse=False
for q in query:
  if len(q)==1:
    reverse=not reverse
  else:
    _,f,c=q
    if f=='1':
      d.append(c) if reverse else d.appendleft(c)
    else:
      d.appendleft(c) if reverse else d.append(c)
print(*reversed(d),sep='') if reverse else print(*d,sep='')

Submission status: Accepted

Solution code:
B = input()
order = True
F = ""
for q in range(int(input())):
    T = input().rstrip().split()
    if T[0] == "1":
        order ^= True
    else:
        f,c = T[1],T[2]
        if not((f == "1") ^ order):
            F += c
        else:
            B += c
    #print(F,B)
F = F[::-1]+B
if not order:
    F = F[::-1]
print(F)

Submission status: Accepted

Solution code:
# print("S=")
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 # 偶数のとき正常, 奇数の時反転
  atama = ""
  oshiri = ""
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      check_reverse += 1
      # S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if int(query[1]) == 2 - check_reverse % 2:
        oshiri += query[2]
      else:
        atama += query[2]
      # print("atama",atama)
      # print("oshiri",oshiri)
  S = atama[::-1] + S + oshiri
  if check_reverse % 2==1:
    S = S[::-1]
  
  return S

print(problem_gksn(S,Q))

Submission status: Accepted

Solution code:
S = input()
# print("Q=")
Q = int(input())

def problem_gksn(S,Q):
  check_reverse = 0 # 偶数のとき正常, 奇数の時反転
  atama = ""
  oshiri = ""
  for _ in range(Q):
    # print("queryを入力してください")
    query = input().split()
    # print("query= ",query)
    if len(query) == 1: #1の場合
      # print("文字を反転させます")
      check_reverse += 1
      # S = S[::-1]
    else: #2の場合
      # print("文字をくっつけます")
      if int(query[1]) == 2 - check_reverse % 2:
        oshiri += query[2]
      else:
        atama += query[2]
      # print("atama",atama)
      # print("oshiri",oshiri)
  S = atama[::-1] + S + oshiri
  if check_reverse % 2==1:
    S = S[::-1]
  
  return S

print(problem_gksn(S,Q))

Submission status: Accepted

Solution code:
from collections import deque

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

lr = 'left'

def reverse_list(lr):
    if lr == 'right':
        return 'left'
    else:
        return 'right'

def moji_tuika(S, f, c, lr):
    if f == 1 and lr == 'left':
        S.appendleft(c)
    elif f == 1 and lr == 'right':
        S.append(c)
    elif f == 2 and lr == 'left':
        S.append(c)
    else:
        S.appendleft(c)
    return S

q = []
for i in range(Q):
    temp = input().split()
    if len(temp) == 1:
        q.append(1)
    else:
        q.append(temp)

S = deque(list(S))
for i in q:

    if type(i) == int:
        lr = reverse_list(lr)
    elif int(i[0]) == 2:
        S = moji_tuika(S,int(i[1]),i[2],lr)
if lr == 'right':
    S.reverse()

S = ''.join(S)
print(S)

Submission status: Accepted

Solution code:
from collections import deque
S = str(input())
Q = int(input())
d = deque()
reverse = 1
for i in S:
    d.append(i)
for j in range(Q):
  Query = input().split()
  if len(Query) == 1:
    reverse *= -1
  else:
    F = int(Query[1])
    C = str(Query[2])
    if reverse == 1:
      if F == 1:
        d.appendleft(C)
      elif F == 2:
        d.append(C)
    elif reverse == -1:
      if F == 1:
        d.append(C)
      elif F == 2:
        d.appendleft(C)
if reverse == -1:
    d.reverse()
ans = ''.join(d)
print(ans)

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
L=list()
B=True
X=''
Y=''
for i in range(Q):
  L=list(input().split())
  if L[0]=='1':
    B=not(B)
  else:
    if ((L[1]=='1' and B) or (not(L[1]=='1' or B))):
      Y=Y+L[2]
    else:
      X=X+L[2]
S=S[::-1]
S=S+Y
S=S[::-1]
S=S+X
if not(B):
  S=S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
for x in input():
    d.append(x)
Q = int(input())
f = 0
for _ in range(Q):
    l = list(input().split())
    if l[0] == '1':
        f ^= 1
    else:
        if f == 0: 
            if l[1] == '2': d.append(l[2])
            else: d.appendleft(l[2])
        else:
            if l[1] == '2': d.appendleft(l[2])
            else: d.append(l[2])
if f:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque #deque
s = input()
q = int(input())
flag = 1
d = deque(s)
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:
                d.appendleft(in_line[2])
            else:
                d.append(in_line[2])
        else:
            if flag == 1:
                d.append(in_line[2])
            else:
                d.appendleft(in_line[2])
if flag == -1:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque
s = str(input())
q = int(input())
s = deque(s)
query = []
w = True
for i in range(q):
    query = list(map(str,input().split()))
    if(query[0] == "1"):
        w = not(w)
    else:
        mem = query[2]
        if(query[1] == "1"):
            if(w):
                s.appendleft(mem)
            else:
                s.append(mem)
        else:
            if(w):
                s.append(mem)
            else:
                s.appendleft(mem)
if(w):
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flg = 0
senntou = ''
ushiro = ''
for i in range(Q):
    A = list(input().split())
    if int(A[0]) == 1 and flg == 0:
        flg = 1
        continue
    elif int(A[0]) == 1 and flg == 1:
        flg = 0
        continue
    if (int(A[1]) == 1 and flg == 0) or (int(A[1]) == 2 and flg == 1):
        senntou = A[2] + senntou
    else:
        ushiro = ushiro + A[2]
if flg == 0:
    print(senntou + S + ushiro)
if flg == 1:
    print(ushiro[::-1] + S[::-1] + senntou[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = input()
d = deque()
header = 1
for i in s:
    d.append(i)
q = int(input())

for i in range(q):
    data = input().split()
    if len(data) == 1:
        header *= -1
    else:
        f = int(data[1])
        c = data[2]
        if header == 1:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if header == -1:
    d.reverse()

s = list(d)
s = "".join(s)

print(s)

Submission status: Accepted

Solution code:
s=input();q=int(input());r,a=0,[[],[]]
for _ in range(q):
  t=input()
  if t=='1':r=-(-~r%2)
  else:a[~-r+int(t[2])].append(t[4])
print(''.join(a[~-r+1][::-1])+s[::r or 1]+''.join(a[~-r+2]))

Submission status: Accepted

Solution code:
word = input()
count = int(input())
reversed_flg = False
head = ''
tail = ''

for i in range(count):
    line = input()
    if len(line) == 1:
        reversed_flg = not reversed_flg
    else:
        str_t, str_f, char = line.split()
        f = int(str_f)

        if (f == 1 and not reversed_flg) or (f == 2 and reversed_flg):
            # word = char + word
            head = char + head
        else:
            # word = word + char
            tail += char

word = head + word + tail

if reversed_flg:
        word = word[::-1]

print(word)

Submission status: Accepted

Solution code:
from collections import deque

s=input()
q=int(input())
count=0 #何回反転があるか
ss=deque(s)
flag=True #通常状態
for i in range(q):
    info=input()
    if info[0]=="1":
        count+=1
        if flag:
            flag=False
        else:
            flag=True
    else:
        _,f,c=info.split()
        if flag:
            if f=="1":
                ss.appendleft(c)
            else:
                ss.append(c)
        else: #反転状態
            if f=="1":
                ss.append(c)
            else:
                ss.appendleft(c)
ans="".join(ss)
print(ans if count%2==0 else ans[::-1])

Submission status: Accepted

Solution code:
def main():
  w = input()
  s = collections.deque(w)
  n = int(input())
  
  order = True
  for _ in range(n):
    q = list(input_list_str())
    if len(q) == 1:
      order = False if order else True
      continue
    t, f, c = q
    if int(f) == 1:
      if order:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if order:
        s.append(c)
      else:
        s.appendleft(c)
  if order is False:
    s.reverse()
  print(''.join(s))

    
def input_list():
  return map(int, input().split())

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Accepted

Solution code:
#D
from collections import deque
Str = str(input())
Q = int(input())
S = deque(list(Str))
#S.append(list(Str))
#上の方法ではダメ
A = 0
B = []
C = []

for i in range(Q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if A%2==0:
            if len(B)>1:
                S.extendleft(B)
                B = []
            elif len(B) == 1:
                S.extendleft(B)
                B = []
            if len(C) > 0:
                S.extend(C)
                C = []
        else:
            if len(C)>1:
                S.extendleft(C)
                C = []
            elif len(C) == 1:
                S.extendleft(C)
                C = []
            if len(B) > 0:
                S.extend(B)
                B = []
        A += 1
    elif a[1] == '1':
        B.append(a[2])
    else:
        C.append(a[2])

if a[0] != '1':
    if A%2==0:
        if len(B)>1:
            S.extendleft(B)
            B = []
        elif len(B) == 1:
            S.extendleft(B)
            B = []
        if len(C) > 0:
            S.extend(C)
            C = []
    else:
        if len(C)>1:
            S.extendleft(C)
            C = []
        elif len(C) == 1:
            S.extendleft(C)
            C = []
        if len(B) > 0:
            S.extend(B)
            B = []

if A%2 != 0:
    S.reverse()
print(''.join(S))

Submission status: Accepted

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

s = input().rstrip()
q = int(input())

isRev = False

head = ''

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        isRev = not isRev
    else:
        if isRev and query[1] == '1':
            s = s + query[2]
        elif query[1] == '1':
            head = head + query[2]
        elif isRev and query[1] == '2':
            head = head + query[2]
        elif query[1] == '2':
            s = s + query[2]
            
if isRev:
    s = s[::-1] + head
else:
    s = head[::-1] + s
        
print(s)

Submission status: Accepted

Solution code:
from collections import deque

S = list(input())
S = deque(S)
Q = int(input())
reverse = 1
query = [input().split() for i in range(Q)]
for i in query:
    if len(i) == 1:
        reverse *= -1
    elif (i[1]=='1' and reverse == 1) or (i[1]=='2' and reverse==-1):
        S.appendleft(i[2])
    else:
        S.append(i[2])
if reverse == -1:
   S.reverse() 
print(''.join(list(S)))

Submission status: Accepted

Solution code:
from collections import deque
ss=deque()
s=list(input())
l=len(s)
for i in range(l):
    ss.append(s[i])
q=int(input())
info=[]
rev=[] #i回までに何回反転があるか
num=0
for i in range(q):
    info.append(list(input().split()))
moji=0 #文字の追加数
for i in range(q):
    if info[i][0]=="1":
        num+=1
    else:
        if info[i][1]=="2":
            rev.append([info[i][2],2,num]) #末尾
        else:
            rev.append([info[i][2],1,num])
        moji+=1
if num%2==1:
    ss.reverse()
#print(rev)
#print(num)
for i in range(moji):
    now=rev[i][1]
    if (num-rev[i][2])%2==1: #これが奇数だと２なら先頭になる
        #print("change")
        if rev[i][1]==2:
            rev[i][1]=1
        else:
            rev[i][1]=2
    if rev[i][1]==2:
        ss.append(rev[i][0])
    else:
        ss.appendleft(rev[i][0])
print("".join(ss))
#print(rev)

Submission status: Accepted

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

q = int(input())
s = deque(s)
r = False
for _ in range(q):
    cmd = input()
    if cmd[0] == '1':
        r = not r
    else:
        cmd, f, c = cmd.split()
        if (f == '2' and not r) or (f=='1' and r):
            s.append(c)
        else:
            s.appendleft(c)
if r:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

s = input()
d = deque()
header = 1
for i in s:
    d.append(i)
q = int(input())
for i in range(q):
        data = input().split()
        if len(data) == 1 :
            header *= -1
        else:
            f = int(data[1])
            c = data[2]
            if header == 1:
                if f == 1:
                    d.appendleft(c)
                else:
                    d.append(c)
            else:
                if f == 1:
                    d.append(c)
                else:
                    d.appendleft(c)
                
if header == -1:
    d.reverse()

s = list(d)
s = "".join(s)



print(s)

Submission status: Accepted

Solution code:
# TLE
import sys
import numpy as np
from collections import deque

# input = sys.stdin.readline

s = list(input())

n = int(input())

d = deque(s)

reverse = False
for ii in range(n):
    q = input()
#     print("".join(s))
    if q[0] == "1":
        reverse += 1
        reverse %= 2
#         s = s[::-1]
    else:
        t, f, c = q.split()
        if f == "1":
            if reverse:
                d.append(c)
            else:
                d.appendleft(c)
        else:
            if reverse:
                d.appendleft(c)
            else:
                d.append(c)
                
if reverse:
    d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque


def input_int_list():
    return [int(thing) for thing in input().split(" ")]


S=input()
Q=int(input())
Q_list=[input().split(" ") for i in range(Q)]
hantenness=1
left=[]
right=[]
ans=deque()
ans.extend(list(S))

for Query in Q_list:
    if Query[0]==str(1):
        hantenness*=-1
    else:
        C=Query[2]
        F=int(Query[1])
        if hantenness==1:
            if F==1:
                ans.appendleft(C)
            else:
                ans.append(C)
        else:
            if F==1:
                ans.append(C)
            else:
                ans.appendleft(C)

print("".join(ans) if hantenness==1 else "".join(ans)[::-1])

Submission status: Accepted

Solution code:
from collections import deque as D 
d = D()
s=input();q=int(input())
ans=0
d.extend(list(s))
for i in range(q):
  k=list(input().split())
  if len(k)==1:
    ans=(ans+1)%2
  else:
    x,y,z=k
    y=(int(y)-1+ans)%2
    d.append(z) if y else d.appendleft(z)
print("".join(list(d)[::(-1)**ans]))

Submission status: Accepted

Solution code:
import sys
from collections import deque

S = deque(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline().rstrip())
flip = False

for i in range(Q):
    Que = sys.stdin.readline().rstrip()
    if Que == "1":
        flip = not(flip)
    else:
        Query = Que.split()
        F = Query[1]
        if (F == "1") ^ flip:
            S.appendleft(Query[2])
        else:
            S.append(Query[2])
print("".join(S) if not flip else "".join(S)[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

que = deque()

for s in S:
    que.append(s)

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

    if query[0] == "1":
        swi ^= 1
    else:
        if query[1] == "1":
            if swi == 0:
                que.appendleft(query[2])
            else:
                que.append(query[2])
        else:
            if swi == 0:
                que.append(query[2])
            else:
                que.appendleft(query[2])

if swi == 1:
    que.reverse()

print("".join(list(que)))

Submission status: Accepted

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

top=""
btm=""

iturn=0
for iq in range(1,q+1):
	t=input().split()
	if t[0]=='1':
		iturn=iturn+1   	
	else:
		iturn2=iturn%2
		if (t[1]=='1' and iturn2==0) or (t[1]=='2' and iturn2==1):
#			s=t[2]+s
#			s='{}{}'.format(t[2],s)
			top=t[2]+top
		else:
#			s=s+t[2]
#			s='{}{}'.format(s,t[2])
			btm=btm+t[2]

if iturn%2==0:
	s=top+s+btm
else:
	s=btm[::-1]+s[::-1]+top[::-1]
print (s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
head = []
tail = []
state = 1

for i in range(q):
    query = input().split()
    if len(query) == 1:
        state *= -1

    elif state == 1:
        if query[1] == "1":
            head.append(query[2])
        else:
            tail.append(query[2])
    else:
        if query[1] == "1":
            tail.append(query[2])
        else:
            head.append(query[2])

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

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
flip = 0
d = deque(list(s))
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flip ^= 1
    else:
        f = int(query[1]) - 1
        c = query[2]
        if f ^ flip == 1:
            d.append(c)
        else:
            d.appendleft(c)
if flip:
    d.reverse()
print(''.join(d))

Submission status: Accepted

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

S = input()
Q = int(input())
d.extend(S)




front = True

for i in range(Q):
    inp = input().split()
    if inp[0] == '1':
        front = not front
    else:
        F = int(inp[1])
        C = inp[2]
        if F == 1:
            if front:
                d.appendleft(C)
            else:
                d.append(C)
        elif F == 2:
            if front:
                d.append(C)
            else:
                d.appendleft(C)

if not front:
    d.reverse()

d_list = list(d)
str = "".join(d_list)

print(str)

Submission status: Accepted

Solution code:
from collections import deque
 
S = input()
L = deque([])
for c in S:
    L.append(c)
Q = int(input())
flag = 1
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag *= -1
    else:
        F = q[1]
        if F == "1":
            if flag == 1:
                L.appendleft(q[2])
            else:
                L.append(q[2])
        else:
            if flag == 1:
                L.append(q[2])
            else:
                L.appendleft(q[2])
if flag == 1:
    print("".join(map(str, L)))
else:
    L.reverse()
    print("".join(map(str, L)))

Submission status: Accepted

Solution code:
from collections import deque

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

reverse = 1 #reverse == 1 -> 正常, reverse == -1 -> 逆から

d = deque(S)

for i in range(Q):
    Query_tmp = list(map(str, input().split()))
    
    if Query_tmp[0] == "1":
        reverse *= -1
        # S = S[::-1]
    else:
        Ci = Query_tmp[2]

        if Query_tmp[1] == "1":
            if reverse == 1:
                d.appendleft(Ci)
            else:
                rev_Ci = Ci[::-1]
                d.append(rev_Ci)
        else:
            if reverse == 1:
                d.append(Ci)
            else:
                rev_Ci = Ci[::-1]
                d.appendleft(rev_Ci)
    
    # if reverse == 1:
    #     print(S)
    # else:
    #     print(S[::-1])

if reverse == -1:
    d.reverse()

print("".join(d))

Submission status: Accepted

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

#output
flip = 0
cap = ""
tail = ""

for i in range(Q):
    if int(P[i][0]) == 1:
        flip += 1
    else:
        n = int(P[i][1])
        v = P[i][2]
        if flip % 2 == 0:
            if n == 1:
                cap = v + cap
            else:
                tail = tail + v
        else:
            if n == 1:
                tail = tail + v
            else:
                cap = v + cap

answer = ""
if flip % 2 == 0:
    answer = cap + S + tail
else:
    answer = tail[-1::-1] + S[-1::-1] + cap[-1::-1]

print(answer)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
lis = [list(input().split()) for _ in range(q)]
rev = 0
z = 0

for i in lis:
    if i[0] == "2":
        if (rev + int(i[1])) % 2 == 1:
            s.appendleft(i[2])
        else:
            s.append(i[2])
        z = 1
    else:
        if z == 1:
            rev += 1
        
if rev % 2 == 1:
    s = reversed(s)
print("".join(s))

Submission status: Accepted

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

k = 0
fo = ''
re = ''
for i in range(q):
    if quer[i][0] == '1':
        k += 1
    else:
        if quer[i][1] == '1':
            if k % 2 == 0:
                re = re + quer[i][2]
            else:
                fo = fo + quer[i][2]
        else:
            if k % 2 == 0:
                fo = fo + quer[i][2]
            else:
                re = re + quer[i][2]

if k % 2 ==0:
    print( ''.join(list(reversed(re))) + s + fo)
else:
    print(''.join(list(reversed(fo))) + ''.join(list(reversed(s))) + re)

Submission status: Accepted

Solution code:
from queue import deque
S=deque(input())
Q=int(input())
s=0
for i in range(Q):
    q=list(input().split())
    if int(q[0])==1:
        if s==0:
            s=1
        else:
            s=0
    else:
        T=int(q[1])
        if s==0:
            if T==2:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if T==1:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if s==1:
    S.reverse()
S="".join(S)
print(S)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(list(input()))
q = int(input())
rev = False
for _ in range(q):
    qr = list(map(str, input().split()))
    if qr[0] == '1':
        rev = 1 - rev
    else:
        if qr[1] == '1':
            if rev: s.append(qr[2])
            else  : s.appendleft(qr[2])
        else:
            if rev: s.appendleft(qr[2])
            else  : s.append(qr[2])
if rev:
    s.reverse()
    print(''.join(s))
else:
    print(''.join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a, b = "", ""
rev = False
for _ in range(q):
    qr = list(map(str, input().split()))
    if qr[0] == '1':
        rev = 1 - rev
    else:
        if qr[1] == '1':
            if rev: b += qr[2]
            else  : a += qr[2]
        else:
            if rev: a += qr[2]
            else  : b += qr[2]
if rev:
    print(b[::-1] + s[::-1] + a)
else:
    print(a[::-1] + s + b)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
lis = [list(input().split()) for _ in range(q)]
rev = 0
z = 0

for i in lis:
    if i[0] == "2":
        if (rev + int(i[1])) % 2 == 1:
            s.appendleft(i[2])
        else:
            s.append(i[2])
        z = 1
    else:
        if z == 1:
            rev += 1
        
if rev % 2 == 1:
    s = reversed(s)
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
L = deque([])
for c in S:
    L.append(c)
Q = int(input())
flag = 1
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag *= -1
    else:
        F = q[1]
        if F == "1":
            if flag == 1:
                L.appendleft(q[2])
            else:
                L.append(q[2])
        else:
            if flag == 1:
                L.append(q[2])
            else:
                L.appendleft(q[2])
if flag == 1:
    print("".join(map(str, L)))
else:
    L.reverse()
    print("".join(map(str, L)))

Submission status: Accepted

Solution code:
s = input()
top = ''
n = int(input())
isReverse = False
for i in range(n):
    Q = input().split()
    if(Q[0] == '1'):
        isReverse = not isReverse
    else:
        if Q[1] == '1':
            if isReverse: s += Q[2]
            else: top += Q[2]
        else:
            if isReverse: top += Q[2]
            else: s += Q[2]
if isReverse: s = s[::-1] + top
else: s = top[::-1] + s
print(s)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
Q = int(input())
s = deque([s])
rev = 1
for i in range(Q):
    tmp = list(input().split())
    if len(tmp)==1:
        rev *= -1
    else:
        f,c = tmp[1], tmp[2]
        if rev > 0:
            if f=='1':
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f=='1':
                s.append(c)
            else:
                s.appendleft(c)
s = ''.join(list(s))
print(s if rev > 0 else s[::-1])

Submission status: Accepted

Solution code:
s = input()
q_num = int(input())
head = []
tail = []
flip = 0
for i in range(q_num):
    q = tuple(input().split())
    if q[0] == '1':
        flip ^= 1
    else:
        f = int(q[1]) - 1
        c = q[2]
        if f ^ flip == 0:
            head.append(c)
        else:
            tail.append(c)
if flip:
    head, tail = tail, head
    s = s[::-1]
head = ''.join(head)
tail = ''.join(tail)
print(head[::-1] + s + tail)

Submission status: Accepted

Solution code:
s = input()
top = ''
n = int(input())
isReverse = False
for i in range(n):
    Q = input().split()
    if(Q[0] == '1'):
        isReverse = not isReverse
    else:
        if Q[1] == '1':
            if isReverse: s += Q[2]
            else: top += Q[2]
        else:
            if isReverse: top += Q[2]
            else: s += Q[2]
if isReverse: s = s[::-1] + top
else: s = top[::-1] + s
print(s)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
q = int(input())
Q = [list(map(str, input().split())) for _ in range(q)]

flip = False

for qi in Q:
    if int(qi[0]) == 1:
      flip = not flip
    else:
      f = int(qi[1]) 
      c = qi[2]
      if flip:
        f = 3-f    
      if f == 1:
        S.appendleft(c)
      else:
        S.append(c)

if flip:
    S.reverse()
print(''.join(S))