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

Solution (Python):
from collections import deque
reverse = -1
S = deque(input())
Q = int(input())
for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        reverse *= -1
    else:
        if Query[1] == "1":
            if reverse == 1:
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
        else:
            if reverse == 1:
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

ans = "".join(S)
if reverse == 1:
    ans = ans[::-1]
print(ans)

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

Solution (Python):
from collections import deque

S=deque(input())
flag=0
Q=int(input())

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

    if q[0]=="1":
        flag=1-flag
    else:
        x,y=q[1],q[2]

        if (flag+int(x))%2==0:
            S.append(y)
        else:
            S.appendleft(y)

if flag==0:
    print("".join(S))
else:
    print("".join(list(S)[::-1]))

Solution (Python):
s = input()
pre = ""
suf = ""
rev = False

for i in range(int(input())):
        q = input().split()
        if len(q)==1: rev = not rev
        else:
                t, f, c = q
                f = int(f)
                if rev: f += 1
                f %= 2
                if f: pre += c
                else: suf += c
if rev: print(suf[::-1] + s[::-1] + pre)
else: print(pre[::-1] + s + suf)

Solution (Python):
S = input().strip()
Q = int(input())

head = ""
tail = ""
rev = False
for _ in range(Q):
	query = input().strip().split()
	if query[0] == "1":
		rev = not rev
	else:
		if query[1] == "1":
			if rev:
				tail = tail + query[2]
			else:
				head = query[2] + head
		else:
			if rev:
				head = query[2] + head
			else:
				tail = tail + query[2]

result = head + S + tail

if rev:
	result = "".join(reversed(result))

print(result)

Solution (Python):
ans = input()
q = int(input())
r = 0
p = ""
for i in range(q):
    d = input().split()
    if len(d)==1:
        r += 1
    else:
        f = int(d[1])
        c = d[2]
        if (f+r)%2 == 1:
            p += c
        else:
            ans += c
ans = p[::-1] + ans
if r%2:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque
S = list(input())
Q = int(input())
num = 1
q = deque(S)
for _ in range(Q):
    L = [i for i in input().split()]
    if len(L) == 1:
        num += 1
        num %= 2
    else:
        if int(L[1])%2 == num:
            q.appendleft(L[2])
        else:
            q.append(L[2])

ans = list(q)
if num == 0:
    ans.reverse()
print("".join(ans))

Solution (Python):
S = input()

Q = int(input())

mode = True
top = []
bottom = []

for i in range(Q):
    q = input().split()
    if len(q)==1:
        q=int(q[0])
        if q==1:
            if mode:
                mode=False
            else:
                mode=True
    else:
        t,f,c = q[0],q[1],q[2]
        if f=="1":
            if mode:
                top.append(c)
            else:
                bottom.append(c)
        else:
            if mode:
                bottom.append(c)
            else:
                top.append(c)

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

Solution (Python):
import sys
from collections import deque
s = deque(list(input()))


q = int(input())
rev = 0
for _ in range(q):
    query = list(input().split())
    if len(query) == 1:
        rev += 1
    else:
        if query[1] == "1":
            if rev % 2 == 0:
                s.appendleft(query[2])
            else:
                s.append(query[2])

        else:
            if rev % 2 == 1:
                s.appendleft(query[2])
            else:
                s.append(query[2])

s = "".join(s)
if rev%2 == 1:
    s = s[::-1]

print(s)

Solution (Python):
from collections import deque


def main():
    S = input()
    q = deque(S)
    order = 1
    Q = int(input())
    for _ in range(Q):
        X = input().split()
        if X[0] == "1":
            order = 3 - order
        else:
            f, c = int(X[1]), X[2]
            if f == order:
                q.appendleft(c)
            else:
                q.append(c)
    ans = "".join(q)
    if order == 2:
        ans = ans[::-1]
    print(ans)


if __name__ == "__main__":
    main()

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

from collections import deque
q = deque(S)
rev = False
for _ in range(Q):
    qr = input()
    if qr == '1':
        rev = not rev
        continue
    _,f,c = qr.split()
    if (f=='2') ^ rev:
        q.append(c)
    else:
        q.appendleft(c)

if rev:
    print(''.join(reversed(list(q))))
else:
    print(''.join(q))

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

r_addition = ""
l_addition = ""
is_rev = False

for i in range(q):
    qs = input().split()

    if len(qs) == 1:
        is_rev = not is_rev
        
    else:
        if int(qs[1]) == 1:
            if is_rev:
                r_addition += qs[2]
            else:
                l_addition += qs[2]
        else:
            if is_rev:
                l_addition += qs[2]
            else:
                r_addition += qs[2]







if is_rev:
    print(r_addition[::-1]+s[::-1]+l_addition)
else:
    print(l_addition[::-1]+s+r_addition)

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

from collections import deque
ans = deque(list(s))
flip = 0

for i in range(q):
    
    _query = query[i]
    if len(_query) == 1:
        flip = 1 - flip
    else:
        if _query[1] == '1':
            if flip:
                ans.append(_query[2])
            else:
                ans.appendleft(_query[2])
        else:
            if not flip:
                ans.append(_query[2])
            else:
                ans.appendleft(_query[2])

if not flip:
    print(''.join(ans))
else:
    print(''.join(list(ans)[::-1]))

Solution (Python):
from collections import deque
s=list(input())
d=deque(s)
q=int(input())
ct=0
for i in range(q):
  l=list(input().split())
  if l[0]=='1':
    ct+=1
  else:
    if ct%2==0:
      if l[1]=='1':
        d.appendleft(l[2])
      else:
        d.append(l[2])
    else:
      if l[1]=='2':
        d.appendleft(l[2])
      else:
        d.append(l[2])
d=list(d)
if ct%2==1:
  d.reverse()
print(''.join(d))

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

s = collections.deque(input())
Q = int(input())
for _ in range(Q):
  I = input()
  if len(I) == 1:
    count += 1
  else:
    F = int(I[2])
    C = I[4]
    k = (count + F ) % 2
    if k == 0:
      s.append(C)
    else:
      s.appendleft(C)
if count % 2:
  s.reverse()
for i in s:
  print(i,end = "")
print()

Solution (Python):

S = list(input())

Q = int(input())

state = 0
s = []
e = []

for loop in range(Q):

    T = input()

    if T == "1":
        state ^= 1
    else:

        t,F,C = T.split()
        F = int(F)
        

        if state == 0 and F == 1:
            s.append(C)
        elif state == 0 and F == 2:
            e.append(C)
        elif F == 1:
            e.append(C)
        else:
            s.append(C)

s.reverse()
ans = s+S+e
if state == 1:
    ans.reverse()

print ("".join(ans))

Solution (Python):
import sys;input=sys.stdin.readline
S=input().strip()
n=len(S)
Qs = []
for c in S:
    Qs.append((1,1,c))
for q in range(int(input())):
    x = input().strip().split()
    if len(x) == 1:
        Qs.append((0,0,0))
    else:
        n+=1
        Qs.append((1,int(x[1])-1,x[2]))

rs = [0]*n
i,j=0,n-1
f=1
for t, tt, c in Qs[::-1]:
    if t==0:
        f=1-f
    else:
        if (tt+f)%2:
            rs[i] = c
            i+=1
        else:
            rs[j] = c
            j -= 1
print("".join(rs))

Solution (Python):
s = input()
q = int(input())
cnt = 0
front = []
back = []
for i in range(q):
    t = input()
    if t[0] == "1":
        cnt += 1
        cnt %= 2
    else:
        _, f, c = t.split()
        f = int(f)
        x = f+cnt
        if x%2 == 1:
            front.append(c)
        else:
            back.append(c)

a = "".join(front)
a = a[::-1]
b = "".join(back)
ans = "".join([a,s,b])
if cnt%2 == 1:
    ans = ans[::-1]
print(ans)

Solution (Python):
from collections import deque
s = input()
s = deque([i for i in s])
q = int(input())
r = False
for _ in range(q):
    qu = input()
    if qu == "1":
        r = not r
    else:
        _, f, c = [i for i in qu.split()]
        if (f != "1") == r:
            s.appendleft(c)
        else:
            s.append(c)

s = "".join(s)
if r:
    s = reversed(s)
print("".join(s))

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline

S = deque(input().rstrip())
Q = int(input())
rev = False
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        rev = rev ^ True
    else:
        F, C = q[1:]
        if (F == '1') ^ rev:
            S.appendleft(C)
        else:
            S.append(C)
S = list(S)
if rev:
    S.reverse()
print(''.join(S))

Solution (Python):
s=list(input())
q=int(input())
r=0
m=[]
u=[]
for i in range(q):
    p=list(map(str,input().split()))
    if int(p[0])==1:
        r+=1
    else:
        if (int(p[1])==1 and r%2==0) or (int(p[1])==2 and r%2==1):
            m.append(p[2])
        else:
            u.append(p[2])
if len(m)>0:
    m.reverse()
ans=m+s+u
if r%2==1:
    ans.reverse()
print(''.join(ans))

Solution (Python):

import sys
input = sys.stdin.readline
from collections import deque

state = 0
s = deque(input().rstrip())
q = int(input())
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        state ^= 1
    else:
        f = int(query[1]); c = query[2]
        if state == 0:
            if f == 1:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == 1:
                s.append(c)
            else:
                s.appendleft(c)

if state == 0:
    ans = "".join([item for item in s])
else:
    s.reverse()
    ans = "".join([item for item in s])

print(ans)

Solution (Python):
from collections import deque
S = deque(input())
Q = int(input())
rev = False
for i in range(Q):
  q = input()
  if len(q) == 1:
    rev = False if rev else True
  else:
    q, f, c = q.split()
    if rev:
      if f == "1":
        S.append(c)
      else:
        S.appendleft(c)
    else:
      if f == "1":
        S.appendleft(c)
      else:
        S.append(c)
print("".join(reversed(S)) if rev else "".join(S))

Solution (Python):
S = input()
Q = int(input())
F,B = "",""
k = 1
for i in range(Q):
    q = list(input().split())
    if int(q[0])==1:k = abs(k-1)
    else:
        if int(q[1])==1:
            if k:F+=q[2]
            else:B+=q[2]
        else:
            if k:B+=q[2]
            else:F+=q[2]
if k:print(F[::-1]+S+B)
else:print(B[::-1]+S[::-1]+F)

Solution (Python):
from collections import deque
S = input()
S = deque(S)

Q = int(input())
flipped = False
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        flipped = not flipped
    elif (q[1] == '1') == flipped:
        S.append(q[2])
    else:
        S.appendleft(q[2])

print(''.join(reversed(S)) if flipped else ''.join(S))

Solution (Python):
import sys

s = input()
q = int(input())
rev = 0
append_front_rev = ''
append_rear = ''

for line in sys.stdin:
    q = line.strip()
    if q == '1':
        rev ^= 1
    else:
        _, f, c = q.split()
        if f == '1':
            if rev == 0:
                append_front_rev += c
            else:
                append_rear += c
        else:
            if rev == 0:
                append_rear += c
            else:
                append_front_rev += c

s = append_front_rev[::-1] + s + append_rear
if rev:
    s = s[::-1]
print(s)

Solution (Python):
def main():
    S = input()
    Q = int(input())
    flip = False
    head = ''
    tail = ''
    for _ in range(Q):
        q = input()
        if q == '1':
            flip = not flip
            head, tail = tail, head
        else:
            _, f, c = q.split()
            if f == '1':
                head += c
            else:
                tail += c
    if flip:
        S = S[::-1]
    return head[::-1] + S + tail
   

print(main())

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline
s = deque(input().rstrip())
q = int(input())
flg = 0
for _ in range(q):
  t = input()
  if t[0] == "1":
    flg = 1-flg
  else:
    _,x,y = t.split()
    if x == "1":
      if flg:
        s.append(y)
      else:
        s.appendleft(y)
    else:
      if flg:
        s.appendleft(y)
      else:
        s.append(y)
if flg:
  u = list(s)[::-1]
  print(*u,sep="")
else:
  print(*s,sep="")

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

jun = 1
front = []
back = []

for _ in range(Q):
    inp = input().split()
    if len(inp) == 1:
        jun = 1 - jun
    else:
        _, F, C = inp
        if jun and F == "1":
            front.append(C)
        elif jun and F == "2":
            back.append(C)
        elif not jun and F == "1":
            back.append(C)
        else:
            front.append(C)

front = front[::-1]
ans = "".join(front) + S + "".join(back)
if not jun:
    ans = ans[::-1]

print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

s = input()
q = ni()
f = 0
top = ''
bot = ''
for _ in range(q):
    w = input().split()
    if w[0] == '1':
        f ^= 1
        top,bot = bot,top
    elif w[1] == '1':
        top += w[2]
    else:
        bot += w[2]
print(top[::-1] + s[::1-f*2] + bot)

Solution (Python):
from collections import deque
d = deque()
S = input()
for i in S:
    d.append(i)
Q = int(input())
f = True
for i in range(Q):
    t = input()
    if t == "1":
        f = bool(not f)
    else:
        _, F, C = (a for a in t.split())
        if f:
            if F == "1":
                d.appendleft(C)
            else:
                d.append(C)
        else:
            if F == "1":
                d.append(C)
            else:
                d.appendleft(C)
if not f:
    d.reverse()
print("".join(d))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

s = S()
s = deque(s)
n = I()
flg = 0
for _ in range(n):
    L = LS()
    if L[0] == '1':
        flg ^= 1
    else:
        if flg ^ (L[1] == '1'):
            s.appendleft(L[2])
        else:
            s.append(L[2])


if flg:
    s.reverse()
ans = ''.join(s)
print(ans)

Solution (Python):
import sys
def input():
	return sys.stdin.readline()[:-1]

s = input()

q = int(input())
front = ""
back = ""
sgn = 0
for _ in range(q):
	query = input().split()
	if query[0] == "1":
		sgn ^= 1
	else:
		f, c = int(query[1])-1, query[2]
		if f ^ sgn == 0:
			front += c
		else:
			back += c
if not sgn:
	print(front[::-1] + s + back)
else:
	print(back[::-1] + s[::-1] + front)

Solution (Python):
s=input()
q=int(input())
t=0
zengo=["",""]
for i in range(q):
    l=input().split()
    if len(l)==1:
        t^=1
    else:
        f,c=int(l[1])-1,l[2]
        zengo[f^t]+=c
if t:
    print(zengo[1][::-1]+s[::-1]+zengo[0])
else:
    print(zengo[0][::-1]+s+zengo[1])

Solution (Python):
rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

from collections import deque

A = deque(rr())
flipped = 0

Q = rri()
for _ in xrange(Q):
    query = rr().split()
    if query[0] == '1':
        flipped ^= 1
    else:
        f, c = query[1:]
        f = int(f)
        if f == 1:
            if flipped == 0:
                A.appendleft(c)
            else:
                A.append(c)
        else:
            if flipped == 0:
                A.append(c)
            else:
                A.appendleft(c)
    

if flipped == 0:
    print "".join(A)
else:
    print "".join(reversed(A))

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    s = input()
    t = deque()
    for i in s:
        t.append(i)
    q = I()
    k = 0
    for _ in range(q):
        qu = input().split()
        if qu[0] == "1":
            k ^= 1
        else:
            f,c = qu[1:]
            key = (int(f)-1)^k
            if not key:
                t.appendleft(c)
            else:
                t.append(c)
    ans = ""
    if not k:
        while t:
            ans += t.popleft()
    else:
        while t:
            ans += t.pop()
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import os
import sys
from collections import deque

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


S = sys.stdin.buffer.readline().decode().rstrip()
Q = int(sys.stdin.buffer.readline())
queries = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(Q)]

S = deque(list(S))
even = True
for query in queries:
    if query[0] == '1':
        even = not even
    else:
        _, f, c = query.split()
        if f == '1':
            if even:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if even:
                S.append(c)
            else:
                S.appendleft(c)

if not even:
    S.reverse()
ans = ''.join(S)
print(ans)

Solution (Python):
import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    S = list(input().strip("\n"))
    q = deque()
    for i, s in enumerate(S): q.append(s)
    state = 0 
    Q = int(input())
    for _ in range(Q):
        query = list(input().strip("\n").split())
        if query[0] == "1": state ^= 1
        else:
            f = int(query[1]) % 2
            c = query[2]
            if state ^ f == 1: q.appendleft(c)
            else: q.append(c)
    Ans = ""
    if state == 0:
        while q: Ans += q.popleft()
    else:
        while q: Ans += q.pop()
    print(Ans)
    
    

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
import sys

stdin = sys.stdin

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

s = ns()
head = []
tail = []
rev = False
q = ni()
for i in range(q):
    cs = stdin.readline().rstrip().split()
    if cs[0] == "1":
        rev ^= True
        head, tail = tail, head
    else:
        if cs[1] == "1":
            head.append(cs[2])
        else:
            tail.append(cs[2])
print("".join(head[::-1]) + (s[::-1] if rev else s) + "".join(tail))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
read = sys.stdin.read




s = input()
u = ""
t = ""
q = int(input())
num = 0

for _ in range(q):
    a = readline().split()
    if len(a)==1:
        u,t = t,u    
        num ^= 1
    else:
        f = int(a[1])
        c = a[2]
        if f==1:
            t += c
        else:
            u += c

if num:
    print(t[::-1]+s[::-1]+u)
else:
    print(t[::-1]+s+u)