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 = int(input())
bs = ''
fs = ''
hanten = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        hanten += 1
    else:
        if query[1] == '1':
            if hanten % 2 == 0:
                fs += query[2]
            else:
                bs += query[2]
        else:
            if hanten % 2 == 0:
                bs += query[2]
            else:
                fs += query[2]
if hanten % 2 == 0:
    print(fs[::-1]+S+bs)
else:
    print(bs[::-1]+S[::-1]+fs)

Submission status: Accepted

Solution code:
from collections import deque
que1 = deque([])
que2 = deque([])
s = input()
q = int(input())
rev = 1
for i in range(q):
  t = input()
  
  if t == '1':
    rev *= -1
  else:
    x,y,sr = t.split()
    if (rev == 1 and y == '2') or (rev == -1 and y == '1'):
      que2.append(sr)
    else:
      que1.appendleft(sr)

s = ''.join(que1+deque([s])+que2)     
if rev == -1:
  s = s[::-1]
  
print(s)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

ans = collections.deque(ns())
q = ni()
now = 0
for i in range(q):
    query = ns().split(' ')
    if query[0] == '1':
        now = 1 - now
    else:
        f, c = query[1], query[2]
        if f == '1':
            if now == 1:
                ans.append(c)
            else:
                ans.appendleft(c)
        else:
            if now != 1:
                ans.append(c)
            else:
                ans.appendleft(c)

if now == 1:
    for i in range(len(ans)):
        print(ans[-(i+1)], end="")
else:
    for i in range(len(ans)):
        print(ans[i], end="")
print()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
inv = False
pr, su = deque(), deque()
for _ in range(q):
    query = input()
    if query[0] == '1':
        inv = not inv
    else:
        _, f, c = query.split()
        if inv:
            if f == '1': su.append(c)
            else: pr.append(c)
        else:
            if f == '1': pr.append(c)
            else: su.append(c)
ans = ''.join(pr)[::-1] + s + ''.join(su)
print(ans[::-1] if inv else ans)

Submission status: Accepted

Solution code:
# ABC 158 D
from collections import deque

S=list(input())
q=deque(S)
Q=int(input())
inv=0
for _ in [0]*Q:
    s=input()
    if s=='1':
        inv+=1
    else:
        t,f,c=s.split()
        if ((f=='1') ^ (inv%2==1)):
            q.appendleft(c)
        else:
            q.append(c)
S=[]
if inv%2:
    while q:
        S.append(q.pop())
else:
    while q:
        S.append(q.popleft())
    
print(*S,sep='')

Submission status: Accepted

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

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

        if query[0] == "1":
            s, t = t, s
        elif query[0] == "2":
            if query[1] == "1":
                t.append(query[2])
            elif query[1] == "2":
                s.append(query[2])

    print(*(t[::-1] + s), sep="")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    s = input()
    q = int(input())
    d = deque(s)
    is_reversed = False

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

        if query[0] == "1":
            if is_reversed:
                is_reversed = False
            else:
                is_reversed = True

        elif query[0] == "2":
            if query[1] == "1":
                if is_reversed:
                    d.append(query[2])
                else:
                    d.appendleft(query[2])
            elif query[1] == "2":
                if is_reversed:
                    d.appendleft(query[2])
                else:
                    d.append(query[2])

    if is_reversed:
        print(*(list(d)[::-1]), sep="")
    else:
        print(*list(d), sep="")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

rev=0

left=""
right=""

for i in range(q):
    
    x = list(map(str,input().split()))
    
    if int(x[0]) == 1:
        rev ^= 1
        
    else:
        if int(x[1])-1 ^ rev == 0:
            left = x[2] + left
        else:
            right = right + x[2]
            
ans = left + s + right

if rev==1:
    ans=ans[::-1]
        
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
reverse, front, tail = False, [], []

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

    if len(query) == 1:
        reverse ^= 1

    elif len(query) == 3:
        f, c = int(query[1]), query[2]
        f -= 1

        if f ^ reverse:
            tail.append(c)
        else:
            front.append(c)

if reverse:
    print(''.join((front[::-1] + S + tail)[::-1]))
else:
    print(''.join(front[::-1] + S + tail))

Submission status: Accepted

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

from collections import deque
s = deque(s)
count = 0
for i in range(q):
    query = input()
    if query == "1":
        count += 1
    else:
        t,f,c = query.split()
        if f == "1":
            if count % 2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if count % 2 == 0:
                s.append(c)
            else:
                s.appendleft(c)
s = "".join(s)
if count % 2 == 0:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

flag = True
for _ in range(q):
    temp = input().split()
    if len(temp) == 1:
        flag = not flag
    else:
        if flag:
            if temp[1] == "1":
                s.appendleft(temp[2])
            else:
                s.append(temp[2])
        else:
            if temp[1] == "1":
                s.append(temp[2])
            else:
                s.appendleft(temp[2])

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

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    s = deque(input())
    q = int(input())

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Submission status: Accepted

Solution code:
from collections import deque

def main():

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

    flag = 0
    query = []

    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            flag += 1
        else:
            query.append(temp + [flag%2])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s.appendleft(q[2])
            else:
                s.append(q[2])
        else:
            if q[1] == "1":
                s.append(q[2])
            else:
                s.appendleft(q[2])

    if flag % 2 == 1:
        print("".join(list(s)[::-1]))
    else:
        print("".join(s))

main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 14:42:59 2020

@author: liang
"""

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

reverse = False

for i in range(Q):
    t = input().split()
    if int(t[0]) == 1:
        if reverse == False:
            reverse = True
        else:
            reverse = False
    else:
        F = int(t[1])
        C = t[2]
        if F == 1:
            if reverse == False:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if reverse == False:
                S.append(C)
            else:
                S.appendleft(C)

                
if reverse == False:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Submission status: Accepted

Solution code:
# D - String Formation
from collections import deque 
S = deque(input())
Q = int(input())
Query = [list(input().split()) for _ in range(Q)]

# 文字列の向き (0: 正, 1: 逆)
rev = 0
for Q in Query:
    if Q[0] == '1':
        if rev == 0:
            rev = 1
        else:
            rev = 0
        continue
    if Q[1] == '1':
        if rev == 0:
            S.appendleft(Q[2])
        else:
            S.append(Q[2])
    else:
        if rev == 0:
            S.append(Q[2])
        else:
            S.appendleft(Q[2])

if rev == 1:
    ans = list(reversed(list(S)))
else:
    ans = list(S)
print(''.join(ans))

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import combinations, permutations, product
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

S = input()
L = deque()
for i in range(len(S)):
    L.append(S[i])
Q = int(input())
rev = False
for _ in range(Q):
    q = input()
    if q[0]=='1':
        rev = not rev
    else:
        _,f,c = q.split()
        if rev==(f=='1'):
            L.append(c)
        else:
            L.appendleft(c)
L = list(L)
if rev:
    L = L[::-1]
print(''.join(L))

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

s = deque(X())
n = k()
normal = 0
for i in range(n):
  a = input().split()
  if int(a[0]) == 1:
    normal ^= 1
  else:
    k = int(a[1]) - 1
    k ^= normal
    if k == 1:
      s.append(a[2])
    else:
      s.appendleft(a[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

S = list(input())
Q = int(input())
answer = deque(S)
reverse = False
for _ in range(Q):
  query = input()
  if query == '1':
    reverse ^= True
  else:
    ope, types, alphabet = query.split()
    if types == '1':
      if reverse:
        answer.append(alphabet)
      else:
        answer.appendleft(alphabet)
    else:
      if reverse:
        answer.appendleft(alphabet)
      else:
        answer.append(alphabet)
if reverse:
  result = ''
  for i in range(len(answer)):
    result += answer.pop()
  print(result)
else:
  result = ''
  for i in range(len(answer)):
    result += answer.popleft()
  print(result)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
n = int(input())
normal = 0
for i in range(n):
  li = input().split()
  if int(li[0]) == 1:
    normal ^= 1
  else:
    k = int(li[1]) - 1
    k ^= normal
    if k == 1:
      s.append(li[2])
    else:
      s.appendleft(li[2])
print(''.join(s) if normal == 0 else ''.join(list(s)[::-1]))

Submission status: Accepted

Solution code:
def main():
	S = list(input())
	T = []
	reverse = False
	n_q = int(input())
	for i in range(n_q):
		q = input().split(" ")
		if q[0] == "1":
			reverse = not reverse
		elif q[0] == "2":
			f = q[1]
			c = q[2]
			if (f == "1" and not reverse) or (f == "2" and reverse):
				T.append(c)
			elif (f == "1" and reverse) or (f == "2" and not reverse):
				S.append(c)
	if reverse:
		S.reverse()
		ans = S + T
	elif not reverse:
		T.reverse()
		ans = T + S
	print("".join(ans))

main()

Submission status: Accepted

Solution code:
from queue import deque
S = input()
Q = int(input())
Query = list(input().split() for _ in range(Q))

count = 0
L, R = deque(), deque()

for i in range(Q):
  if Query[i][0] == "1": count += 1
  else:
    if Query[i][1] == "1":
      if count % 2 == 0: L.appendleft(Query[i][2])
      else: R.append(Query[i][2])
    else:
      if count % 2 == 0: R.append(Query[i][2])
      else: L.appendleft(Query[i][2])
L, R = "".join(L), "".join(R)
if count % 2 == 0: print(L + S + R)
else: print(R[::-1] + S[::-1] + L[::-1])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = list(input().split() for _ in range(Q))

count = 0
L, R = "", ""

for i in range(Q):
  if Query[i][0] == "1": count += 1
  else:
    if Query[i][1] == "1":
      if count % 2 == 0: L = Query[i][2] + L
      else: R += Query[i][2]
    else:
      if count % 2 == 0: R += Query[i][2]
      else: L = Query[i][2] + L

if count % 2 == 0: print(L + S + R)
else: print(R[::-1] + S[::-1] + L[::-1])

Submission status: Accepted

Solution code:

s = input()

q = int(input())
x = []
y = []

flag = 0

for i in range(q):
    li = list(input().split())
    if flag%2 == 0:
        if li[0] == "2":
            if li[1] =="1":
                x.append(li[2])
            else:
                y.append(li[2])
        else:
            flag += 1

    else:
        if li[0] == "2":
            if li[1] =="1":
                y.append(li[2])
            else:
                x.append(li[2])
        else:
            flag += 1

if flag%2 == 0:
    x = x[::-1]
    print("".join(x)+s+"".join(y))

else:
    s = s[::-1]
    y = y[::-1]
    print("".join(y)+s+"".join(x))

Submission status: Accepted

Solution code:
S,Q=input(),int(input())
l,r='',''
reverse=0
for i in range(Q):
    T=input().split()
    if T[0]=='1':reverse=0 if reverse else 1
    else:
        if T[1]=='1':
            if reverse:r=r+T[2]
            else:l=T[2]+l
        else:
            if reverse:l=T[2]+l
            else:r=r+T[2]
if reverse:
    s=list(l+S+r);s.reverse()
    print(''.join(s))
else:
    print(l+S+r)

Submission status: Accepted

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

flag = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": flag = 1 - flag
  else:
    if query[1] == "1":
      if flag == 0: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if flag == 0: s.append(query[2])
      else: s.appendleft(query[2])

if flag == 1: s.reverse()
print(*s, sep="")

Submission status: Accepted

Solution code:
S = list(input())
N = int(input())
normalFlag = True
front = []
back = []
for _ in range(N):
    q = list(map(str,input().split()))
    if q[0] == '1':
        normalFlag = not normalFlag
    else:
        if q[1] == '1':
            if normalFlag:
                front.append(q[2])
            else:
                back.append(q[2])
        else:
            if normalFlag:
                back.append(q[2])
            else:
                front.append(q[2])

if normalFlag:
    front.reverse()
    ans = front+S+back
else:
    back.reverse()
    S.reverse()
    ans = back+S+front
print(''.join(map(str,ans)))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
S = [c for c in s]
deque = deque(S)
reversed = 0
for i in range(q):
    line = input()
    if line[0] == '1':
        reversed = reversed ^ 1
    else:
        op, f, c = line.split()
        f = int(f)
        if reversed == 0:
            if f == 1:
                deque.appendleft(c)
            else:
                deque.append(c)
        else:
            if f == 1:
                deque.append(c)
            else:
                deque.appendleft(c)
if reversed:
    deque.reverse()
result = "".join(deque)
print(result)

Submission status: Accepted

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

d = deque()
for i in s:
    d.append(i)

c = 1
for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        c *= -1
    else:
        if t[1] == '1':
            if c == 1:
                d.appendleft(t[2])
            else:
                d.append(t[2])
        else:
            if c == 1:
                d.append(t[2])
            else:
                d.appendleft(t[2])

print(''.join(list(d)[::c]))

Submission status: Accepted

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

d = deque()
for i in s:
    d.append(i)

c = 1
for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        c *= -1
    else:
        if t[1] == '1':
            if c == 1:
                d.appendleft(t[2])
            else:
                d.append(t[2])
        else:
            if c == 1:
                d.append(t[2])
            else:
                d.appendleft(t[2])

print(''.join(list(d)[::c]))

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    order = [list(input().split()) for _ in range(Q)]
    left_flag = 0
    right_flag = 0
    cnt = 0
    for i in range(Q):
        if order[i][0] == '1':
            cnt += 1
        else:
            if cnt%2 == 0:
                if order[i][1] == '1':
                    if left_flag == 1:
                        left = order[i][2] + left
                    else:
                        left = order[i][2]
                        left_flag = 1
                else:
                    if right_flag == 1:
                        right = right + order[i][2]
                    else:
                        right = order[i][2]
                        right_flag = 1
            else:
                if order[i][1] == '2':
                    if left_flag == 1:
                        left = order[i][2] + left
                    else:
                        left = order[i][2]
                        left_flag = 1
                else:
                    if right_flag == 1:
                        right = right + order[i][2]
                    else:
                        right = order[i][2]
                        right_flag = 1
    if left_flag == 1 and right_flag == 1:
        S = left + S + right
    elif left_flag == 1 and right_flag == 0:
        S = left + S
    elif left_flag == 0 and right_flag == 1:
        S = S + right
    else:
        S = S

    if cnt%2 == 0:
        return(S)
    else:
        S2 = S[-1]
        for i in range(len(S)-2,-1,-1):
            S2 = S2 + S[i]
        return S2

print(main())

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    S = SI()
    Q = NI()
    
    que = deque(list(S))
    
    reverse = False
    
    for q in range(Q):
        query = input()

        if query == "1":
            if reverse == False:
                reverse = True
            else:
                reverse = False
        else:
            x,y,z = map(str,query.split())
            if reverse == False:
                if y == "1":
                    que.appendleft(z)
                else:
                    que.append(z)
            else:
                if y == "1":
                    que.append(z)
                else:
                    que.appendleft(z)

    if reverse==True:
        print("".join(reversed(list(que))))
    else:
        print("".join(list(que)))#print(x,y,z)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

flg = True
for q in Q:
    if q[0] == "1":
        flg = not flg
    else:
        F = int(q[1])
        if not flg:
            F = 3 - F
        if F == 1:
            S.appendleft(q[2])
        if F == 2:
            S.append(q[2])

if not flg:
    S.reverse()
print("".join(S))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    turn=0
    q=I()
    ortop=[]
    orbot=[]
    for i in range(q):
        query=list(input().rstrip().split(" "))
        if query[0]=="1":
            turn=(turn+1)%2
        else:
            x=int(query[1])
            if (x+turn)%2==1:
                ortop.append(query[2])
            else:
                orbot.append(query[2])
    if turn%2==0:
        s1="".join(list(reversed(ortop)))
        s2="".join(orbot)
        print(s1+s+s2)
    else:
        s1="".join(list(reversed(orbot)))
        s2="".join(ortop)
        s="".join(list(reversed(s)))
        
        print(s1+s+s2)
        
        
            
            
            
    

        
    
            
        

        
if __name__=="__main__":
    main()

Submission status: Accepted

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

S=pin()[:-1]
Q=int(pin())
now=1
ans1=""
ans2=""
for i in range(Q):
  D=pin().split()
  T=0
  F=0
  C=""
  if len(D)==1:
    T=int(D[0])
  else:
    T=int(D[0])
    F=int(D[1])
    C=D[2]
  if T==1:
    now*=-1
  else:
    if F==1:
      if now==1:
        ans1=C+ans1
      else:
        ans2=ans2+C
    else:
      if now==1:
        ans2=ans2+C
      else:
        ans1=C+ans1
if now==1:
  print(ans1+S+ans2)
else:
  print(ans2[::-1]+S[::-1]+ans1[::-1])

Submission status: Accepted

Solution code:
s=input()
q=int(input())
que=[list(map(str,input().split())) for i in range(q)]
count=0
for i in que:
  if i[0]=="1":
    count+=1
ans=[[],[]]
ak=count
for i in que:
  if i[0]=="1":
    count-=1
  else:
    if i[1]=="1":
      ans[count%2].append(i[2])
    else:
      ans[(count+1)%2].append(i[2])
if ak%2==0:
  print("".join(reversed(ans[0]))+s+"".join(ans[1]))
else:
  print("".join(reversed(ans[0]))+"".join(reversed(list(s)))+"".join(ans[1]))

Submission status: Accepted

Solution code:
from collections import deque
S = str(input());s = []
for i in range(len(S)):
  s.append(S[i])
s = deque(s)
Q = int(input())
eo = 0
for query in range(Q):
  temp = list(map(str,input().split()))
  if temp[0] == "1":
    eo += 1 #反転したということになる
  else:
    if temp[1] == "1":
      if eo%2 == 0:
        s.appendleft(temp[2])
      else:
        s.append(temp[2])
    else:
      if eo%2 == 0:
        s.append(temp[2])
      else:
        s.appendleft(temp[2])
#print(s)
s = list(s)
if eo%2 == 0:
  ans = "".join(s)
else:
  s = s[::-1]
  ans = "".join(s)
print(ans)

Submission status: Accepted

Solution code:
S = input().strip()
L = []
R = []
Q = int(input())
flag = 0
for _ in range(Q):
    q = list(input().split())
    if len(q)==1:
        flag = 1-flag
    else:
        F = q[1]
        C = q[2]
        if flag==0 and F=="1":
            L.append(C)
        elif flag==0 and F=="2":
            R.append(C)
        elif flag==1 and F=="1":
            R.append(C)
        elif flag==1 and F=="2":
            L.append(C)
L = "".join(L)
R = "".join(R)
if flag==1:
    S = S[::-1]
    R = R[::-1]
    S = R+S+L
else:
    L = L[::-1]
    S = L+S+R
print(S)

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
Q = int(input())
r = False

q = deque(s)
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        r = not r
    else:
        f,c = query[1],query[2]
        if f == '1':
            if not r:
                q.appendleft(c)
            else:
                q.append(c)
        else:
            if not r:
                q.append(c)
            else:
                q.appendleft(c)

ans = list(q)
if r: ans = ans[::-1]
print(''.join(ans))

Submission status: Accepted

Solution code:
from collections import deque
import sys


def input():
    return sys.stdin.readline().strip()


q = deque(input())
Q = int(input())
flipped = False
for _ in range(Q):
    q_t, *q_b = input().split()
    if q_t == "1":
        flipped = not flipped
    else:
        F, C = q_b
        if (F == "2" and not flipped) or (F == "1" and flipped):
            q.append(C)
        else:
            q.appendleft(C)
q = list(q)
if flipped:
    print("".join(q[::-1]))
else:
    print("".join(q))

Submission status: Accepted

Solution code:
from collections import deque
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline
S = list(input().rstrip())
S = deque(S)
input()
rev = 0
for line in readlines():
    q, *query, = line.split()
    if q == '1':
        rev ^= 1
    else:
        f, c = query
        if f == '1':
            if not rev:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if not rev:
                S.append(c)
            else:
                S.appendleft(c)

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

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
rv=False
right=[]
left=[]
for _ in range(q):
  query=input()
  if query[0]=='1':
    rv=not rv
  else:
    _,f,c=query.split()
    if f=='1' and rv==False:
      left.append(c)
    elif f=='1' and rv==True:
      right.append(c)
    elif f=='2' and rv==False:
      right.append(c)
    elif f=='2' and rv==True:
      left.append(c)
if not rv:
  left.reverse()
  print(''.join(left)+''.join(s)+''.join(right))
else:
  right.reverse()
  s.reverse()
  print(''.join(right)+''.join(s)+''.join(left))

Submission status: Accepted

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

Submission status: Accepted

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


for i in range(n) :
    x=input()
    if x[0]=="1" :
        count+=1
        count%=2

    elif x[2]=="1" :
        if count%2==0 :
            s.appendleft(x[4])
        else :
            s.append(x[4])
    else :
        if count%2==0 :
            s.append(x[4])
        else :
            s.appendleft(x[4])


if count%2==1 :
    s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

z=list(input())
Q=deque()
for i in z:
  Q.append(i)
q=int(input())

cnt=0

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

    else:
        if cnt%2==0:
            Q.append(x[4])
        else:
            Q.appendleft(x[4])
      
if cnt%2==1:
  Q.reverse()


print(''.join(Q))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
query = [input().split() for _ in range(Q)]

switch = 1
top = []
bottom = []

for i in range(Q):
  if query[i][0] == "1":
    switch *= -1

  else:
    f = query[i][1]
    c = query[i][2]
    if switch == 1:
      if f == "1":
        top.append(c)
      else:
        bottom.append(c)
    else:
      if f == "1":
        bottom.append(c)
      else:
        top.append(c)

if switch == 1:
  ans = top[::-1] + list(S) + bottom
else:
  ans = bottom[::-1] + list(S)[::-1] + top
  
print("".join(ans))