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:
from collections import deque
s = input()
q = int(input())

s = deque(s)
cnt = 0

for i in range(q):
    Q = list(input().split())
    if int(Q[0]) == 1:
        cnt += 1
    else:
        if int(Q[1]) == 1:
            if cnt % 2 == 0:
                s.appendleft(Q[2])
            else:
                s.append(Q[2])
        else:
            if cnt % 2 == 0:
                s.append(Q[2])
            else:
                s.appendleft(Q[2])

ans = ''

for _ in range(len(s)):
    if cnt % 2 == 0:
        ans += s.popleft()
    else:
        ans += s.pop()

print(ans)

Submission status: Accepted

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

flg = False
for i in range(q):
    p = list(map(str, input().split()))
    if p[0] == "1":
        flg = True if flg == False else False
        continue
    else:
        if (p[1] == "1" and not flg) or (p[1] == "2" and flg):
            former.appendleft(p[2])
        else:
            latter.append(p[2])

ans = list(former) + list(s) + list(latter)
ans = "".join(ans)
print(ans if not flg else ans[::-1])

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
cnt = 0
for i in range(Q):
    q = list(input().split())
    if len(q)==1:
        cnt += 1
    else:
        F = int(q[1])
        C = q[2]
        if cnt%2==0:
            if F==1:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F==1:
                S.append(C)
            else:
                S.appendleft(C)
if cnt%2==0:
    print("".join(S))
else:
    S = list(S)[::-1]
    print("".join(S))

Submission status: Accepted

Solution code:
flip = 0
front = ""
back = ""

S = input()
Q = int(input())
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        flip ^= 1
        continue
    _, f, c = q
    if not flip:
        if f == "1":
            front = c + front
        else:
            back += c
    else:
        if f == "1":
            back += c
        else:
            front = c + front

ans = front + S + back
if flip:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    S = deque([s])
    flg = 1
    for i in range(q):
        if query[i][0] == "1":
            flg ^= 1
        else:
            _, f, c = query[i]
            if f == "1":
                S.appendleft(c) if flg else S.append(c)
            else:
                S.append(c) if flg else S.appendleft(c)

    print("".join(S) if flg else "".join(S)[::-1])



if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

ans = deque(s)
ok = True
for v in q:
  if v[0] == "1":
    if ok: ok = False
    else: ok = True
  else:
    if v[1] == "1":
      if ok: ans.appendleft(v[2])
      else: ans.append(v[2])
    else:
      if ok: ans.append(v[2])
      else: ans.appendleft(v[2])

ans = list(ans)
if not ok: ans = ans[::-1]
print("".join(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
flag = 1
for i in range(Q):
    a = list(input().split())
    if a[0] == '1':
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        if a[1] == '1':
            if flag == 1:
                S.appendleft(a[2])
            else:
                S.append(a[2])
        else:
            if flag == 1:
                S.append(a[2])
            else:
                S.appendleft(a[2])
if flag == 1:
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
S = input()
SS = deque()
for s in S:
    SS.append(s)
Q = int(input())
Que = [list(input().split()) for _ in range(Q)]
r = 0

for q in Que:
    if q[0] == '1':
        r += 1
    else:
        if (q[1] == '1' and r % 2 == 1) or (q[1] == '2' and r % 2 == 0):
            SS.append(q[2])
        else:
            SS.appendleft(q[2])
if r % 2 == 0:
   while len(SS) > 0:
        s = SS.popleft()
        print(s, end='')    
else:
    while len(SS) > 0:
        s = SS.pop()
        print(s, end='')
print('')

Submission status: Accepted

Solution code:
from collections import deque

s = list(input())
s = deque(s)
n = int(input())
dire = False

for i in range(n):
    q = input().split()
    if q[0] == '1':
        dire = not dire
    else:
        t = int(q[1])-1

        if dire ^ t:
            s.append(q[2])
        else:
            s.appendleft(q[2])
if dire:
    s.reverse()

print(''.join(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
S = collections.deque(list(input()))
Q = int(input())
spin = False
for i in range(Q):
    temp = list(input().split())
    if temp[0] == "1":
        spin = not spin
    else:
        if spin:
            if temp[1] == "1":
                S.append(temp[2])
            else:
                S.appendleft(temp[2])
        else:
            if temp[1] == "1":
                S.appendleft(temp[2])
            else:
                S.append(temp[2])

res = ""
if spin:
    S.reverse()
    print("".join(S))
else:
    print("".join(S))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
keep=1
mae=""
ato=""
for i in range(q):
  lis=list(map(str,input().split()))
  if lis[0]=="1":
    keep*=-1
  elif lis[1]=="1":
    if keep==1:
      mae=lis[2]+mae
    else:
      ato+=lis[2]
  else:
    if keep==1:
      ato+=lis[2]
    else:
      mae=lis[2]+mae

if keep==1:
  print(mae+s+ato)
else:
  print(ato[::-1]+s[::-1]+mae[::-1])

Submission status: Accepted

Solution code:
#D
#import sys
#input = sys.stdin.readline
from collections import deque
s = deque(input())
q = int(input())
#flg:反転してるときtrue,してないときFalse
flg = False

for _ in range(q):
    t = tuple(map(str,input().split()))
    if t[0] == '1':
        if flg:
            flg = False
        else:
            flg = True
    else:
        if t[1] == "1" and flg == False:
            s.appendleft(t[2])
        elif t[1] == "1" and flg == True:
            s.append(t[2])
        elif t[1] == "2" and flg == False:
            s.append(t[2])
        else:
            s.appendleft(t[2])
 
    
if flg:
    s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from  collections import deque

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

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

deque_S = deque(S)
re_flag = 1
for i in range(Q):
    que = str(input())

    if len(que) == 1:
        #S = S[::-1]
        re_flag *= -1

    else:
        if que[2] == '1':
            if re_flag == 1:
                #S = que[4:] + S
                deque_S.appendleft(que[4:])
            else:
                #S =  S + que[-1:3:-1]
                deque_S.append(que[-1:3:-1])
        if que[2] == '2':
            if re_flag == 1:
                #S = S + que[4:]
                deque_S.append(que[4:])
            else:
                #S = que[-1:3:-1] + S
                deque_S.appendleft(que[-1:3:-1])

ans = ''
if re_flag == 1:
    ans = ''.join(deque_S)
else:
    ans = ''.join(deque_S)[::-1]
#print(deque_S)

print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    S = _S()
    Q = I()
    Querys = [LS() for _ in range(Q)]
    rev_flg = -1
    dq = collections.deque(S)
    for q in Querys:
        if q[0]=='1':
            rev_flg *= -1
        else:
            ti,fi,ci = q
            if int(fi)*rev_flg in (1,-2):
                dq.append(ci)
            else:
                dq.appendleft(ci)

    print("".join(dq)[::-1] if rev_flg==1 else "".join(dq))

main()

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 tmp[1] == '1':
      if rev == False:
        head.append(tmp[2])
      if rev == True:
        tail.append(tmp[2])
    if tmp[1] == '2':
      if rev == False:
        tail.append(tmp[2])
      if rev == True:
        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:
s = input()
q = int(input())
t = 0
f = []
b = []
for _ in range(q):
    a = input().split()
    if len(a) == 1:
        t += 1
    else:
        m, n = int(a[1]), a[2]
        if t % 2 == 0:
            if m == 1:
                f.append(n)
            else:
                b.append(n)
        else:
            if m == 1:
                b.append(n)
            else:
                f.append(n)
if t % 2 == 0:
    x = ''
    for i in f:
        x = i + x
    x = x + s
    for i in b:
        x = x + i
    print(x)
else:
    x = ''
    for i in b:
        x = i + x
    x = x + s[::-1]
    for i in f:
        x = x + i
    print(x)

Submission status: Accepted

Solution code:
s = [ str( input() ) ]
q = int( input() )
query = []
rev_flag_k = 0
for _ in range( q ):
    query_i = list( input().split() )
    query.append( query_i )
    if query_i[ 0 ] == "1":
        rev_flag_k += 1

top = []
rev_flag = 0
for query_i in query:
    t = query_i[ 0 ]
    if t == "1":
        rev_flag += 1
    else:
        f = query_i[ 1 ]
        c = query_i[ 2 ]
        if f == "1" and rev_flag % 2 == 1:
            s.append( str( c ) )
        elif f == "2" and rev_flag % 2 == 0:
            s.append( str( c ) )
        else:
            top.append( str( c ) )
s = "".join( top )[::-1] + "".join( s )
if rev_flag_k % 2 == 1:
    s = s[::-1]

print( s )

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
que = deque([s])
flg=False
for _ in range(q):
    a = list(input().split())
    if a[0]=='1':
        flg=not(flg)
#        s=s[::-1]
    else:
        if flg ^ (a[1]!='1'):
            que.append(a[2])
        else:
            que.appendleft(a[2])

ans = ''.join(que)

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

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
que = deque([s])
flg=False
for _ in range(q):
    a = list(input().split())
    if a[0]=='1':
        flg=not(flg)
#        s=s[::-1]
    else:
        if flg and a[1]=='1' or not flg and a[1]=='2':
            que.append(a[2])
        else:
            que.appendleft(a[2])

ans = ''.join(que)

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

Submission status: Accepted

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

S = deque(input().rstrip())
Q = int(input())
lst = deque()
 
for i in range(Q):
    tmp = list(input().split())
    lst.append(tmp)

reverse = 0
c = 1
for i in range(Q):
 
    if int(lst[i][0]) == 1:
        reverse += 1
        c += 1
    
    if int(lst[i][0]) == 2:
        if c%2 == 1:
            if int(lst[i][1]) == 1:
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
        else:
            if int(lst[i][1]) == 2:
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
 
if reverse%2 == 1:
    S.reverse()
 
ans = "".join(S)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import collections

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

S = ns()
Q = ni()


def solve():
    ds = collections.deque(S)
    rev = False
    for i in range(Q):
        qt, *q = ns().split()
        if qt == "1":
            rev = not rev
            continue
        assert qt == "2"
        if (q[0] == "1") ^ rev:
            ds.appendleft(q[1])
        else:
            assert (q[0] == "2") ^ rev
            ds.append(q[1])
    if rev:
        ds = reversed(ds)
    return "".join(ds)


print(solve())

Submission status: Accepted

Solution code:
from collections import deque

S = input()
Q = int(input())
s = deque(list(S))
re = False
 
for i in range(Q):
    q = list(map(str,input().split()))
    if q[0] == "1":
        re = not re
    else:
        b = int(q[1])-1
        if (re and b) or not(re or b):
            s.appendleft(q[2])
        else:
            s.append(q[2])
 
ans = ""
if re:
    while s:
    	ans += s.pop()
else:
    while s:
        ans += s.popleft()
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
from collections import deque
s = deque([i for i in S])
re = False

for i in range(Q):
    q = list(map(str,input().split()))
    if q[0] == "1":
        re = not re
    else:
        b = int(q[1])-1
        if (re and b) or not(re or b):
            s.appendleft(q[2])
        else:
            s.append(q[2])

ans = ""
if re:
    while s:
    	ans += s.pop()
else:
    while s:
        ans += s.popleft()
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
d = deque([])
ans = input()
d.append(ans)
q = int(input())
rev = True # Trueなら後ろに # Falseなら前に
for i in range(q):
    Query = list(map(str,input().split()))
    if Query[0] == '2':
        if Query[1] == '1':
            if rev:
                d.append(Query[2])
            else:
                d.appendleft(Query[2])
        elif Query[1] == '2':
            if rev:
                d.appendleft(Query[2])
            else:
                d.append(Query[2])
    else:
        rev = rev^True
output = ""
while d:
    output += d.pop()
if rev:
    print(output)
else:
    print(output[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

mae = deque()
usi = deque()
k = 0
m = 0
for i in range(q):
    a = input().split()
    if(a[0] == '1'):
        k += 1
    if(a[0] == '2'):
        if(k %2 == 0):
            if(a[1] == '1'):
                mae.appendleft(a[2])
            else:
                usi.append(a[2])
        else:
            if(a[1] == '1'):
                usi.append(a[2])
            else:
                mae.appendleft(a[2])
mae = list(mae)
usi = list(usi)

if(k % 2 == 1):
    mae = mae[::-1]
    usi=usi[::-1]
    s = s[::-1]
    s = ''.join(usi+s+mae)
else:
    s = ''.join(mae+s+usi)
print(s)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            S.appendleft(t[2])
        else:
            S.append(t[2])
if r:
    S = list(S)
    print(''.join(S[::-1]))
else:
    print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
#入力
s = input()
m = int(input())
q_list = [list(input().split()) 
          for nesya in range(m)]
d = deque(s)
n = 0
for q in q_list:
  if q[0] == '1':
    n += 1
  else:
    if (n%2 == 0 and q[1] == '1') or (n%2 == 1 and q[1] == '2'):
      d.appendleft(q[2])
    else:
      d.append(q[2])
ans = ''.join(d)
if n%2 == 0:
  print(ans)
else:
  print(ans[::-1])

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num__grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]
# ''.join(s)

s = list(s())
s_deque = deque(s)
q = i()
query = ms(q)

cnt = 0
v = ["r", "l"]
vector = v[cnt]

for qu in query:
    if qu[0] == "1":
        cnt += 1
        vector = v[cnt % 2]
    else:
        if qu[1] == "1":
            if vector == "r":
                s_deque.appendleft(qu[2])
            else:
                s_deque.append(qu[2])
        elif qu[1] == "2":
            if vector == "r":
                s_deque.append(qu[2])
            else:
                s_deque.appendleft(qu[2])

print(''.join(s_deque)[::(-1) ** (cnt % 2)])

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
flip = 1
for i in range(q):
    T = input().split()
    if T[0] == '1':
        flip *= -1
    else:
        _flip = 1 if T[1]=='1' else -1
        if (flip * _flip)==1:
            s.appendleft(T[2])
        else:
            s.append(T[2])
print("".join(s) if flip==1 else "".join(reversed(s)))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
querys = [list(map(str,input().split())) for _ in range(q)]

direction = 0
mae = ""
ato = ""

for i,query in enumerate(querys):
    if query[0] == "1":
        direction = (direction+1)%2
    else:
        if (int(query[1])-1 + direction)%2 == 0:
            mae += query[2]
        else:
            ato += query[2]

s = mae[::-1] + s
s = s + ato

if direction == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
s = str(input())
q = int(input())
cnt = 0
L = []
R = []
for _ in range(q):
    query = list(input().split())
    if query[0] == '1':
        cnt += 1
    else:
        if query[1] == '1':
            if cnt % 2:
                R.append(query[2])
            else:
                L.append(query[2])
        else:
            if cnt % 2:
                L.append(query[2])
            else:
                R.append(query[2])
s = (''.join(L[::-1]) + s + ''.join(R))
if cnt % 2:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = input()
    Q = int(input())
    
    SS = deque()
    for s in S:
        SS.append(s)
    
    cnt = 0
    for i in range(Q):
        query = input().split()
        if(query[0] == "1"):
            cnt += 1
        else:
            if(query[1] == "1"):
                if(cnt%2 == 0):
                    SS.appendleft(query[2])
                else:
                    SS.append(query[2])
            else:
                if(cnt%2 == 0):
                    SS.append(query[2])
                else:
                    SS.appendleft(query[2])
    
    ans = list(SS)
    if(cnt%2 == 0):
        print(''.join(ans))
    else:
        print(''.join(ans[::-1]))
main()

Submission status: Accepted

Solution code:
# import sys
# import math
# import itertools
from collections import deque
# from collections import defaultdict
# import heapq
# import copy
# import bisect
# import numpy as np
# from scipy.special import comb

# def my_input(): return sys.stdin.readline().rstrip()
# sys.setrecursionlimit(100000)
# INF = 1001001001001
# MOD = 1000000007


def main():

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

    str_q = deque()

    for s in S:
        str_q.append(s)

    reverse = False
    for _ in range(Q):
        t, *query = input().split()
        if t == '1':
            reverse = not reverse
        else:
            f, c = query
            if f == '1':
                if reverse:
                    str_q.append(c)
                else:
                    str_q.appendleft(c)
            else:
                if reverse:
                    str_q.appendleft(c)
                else:
                    str_q.append(c)

    ans = list(str_q)
    if reverse:
        ans = ans[::-1]
    print(''.join(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def ep(*params):
    print(*params,file=sys.stderr)

l = ''
r = ''

S = input().rstrip()

Q = int(input())
mode = 0
for _ in range(Q):
    ope = list(input().split())
    if ope[0] == '1':
        mode = (mode + 1)%2
    else:
        op = (int(ope[1]) + mode)%2
        if op == 1:
            l += ope[2]
        else:
            r += ope[2]
    #print(mode,l[::-1],S,r,sep=":",file=sys.stderr)    

if mode == 0:
    print(l[::-1],S,r,sep="")    
else:
    print(r[::-1],S[::-1],l,sep="")

Submission status: Accepted

Solution code:
from collections import deque


def main():
    s = deque(list(input()))
    q = int(input())
    rev = False
    for _ in range(q):
        query = input().split()
        if query[0] == "1":
            if not rev:
                rev = True
            else:
                rev = False
        else:
            if query[1] == '1':
                pass
                if rev:
                    s.append(query[2])
                else:
                    s.appendleft(query[2])
            else:
                if rev:
                    s.appendleft(query[2])
                else:
                    s.append(query[2])
    if rev:
        s.reverse()
    print(*s, sep="")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

s = deque(list(map(str, input().rstrip())))
q = int(input())

state = 1
for _ in range(q):
    li = list(map(str, input().split()))
    if li[0] == "1":
        state *= -1
    elif (li[1] == "1" and state == 1) or (li[1] == "2" and state == -1):
        s.appendleft(li[2])
    else:
        s.append(li[2])

if state == -1:
    s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque 
def main():
    s = deque(readline().rstrip().decode())
    q = int(readline())
    dir = 1
    for _ in range(q):
        txt = readline().rstrip().decode()
        if txt[0] == '1':
            dir += 1
            dir%=2
        elif txt[0] == '2':
            f = txt[2]
            c = txt[4]
            if ((dir==1) and (f=='1')) or ((dir==0) and (f=='2')):
                s.appendleft(c)
            else:
                s.append(c)
    if dir==0:
        print(''.join(reversed(s)))
    else:
        print(''.join(s))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque 
def main():
    s = deque(readline().rstrip().decode())
    q = int(readline())
    dir = 1
    for _ in range(q):
        txt = readline().rstrip().decode()
        if txt[0] == '1':
            dir ^= 1
        elif txt[0] == '2':
            f = txt[2]
            c = txt[4]
            if ((dir==1) and (f=='1')) or ((dir==0) and (f=='2')):
                s.appendleft(c)
            else:
                s.append(c)
    if dir!=1:
        print(''.join(reversed(s)))
    else:
        print(''.join(s))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
rev = False
for _ in range(q):
    que = input()
    if que == "1":
        rev = not rev
    else:
        f,c = que[2:].split()
        if (f == "1") ^ rev:
            s.appendleft(c)
        else:
            s.append(c)

if rev: s.reverse()

print("".join(s))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
nT = 0
s1 = []
s2 = []
for i in range(Q):
  Query = input()
  if Query == "1":
    nT += 1
    continue
  T, F, C = Query.split()
  if (F == "1") ^ (nT%2 == 1):
    s1.append(C)
  else:
    s2.append(C)
#print(s1, s2)
r = "".join(reversed(s1))+S+"".join(s2)
if nT%2 == 1:
  r = "".join(reversed(r))
print(r)

Submission status: Accepted

Solution code:
s = input()
d = True
f = '' # Front
b = '' # Back
q = int(input())
for _ in range(q):
    a = input().split()
    if a[0] == '1':
        d = not d
    else:
        if a[1] == '1':
            if d:
                f = f + a[2]
            else:
                b = b + a[2]
        else:
            if d:
                b = b + a[2]
            else:
                f = f + a[2]

if not d:
    print(''.join([b[::-1], s[::-1], f]))
else:
    print(''.join([f[::-1], s, b]))

Submission status: Accepted

Solution code:
s = input()
d = True
f = '' # Front
b = '' # Back
q = int(input())
for _ in range(q):
    a = input().split()
    if a[0] == '1':
        d = not d
    else:
        if a[1] == '1':
            if d:
                f = ''.join([a[2], f])
            else:
                b = ''.join([b, a[2]])
        else:
            if d:
                b = ''.join([b, a[2]])
            else:
                f = ''.join([a[2], f])

if not d:
    print(''.join([b[::-1], s[::-1], f[::-1]]))
else:
    print(''.join([f, s, b]))

Submission status: Accepted

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

tag = False
que = deque([S])

for i in range(Q):
    if query[i][0] == str(1):
        if tag == False:
            tag = True
        else:
            tag = False
    elif query[i][0] == str(2):
        if query[i][1] == str(1):
            if tag == False:
                que.appendleft(query[i][2])
            else:
                que.append(query[i][2])
        else:
            if tag == False:
                que.append(query[i][2])
            else:
                que.appendleft(query[i][2])
ans = "".join(que)
if tag == True:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
 
a = []
b = []
rev = 0
 
for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        a, b = b, a
        rev += 1
    else:
        if q[1] == '1':
            a.append(q[2])
        else:
            b.append(q[2])
if rev % 2 == 1:
    S = S[::-1]
ans = ''.join(a[::-1]) + S + ''.join(b)
print(ans)

Submission status: Accepted

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

reverse=0
Top=[]
Bottom=[]
for i in range(q):
    if Q[i][0]=="1":
        reverse+=1
    elif Q[i][0]=="2" and reverse%2==0:
        if Q[i][1]=="1":
            Top.append(Q[i][2])
        elif Q[i][1]=="2":
            Bottom.append(Q[i][2])
    elif Q[i][0]=="2" and reverse%2==1:
        if Q[i][1]=="2":
            Top.append(Q[i][2])
        elif Q[i][1]=="1":
            Bottom.append(Q[i][2])
if reverse%2==1:
    Top.reverse()
    SS=Top+S+Bottom
    SS.reverse()
elif reverse%2==0:
    Top.reverse()
    SS=Top+S+Bottom
for i in SS:
    print(i,end="")