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 sys
input = lambda: sys.stdin.readline().strip()
from collections import deque
s=deque(str(input()))
q=int(input())
flag=0
for i in range(q):
    j=input()
    if j[0]=="1":
        flag+=1
    else:
        if flag%2==1:
            if j[2]=="1":
                s.append(j[4])
            else:
                s.appendleft(j[4])
        else:
            if j[2]=="1":
                s.appendleft(j[4])
            else:
                s.append(j[4])
s=''.join(s)
if flag%2==1:
    s=s[::-1]
print(s)

Submission status: Accepted

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

from collections import deque
d = deque()
d.append(s)

count = 1
for i in range(q):
#     print(s)
    q_y_i = input().split()
#     print(q_y_i)
#     print(count)
#     print(d)
    if q_y_i[0] == '1':
        count +=1
    else:
        if count % 2 == 1:
            if q_y_i[1] == '1':
                d.appendleft(q_y_i[2])
#                 print('p1')
#                 print(d)
            elif q_y_i[1] == '2':
                d.append(q_y_i[2])
#                 print('p2')
        elif count % 2 == 0:
            if q_y_i[1] == '1':
                d.append(q_y_i[2])
#                 print('p3')
            elif q_y_i[1] == '2':
                d.appendleft(q_y_i[2])
#                 print('p4')

ans = ''
for v in d:
    ans = ans + v
if count % 2 == 0:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque([str for str in input()])
dic = 1
Q = int(input())
for i in range(0,Q):
    Query = input()
    if Query == '1':
        dic = dic*(-1)
    else:
        T, F, C = Query.split()
        if (int(F)*dic == 2) or (int(F)*dic == -1):
            S.append(C)
        else:
            S.appendleft(C)
print(''.join(list(S)[::dic]))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
result_q = deque(s)
t = False
for _ in range(q):
    List=[i for i in input().split()]
    if List[0] == '1':
        if t:
            t = False
        else:
            t = True
    else:
        if List[1] == '1': # F=1
            if t: # 反転時
                result_q.append(List[2])
            else:
                result_q.appendleft(List[2])
        else:
            if t: # 反転時
                result_q.appendleft(List[2])
            else:
                result_q.append(List[2])
if t:
    result_q.reverse()
for s in list(result_q):
    print(s, end="")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    s = input()
    q = int(input())

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

    flg = True
    result = s
    pre = ""
    post = ""
    for query in queries:
        if "1" == query[0]:
            flg = not flg
        elif "2" == query[0]:
            if ("1" == query[1] and flg) or ("2" == query[1] and not flg):
                pre = query[2] + pre
            else:
                post += query[2]

    if flg:
        print(pre + result + post)
    else:
        print((pre + result + post)[::-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    S = deque(input())
    Q = int(input())
    direction = 1

    for _ in range(Q):
        query = input().split()
        t = int(query[0])

        if t == 1:
            direction = direction *  -1

        elif t == 2:
            f = int(query[1])
            c = query[2]
            if f == 1:
                # add to front
                if direction == 1:
                   S.appendleft(c)
                # add to back
                if direction == -1:
                   S.append(c)
            elif f == 2:
                # add to back
                if direction == 1:
                   S.append(c)
                # add to front
                if direction == -1:
                   S.appendleft(c)
    S = list(S)
    if direction == -1:
        S = S[::-1]

    print(''.join(S))
    pass

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
is_r = False
f_list = []
b_list = []

for i in range(q):
    q_i = input().split(' ')
    t_i = q_i[0]
    if t_i == '1':
        is_r = not is_r
    else:
        f_i = q_i[1]
        if (f_i == '1' and not is_r) or (f_i == '2' and is_r):
            f_list.append(q_i[2])
        else:
            b_list.append(q_i[2])

f_list = f_list[::-1]
f_list.append(s)
f_list.extend(b_list)
s = ''.join(f_list) if not is_r else ''.join(f_list)[::-1]
print(s)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


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


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    st = S()
    Q = I()
    queries = []
    # print(st[0])
    # s = [st[i] for i in range(len(st))]
    s = deque()

    for i in range(len(st)):
        s.append(st[i])

    for i in range(Q):
        queries.append(list(MS()))

    op = True

    for i in range(Q):
        query = queries[i]
        if len(query) == 1:
            op = not op
        else:
            if (query[1] == '1' and op is True) or (query[1] == '2' and op is False):
                s.appendleft(query[2])
            elif (query[1] == '1' and op is False) or (query[1] == '2' and op is True):
                s.append(query[2])
            else:
                exit(-1)
                return

    # print(op)
    if op is True:
        print(''.join(s))
    else:
        n = []
        while len(s):
            n.append(s.pop())
        print(''.join(n))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

s=input()
q=int(input())
queries = []
rev = [0]*q
flag=0
for i in range(q):
    query = list( input().split() )
    queries.append( query )
    if query==["1"]:
        flag+=1
    rev[i]=flag

n_rev = flag
from collections import deque
left=deque([])
right=deque([])
for r,query in zip(rev,queries):
    if query!=["1"]:
        q1 = query[1]
        q2 = query[2]
        if (r+n_rev)%2==0:
            if q1=="1":
                left.appendleft(q2)
            else:
                right.append(q2)
        else:
            if q1=="1":
                right.append(q2)
            else:
                left.appendleft(q2) 
#     print(i,left,s,right)
if n_rev%2==1:
    s=s[::-1]
print("".join(["".join(left),s,"".join(right)]) )

Submission status: Accepted

Solution code:
from collections import deque

a = input()
q = int(input())
rflag = 0
b = []
for i in range(q):
    b.append(input())
# print(b)
# print(len(b[0]))
ans = deque(list(a))

for i in range(len(b)):
    if len(b[i]) == 1:
        rflag = (rflag ^ 1)
    else:
        c = int(b[i][2])
        # print("c="+b[i][2])

        if not rflag:

            if c == 1:
                # print("extend={}".format(b[i][4]))
                extend = b[i][4]
                ans.appendleft(extend)
            else:
                # print('ll')
                ans.append(b[i][4])
        else:
            if c == 1:
                ans.append(b[i][4])
            else:
                # print("extend={}".format(b[i][4]))
                extend = b[i][4]
                ans.appendleft(extend)
        # print(ans,rflag)

if not rflag:
    print("".join(ans))
else:
    h="".join(ans)
    print(h[::-1])

Submission status: Accepted

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

res = S

is_rev = False

before = []
after = []

def add_char(s, f, c, is_rev):
  if(f == "1"):
    before.append(c)
  else:
    after.append(c)

for _ in range(Q):
  q = input().split()
  
  if(q[0] == '1'): # Ti = 1
    is_rev = not is_rev
    before, after = after, before
  
  else: #Ti = 2
    add_char(res, q[1], q[2], is_rev)

before.reverse()

res = "".join(before) + (res[::-1] if is_rev else res) + "".join(after)

print(res)

Submission status: Accepted

Solution code:
from collections import deque

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

st=False#順方向

for _ in range(Q):
    query=input()
    if len(query)==1:
        st= not st
    else:
        _,F,C=query.split()
        F=int(F)
        if st==False:
            if F==1:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F==1:
                S.append(C)
            else:
                S.appendleft(C)

if st==False:
    ans=S
else:
    ans=reversed(S)

ans="".join(list(ans))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
d.extend(input())
q = int(input())
r = False
for i in range(q):
  qu = input().split()
  if qu[0] == "1":
    r = not r
  else:
    if (qu[1] == "1" and not r) or (qu[1] == "2" and r):
      d.extendleft(qu[2])
    else:
      d.extend(qu[2])

if r:
  d.reverse()

print(''.join(list(d)))

Submission status: Accepted

Solution code:
from sys import stdin as s
init=s.readline().rstrip()
state=1
s1=""
s2=""
q=int(s.readline())
for i in range(q):
  t=s.readline().rstrip()
  if t=="1":
    state=(state+1)%2
  else:
    a,b,c=t.split()
    if int(b)+state==2:
      s1=c+s1
    else:
      s2+=c
ans=s1+init+s2
if state:
  print(ans)
else:
  print(ans[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
length = len(s)
que = [[''] * 3 for i in range(q)]
for i in range(q):
    c = input()
    if c[0] == '1':
        que[i][0] = 1
    else:
        n, f, c = map(str, c.split())
        que[i][0], que[i][1], que[i][2] = int(n), int(f), c

rev_cnt = 0
left = [''] * 200000
right = [''] * 200000
left_cnt = 0
right_cnt = 0
for com in que:
    if com[0] == 1:
        rev_cnt += 1
        left, right = right, left
        left_cnt, right_cnt = right_cnt, left_cnt
    else:
        if com[1] == 1:
            left[left_cnt] = com[2]
            left_cnt += 1
        else:
            right[right_cnt] = com[2]
            right_cnt += 1

l = list(reversed(left[0:left_cnt]))
if rev_cnt % 2 == 0:
    center = list(s)
else:
    center = list(reversed(list(s)))
ans = l + center + right[0:right_cnt]
for c in ans:
    print(c, end='')
print()

Submission status: Accepted

Solution code:
s = input()
n = int(input())
b = ''
a = ''
h = 0
for i in range(n):
    qq = list(input().split())
    if qq[0] == '1':
        h += 1
    else:
        if qq[1] == '1':
            if h % 2 ==1:
                a = a + qq[2]
            else:
                b = qq[2] + b
        else:
            if h % 2 ==1:
                b = qq[2] + b
            else:
                a = a + qq[2]

s = b + s + a
if h % 2 ==1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
import sys
import math
import collections
from collections import deque
#n = int(input())
#a, b = map(int, input().split())
s = input()
#x = list(map(int, input().split()))
q = int(input())
d = deque([])
for i in range(len(s)):
    d.append(s[i])

rev = 1
for i in range(q):
    que = list(map(str, input().split()))
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                d.appendleft(que[2])
            else:
                d.append(que[2])

        else:
            if que[1] == "1":
                d.append(que[2])
            else:
                d.appendleft(que[2])


if rev == 1:
    for i in range(len(d)):
        print(d.popleft(), end="")
else:
    for i in range(len(d)):
        print(d.pop(), end="")

Submission status: Accepted

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

qs = list()
for i in range(Q):
    qs.append(input().split())

# ns = s
cnt_r = 0
bs, es = '', ''
# for i in range(Q):
#     q = input().split()
for q in qs:
    if len(q) == 1:
        cnt_r += 1
    else:
        f, c = int(q[1]), q[2]
        if f == 1:
            if cnt_r % 2 == 1:
                es += c
                # ns = ns + c
            else:
                bs = c + bs
                # ns = c + ns
        else:
            if cnt_r % 2 == 1:
                bs = c + bs
                # ns = c + ns
            else:
                es += c
                # ns = ns + c

ns = bs + s + es
if cnt_r % 2 == 1:
    print(ns[::-1])
else:
    print(ns)

Submission status: Accepted

Solution code:
def solve():
    a = input()
    q = int(input())
    
    top = []
    tail = []
    rev = 0
    for _ in range(q):
        _q = [i for i in input().split()]
        t, f, c =0, 0, 0
        t = int(_q[0])
        if t == 2:
            f, c = int(_q[1]), _q[2]
        
        if t == 1:
            rev = 1 - rev
        elif t == 2:
            if (f + rev) % 2 == 1:
                top.append(c)
            else:
                tail.append(c)
                
    if rev == 1:
        a = a[::-1]
        _top = tail
        tail = top
        top = _top
    
    print("".join(list(reversed(top))), end="")
    print(a, end="")
    print("".join(tail))


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    S = input()
    Q = int(input())
    mae = ''
    usiro = ''
    s = True
    for _ in range(Q):
        q = list(input().split())
        if q[0]=='1':
            s = not s
        else:
            if q[1]=='1' and s:
                mae = q[2] + mae
            elif q[1]=='2' and s:
                usiro = usiro + q[2] 
            elif q[1]=='2' and not s:
                mae = q[2] + mae
            else:
                usiro = usiro + q[2]
    if s:
        print(mae+S+usiro)
    else:
        print(usiro[::-1]+S[::-1]+mae[::-1])




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
Qs = [list(input().split()) for i in range(Q)]

is_reverse = False
for query in Qs:
    if query[0] == '1' and len(S) != 1:
        is_reverse = not is_reverse
    else:
        f = int(query[1])
        c = query[2]
        if (f == 1 and is_reverse) or (f == 2 and not is_reverse):
            S.append(c)
        else:
            S.appendleft(c)

if is_reverse:
    S.reverse()

print(''.join(S))

Submission status: Accepted

Solution code:
import heapq
from collections import deque
s = input()
s_list = list(s)
s_list = deque(s_list)
q = int(input())
q_list = []
for i in range(q):
  q_list.append(list(map(str,input().split())))
check = 0
for list_ in q_list:
  if int(list_[0]) == 1:
    check += 1
  if int(list_[0]) == 2:
    if (int(list_[1]) + check) % 2 ==1:
      s_list.appendleft(list_[2])
    else:
      s_list.append(list_[2])
s_list = list(s_list)
if check % 2 == 1:
  s_list = s_list[::-1]
print("".join(s_list))

Submission status: Accepted

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

# 毎回反転させていたら時間切れになる
is_reverse = False
forward = dict()  # あとで別のデータ構造に変更する
backward = dict()  # あとで別のデータ構造に変更する
# forward = list()
# backward = list()
forward_num = 0
backward_num = 0

for _ in range(q):
    query = input().split()
    if len(query) == 1:
        if is_reverse:
            is_reverse = False
        else:
            is_reverse = True
    else:
        _, f, c = query
        if f == '1':
            if is_reverse:
                backward[backward_num] = c
                backward_num += 1
                # s = s + c
                # backward.append(c)
            else:
                forward[forward_num] = c
                forward_num += 1
                # s = c + s
                # forward.insert(0, c)
        else:
            if is_reverse:
                forward[forward_num] = c
                forward_num += 1
                # forward.insert(0, c)
                # s = c + s
            else:
                backward[backward_num] = c
                backward_num += 1
                # backward.append(c)
                # s = s + c

# print(forward)
# print(backward)
forward = list(forward.values())[::-1]
backward = list(backward.values())
# print(is_reverse)
# print(forward)
# print(backward)

if is_reverse:
    s = s[::-1]
    print(''.join(backward)[::-1] + s + ''.join(forward)[::-1])
else:
    print(''.join(forward) + s + ''.join(backward))

Submission status: Accepted

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

ans = [" " for n in range(2*q + 1)]
ans[q] = s
frontIndex = q - 1
endIndex = q + 1
isReverse = -1

for i in range(q):
    query = input().split()
    if(query[0] == "1"):
        isReverse *= -1
    else:
        if(query[1] == "1"):
            if(isReverse == 1):
                ans[endIndex] = query[2]  
                endIndex += 1
            else:
                ans[frontIndex] = query[2]
                frontIndex -= 1
        else:
            if(isReverse == 1):
                ans[frontIndex] = query[2] 
                frontIndex -= 1
            else:
                ans[endIndex] = query[2]
                endIndex += 1
ans = "".join(ans).strip()
if(isReverse == -1):
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
import sys
import math
import collections
from collections import deque
#n = int(input())
#a, b = map(int, input().split())
s = input()
#x = list(map(int, input().split()))
q = int(input())
d = deque([])
for i in range(len(s)):
    d.append(s[i])

rev = 1
for i in range(q):
    que = sys.stdin.readline().split()
    if que[0] == "1":
        rev *= -1

    else:
        if rev == 1:
            if que[1] == "1":
                d.appendleft(que[2])
            else:
                d.append(que[2])

        else:
            if que[1] == "1":
                d.append(que[2])
            else:
                d.appendleft(que[2])


if rev == 1:
    for i in range(len(d)):
        print(d.popleft(), end="")
else:
    for i in range(len(d)):
        print(d.pop(), end="")

Submission status: Accepted

Solution code:
s = input()
q = int(input())
t = 0
f = -1
x = ["",""]
for i in range(q):
    que = input().split()
    if que[0] == "1":
        t += 1
    else:
        x[(int(que[1])-1+t)%2] += que[2]

s = x[0][::-1] + s + x[1]
if t%2 == 1:
    s = s[::-1]

print(s)

Submission status: Accepted

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

head = ''
tail = ''
reverse = 0

for q in Q:
    if int(q[0]) == 1:
        reverse = 1 - reverse
    elif int(q[1]) == 1:
        if reverse:
            tail += q[2]
        else:
            head += q[2]
    else:
        if reverse:
            head += q[2]
        else:
            tail += q[2]

if reverse:
    print(tail[::-1] + s[::-1] + head)
else:
    print(head[::-1] + s + tail)

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


s = input()

n = ii()
r = 0
head = ''
tail = ''
for i in range(n):
    q = input().split()

    if(q[0] == '1'):
        # reverse
        r += 1
    else:
        if(q[1] == '1'):
            if(r%2 ==0):
                # s = q[2] + s
                head = q[2] + head
            else:
                # s = s + q[2]
                tail = tail + q[2]
        else:
            if(r%2 ==0):
                # s = s + q[2]
                tail = tail + q[2]
            else:
                # s = q[2] + s
                head = q[2] + head

    # print('*********************',i,head,tail)
    

if(r%2==1):
    # s = s[::-1]
    # print('rev')
    print( tail[::-1] + s[::-1]+ head[::-1])
else:

    print(head +  s + tail)

Submission status: Accepted

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

A=""
Z=""
AZFlag = 0
for i in range(Q):
    Que = input().split()

    if Que[0] == '1':
        if AZFlag == 0:
            AZFlag = 1
            continue

        if AZFlag == 1:
            AZFlag = 0
            continue

    if Que[0] == '2':
        if Que[1] == '1' and AZFlag == 0:
            A = Que[2]+A
            continue

        if Que[1] == '1' and AZFlag == 1:
            Z = Z+Que[2]
            continue
        
        if Que[1] == '2' and AZFlag == 0:
            Z = Z+Que[2]
            continue

        if Que[1] == '2' and AZFlag == 1:
            A = Que[2]+A
            continue


if AZFlag == 0:
    print(A+S+Z)
if AZFlag == 1:
    print(Z[::-1]+S[::-1]+A[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

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

d = deque(s)
inv = 0

for _ in range(q):
  Q = list(input().split())
  
  if Q[0]=='1':
    inv += 1
  elif Q[0]=='2':
    if Q[1]=='1':
      if inv%2==0:
        d.appendleft(Q[2])
      else:
        d.append(Q[2])
    elif Q[1]=='2':
      if inv%2==0:
        d.append(Q[2])
      else:
        d.appendleft(Q[2])

if inv%2==0:
  print(*d, sep='')
else:
  print(*deque(reversed(d)), sep='')

Submission status: Accepted

Solution code:
s=input()
q=int(input())
is_reverse=False
pre=[]
post=[]
for i in range(q):
    qi=list(input().split())
    if(len(qi)==1):
        is_reverse = not is_reverse
    elif(qi[1]=="1"):
        if(is_reverse):
            post.append(qi[2])
        else:
            pre.append(qi[2])
    else:
        if(is_reverse):
            pre.append(qi[2])
        else:
            post.append(qi[2])

if(is_reverse):
    print(''.join(post)[::-1] + s[::-1] + ''.join(pre))
else:
    print(''.join(pre)[::-1] + s + ''.join(post))

Submission status: Accepted

Solution code:
#n=int(input())
#a,b=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

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

#print(l)

count=0
isR=False
from collections import deque
sl=deque(sl)
for i in range(q):
    order=l[i]
    if order[0]=="1":
        count+=1
        #反転
    if count%2==0:
        isR=False
    else:
        isR=True
    if order[0]!="1":
        f=order[1]
        c=order[2]
        temp_f=f
        if isR:
            if temp_f=="1":
                temp_f="2"
            else:
                temp_f="1"
        if temp_f=="1":
            #tempq=deque(sl)
            sl.appendleft(c)
            #tempq.appendleft(c)
            #sl=list(tempq)
        else:
            sl.append(c)
sl=list(sl)
ln=len(sl)
ansl=[]
if isR:
    for i in range(ln-1,-1,-1):
        ansl.append(sl[i])
else:
    ansl=sl
ans=''.join(ansl)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
moj = str(input())
kai = int(input())
han = 0
omo = ''
ura = ''
for i in range(kai):
    kyu = []
    kyu = list(input().split(" "))
    if kyu[0] == "1":
        if han == 0:
            han = 1
        elif han == 1:
            han = 0
    elif kyu[0] == "2":
        if han == 0:
            if kyu[1] == "1":
                ura += kyu[2]
            elif kyu[1] == "2":
                omo += kyu[2]
        if han == 1:
            if kyu[1] == "1":
                omo += kyu[2]
            elif kyu[1] == "2":
                ura += kyu[2]
if han == 1:
    moj = moj[::-1]
    omo = omo[::-1]
    moj = omo + moj + ura
elif han == 0:
    ura = ura[::-1]
    moj = ura + moj + omo
    
print(moj)

Submission status: Accepted

Solution code:
s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
cnt = 0
query = [list(map(str, input().split())) for _ in range(Q)]
before = []  # 先頭に追加する時
after = []  # 末尾に追加する時
for i in range(Q):
    a = query[i]
    if len(a) == 1:  # 1のqueryのとき
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = a[1], a[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                # s = c + s  # 偶数回の反転なら素直に先頭に追加する
                before.append(c)
            else:
                # s += c  # 奇数会の反転なら後ろに追加する
                after.append(c)
        else:  # 末尾に追加する
            if flag:
                # s += c  # 末尾に追加
                after.append(c)
            else:
                # s = c + s  # 先頭に追加
                before.append(c)
before.reverse()
# print(before)
# print(s)
# print(after)
s = "".join(before) + s + "".join(after)
if cnt % 2 == 1 and cnt != 0:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque

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

queue = deque(list(s))
is_reverse = False

for _ in range(q):
    tfc = input()
    if tfc[0] == '1':
        is_reverse = not is_reverse
        continue
    else:
        if is_reverse == False and tfc[2] == '1':
            queue.appendleft(tfc[4])
        elif is_reverse == False and tfc[2] == '2':
            queue.append(tfc[4])
        elif is_reverse == True and tfc[2] == '1':
            queue.append(tfc[4])
        elif is_reverse == True and tfc[2] == '2':
            queue.appendleft(tfc[4])

if is_reverse:
    ans = list(queue)
    new_ans = ans[::-1]
    print(*new_ans, sep='')
else:
    ans = queue
    print(*ans, sep='')

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    S = input()
    Q = int(input())

    reverse = False
    header = [''] * Q
    footer = [''] * Q
    for i in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            _, f, c = query.split()
            if reverse:
                if f == '1':
                    footer[i] = c
                else:
                    header[i] = c
            else:
                if f == '1':
                    header[i] = c
                else:
                    footer[i] = c
    if reverse:
        print("".join(footer[::-1] + list(S)[::-1] + header))
    else:
        print("".join(header[::-1]) + S + "".join(footer))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

day = deque(S)

r = False
for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        r = not r
        
    else:
        if x[1] == "1":
            if r == False :
                day.appendleft(x[2])
            else:
                day.append(x[2])
        else:
            if r == True :
                day.appendleft(x[2])
            else:
                day.append(x[2])
                
day = list(day)
        
if r:
    print("".join(day[::-1]))
else:
    print("".join(day))

Submission status: Accepted

Solution code:
# coding: utf-8
s = list(input())
q = int(input())

first = []
end = []
f = 0
for _ in range(q):
    li = list(input().split())
    if len(li) == 1:
        f = ~f
    else:
        if li[1] == '1':
            if f == 0:
                first.append(li[2])
            else:
                end.append(li[2])
        else:
            if f == 0:
                end.append(li[2])
            else:
                first.append(li[2])
s = first[::-1] + s + end
if f == -1:
    s = s[::-1]
    
print(''.join(s))