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())
query = []
for i in range(Q):
    query.append(list(map(str, input().split())))


def start(S, x):
    S = x + S
    return S


def end(S, x):
    S = S + x
    return S


front = ''
back = ''
flg = True
for q in query:
    if len(q) == 1:
        flg = not flg
    else:
        if flg == True:
            if q[1] == '1':
                front = start(front, q[2])
            else:
                back = end(back, q[2])
        else:
            if q[1] == '1':
                back = end(back, q[2])
            else:
                front = start(front, q[2])
else:
    if flg == True:
        print(front + S + back)
    else:
        print(back[::-1] + S[::-1] + front[::-1])

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    s = str(input())
    q = inp()
    before = []
    after = []
    flag = True
    for _ in range(q):
        query = inpls()
        if query[0] == '1':
            if flag:
                flag = False
            else:
                flag = True
        else:
            f = query[1]
            S = query[2]
            if (flag and f=='2') or ((not flag) and f=='1'):
                after.append(S)
            else:
                before.append(S)
    ans = []
    for i in range(len(before)-1,-1,-1):
        ans.append(before[i])
    s = list(s)
    for i in range(len(s)):
        ans.append(s[i])
    for i in range(len(after)):
        ans.append(after[i])
    if not flag:
        ans.reverse()
    print(''.join(ans))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
Q=[input().split() for _ in range(q)]
word_l,word_r=deque([]),deque([])

flag=1
for i in Q:
    if i[0]=="1" and flag==1:
        flag=0
    elif i[0]=="1" and flag==0:
        flag=1

    elif i[0]=="2":
        num,f,c=i
        if f=="1" and flag==1:
            word_l.appendleft(c)
        elif f=="1" and flag==0:
            word_r.append(c)
        elif f=="2" and flag==1:
            word_r.append(c)
        elif f=="2" and flag==0:
            word_l.appendleft(c)

word_r="".join(word_r)
word_l="".join(word_l)
if flag==1:
    print(word_l+s+word_r)
else:
    print(word_r[::-1]+s[::-1]+word_l[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

is_reversed = False

S = deque(S)

for _ in range(Q):
    q = input().split(" ")
    if q[0] == "1":
        is_reversed = not is_reversed
    else:
        if q[1] == "1":
            if is_reversed:
                S.append(q[2])
            else:
                S.appendleft(q[2])
        else:
            if is_reversed:
                S.appendleft(q[2])
            else:
                S.append(q[2])

S = list(S)
if is_reversed:
    S = ''.join(list(reversed(S)))
else:
    S = ''.join((S))

print(S)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input().strip())
q = int(input())

rev = False
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        rev = not rev
    else:
        if (query[1] == '1') ^ rev:
            S.appendleft(query[2])
        else:
            S.append(query[2])
S = S if not rev else reversed(S)
print(*S, sep='')

Submission status: Accepted

Solution code:
#for _ in range(int(input())):
s = input()
q = int(input())
b=0
f,l="",""
for _ in range(q):
    qi = input().split(' ')
    #print(len(qi))
    if(len(qi)==1):
        b=b^1
    else:
        fi = int(qi[1])
        if(fi==1):
            if(b==0):
                f=f+qi[2]
            else:
                l=l+qi[2]
        elif(fi==2):
            if(b==0):
                l=l+qi[2]
            else:
                f=f+qi[2]
s = f[::-1]+s+l
if(b==1):
    s=s[::-1]

print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
ansList = [""] * (2 * 10 ** 5 * 2 + 1)
last = 2 * 10 ** 5
first = 2 * 10 ** 5 - 1
for x in range(len(s)):
    ansList[last] = s[x]
    last += 1
isReverce = False
for x in range(q):
    query = list(map(str, input().split()))
    if query[0] == "1": isReverce = not isReverce
    else:
        if (query[1] == "1" and isReverce == False) or (query[1] == "2" and isReverce == True):
            ansList[first] = query[2]
            first -= 1
        else:
            ansList[last] = query[2]
            last += 1
ans = "".join(ansList)
if isReverce == False: print(ans)
else: print(ans[::-1])

Submission status: Accepted

Solution code:
import math

S = input().strip()
Q = int(input().strip())
order = 1
tmp = 'T'

for i in range(Q):
  q = input().strip().split()
  #print(q)
  if q[0] == '1':
    # 反転
    # S = S[::-1]
    order *= -1
  else:
    F = q[1]
    if F == '1':
      if order == 1:
        # 先頭にCを追加
        # S = q[2] + S
        tmp = q[2] + tmp
      else:
        # 末尾にCを追加
        # S = S + q[2]
        tmp = tmp + q[2]
    else:
      if order == 1:
        # 末尾にCを追加
        # S = S + q[2]        
        tmp = tmp + q[2]
      else:
        # 先頭にCを追加
        # S = q[2] + S
        tmp = q[2] + tmp

S = tmp.replace('T', S)        
print(S[::order])

Submission status: Accepted

Solution code:
from collections import deque

S = [l for l in input()]
Q = int(input())
d = deque(S)
direction = 'f'

for i in range(Q):
    query = input()
    if query[0] == '1':
        if direction == 'f':
            direction = 'r'
        else :
            direction = 'f'
    else :
        t, f, c = query.split()
        if f == '1':
            if direction == 'f':
                d.appendleft(c)
            else :
                d.append(c)
        else :
            if direction == 'f':
                d.append(c)
            else :
                d.appendleft(c)

if direction == 'r':
    d.reverse()

print(''.join(d))

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

s = input()
s = deque(s)
q = INT()

r_count = 0
L = [S_LIST() for i in range(q)]
for i in range(q):
    if L[i][0] == "1":
        r_count ^= 1
        # s.reverse()
    else:
        query, sent = int(L[i][1]), str(L[i][2])
        if r_count == 0:
            if query == 1:
                s.appendleft(sent)
            else:
                s.append(sent)
        else:
            if query == 2:
                s.appendleft(sent)
            else:
                s.append(sent)

if r_count != 0:
    s.reverse()
print("".join(list(s)))

Submission status: Accepted

Solution code:
import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq
# import time
# import itertools
import collections
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29

def main():
    s = collections.deque(list(input()))
    q = int(input())
    count = 0
    for i in range(q):
        tfc = list(input().split())
        if tfc[0]=='1':
            count = abs(count - 1)
        else:
            t,f,c = tfc
            if count:
                if f=='1':
                    s.append(c)
                else:
                    s.appendleft(c)
            else:
                if f=='1':
                    s.appendleft(c)
                else:
                    s.append(c)
    if count:
        t = []
        for i in range(len(s)):
            t.append(s.pop())
        print(''.join(t))
    else:
        print(''.join(s))
    return

if __name__=='__main__':
    main()

Submission status: Accepted

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

S_front, S_back = [], []
reverse = False

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        reverse = not reverse
    else:
        T = False if (query[1] == "1") else True
        if reverse ^ T is False:
            S_front += query[2]
        else:
            S_back += query[2]

if reverse is True:
    S = S[::-1]
    ans = "".join(S_back)[::-1] + S + "".join(S_front)
else:
    ans = "".join(S_front)[::-1] + S + "".join(S_back)
print(ans)

Submission status: Accepted

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

cnt = 0
left = []
right = []
for j in range(q):
    a = [str(i) for i in input().split()]
    if a[0] == '1':
        cnt += 1
    else:
        if (a[1] == '1' and cnt % 2 == 0) or (a[1] == '2' and cnt % 2 == 1):
            right.append(a[2])
        else:
            left.append(a[2])

if cnt % 2 == 0:
    print(''.join(reversed(right)) + s + ''.join(left))
else:
    print(''.join(reversed(left)) + ''.join(list(reversed(s))) + ''.join(right))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
top = ''
end = ''
reverse_flag = 1
for _ in range(Q):
    query = input()
    if query == '1':
        reverse_flag *= -1
    else:
        _, F, C = query.split()
        if (int(F)-1.5) * reverse_flag > 0:
            end += C
        else:
            top += C
ans = top[::-1] + S + end
if reverse_flag == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
c=0
left=""
right=""
for i in range(Q):
    T=list(map(str,input().split()))
    if len(T)==1:
        c+=1
    else:
        if (T[1]=="1" and c%2==0) or (T[1]=="2" and c%2==1):
            left+=T[2]
        else:
            right+=T[2]

if c%2==0:
    ans=left[::-1]+S+right
else:
    ans=right[::-1]+S[::-1]+left
print(ans)

Submission status: Accepted

Solution code:
a = input()

q = int(input())

queries = [list(input().split()) for i in range(q)]

revers = False

head = []

tail = []

for query in queries:
    if len(query) == 1:
        revers = not revers
    else:
        command, f, chara = query
        if f == '1':
            if not revers:
                head.append(chara)
            else:
                tail.append(chara)
        else:
            if not revers:
                tail.append(chara)
            else:
                head.append(chara)

# print(revers)
#
# print(head)
#
# print(tail)

if revers:
    tail.reverse()
    print(''.join(tail) + a[::-1] + ''.join(head))
else:
    head.reverse()
    print(''.join(head) + a + ''.join(tail))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = input()
    q = int(input())

    pref = []
    suff = []
    rev = False

    for i in range(q):
        curr = [c for c in input().split()]
        if curr[0] == '1':
            rev = not rev
        else:
            if curr[1] == '1':
                if not rev:
                    pref.append(curr[2])
                else:
                    suff.append(curr[2])
            else:
                if not rev:
                    suff.append(curr[2])
                else:
                    pref.append(curr[2])

    res = "{}{}{}".format("".join(pref[::-1]), s, "".join(suff))
    if rev:
        print(res[::-1])
    else:
        print(res)

Submission status: Accepted

Solution code:
# list(input())
# list(map(int, input().split()))
# int(input())
from collections import deque
S = deque(list(input()))
Q = int(input())
queries = []
rv_flag = False
for _ in range(Q):
    q = list(input().split())
    if len(q) == 1:
        rv_flag = not rv_flag
        continue
    _, f, c = q
    if f == '1' and rv_flag:
        S.append(c)
    elif f == '1' and not rv_flag:
        S.appendleft(c)
    elif f == '2' and rv_flag:
        S.appendleft(c)
    elif f == '2' and not rv_flag:
        S.append(c)

ans = ''.join(S)
if rv_flag:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
len_queries = int(input())
queries = [input().split() for _ in range(len_queries)]

is_forward = True
for query in queries:
    if "1" == query[0]:
        is_forward = not is_forward
    else:
        if is_forward:
            if "1" == query[1]:
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if "1" == query[1]:
                s.append(query[2])
            else:
                s.appendleft(query[2])
if not is_forward:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
def main():
	S=input()                                               # 1行1列
	Q=int(input())                                          # 1行1列
	Q_LIST=[input().split() for i in range(Q)]                # N行C列・・・Cは任意
	 
	dir = 0 # 0が正常

	S_HEAD = ''
	S_BACK = ''

	for query in Q_LIST:
		if query[0] == '1':
			if dir == 0:
				dir = 1
			else:
				dir = 0
	 
		if dir == 0:
			HEAD = '1'
			BACK = '2'
		else:
			HEAD = '2'
			BACK = '1'
	 
		if query[0] == '2':
			if query[1] == HEAD:
				S_HEAD = query[2] + S_HEAD
			if query[1] == BACK:
				S_BACK =  S_BACK + query[2]

	S = S_HEAD + S + S_BACK

	if dir == 0:
		print(S)
	else:
		print(S[::-1])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
S = input()
Q = int(input())
pin = 0
left = ''
right = ''
for i in range(Q):
  t = list(sys.stdin.readline().split())
  if int(t[0]) == 1:
    pin += 1
  else:
    if int(t[1]) % 2 == pin % 2 :
      right = right + t[2]
    else:
      left = t[2] + left
S = left + S + right
if pin % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = [int(input()) for i in range(n)]
    #a = list(map(int, input().split()))
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    from collections import deque
    deq = deque(input())
    q = int(input())
    r = [input().split() for i in range(q)]
    cnt = 0
    for i in range(q):
        tmp = r[i]
        if tmp[0] == "1":
            cnt += 1
        else:
            if tmp[1] == "1" and cnt % 2 == 0 or tmp[1] != "1" and cnt % 2 != 0:
                deq.appendleft(tmp[2])
            else:
                deq.append(tmp[2])
    if cnt % 2 == 0:
        print(*(deq), sep="")
    else:
        print(*(deque(reversed(deq))), sep="")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
t = input()

flont = []
back = []

if len(t) == 1:
    flont = [t[0]]
else:
    flont = list(reversed(t[:-1]))
    back = [t[-1]]
q = int(input())
for i in range(q):
    s = input().split()
    if int(s[0]) == 1:
        sw = flont
        flont = back
        back = sw
    else:
        f = int(s[1])
        c = s[2]
        if f == 1:
            flont.append(c)
        else:
            back.append(c)
[sys.stdout.write(i) for i in list(reversed(flont))]
[sys.stdout.write(i) for i in back]
print("")

Submission status: Accepted

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

ans = S
X = []
Y = []
state = 1

for k in range(Q):
    if Query[k][0]=="1":
        state *= -1
    elif Query[k][0]=="2":
        if Query[k][1]=="1":
            if state == 1:
                X.append(Query[k][2])
            else:
                Y.append(Query[k][2])
        else:
            if state == 1:
                Y.append(Query[k][2])
            else:
                X.append(Query[k][2])

if state == 1:
    X = list(reversed(X))
    ans = X + ans + Y
else:
    Y = list(reversed(Y))
    ans = list(reversed(ans))
    ans = Y + ans + X

print("".join(ans))

Submission status: Accepted

Solution code:
from collections import deque

def iin(): return int(input())
def iln(): return list(map(int, input().split()))


S = input()

queue = deque()
for c in S:
    queue.append(c)

is_reversed = False

QN = iin()

for i in range(QN):
    operation = input().split()
    if len(operation) == 1:
        is_reversed = not is_reversed
    else:
        _, op, new_char = operation
        if op == "1":
            # append to head
            if is_reversed:
                queue.append(new_char)
            else:
                queue.appendleft(new_char)
        else:
            # append to tail
            if is_reversed:
                queue.appendleft(new_char)
            else:
                queue.append(new_char)

if is_reversed:
    print("".join(reversed(queue)))
else:
    print("".join(queue))

Submission status: Accepted

Solution code:
from collections import deque

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

cont = 0
for i in range(q):
    q_uery = list(map(str, input().split()))
    if int(q_uery[0]) == 1:
        cont += 1
    if int(q_uery[0]) == 2:
        if (int(q_uery[1]) + cont) % 2 == 0:
            s.append(q_uery[2])
        else:
            s.appendleft(q_uery[2])
if cont % 2 == 1:
    s.reverse()

print(''.join(s))

Submission status: Accepted

Solution code:
# Author: cr4zjh0bp
# Created: Sat Mar  7 12:47:52 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

from collections import deque

s = deque(ns())
q = ni()
p = 0
for i in range(q):
    que = nas()
    if int(que[0]) == 1:
        p += 1
    elif int(que[0]) == 2:
        f, c = int(que[1]), que[2]
        if p % 2 == 0:
            if f == 1:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == 1:
                s.append(c)
            else:
                s.appendleft(c)

for i in range(len(s)):
    if p % 2 == 1:
        print(s.pop(), end="")
    else:
        print(s.popleft(), end="")
print()

Submission status: Accepted

Solution code:
from collections import deque

t = input()
s = deque()
for i in range(len(t)): s.append(t[i])
q = int(input())
flg = False
for i in range(q):
    query = input()
    if query[0] == '1': flg = not flg
    else:
        t,f,c = query.split()
        if flg:
            s.appendleft(c) if f=='2' else s.append(c)
        else:
            s.appendleft(c) if f=='1' else s.append(c)
if flg: s = reversed(s)
print(''.join(s))

Submission status: Accepted

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

x = []
pre = ''
post = ''
isone = False
for i in range(q):
    x = input().split()
    if x[0] == '1':
        if isone:
            isone = False
        else:
            isone = True
    elif x[1] == '1':
        if isone:
            post += x[2]
        else:
            pre += x[2]
    else:
        if isone:
            pre +=x [2]
        else:
            post += x[2]
    if i == q - 1:
        if isone:
            s = post[::-1] + s[::-1] + pre
        else:
            s = pre[::-1] + s + post
print(s)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
#import numpy as np
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
#S = str(input())

S = str(input())
Q = int(input())
#query = [["" for _ in range(3)] for _ in range(Q)]

sl = [] #先頭リスト
el = [] #末尾リスト
f = True #反転正負
for _ in range(Q) :
  stmp = input().split()
  #print (stmp)
  
  if stmp[0] == "1" :
    f = not f
    tl = sl
    sl = el
    el = tl
    
  else :
    if stmp[1] == '1' :
      sl.append(stmp[2])
    else :
      el.append(stmp[2])

ans = ""
for i in range(len(sl)-1, -1, -1) :
  ans += sl[i]
if f :
  ans += S
else :
  ans += S[::-1]
for i in range(len(el)) :
  ans += el[i]
  
print (ans)

Submission status: Accepted

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

num_turn = 0

front = []
end = []

for i in range(Q):
    query = input()
    if query == '1':
        num_turn += 1
    else:
        _, f, c = query.split()
        f = int(f)
        if f == 1:
            if num_turn%2==0:
                front.append(c)
            else:
                end.append(c)
        else:
            if num_turn%2==0:
                end.append(c)
            else:
                front.append(c)

S = list(S)
if num_turn%2==1:
    ans = end[::-1] + S[::-1] + front
else:
    ans = front[::-1] + S + end
print(''.join(ans))

Submission status: Accepted

Solution code:
from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  rev=0
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      rev+=1
    else:
      if (int(q[1])+rev)%2==1:
        d.appendleft(q[2])
      else:
        d.append(q[2])
  if rev%2==0:
    print(''.join(list(d)))
  else:
    d.reverse()
    print(''.join(list(d)))
main()

Submission status: Accepted

Solution code:
from collections import deque
que = deque()
ans = ''
s = list(input())
for i in range(len(s)):
    que.append(s[i])
q = int(input())
flag = 0# if head->0 elif tail ->1
for i in range(q):
    a = list(map(str, input().split()))
    if a[0] == '1':
        if flag == 0:
            flag =1
        else:
            flag = 0
    elif a[0] == '2':
        if a[1] == '1':
            if flag == 0:
                que.appendleft(str(a[2]))
            else:
                que.append(str(a[2]))
        elif a[1] == '2':
            if flag == 1:
                que.appendleft(str(a[2]))
            else:
                que.append(str(a[2]))
if flag == 0:
    for i in range(len(que)):
        ans += str(que.popleft())
elif flag == 1:
    for i in range(len(que)):
        ans += str(que.pop())
print(ans)

Submission status: Accepted

Solution code:
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
S = input()
Q = int(input())

inv = 0
head=[]
tail=[]
for _ in range(Q):
    q = input()
    if q == '1':
        inv = 1 - inv
    else:
        (T,F,C) = list(q.split())
        #print(T,F,C)
        if inv == 0:
            if F == '1':
                head.append(C)
            else:
                tail.append(C)
        else:
            if F == '1':
                tail.append(C)
            else:
                head.append(C)
    # print(head,S,tail)

if inv == 0:
    print(''.join(list(reversed(head))) + S + ''.join(tail))
else:
    print(''.join(list(reversed(tail))) + ''.join(list(reversed(S))) + ''.join(head))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
jun = 1
f = "";t = ""
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = 1 if jun==0 else 0 
  else:
    _,ff,c = tmp.split()
    if jun+int(ff) ==2:
      f = c+f
    else:
      t += c
if jun==1:
  print(f+s+t)
else:
  print((f+s+t)[::-1])

Submission status: Accepted

Solution code:
import sys
from collections import deque


def main():
    s = sys.stdin.readline().strip()
    q = int(sys.stdin.readline().strip())
    r = 0
    head = []
    bottom = []
    do_reverse = False
    for _ in range(q):
        command = sys.stdin.readline().strip().split(" ")
        # if command[0] == "1":
        #     s = s[::-1]
        # else:
        #     if command[1] == "1":
        #         s = command[2] + s
        #     else:
        #         s = s + command[2]
        if command[0] == "1":
            do_reverse = not do_reverse
            r += 1
        else:
            position_head = command[1] == "1"
            if do_reverse:
                position_head = not position_head
            if position_head:
                head.append(command[2])
            else:
                bottom.append(command[2])
    if r % 2 == 0:
        print("".join(head[::-1]) + s + "".join(bottom))
    else:
        print("".join(bottom[::-1]) + s[::-1] + "".join(head))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()

Q = int(input())

array = ["" for i in range(Q*2+1)]

array[Q] = S
order = 0

for i in range(Q):
    s = input().split()
    #print ("s=", s)
    if s[0] == '1':
        order = 1 - order
    elif s[1] == '1':
        if order == 0:
            array[Q-(i+1)] = s[2]
        else:
            array[Q+(i+1)] = s[2]
    elif s[1] == '2':
        if order == 0:
            array[Q+(i+1)] = s[2]
        else:
            array[Q-(i+1)] = s[2]

#print("array=",array)

if order == 0:
    for i in range(2*Q+1):
        print(array[i],sep="",end="")
else:
    for i in range(2*Q, -1,-1):
        for j in range(len(array[i])):
            print(array[i][len(array[i])-1-j],sep="",end="")

Submission status: Accepted

Solution code:
#!python3.4
# -*- coding: utf-8 -*-
# abc158/abc158_d
import sys
from collections import deque

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    S = deque(i2s())
    Q = i2n()
    reverse = False
    for _ in range(Q):
        T, *args = i2s().split(' ')
        if T == '1':
            reverse = not reverse
            continue
        F, C = args
        toHead = F == '1'
        if toHead ^ reverse:
            # 先頭
            S.appendleft(C)
        else:
            # 末尾
            S.append(C)
    if not reverse:
        print(''.join(S))
    else:
        print(''.join(reversed(S)))
    return

main()

Submission status: Accepted

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

cnt_t1 = 0
s_1 = ""
s_2 = ""

for i in range(q):
    tmp = str(input())

    if tmp[0] == "1":
        cnt_t1 += 1

    else:
        if tmp[2] == "1":
            if cnt_t1 % 2 == 0:
                s_1 = tmp[4] + s_1
            else:
                s_2 = s_2 + tmp[4]
        else:
            if cnt_t1 % 2 == 1:
                s_1 = tmp[4] + s_1
            else:
                s_2 = s_2 + tmp[4]

s = s_1 + s + s_2
if cnt_t1 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque


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

d = deque(list(S))

left_head = True

for i in range(Q):
    inp = input()

    if inp == '1':
        left_head = not left_head
    else:
        T, F, c = inp.split()

        # 戦闘
        if F == '1':
            if left_head:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if left_head:
                d.append(c)
            else:
                d.appendleft(c)


if left_head:
    print(''.join(d))
else:
    print(''.join(d)[::-1])

Submission status: Accepted

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

left = []
right = []

reversed_ = False

for _ in range(Q):
    INPUT = input()

    if INPUT == '1':
        reversed_ = not(reversed_)
        left, right = right, left
        continue

    INPUT = INPUT.split()
    F = INPUT[1]
    C = INPUT[2]

    if F == '1':
        left.append(C)
    else:
        right.append(C)

for c in reversed(left):
    print(c, end='')

if reversed_:
    for c in reversed(S):
        print(c, end='')
else:
    print(S, end='')

for c in right:
    print(c, end='')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
rev = 0
fir = ''
end = ''
for i in range(q):
    qvery = input()
    if len(qvery) == 1: #t == 1
        rev += 1
    else:
        qq, f, c = qvery.split(' ')
        f = int(f)
        if rev % 2 == 0:
            if f == 1:
                fir += c
            else:
                end += c
        else:
            if f == 2:
                fir += c
            else:
                end += c
fir = fir[::-1]
s = fir + s + end
if rev % 2 == 1:
    s = s[::-1]
print(s)