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:
import copy
import itertools

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

ct = 0
ls = []
rs = []
for i in range(q):
    qtmp = input().split()
    #qtmp = [2,1,'c']
    t = int(qtmp[0])
    if(t == 1):
        ct += 1
        ct = ct % 2
    else:
        f = int(qtmp[1])
        if(ct+1 == f):
            ls.append( qtmp[2])
        else:
            rs.append( qtmp[2])
if(ct == 1):
    tmp = rs
    rs = ls
    ls = tmp
    s = s[::-1]
for i in range(len(ls)):
    print(ls[len(ls)-i-1], end='')
print(s, end='')
for i in range(len(rs)):
    print(rs[i], end='')
print("")

Submission status: Accepted

Solution code:
import collections
q = collections.deque(list(raw_input()))
operations = [raw_input().split(' ') for _ in range(int(raw_input()))]
rev = False

for op in operations:
	if len(op) == 1:
		rev = not(rev)
	elif len(op) == 3:
		_, where, char = op
		where = bool(int(where) - 1) ^ rev
		if where:			
			q.append(char)
		else:
			q.appendleft(char)

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

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = input()
    n = ni()

    front = ""
    rear = ""

    cnt = 0
    for i in range(n):
        d = list(sys.stdin.readline().split())
        if int(d[0]) == 1:
            cnt = (cnt + 1) % 2
        else:
            if not cnt:
                if int(d[1]) == 1:
                    front = d[2] + front
                else:
                    rear = rear + d[2]
            else:
                if int(d[1]) == 1:
                    rear = rear + d[2]
                else:
                    front = d[2] + front

    if cnt:
        s = s[::-1]
        front = front[::-1]
        rear = rear[::-1]
        ans = rear + s + front
    else:
        ans = front + s + rear

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flag = 0   #"1" represents inversion
head = ""
tail = ""
for i in range(Q):
    quary = list(map(str, input().split()))
    if quary[0] == "1":
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if flag == 1:
            if quary[1] == "1":
                tail = tail+quary[2]
            else:
                head = quary[2]+head
        else:
            if quary[1] == "1":
                head = quary[2]+head
            else:
                tail = tail+quary[2]
S = head+S+tail
if flag == 1:
    S = S[::-1]
print(S)

Submission status: Accepted

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

dire = 0
front = ""
back = ""

for i in range(Q):
    quary = [_ for _ in input().split()]
    if len(quary) == 1:
        T = quary[0]
        dire += 1
    else:
        T, F, C = quary
        if F == "1":
            if dire % 2 == 0:
                front += C
            else:
                back += C
        else:
            if dire % 2 == 1:
                front += C
            else:
                back += C

S = front[::-1] + S + back

if dire % 2 == 1:
    print(S[::-1])
else:
    print(S)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def LI(): return list(map(int, stdin.readline().split()))
def LS(): return list(stdin.readline())

s = input()
q = int(input())
# print(s,q)
head = True
right = ''
left = ''
for i in range(q):
    query = input().split(' ')
    # print(query)
    if query[0]=='1':
        head = not head
    else:
        if query[1]=='1':
            if head:
                left += query[2]
            else:
                right += query[2]
        else:
            if head:
                right += query[2]
            else:
                left += query[2]
    # print(left,  right)
s = left[::-1]+s+right
if head:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

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

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

left = ''
right = ''
t = False
for ls in query:
    if len(ls) == 1:
        t = not t
    else:
        two, f, c = ls
        if f == '1':
            if t:
                right += c
            else:
                left += c
        else:
            if t:
                left += c
            else:
                right += c

if t:
    print(right[::-1] + s[::-1] + left)
else:
    print(left[::-1] + s + right)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
f = False
# Falseだと左が先頭
for _ in range(q):
    t = input().split()
    if t[0]=="1":
        f = not f
    elif t[0]=="2":
        t[2]=list(t[2])
        if f:
            t[2]=t[2][::-1]
            if t[1]=="1":
                s.extend(t[2])
            elif t[1]=="2":
                s.extendleft(t[2])
        else:
            if t[1]=="1":
                s.extendleft(t[2])
            elif t[1]=="2":
                s.extend(t[2])
if f:
    print("".join(s)[::-1])
else:
    print("".join(s))

Submission status: Accepted

Solution code:
S=input()
S_=list(S)
Q=int(input())
F1=[]
F2=[]
Q_=[]
for i in range(Q):
  Q_.append(list(input().split()))
count=0  
for i in range(len(Q_)):
  if Q_[i]==['1']:
    count+=1
  elif Q_[i][0:2]==['2','1']:
    if count%2==0:
      F1.append(Q_[i][2])
    else:
      F2.append(Q_[i][2])
  else:
    if count%2==0:
      F2.append(Q_[i][2])
    else:
      F1.append(Q_[i][2])
SS=F1[::-1]+S_+F2                
if count%2==0:
  print(''.join(SS))
else:
  print(''.join(SS[::-1]))

Submission status: Accepted

Solution code:
import sys
from collections import deque
s = input()
q = int(input())
dq = deque(s)
rev = False
for i in range(q):
    t = input().split()
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            if rev:
                dq.append(t[2])
            else:
                dq.appendleft(t[2])
        else:
            if rev:
                dq.appendleft(t[2])
            else:
                dq.append(t[2])
s = "".join(list(dq))
if rev:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

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

up = 0

head = []
tail = []

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

    if query[0] == "1":
        if up:
            up = 0
        else:
            up = 1

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

head=''.join(map(str,head))
tail=''.join(map(str,tail))

ans = head[::-1] + s + tail

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 13:56:40 2020

@author: shinba
"""
import sys
from collections import deque

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

dq = deque(s)
rev = False

for i in range(q):
    t = sys.stdin.readline().rstrip().split()
    if t[0] == "1":
        rev = not rev
    else:
        if t[1] == "1":
            if rev:
                dq.append(t[2])
            else:
                dq.appendleft(t[2])
        else:
            if rev:
                dq.appendleft(t[2])
            else:
                dq.append(t[2])

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

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

Submission status: Accepted

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

dq = deque(s)
rev = False

for _ in range(q):
    t = list(input().split())
    if t[0] == "1":
        rev = not rev
    elif t[0] == "2":
        f = t[1]
        c = t[2]
        if f == "1":
            if rev:
                dq.append(c)
            else:
                dq.appendleft(c)
        else:
            if rev:
                dq.appendleft(c)
            else:
                dq.append(c)
                
if rev:
    print("".join(list(dq)[::-1]))
else:
    print("".join(list(dq)))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
cnt = 0
for i in range(Q):
  x = list(input().split())
  if(int(x[0]) == 1):
    cnt += 1
  else:
    if((cnt+int(x[1])) % 2 == 1):
      S.appendleft(x[2])
    else:
      S.append(x[2])

S = list(S)
if(cnt % 2 == 1):
  S = S[::-1]
print("".join(S))

Submission status: Accepted

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

S = input()

dq = deque(S.rstrip())
Q = int(input());

reverse = False

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        reverse ^= 1
    else:
        F, C = q[1:]
        if reverse:
            if F == '1':
                dq.append(C)
            else:
                dq.appendleft(C)
        else:
            if F == '1':
                dq.appendleft(C)
            else:
                dq.append(C)

ans = ""
if reverse:
    while dq:
        ans += dq.pop()
else:
    while dq:
        ans += dq.popleft()
print(ans)

Submission status: Accepted

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

S = input()

dq = deque(S.rstrip())
Q = int(input());

reverse = False

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        reverse ^= 1
    else:
        F, C = q[1:]
        if (F == '1') ^ reverse:
            dq.appendleft(C)
        else:
            dq.append(C)

ans = ""
if reverse:
    while dq:
        ans += dq.pop()
else:
    while dq:
        ans += dq.popleft()
print(ans)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
head=[]
tail=[]
rev=False
for _ in range(q):
  tmp=list(input().split())
  if tmp[0]=='1':
    if rev==False:
      rev=True
    else:
      rev=False
  if tmp[0]=='2':
    if rev==False:
      if tmp[1]=='1':
        head.append(tmp[2])
      if tmp[1]=='2':
        tail.append(tmp[2])
    if rev==True:
      if tmp[1]=='1':
        tail.append(tmp[2])
      if tmp[1]=='2':
        head.append(tmp[2])
head=''.join(map(str,head))
tail=''.join(map(str,tail))
ans=head[::-1]+s+tail
if rev==True:
  ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = deque(s)

reverse_num = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])
    else:
      if reverse_num % 2 != 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])

res = ''
if reverse_num % 2 == 0: 
  for i in range(len(ans)):
    res += ans.popleft()
  print(res)
else:
  for i in range(len(ans)):
    res += ans.pop()
  print(res)

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
s = deque(S)
reverse = True

for i in range(Q):
    tmp = list(input().split())
    if tmp[0] == '1':
        reverse = not reverse
    if tmp[0] == '2':
        F = tmp[1]
        C = tmp[2]
        if F == '1':
            if reverse:
                s.appendleft(C)
            else:
                s.append(C)
        else:
            if reverse:
                s.append(C)
            else:
                s.appendleft(C)
if reverse:
    print(''.join(s))
else:
    print(''.join(list(s)[::-1]))

Submission status: Accepted

Solution code:
import sys
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = input()
q = int(input())
ans = deque(s)
cnt = 0
for _ in range(q):
    inp = readline().rstrip()
    if inp[0] == "2":
        if inp[2] == "1":
            if cnt % 2 == 0:
                ans.appendleft(inp[4])
            else:
                ans.append(inp[4])
        else:
            if cnt % 2 == 1:
                ans.appendleft(inp[4])
            else:
                ans.append(inp[4])
    else:
        cnt += 1

if cnt % 2 == 1:
    ans = list(ans)[::-1]
    
print("".join(ans))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
dir=True
ans=s
right=""
left=""
query=[]
for i in range(q):
  temp=input()
  if temp[0] == "1":
    dir = dir ^ True
  if temp[0] == "2":
    t,f,c = temp.split()

    if (f == "1" and dir) or (f=="2" and not dir):
      left=c+left
    else:
      right=right+c

def reverse(s):
  t=""
  for i in range(len(s)-1,-1,-1):
    t+=s[i]
  return t

if dir:
  print(left+ans+right)
else:
  t=reverse(right)+reverse(ans)+reverse(left)
  print(t)

Submission status: Accepted

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

    rev = 0
    append_front_rev = ''
    append_rear = ''

    for i in range(Q):
        T = input().split()

        if (T[0] == "1"):
            rev ^= 1
            continue

        if (T[0] == "2" and T[1] == "1"):
            if rev == 0:
                append_front_rev += T[2]
            else:
                append_rear += T[2]
            continue

        if (T[0] == "2" and T[1] == "2"):
            if rev == 0:
                append_rear += T[2]
            else:
                append_front_rev += T[2]
            continue

    ans = append_front_rev[::-1] + S + append_rear
    if rev:
        ans = ans[::-1]

    print("".join(ans))


resolve()

Submission status: Accepted

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

for i in range(q):
    inp=list(input().split())
    if inp[0] == "1":
        quelist.append([False,0,0])
    else:
        quelist.append([True,int(inp[1]),inp[2]])

gflag=True
maelist=[]
atolist=[]
for que in quelist:
    if que[0]:
        if gflag:
            if que[1] ==1:
                maelist.append(que[2])
            else:
                atolist.append(que[2])
        else:
            if que[1] ==1:
                atolist.append(que[2])
            else:
                maelist.append(que[2])
    else:
        if gflag:
            gflag=False
        else:
            gflag=True

if gflag:
    maelist.reverse()
    maelist.append(s)
    maelist.extend(atolist)
    output="".join(maelist)
else:
    atolist.reverse()
    s=s[::-1]
    atolist.append(s)
    atolist.extend(maelist)
    output="".join(atolist)

print(output)

Submission status: Accepted

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

n = S
mode = 0
operations = []
deq = deque(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))

Submission status: Accepted

Solution code:
from collections import deque
D=deque(input().rstrip())
Q=int(input())
count=0
for _ in range(Q):
	Y=list(input().split())
	if Y[0]=="1":
		count=(count+1)%2
	else:
		if Y[1]=="1":
			if count==0:
				D.appendleft(Y[2])
			else:
				D.append(Y[2])
		else:
			if count==0:
				D.append(Y[2])
			else:
				D.appendleft(Y[2])
D=list(D)
if count==0:
	print("".join(D))
else:
	D.reverse()
	print("".join(D))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
l = list(list(input().split()) for _ in range(q))
t = 0
for i in l:
  if len(i)==1:
    t += 1
  else:
    a,f,c = i
    if (f=="1" and t%2==0) or (f=="2" and t%2==1) :
      s.appendleft(c)
    else:
      s.append(c)
if t%2==1:
  s.reverse()
print(*s,sep="")

Submission status: Accepted

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
rev=0
for i in range(q):
  t=list(input().split(" "))
  if t[0]=="1":
    rev=rev+1
  else:
    if t[1]=="1":
      if rev%2==0:
        s.appendleft(t[2])
      else:
        s.append(t[2])
    else:
      if rev%2==0:
        s.append(t[2])
      else:
        s.appendleft(t[2])
if rev%2==0:
  print("".join(s))
else:
  s.reverse()
  print("".join(s))

Submission status: Accepted

Solution code:

from collections import deque

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

for i in range(q):
    L = list(input().split())
    if L[0] == '1' :
        head = (head + 1)%2
    if L[0] == '2' :
        if head == 0:
            if L[1] == '1' :
                s.appendleft(L[2])
            if L[1] == '2' :
                s.append(L[2])
        if head == 1:
            if L[1] == '1' :
                s.append(L[2])
            if L[1] == '2' :
                s.appendleft(L[2])
if head == 0:
    print(''.join(s))
else: 
    print(''.join(reversed(s)))

Submission status: Accepted

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

flag = 1
s1 = ''

for i in range(Q):
	q = input()
	if len(q) == 1:
		s1,S = S,s1
	else:
		T,F,C = q.split(' ')
		if int(F) == 1:
			s1 = s1 + C
		else:
			S = S + C

print(s1[::-1] + S)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
t = ''
for i in range(Q):
  instructions = list(map(str, input().split()))
  if instructions[0] == '1':
    S, t = t, S
  else:
    if instructions[1] == '1':
      t += instructions[2]
    else:
      S += instructions[2]
print(t[::-1] + S)

Submission status: Accepted

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
rev=0
for i in range(q):
    t=list(input().split())
    if t[0]=="1":
        rev=rev+1
    else:
        if t[1]=="1":
            if rev%2==0:
                s.appendleft(t[2])
            else:
                s.append(t[2])
        else:
            if rev%2==0:
                s.append(t[2])
            else:
                s.appendleft(t[2])
if rev%2==0:
    print("".join(s))
else:
    s.reverse()
    print("".join(s))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

from collections import deque
def solve():
    S = deque(list(input()))

    Q = II()
    rev = False
    for i in range(Q):
        q = list(input().split())
        # print(q)
        if q[0] == '1':
            rev = ~rev
        else:
            op = q[1]
            if (op == '1' and not rev) or op == '2' and rev:
                S.appendleft(q[2])
            else:
                S.append(q[2])

    S = list(S)
    if rev:
        S = S[::-1]

    print(''.join(S))

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

s = S()
q = I()
flag = 0
ans = deque(s)

for i in range(q):
    query = input()
    if query[0] == '1':
        flag = flag ^ 1  # flagを反転
    else:
        if (query[2] == '1' and not flag) or (query[2] == '2' and flag):
            ans.appendleft(query[4])
        else:
            ans.append(query[4])

if not flag:
    print("".join(ans))
else:
    print("".join(reversed(ans)))

Submission status: Accepted

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

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

print(t[::-1] + s)

Submission status: Accepted

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

s_mae = ''
s_ato = s

for _ in range(n):
    ipt = input()

    if ipt[0] == '1':
        s_mae, s_ato = s_ato, s_mae
    else:
        _, id_, s_ = ipt.split()
        is_head = True if id_ == '1' else False
        if is_head:
            s_mae += s_
        else:
            s_ato += s_

print(s_mae[::-1] + s_ato)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s, _, *q = open(0)
g = ""
s = s.strip()
for q in q:
  q = q.strip().split()
  if q[0] == "1":
    g, s = s, g
  else:
    if q[1] == "1":
      g += q[2]
    else:
      s += q[2]
print(g[::-1] + s)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

S = str(input())
Q = int(input())
q = [0 for i in range(Q)]
for i in range(Q):
    q[i] = list(input().split(" "))
# print(q)

status = True # True: normal, False: reversed
ans = collections.deque()
for s in list(S):
    ans.append(s)
for i in range(Q):
    query = q[i]
    if query[0] == "1":
        status = not(status)
    else:
        if query[1] == "1":
            if status:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        else:
            if status:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
if status:
    print("".join(list(ans)))
else:
    print("".join(reversed(list(ans))))

Submission status: Accepted

Solution code:
from collections import deque

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

judge = True
S = deque()
for string in s:
    S.append(string)

for query in queries:
    if query[0] == '1':
        judge = not judge
    else:
        if judge:
            if query[1] == '1':
                S.appendleft(query[2])
            else:
                S.append(query[2])
        else:
            if query[1] == '1':
                S.append(query[2])
            else:
                S.appendleft(query[2])
ans = ''.join(S)
if not judge:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

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


def main():
    S = input().strip()
    Q = int(input())
    Query = [list(input().split()) for _ in range(Q)]
    reverse = 1
    head = ""
    tail = ""
    for T in Query:
        if T[0] == "1":
            if reverse == 1:
                reverse = 2
            else:
                reverse = 1
        elif T[0] == "2":
            if int(T[1]) == reverse:
                head = T[2] + head
            else:
                tail = tail + T[2]
    S = head + S + tail
    print(S) if reverse == 1 else print(S[::-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
s = readline().rstrip()
q = int(readline())
queries = [[i for i in readline().split()] for _ in range(q)]

t = ''

for query in queries:
    if query[0] == '1':
        s,t = t,s
    else:
        if query[1] == '1':
            t += query[2]
        else:
            s += query[2]
            
print(t[::-1]+s)

Submission status: Accepted

Solution code:
from collections import deque
S = deque()
tmp = input()
for i in range(len(tmp)):
  S.append(tmp[i])
Q = int(input())
flag = True
for i in range(Q):
  string = input().split()
  if string[0] == '1':
    flag = not flag
  elif flag ^ (string[1] == '1'):
      S.append(string[2])
  else:
      S.appendleft(string[2])
if not flag:
  S.reverse()
for s in S:
  print(s, end='')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
s = readline().rstrip()
q = int(readline())
queries = [[i for i in readline().split()] for _ in range(q)]

from collections import deque

d = deque([i for i in s])
rev = 1

for query in queries:
    if query[0] == '1':
        rev *= -1
    else:
        if rev == 1:
            if query[1] == '1':
                d.appendleft(query[2])
            else:
                d.append(query[2])
        else:
            if query[1] == '1':
                d.append(query[2])
            else:
                d.appendleft(query[2])

s = ''.join(d)
if rev == 1:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
booh = 0
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

fi=""
la=""
q = queue.deque()
for a in qa:
	if a[0]==1:
		booh = (booh+1)%2
	else:
		if (a[1]+booh)%2==1:
			fi=a[2]+fi
			q.appendleft(a[2])
		else:
			la+=a[2]

s = "".join(list(q))+s+la
if booh==1:
	s = t1(s)

print(s)

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
booh = 0
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

fi=""
la=""
for a in qa:
	if a[0]==1:
		booh = (booh+1)%2
	else:
		if (a[1]+booh)%2==1:
			fi=a[2]+fi
		else:
			la+=a[2]

s = fi+s+la
if booh==1:
	s = t1(s)

print(s)

Submission status: Accepted

Solution code:
# D - String Formation

from collections import deque

S = deque([s for s in input()])
Q = int(input())
rev = False

for _ in range(Q):
    T = [t for t in input().split()]
    t = int(T[0])
    if t == 1:
        rev = not rev
    if t == 2:
        f, c = int(T[1]), T[2]
        if f == 1:
            if rev:
                S.append(c)
            else:
                S.appendleft(c)
        if f == 2:
            if rev:
                S.appendleft(c)
            else:
                S.append(c)

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
m = 0
t = ''
l = ''
for i in range(q):
    Q = input().split()
    if(Q[0] == '1'):
        m = m ^ 1
        # t,l = l[::-1],t[::-1]
    elif(Q[0] == '2'):
        if(Q[1] == '1'):
            if(m == 0):t += Q[2]
            else:l += Q[2]
        elif(Q[1] == '2'):
            if(m == 0):l += Q[2]
            else:t += Q[2]
# print(t,s,l)
s = t[::-1] + s + l

if(m == 1):s = s[::-1]
print(s)