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 = str(input())
Q = int(input())

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

x, y = S, ""
for ls in qs:
    if ls[0] == "1":
        x, y = y, x
    else:
        if ls[1] == "1":
            y += ls[2]
        else:
            x += ls[2]
            
y = ''.join(list(reversed(y)))
print(y+x)

Submission status: Accepted

Solution code:
def main():
  s = input()
  q = int(input())
  de = collections.deque(s)
  rev = True
  for _ in range(q):
    query = input().split()
    if len(query) == 1:
      rev = False if rev else True
      continue
    typ, f, c = query
    if rev:
      if f == '1':
        de.appendleft(c)
      if f == '2':
        de.append(c)
    else:
      if f == '1':
        de.append(c)
      if f == '2':
        de.appendleft(c)
  ans = list(de)
  if rev is False:
    ans.reverse()
  print(''.join(ans))
      

import math
import fractions
import itertools
import collections
main()

Submission status: Accepted

Solution code:
S=input()
# Python3 program to Split string into characters 
def split(word): 
    return [char for char in word]  
def convert(s): 
  
    # initialization of string to "" 
    new = "" 
  
    # traverse in the string  
    for x in s: 
        new += x  
  
    # return string  
    return new 
      
      
# Driver code 
word = S
splits=split(S)
Q=int(input())
flipcounter=0
prestring=[]
poststring=[]
for i in range(0,Q):
  inputforround=input()
  if inputforround=='1':
    flipcounter=flipcounter+1
  else:
    T,F,C=map(str,inputforround.split())
    if (int(F)+flipcounter)%2==1:
      prestring.append(C)
    else:
      poststring.append(C)

prestring.reverse()
for j in splits:
  prestring.append(j)
for k in poststring:
  prestring.append(k)
if flipcounter%2==1:
  prestring.reverse()
ans=convert(prestring)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys
input=sys.stdin.readline
S=input().splitlines()[0]
Q = int(input())
qs = [input().split() for _ in range(Q)]
flipcnt = 0
S =deque(S)
for q in qs:
    if len(q) == 1:
        flipcnt += 1
        continue
    else:
        if (int(q[1]) + flipcnt) % 2 == 1:
            #前
            S.appendleft(q[2])
        else:
            S.append(q[2])
    #print(S)

if flipcnt % 2 == 1:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    s = deque(s)
    r = 1
    for q in qs:
        if len(q) == 1:
            r = r * -1

        else:
            if q[1] == '1':
                if r == 1:
                    s.appendleft(q[2])
                else:
                    s.append(q[2])
            else:
                if r == 1:
                    s.append(q[2])
                else:
                    s.appendleft(q[2])

    if r == 1:
        print(''.join(s))
    else:
        s.reverse()
        print(''.join(s))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

deque = deque([c for c in S])
flag = 0

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

  T = int(pack[0])
  if T == 1:
    flag = flag ^ 1

  if T == 2:
    F, C = int(pack[1]), pack[2]
    if F == 1:
      if flag:
        deque.append(C)
      else:
        # 最初に追加するパターン
        deque.appendleft(C)
    if F == 2:
      if flag:
        deque.appendleft(C)
      else:
        # 最後尾に追加するパターン
        deque.append(C)

print("".join(reversed(deque) if flag else deque))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())
reverse = True
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:
        right = query[1] == '1'
        if right == reverse:
            S.appendleft(query[2])
        else:
            S.append(query[2])
if not reverse:
    print(''.join(list(reversed(S))))
else:
    print(''.join(list(S)))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
flag = True
for i in range(q):
    x = input()
    if x == '1':
        flag = not flag
    else :
        if x[2] == '1' and flag :
            s.appendleft(x[4])
        elif x[2] == '2' and not flag :
            s.appendleft(x[4])
        else :
            s.append(x[4])
if flag:
    for i in s :
        print(i, end='')
else :
    for i in range(1, len(s)+1):
        print(s[-i], end='')
print('')

Submission status: Accepted

Solution code:
from collections import deque


reverse = False

string = deque(input().rstrip())
operate_times = int(input().rstrip())

for i in range(operate_times):
    row = input().rstrip()
    if row[0]=='1':
        if reverse:
            reverse = False
        else:
            reverse = True
    
    if row[0]=='2':
        row = row.split()
        head_or_tail = int(row[1])
        add_string = row[2]

        if reverse:
            if head_or_tail==1:
                string.append(add_string)
            else:
                string.appendleft(add_string)
        
        else:
            if head_or_tail==2:
                string.append(add_string)
            else:
                string.appendleft(add_string)


answer_string = ''
if reverse:
    for _ in range(len(string)):
        right = string.pop()
        answer_string+=right
    print(answer_string)
else:
    print(''.join(string))

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = input()
    Q = int(input())

    S = deque(list(S))
    rev = 1  # 正方向: 1, 逆方向: -1
    for _ in range(Q):
        input_line = input().split()
        if len(input_line) == 1:
            rev *= -1
        else:
            _, f, c = input_line
            if f == '1':
                if rev == 1:
                    S.appendleft(c)
                else:
                    S.append(c)
            else:
                if rev == 1:
                    S.append(c)
                else:
                    S.appendleft(c)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
head = 1
d = deque([S])

for _ in range(Q):
    q = input().split()
    if q[0]=='1':
        head = head*-1
    else:
        if q[1]=='1':
            h = head*1
        else:
            h = head*-1
        
        if h==1:
            d.appendleft(q[2])
        else:
            d.append(q[2])
ans = "".join(list(d))
if head == -1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
import collections
S = list(input())
Q = int(input())

ans = collections.deque(S)
rev = False

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

if rev:
    print("".join(reversed(ans)))
else:
    print("".join(ans))

Submission status: Accepted

Solution code:
from collections import deque
d = deque(list(input()))
rev = False
for _ in range(int(input())):
    q = list(input().split(" "))
    t = int(q[0])
    if t == 1:
        if rev:
            rev = False
        else:
            rev = True
    else:
        if int(q[1]) == 1:
            if rev:
                d.append(q[2])
            else:
                d.appendleft(q[2])
        elif int(q[1]) == 2:
            if rev:
                d.appendleft(q[2])
            else:
                d.append(q[2])
if rev:
    d.reverse()
for res in d:
    print(res, end="")

Submission status: Accepted

Solution code:
# D

from collections import deque
S = deque(input())
Q = int(input())

#q = []
#for _ in range(Q):
#  q += [input().split(" ")]

hc = 0

for i in range(Q):
  #qi = q[i]
  qi = input().split(" ")
  if len(qi)==1:
    #S = S[::-1]
    if hc==0:
      hc = 1
    else:
      hc = 0
  else:
    fi = int(qi[1])
    ci = qi[2]
    if hc ==0:
      if fi==1:
        S.appendleft(ci)
      else:
        S.append(ci)
    else:
      if fi==1:
        S.append(ci)
      else:
        S.appendleft(ci)

S = list(S)
if hc == 1:
  S = S[::-1]
print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
quer = [input().split() for _ in range(Q)]

hanten = 0
t = 0
d = deque(S)
for i in quer:
    quary = i
    if quary[0]=='1':
        #S = S[::-1]
        hanten = hanten ^ 1
    else:
        t = (int(quary[1])-1) ^ hanten
        if t:
            #S = S + quary[2]
            d.append(quary[2])
        else:
            #S = quary[2] + S
            d.appendleft(quary[2])
    #print(S)
if hanten:
    d.reverse()
s = ''.join(list(d))
print(s)

Submission status: Accepted

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

flag = 1
front = ""
back = ""

for i in range(q):
    t = query[i]
    if t[0] == "1":
        flag *= -1
    else:
        dest = (1 if t[2] == "1" else -1) 
        tmp = t[4]
        if flag*dest == 1:
            front += tmp
        else:
            back += tmp
            
if flag == 1:
    s = front[::-1] + s + back
    print(s)
else:
    s = back[::-1] + s[::-1] + front
    print(s)

Submission status: Accepted

Solution code:
S= input()
q=int(input())
h=0
a=[]
for i in range(q):
    a.append(input())
t=""
w=""
for i in range(1,q+1):
    if a[q-i][0]=="2":
        if a[q - i][2] == "1":
            if h%2==0:
                t=a[q-i][4]+t
            else:
                w =  a[q-i][4]+w
        else:
            if h%2==0:
                w=a[q-i][4]+w
            else:
                t = a[q - i][4]+t
    else:
        h+=1
if h%2==0:
    print(t[::-1]+S+w)
else:
    print(t[::-1]+S[::-1]+w)

Submission status: Accepted

Solution code:
import numpy as np
import sys

input = sys.stdin.readline

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

left = []
right = []
rev = False

for i in range(Q):
    l = input().split()
    if l[0] == '1':
        rev = not rev
    else:
        if (l[1] == '1' and not rev) or (l[1] == '2' and rev):
            left.append(l[2])
        else:
            right.append(l[2])

S = ''.join(left[::-1]) + S + ''.join(right)

if rev:
    print(S[::-1].replace('\n',''))
else:
    print(S.replace('\n',''))

Submission status: Accepted

Solution code:
S= input()
q=int(input())
h=0
a=[]
for i in range(q):
    a.append(input())
t=""
w=""
for i in range(1,q+1):
    if a[q-i][0]=="2":
        if a[q - i][2] == "1":
            if h%2==0:
                t=t+a[q-i][4]
            else:
                w =  w+a[q - i][4]
        else:
            if h%2==0:
                w=w+a[q-i][4]
            else:
                t = t + a[q - i][4]
    else:
        h+=1
if h%2==0:
    print(t+S+w[::-1])
else:
    print(t+S[::-1]+w[::-1])

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
left = []
right = []
rev = False
for i in range(q):
    x = list(input())
    if len(x) == 1:
        rev = not rev
        #s.reverse()
    else:
        if (x[2] == "1" and not rev) or (x[2] == "2" and rev):
            left.append(x[4])
        else:
            right.append(x[4])
ans = "".join(left[::-1]) + "".join(s) + "".join(right)
if rev:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

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

def main():
    S = input()[:-1]
    Q = int(input())
    li = [[] for _ in range(2)]
    now = 0
    for _ in range(Q):
        x = input()
        if(x[0] == '2'):
            if(x[2]=='1'):
                li[1-now].append(x[4])
            else:
                li[now].append(x[4])
        else:
            now = 1-now

    s = ''.join(li[1-now][::-1]) + S[::-(now*2-1)] + ''.join(li[now])
    print(s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
left = []
right = []
rev = False
for i in range(Q):
  l = input().split()
  if l[0] == '1':
    rev = not rev
  else:
    if (l[1] == '1' and not rev) or (l[1] == '2' and rev):
      left.append(l[2])
    else:
      right.append(l[2])
S = ''.join(left[::-1]) + S + ''.join(right)
if rev:
  print(S[::-1])
else:
  print(S)

Submission status: Accepted

Solution code:
from collections import deque
 
s = deque(input())
q = int(input())
 
direction = 0
for _ in range(q):
    a = input()
    if a[0]=='1':
        direction+=1
    else:
        t,f,c = a.split()
        if f=='1':
            if direction%2==0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if direction%2==0:
                s.append(c)
            else:
                s.appendleft(c)
 
if direction%2==1:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

direction = 0
for _ in range(q):
    a = input()
    if a[0]=='1':
        direction+=1
    else:
        t,f,c = a.split()
        if f=='1':
            if direction%2==0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if direction%2==0:
                s.append(c)
            else:
                s.appendleft(c)

if direction%2==1:
    s.reverse()
print(''.join(s))

Submission status: Accepted

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

Query=[]
for i in range(Q):
    Query.append(list(input().split()))

N=len(S)//2
S_left_reverse=S[:N+1][::-1]
S_right=S[N+1:]
#print(S_left_reverse,S_right)


check=1
for j in range(Q):
    if len(Query[j])==1:
        check*=-1
        #print(S_left_reverse,S_right)
    else:
        if Query[j][1]=='1':
            if check==1:
                S_left_reverse+=Query[j][2]
                #print(S_left_reverse,S_right)
            else:
                S_right+=Query[j][2]
                #print(S_left_reverse,S_right)
        else:
            if check==1:
                S_right+=Query[j][2]
                #print(S_left_reverse,S_right)
            else:
                S_left_reverse+=Query[j][2]
                #print(S_left_reverse,S_right)


ans=S_left_reverse[::-1]+S_right
if check==1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
 
S = deque(readline().rstrip().decode())
Q = int(readline())
query = readlines()
 
rev = False
for q in query:
    if q[0] == ord('1'):
        rev = not rev
    else:
        _, f, c = q.decode().split()
        f = int(f)
        if rev ^ (f == 1):
            S.appendleft(c)
        else:
            S.append(c)
 
T = ''.join(S)
if rev:
    T = T[::-1]
print(T)

Submission status: Accepted

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

# deq=deque()
# for s in S:
#     deq.append(s)

rev=False
front=""
last=""
# ans+=S

for _ in range(Q):
    query=input().split()
    if query[0]=="1":
        rev = not rev
        # print(rev)
    else:
        if rev:
            query[1]=str(3-int(query[1]))
        if query[1]=="1":
            # deq.appendleft(query[2])
            front="".join([query[2],front])
        else:
            # deq.append(query[2])
            last="".join([last,query[2]])

# if rev:
#     deq.reverse()
# # ans=""
# for i in range(len(deq)):
#     ans+=deq[i]

if not rev:
    print(front+S+last)
else:
    print(last[::-1]+S[::-1]+front[::-1])

Submission status: Accepted

Solution code:

from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from functools import reduce

from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import statistics
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm(a, b):
  return a*b//gcd(a, b)

def main():
  S = input()
  Q = int(input())
  TFC = list(list(input().split()) for _ in range(Q))
  rev = 0
  q = deque(S)
  for i in range(Q):
    if TFC[i][0] == '1':
      rev ^= 1
    else:
      if TFC[i][1] == '1':
        if rev == 0:
          q.appendleft(TFC[i][2])
        else:
          q.append(TFC[i][2])
      else:
        if rev == 0:
          q.append(TFC[i][2])
        else:
          q.appendleft(TFC[i][2])
  ans = ''.join(q)
  print(ans if rev==0 else ans[::-1])
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = [input().split() for i in range(Q)]
countA,countB = 0,0
latter = []
former = []
for i in range(Q):
    if(Query[Q-i-1][0] == "1"):
        countA += 1
        countB += 1
    else:
        t = lambda x:1 if x=="1" else 0
        if((t(Query[Q-i-1][1])+countB)%2 == 1):
            former.append(Query[Q-i-1][2])
        else:
            latter.append(Query[Q-i-1][2])

for i in former:
    print(i,end="")
if(countA%2 == 0):
    print(S,end="")
else:
    for j in range(len(S)):
        print(S[len(S)-j-1],end="")
for i in range(len(latter)):
    print(latter[len(latter)-i-1],end="")

Submission status: Accepted

Solution code:
from collections import deque

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

reverse = False
for i in query:
    if len(i) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        if reverse:
            if i[1] == '1':
                s.append(i[2])
            else:
                s.appendleft(i[2])
        else:
            if i[1] == '1':
                s.appendleft(i[2])
            else:
                s.append(i[2])
if reverse:
    s = reversed(s)
    s = ''.join(s)
    print(s)
else:
    s = ''.join(s)
    print(s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
top_str = ""
last_str = ""
s_parity = 0
for i in range(Q):
    q_list=input().split(" ")
    if q_list[0] == "1":
        s_parity += 1
    elif (s_parity%2) == 0:
        if q_list[1] == "1":
            top_str = q_list[2] + top_str
        else:
            last_str = last_str + q_list[2]
    else:
        if  q_list[1] == "1":
            last_str = last_str + q_list[2]
        else:
            top_str = q_list[2] + top_str


if s_parity % 2 ==1:
    S = S[::-1]
    S = last_str[::-1] + S + top_str[::-1]
else :
    S = top_str + S + last_str
print(S)

Submission status: Accepted

Solution code:
from collections import deque
a = list(input())
Q = int(input())
d = deque(a)
ans = 0
for i in range(Q):
  b = list(input().split())
  if len(b) == 1:
    ans += 1
  elif b[1] == "1" and ans % 2 == 0:
    d.appendleft(b[2])
  elif b[1] == "1" and ans % 2 == 1:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 0:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 1:
    d.appendleft(b[2])
if ans % 2 == 1:
  d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
from collections import deque
a = list(input())
Q = int(input())
d = deque(a)
ans = 1
for i in range(Q):
  b = list(input().split())
  if len(b) == 1:
    ans += 1
  elif b[1] == "1" and ans % 2 == 1:
    d.appendleft(b[2])
  elif b[1] == "1" and ans % 2 == 0:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 1:
    d.append(b[2])
  elif b[1] == "2" and ans % 2 == 0:
    d.appendleft(b[2])
if ans % 2 == 0:
  d.reverse()
print("".join(d))

Submission status: Accepted

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from collections import deque 
s=s0()
n=n0()
q=s2(n)

d1=deque()
d2=deque()

f=False
for a in q:
    if a[0]=="1":
        f=not(f)
    else:
        if a[2]=="1":
            if f==False:
                d1.appendleft(a[4])
            else:
                d2.append(a[4])                
        else:
            if f==False:
                d2.append(a[4])
            else:
                d1.appendleft(a[4])
ss=''.join(list(d1))+s+''.join(list(d2))
print(ss[::-1] if f else ss)

Submission status: Accepted

Solution code:
import math
import random
import sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from functools import lru_cache, reduce
from heapq import heapify, heappop, heappush
from itertools import (chain, combinations, combinations_with_replacement,
                       permutations)

MAX_INT = 2**62-1
sys.setrecursionlimit(10**7)

input = sys.stdin.readline


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


def main():
    s = input().strip()
    q = int(input())
    queue = deque(s)
    is_reversed = 0

    for _ in range(q):
        t, *x = input().strip().split()
        # print(t, x, queue)
        if t == '1':
            is_reversed ^= 1
        else:
            where, c = x
            where = int(where) & 1
            if where ^ is_reversed:
                queue.appendleft(c)
            else:
                queue.append(c)
    if is_reversed:
        queue.reverse()

    print(''.join(queue))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
 
count = 0
s_f = ""
for i in range(q):
    q_i = input()
    if len(q_i) == 1:
        count += 1
    else:
        lis = [i for i in q_i.split()]
        x = count
        x += int(lis[1])
        if x % 2 == 0:
            s += lis[2]
        else:
            s_f += lis[2]

ans = ""
if count % 2 == 1:
    for i in range(len(s)):
        ans += s[-i-1]
    ans += s_f
else:
    s_f_ = ""
    for i in range(len(s_f)):
        s_f_ += s_f[-i-1]
    ans = s_f_ + s
print(ans)

Submission status: Accepted

Solution code:

S = str(input())
Q = int(input())
query=[]
a=""
b=""
# for line in range(Q):
# 	query.append(list(input().split()))
t=True

for i in range(Q):
	q=list(input().split())
	if int(q[0])==1:
		t= not t
	else:
		if int(q[1])==1:
			if t:
				a=q[2]+a
			else:
				b=b+q[2]

		else:
			if t:
				b=b+q[2]
			else:
				a=q[2]+a

if t:
	print(a+S+b)
else:
	print(b[::-1]+S[::-1]+a[::-1])

Submission status: Accepted

Solution code:
from collections import deque
def main():
  s = deque(list(input()))
  q = int(input())
  f = 0
  for _ in range(q):
    l = input()
    if l == "1":
      f ^= 1
    else:
      a, b, c = l.split()
      if f ^ (b=="1"):
        s.appendleft(c)
      else:
        s.append(c)
  if f:
    ans = list(s)[::-1]
  else:
    ans = list(s)
  print(*ans, sep="")
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
s=input()
n=int(input())
t=True
a=str()
b=str()
for i in range(n):
    q=input().split()
    if(len(q)==1):
        t=not t
        continue
    if(int(q[1])==1):
        if(t):a=q[2]+a
        else:b=b+q[2]
    if(int(q[1])==2):
        if(t):b=b+q[2]
        else:a=q[2]+a
if(t):print(a+s+b)
else:print(b[::-1]+s[::-1]+a[::-1])

Submission status: Accepted

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

head = ''
back = ''
direction = 1

for i in range(Q):
    q = list(map(str, input().split())) 
    if q[0] == "1":
        direction *= (-1)
        #S = S[::-1]
    else:
        if q[1] == "1":
            if direction == 1:
                head = head + q[2]
            else:
                back = back + q[2]
        else:
            if direction == 1:
                back = back + q[2]
            else:
                head = head + q[2]

if direction == 1:
    print(head[::-1]+S+back)
else:
    print(back[::-1]+S[::-1]+head)

Submission status: Accepted

Solution code:
from collections import deque

S, inv = deque(input()), False
Q = int(input())
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        inv = not inv
        continue
    F, C = q[1], q[2]
    if (F == '1') ^ inv:
        S.appendleft(C)
    else:
        S.append(C)
print(''.join(S)[::-1] if inv else ''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = input().split()
sd = deque()
for i in s:
    sd.append(i)
N = int(input())
S = []
order = 1
for _ in range(N):
    res = input().split()
    if res[0] == "1":
        order = order * - 1
    elif res[1] == "1":
        if order == 1:
            sd.appendleft(res[2])
        else:
            sd.append(res[2])
    else:
        if order == 1:
            sd.append(res[2])
        else:
            sd.appendleft(res[2])

sd_str="".join(list(sd))
if order == -1:
    print(sd_str[::-1])
else:
    print(sd_str)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = input()
    Q = int(input())
    q = deque(S)
    cnt = 0
    for _ in range(Q):
        query = list(input().split())
        if query[0] == '1':
            cnt += 1
        else:
            if query[1] == '1':
                if cnt % 2 == 0:
                    q.appendleft(query[2])
                else:
                    q.append(query[2])
            else:
                if cnt % 2 == 1:
                    q.appendleft(query[2])
                else:
                    q.append(query[2])

    if cnt % 2 == 0:
        print(*q, sep ="")
    else:
        q.reverse()
        print(*q, sep ="")







if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

Sq = deque(S)


flg = True
for i in range(Q):
    que = input()
    if que == "1":
        if flg:
            flg = False
        else:
            flg = True
    else:
        j, F, C = que.split()
        if int(F) == 1:
            if flg:
                Sq.appendleft(C)
            else:
                Sq.append(C)
        else:
            if flg:
                Sq.append(C)
            else:
                Sq.appendleft(C)

if flg:
    for i in Sq:
        print(i, end="")
else:
    Sq.reverse()
    for i in Sq:
        print(i, end="")

Submission status: Accepted

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

head = ''
back = ''
flg = 1
for _ in range(q):
    i = list(map(str, input().split()))
    if i[0] == '1':
        #head, back = back, head
        flg *= -1
    else:
        if i[1] == '1':
            if flg == 1:
                head = head + i[2]
            else:
                back = back + i[2]
        else:
            if flg == 1:
                back = back + i[2]
            else:
                head = head+ i[2]

if flg == 1:
    print(head[::-1]+s+back)
else:
    print(back[::-1]+s[::-1]+head)

Submission status: Accepted

Solution code:
from collections import deque

deq=deque()
k = input()
for i in range(len(k)):
	deq.append(k[i])
q=int(input())
x=True
for i in range(q):
	a=input()
	if a[0]=="1":
		x = not x
	elif a[2]=="1":
		if x==True:
			deq.appendleft(a[4])
		else:
			deq.append(a[4])
	else:
		if x==True:
			deq.append(a[4])
		else:
			deq.appendleft(a[4])
if x==True:
	c="".join(deq)
else:
	deq.reverse()
	c="".join(deq)
print(c)

Submission status: Accepted

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

rev = False
head = []
tail = []

for i in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split()
        if (not rev and f == "1") or (rev and f == "2"):
            head.append(c)
        else:
            tail.append(c)

if not rev:
    res = head[::-1] + s + tail
else:
    res = tail[::-1] + s[::-1] + head

print(*res, sep="")

Submission status: Accepted

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

order = True
for i in query:
    if i[0] == "1":
        order = not order
    elif i[1] == "1":
        if order:
            s.appendleft(i[2])
        else:
            s.append(i[2])
    else:
        if order:
            s.append(i[2])
        else:
            s.appendleft(i[2])

if not order:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
S=list(input())
Q=int(input())
Rev=True
front=[]
back=[]
for i in range(Q):
    a=input()
    if a=='1':
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == '1':
            if Rev:
                front.append(x)
            else:
                back.append(x)
        else:
            if Rev:
                back.append(x)
            else:
                front.append(x)
if Rev:
    front.reverse()
    front.extend(S)
    front.extend(back)
    print("".join(front))
else:
    back.reverse()
    S.reverse()
    back.extend(S)
    back.extend(front)
    print("".join(back))