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 decorator import stop_watch
# 
# 
# @stop_watch
def solve(S, Q, Qs):
    rev_flg = False
    bef = ''
    aft = ''
    for q in Qs:
        if q[0] == 1:
            rev_flg = not rev_flg
            bef, aft = aft, bef
        else:
            bf = q[1]
            if bf == 1:
                bef += q[2]
            elif bf == 2:
                aft += q[2]
    bef = bef[::-1]
    if rev_flg:
        S = S[::-1]
    print(bef + S + aft)


    # for q in Qs:
    #     if q[0] == 1:
    #         rev_flg = not rev_flg
    #     else:
    #         bf = q[1]
    #         if (bf == 1 and not rev_flg) or (bf == 2 and rev_flg):
    #             ans = q[2] + ans
    #         else:
    #             ans = ans + q[2]
    # if rev_flg:
    #     ans = ans[::-1]
    # print(ans)


if __name__ == '__main__':
    # # handmade
    # S = 'h' * 10 ** 5
    # Q = 2 * 10 ** 5
    # Qs = []
    # for i in range(Q - 1):
    #     if i % 2 == 0:
    #         Qs.append([2, 1, 'a'])
    #     else:
    #         Qs.append([2, 2, 'b'])
    # Qs.append([1])

    # input
    S = input()
    Q = int(input())
    Qs = []
    i = 0
    for _ in range(Q):
        s = input()
        i += 1
        if s[0] == '1':
            q = [1]
        else:
            q = s.split()
            q[0] = int(q[0])
            q[1] = int(q[1])

        Qs.append(q)
    solve(S, Q, Qs)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
f = ""
b = ""
forward = True
for _ in range(q):
    que = list(input().split())
    if int(que[0]) == 1:
        if forward:
            forward = False
        else:
            forward = True
    else:
        if (int(que[1]) == 1 and forward) or (int(que[1]) == 2 and not forward):
            f+=que[2]
        else:
            b+=que[2]

if forward:
    print(f[::-1]+s+b)
else:

    print(b[::-1]+s[::-1]+f)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
re = False

for i in range(q):
    try:
        a,f,c = map(str, input().split())
    except ValueError:
        re = not re
        continue

    if f == "1":
        if not re:
            s.appendleft(c)
        else:
            s.append(c)
    else:
        if not re:
            s.append(c)
        else:
            s.appendleft(c)

if re:
    s = list(reversed(s))

print("".join(s))

Submission status: Accepted

Solution code:
import bisect

def main():
    s = input()
    qc = int(input())
    front = ""
    back = ""
    direct = 1
    for _ in range(qc):
        t = input()
        if t[0] == "1":
            direct = 2 if direct == 1 else 1
        else:
            _,f,c = t.split()
            if f == "1":
                if direct == 1:
                    front += c
                else:
                    back += c
            else:
                if direct == 1:
                    back += c
                else:
                    front += c
    if direct % 2 == 0:
        print((s+back)[::-1] + front)
    else:
        print(front[::-1]+s+back)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
S=input()
N=int(input())
q=deque([S[i] for i in range(len(S))])

c=0
for i in range(N):
  A=list(map(str,input().split()))
  if A[0]=='1':
     c+=1
     c%=2
  else:
     if (c+int(A[1]))%2==1:
       q.appendleft(A[2])
     if (c+int(A[1]))%2==0:
       q.append(A[2])
s=''
if c==0:
  for i in range(len(q)):
    r=q.popleft()
    s+=r
if c==1:
  for i in range(len(q)):
    d=q.pop()
    s+=d
print(s)

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)
    switch = "F"
    count = 0

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            count += 1
            if switch == "F":
                switch = "R"
            elif switch == "R":
                switch = "F"
        else:
            if switch == "F":
                if int(arg[0]) == 1:
                    ans.appendleft(arg[1])
                else:
                    ans.append(arg[1])
            else:
                if int(arg[0]) == 1:
                    ans.append(arg[1])
                else:
                    ans.appendleft(arg[1])

    if count % 2 == 0:
        print("".join(ans))
    else:
        ans.reverse()
        print("".join(ans))
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S = input()
    Q = int(input())
    ans = deque(S)
    switch = 1
    count = 0

    for q in range(Q):
        T, *arg = input().split()
        T = int(T)

        if T == 1:
            count += 1
            if switch == 1:
                switch = 2
            elif switch == 2:
                switch = 1
        else:
            if switch == 1:
                if int(arg[0]) == 1:
                    ans.appendleft(arg[1])
                else:
                    ans.append(arg[1])
            else:
                if int(arg[0]) == 1:
                    ans.append(arg[1])
                else:
       
                    ans.appendleft(arg[1])

    if count % 2 == 0:
        print("".join(ans))
    else:
        ans.reverse()
        print("".join(ans))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
Flag = True
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        Flag = False if Flag else True
    else:
        if Flag:
            if q[1] == '1':
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == '1':
                S.append(q[2])
            else:
                S.appendleft(q[2])
if Flag:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
s = deque(s)
q = int(input())
reverse = False
for i in range(q):
    query = input()
    if len(query) == 1:
        reverse = not reverse
    else:
        if not reverse:
            if query[2] == '1':
                s.appendleft(query[4])
            else:
                s.append(query[4])
        else:
            if query[2] == '2':
                s.appendleft(query[4])
            else:
                s.append(query[4])
s ="".join(list(s))
if reverse:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import  deque
S = input()
Q = int(input())
rev = 1
q = deque(list(S))
for _ in range(Q):
    op = input().split(" ")
    if int(op[0]) == 1:
        rev *= -1
    else:
        f, ch = op[1:]
        if rev == 1:
            if f == "1":
                q.appendleft(ch)
            else:
                q.append(ch)
        else:
            if f == "1":
                q.append(ch)
            else:
                q.appendleft(ch)
S = "".join(q)
if rev == -1:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque
Q=deque()
a=input()
Q.append(a)
q=int(input())
reverse=False
for i in range(q):
    d=input()
    if d[0]=="1": reverse= not reverse
    elif d[0]=="2":
        _, f, c=d.split()
        if f == "1":
            if reverse:
                Q.append(c)
            else:
                Q.appendleft(c)
        elif f =="2":
            if reverse:
                Q.appendleft(c)
            else:
                Q.append(c)
ans="".join(Q)
if reverse:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s=input()
Q=int(input())
hanten = 0
mae = deque()
ushiro = deque()
for i in range(Q):
	q=input()
	if q=="1":
		hanten ^= 1
	else:
		if q[2] == "1":
			if hanten == 0:
				mae.appendleft(q[4])
			else:
				ushiro.append(q[4])
		else:
			if hanten == 0:
				ushiro.append(q[4])
			else:
				mae.appendleft(q[4])
if hanten == 0:
	print("".join(mae) + s + "".join(ushiro))
else:
	print(("".join(mae) + s + "".join(ushiro))[::-1])

Submission status: Accepted

Solution code:
from collections import deque
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
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 S(): return input()
def LS(): return input().split()


s = S()
q = I()
queries = SR(q)
que = deque(list(s))
top = True  # 先頭の位置 Trueが左（初期位置）
for query in queries:
    if query[0] == '1':
        top = not(top)
    else:
        _, f, c = query.split()
        if f == '1':
            if top:
                que.appendleft(c)
            else:
                que.append(c)
        else:
            if top:
                que.append(c)
            else:
                que.appendleft(c)
if top:
    print(''.join(list(que)))
else:
    print(''.join(list(que))[::-1])

Submission status: Accepted

Solution code:
from collections import deque
que=deque(input())
rev=False
for _ in range(int(input())):
    t=input()
    if t=='1':
        rev=not rev
    else:
        tt,f,c=t.split()
        if (f=='1' and rev==False)  or (f=='2' and rev):
            que.appendleft(c)
        else:
            que.append(c)
if rev:
    que.reverse()

print(''.join(que))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
#W, H, N = map(int, input().split())
lst = []
for i in range(Q):
    lst.append(list(input().split()))
#print(lst)

flag = 'left' #文字列が左向き
for i in range(Q):
    if flag == 'left':
        if lst[i][0] == '1':
            flag = 'right'
        else:
            if lst[i][1] == '1':
                S.appendleft(lst[i][2])
            else:
                S.append(lst[i][2])
    else: #flag == right
        if lst[i][0] == '1':
            flag = 'left'
        else:
            if lst[i][1] == '1':
                S.append(lst[i][2])
            else:
                S.appendleft(lst[i][2])
if flag == 'left':
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
check=0
query=["","",""]
tfel=""
right=""
for i in range(q):
    query=list(map(str,input().split()))
    if (query[0]=="1"):
        check+=1
    else:
        if (query[1]=="1"):
            if (check%2==0):
                tfel+=query[2]
            else:
                right+=query[2]
        else:
            if (check%2==0):
                right+=query[2]
            else:
                tfel+=query[2]
            
s=tfel[::-1]+s+right
print(s if check%2==0 else s[::-1])

Submission status: Accepted

Solution code:
S = input()

from collections import deque
S = deque(S)
Q = int(input())
query = [input().split() for _ in range(Q)]
reverse = 0
for i in range(Q):
    if query[i][0] == "1":
        reverse += 1
    else:
        if query[i][1] == "1":
            if reverse % 2 != 0:
                S.append(query[i][2])
            else:
                S.appendleft(query[i][2])
        else:
            if reverse % 2 != 0:
                S.appendleft(query[i][2])
            else:
                S.append(query[i][2])

result = "".join(S)
if reverse % 2 != 0:
    result = result[::-1]

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input()))
Q = int(input())
Flag = True
for _ in range(Q):
    q = input().split()
    if len(q) == 1:
        Flag = False if Flag else True
    else:
        if Flag:
            if q[1] == '1':
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if q[1] == '1':
                S.append(q[2])
            else:
                S.appendleft(q[2])

if Flag:
    print(''.join(S))
else:
    print(''.join(reversed(S)))

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
added=(list(),list())
head=0
for i in range(Q):
    l=input().split()
    if int(l[0])==1:
        head=(head+1) % 2
    else:
        adding=(int(l[1])+head-1)%2
        added[adding].append(l[2])
#    print(added,head)

#print(added)
print(''.join(reversed(added[head])),end="")
if head==1:
    S=S[::-1]
print(S,end="")
print(''.join(added[(head+1)%2]))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a = []
# for j in range(q):
#     a.extend([list(map(str,input().split()))])
# # print(a)
# for i in a:
pre = ""
nex = ""
c = 1
TFC = [input().split() for _ in range(q)]
for i in TFC:
# for j in range(q):
#     i = list(map(str,input().split()))
    if i ==["1"]:
        c *= -1
        # s = s[::-1]
    elif i[0]=="2" and i[1]=="1":
        # s = i[2]+s
        if c == 1:
            pre=pre+i[2]
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        # s = s +i[2]
        if c ==1:
            nex = nex +i[2]
        else:
            pre = pre+i[2]
if c==-1:
    print(nex[::-1]+s[::-1]+pre)
else:
    print(pre[::-1]+s+nex)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a = []
# for j in range(q):
#     a.extend([list(map(str,input().split()))])
# # print(a)
# for i in a:
pre = ""
nex = ""
c = 1
TFC = [input().split() for _ in range(q)]
for i in TFC:
# for j in range(q):
#     i = list(map(str,input().split()))
    if i ==["1"]:
        c *= -1
        # s = s[::-1]
    elif i[0]=="2" and i[1]=="1":
        # s = i[2]+s
        if c == 1:
            pre=i[2]+pre
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        # s = s +i[2]
        if c ==1:
            nex = nex +i[2]
        else:
            pre = i[2]+pre
s = pre+s+nex
if c ==-1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
a = []
# for j in range(q):
#     a.extend([list(map(str,input().split()))])
# # print(a)
# for i in a:
pre = ""
nex = ""
c = 1
for j in range(q):
    i = list(map(str,input().split()))
    if i ==["1"]:
        c *= -1
        # s = s[::-1]
    elif i[0]=="2" and i[1]=="1":
        # s = i[2]+s
        if c == 1:
            pre=i[2]+pre
        else:
            nex = nex + i[2]
    elif i[0]=="2" and i[1]=="2":
        # s = s +i[2]
        if c ==1:
            nex = nex +i[2]
        else:
            pre = i[2]+pre
s = pre+s+nex
if c ==-1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
x = 0
p = ''
q = ''
for i in range(Q):
    A = list(input().split())
    if A[0] == '1':
        x += 1
    elif x%2 ==0:
        if A[1] == '1':
            p = A[2] + p
        else:
            q = q + A[2]
    else:
        if A[1] == '1':
            q = q + A[2] 
        else:
            p = A[2] + p
S = p + S + q
if x%2 == 1:
    S = ''.join(reversed(list(S)))
print(S)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
TFC = [input().split() for _ in range(Q)]
is_reversed = False
head = ''
tail = ''
for tfc in TFC:
    if tfc[0] == '1':
        is_reversed = not is_reversed
    else:
        if tfc[1] == '1':
            if is_reversed:
                tail += tfc[2]
            else:
                head += tfc[2]
        else:
            if is_reversed:
                head += tfc[2]
            else:
                tail += tfc[2]
if is_reversed:
    print(tail[::-1]+S[::-1]+head)
else:
    print(head[::-1]+S+tail)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
Query = [input().split() for i in range(Q)]
order = 1

for i in range(Q):
    if Query[i][0] == '1':
        order *= -1
    else:
        F = Query[i][1]
        C = Query[i][2]

        if F == '1':
            if order == 1:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if order == 1:
                S.append(C)
            else:
                S.appendleft(C)

S = ''.join(list(S))

print(S[::order])

Submission status: Accepted

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

Submission status: Accepted

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

s = deque(list(input()))
q = int(input())
rev = -1

for i in range(q):
    a = list(input().split())
    if a[0] == '1': #T1
        rev *= -1 
    
    else: #T2
        if a[1] == '1': #Fi = 1
            if rev == -1:
                s.appendleft(a[2])
            else:
                s.append(a[2])
                
        else: #Fi = 2
            if rev == -1:
                s.append(a[2])
            else:
                s.appendleft(a[2])
                
if rev == -1:
    print(''.join(s))
else:
    s = list(s)
    print(''.join(s[::-1]))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
num_of_query = int(input())

is_reverse = False

for _ in range(num_of_query):
    query = list(input().split())
    
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[1] == '1':
            if is_reverse:
                S.append(query[2])
            else:
                S.appendleft(query[2])

        else:
            if is_reverse:
                S.appendleft(query[2])
            else:
                S.append(query[2])

S = list(S)

if is_reverse:
    S = S[::-1]

print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
num_of_query = int(input())
querys = []
is_reverse = False

for _ in range(num_of_query):
    # querys.append(list(input().split()))
    query = list(input().split())
    
    if query[0] == '1':
        is_reverse = not is_reverse
    else:
        if query[1] == '1':
            if is_reverse:
                S.append(query[2])
            else:
                S.appendleft(query[2])

        else:
            if is_reverse:
                S.appendleft(query[2])
            else:
                S.append(query[2])

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

# for query in querys:
#     if query[0] == '1':
#         S = S[::-1]
#     else:
#         if query[1] == '1':
#             S = deque(S)
#             S.appendleft(query[2])
#             S = list(S)
#         else:
#             S = deque(S)
#             S.append(query[2])
#             S = list(S)

print(''.join(S))

Submission status: Accepted

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


def solve(S, Q, query):

    state = 1 # forward
    head = deque([])
    tail = deque([])

    for qq in query:
        if qq[0] == "1":
            state *= -1
        else:
            if qq[1] == "1":
                is_head = (state==1)
            elif qq[1] == "2":
                is_head = (state==-1)
            
            if is_head:
                head.append(qq[2])
            else:
                tail.append(qq[2])
    
    head = "".join([head.pop() for _ in range(len(head))])
    tail = "".join([tail.popleft() for _ in range(len(tail))])

    print((head + S + tail)[::state])

    
    return


# 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():
    def iterate_tokens():
        for line in sys.stdin:
            # for word in line.split():
            yield line
    tokens = iterate_tokens()
    S = str(next(tokens)).replace('\n', '')  # type: str
    Q = str(next(tokens)).replace('\n', '')  # type: str
    query = []
    for x in tokens:
        sp = x.split()
        # print(sp)
        if sp[0] == "1": query.append(sp[0])
        else :
            query.append((sp[0], sp[1], sp[2]))
    # print(query)

    solve(S, Q, query)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)
if flg == 1:
	print(*q, sep = "")
else:
	print(*list(q)[::-1], sep = "")

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)


ans = "".join(q)
if flg == -1:
	ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

q = deque()
rev = 0

for s in S:
  q.append(s)

for _ in range(Q):
  TFC = input()
  if len(TFC) == 1: # T == 1
    rev ^= 1
  else:
    T, F, C = (i for i in TFC.split())  
    if (F == '2' and not rev) or (F == '1' and rev):
      q.append(C)
    else:
      q.appendleft(C)
      
ans = ''.join(q)

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

Submission status: Accepted

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

def resolve():
    from collections import deque
    d=deque(input())
    q=int(input())
    rev=False
    for i in range(q):
        l=input().split()
        if len(l)==1:
            if rev:
                rev=False
            else:
                rev=True
        else:
            if rev:
                if l[1]=='1':
                    d.append(l[2])
                else:
                    d.appendleft(l[2])
            else:
                if l[1]=='1':
                    d.appendleft(l[2])
                else:
                    d.append(l[2])
    if rev:
        d=reversed(d)
    print(''.join(d))
resolve()

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    s = input().rstrip()
    q = int(input())

    dq = deque(s)
    head_is_0 = 1
    for _ in range(q):
        query = iter(input().rstrip().split())
        if next(query) == '1':
            head_is_0 ^= 1
        elif (next(query) == '2') ^ head_is_0:
            dq.appendleft(next(query))
        else:
            dq.append(next(query))

    if not head_is_0:
        dq.reverse()

    ret = ''.join(dq)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

S = "".join(S)
print(S if not need_reverse else S[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
flag = True
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        flag = not flag
    else:
        if (query[1] == "1" and flag) or (query[1] == "2" and not flag):
            s.appendleft(query[2])
        else:
            s.append(query[2])
if not flag:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
from collections import deque

q = deque(input())
Q = int(input())
reverse = -1

for i in range(Q):
    X = input().split()
    if X[0] == "1":
        reverse *= -1
    # if X[1] == "1":
    elif X[1] == "1":
        if reverse == -1:
            q.appendleft(X[2])
        else:
            q.append(X[2])
    elif X[1] == "2":
        if reverse == -1:
            q.append(X[2])
        else:
            q.appendleft(X[2])
# if reverse == 1: q.reverse()

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

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
qu = [[0] * 3] * q
count = 0

for i in range(q):
    qu[i] = input().split()

for i in range(q):
    if qu[i][0] == '1':
        count += 1
        continue

    else:
        if (count + int(qu[i][1])) % 2:
            s.appendleft(qu[i][2])
        else:
            s.append(qu[i][2])

if count % 2:
    s.reverse()

print(''.join(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
flag = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        flag *= -1
    elif query[1] == '1':
        if flag == 1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
    elif query[1] == '2':
        if flag == -1:
            s.appendleft(query[2])
        else:
            s.append(query[2])
if flag == 1:
    print(''.join(s))
else:
    print(''.join(list(s)[::-1]))

Submission status: Accepted

Solution code:
from collections import deque
q = deque(list(input()))
n = int(input())
han = 0
zengo = 0
for i in range(n):
    a = input().split()
    # print(a[1])
    if len(a)==1:
        zengo ^=1
        han += 1
    else:
        a[1] = int(a[1])-1
        if a[1] == 0:
            if zengo == 0:#0
                q.appendleft(a[2])
            else:#0
                q.append(a[2])
        else:#1
            if zengo == 0:
                q.append(a[2])
            else:
                q.appendleft(a[2])
q = list(q)
if han >2 and han%2 :
    q.reverse()
print("".join(q))

Submission status: Accepted

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

s = deque(s)
flip = False

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

ans = ''

for _ in range(len(s)):
    if not flip:
        ans += s.popleft()
    else:
        ans += s.pop()

print(ans)