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:
s = input()
q = int(input())
adv = ''
bac = ''
Flag = True
for i in range(q):
    xxx = input()
    if len(xxx) == 1:
        if Flag == True:
            Flag = False
        else:
            Flag = True
    else:#T=2
        T,F,C=xxx.split()
        F = int(F)
        if F == 1:
            if Flag == True:
                adv +=C
            else:
                bac +=C
        else:#F=2
            if Flag == True:
                bac +=C
            else:
                adv +=C
if Flag == True:
    s = adv[::-1] + s + bac
    print(s)
else:
    s = bac[::-1] + s[::-1] + adv
    print(s)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
muki=True
a=[[_ for _ in input().split()] for i in range(q)]
front=""
back=""
for i in a:
    if i[0]=="1":
        muki=not muki
        continue
    if (i[1]=="1" and muki) or (i[1]=="2" and not muki):
        front=i[2]+front
    else:
        back+=i[2]
if muki:
    print(front+s+back)
else:
    print((front+s+back)[::-1])

Submission status: Accepted

Solution code:
moji=input()
times=int(input())

sento=[]
sippo=[]
plus=[sento,sippo]
dictionary={"sento":0,"sippo":1}

hantenkaisuu=0

for i in range(times):
    command=input().split()
    
    #反転の命令
    if command[0]=="1":
        hantenkaisuu+=1
        dictionary["sento"]=(dictionary["sento"]+1)%2
        dictionary["sippo"]=(dictionary["sippo"]+1)%2
        
    #追加の命令
    else:
        if command[1]=="1":
            plus[dictionary["sento"]].append(command[2])
        else:
            plus[dictionary["sippo"]].append(command[2])
           
    
            
    
    
if hantenkaisuu%2==0:
    sento=sento[::-1]
    sento="".join(sento)
    sippo="".join(sippo)
    print(sento+moji+sippo)
else:
    sippo=sippo[::-1]
    sento="".join(sento)
    sippo="".join(sippo)
    moji=moji[::-1]
    print(sippo+moji+sento)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = -1
left = ""
right = ""
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    reverse *= (-1)
  else:
    if reverse > 0:
      if q[1] == '1':
        right = right + q[2]
      else:
        left = q[2] + left
    else:
      if q[1] == '1':
        left = q[2] + left
      else:
        right = right + q[2]
if reverse>0:
  print((left+S+right)[::-1])
else:
  print(left+S+right)

Submission status: Accepted

Solution code:
S, Q = input(), int(input())
rev = False
st = []
ed = []
for _ in range(Q):
  a = input().split(' ')
  if a[0] == '1':
    rev = not rev
    continue
  if not rev:
    if a[1] == '1':
      st.append(a[2])
    else:
      ed.append(a[2])
  else:
    if a[1] == '1':
      ed.append(a[2])
    else:
      st.append(a[2])
if rev:
  print(''.join(reversed(ed)) + S[::-1] + ''.join(st))
else:
  print(''.join(reversed(st)) + S + ''.join(ed))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
mode = 0
ans_1 = ""
ans_2 = ""
for i in range(Q):
    K = input().split()
    if K[0] == "1":
        if mode == 0:
            mode = 1
        else:
            mode = 0
    else:
        if K[1] == "1":
            if mode == 0:
                ans_1 += K[2]
            else:
                ans_2 += K[2]
        else:
            if mode == 0:
                ans_2 += K[2]
            else:
                ans_1 += K[2]
ans_1 = ans_1[::-1]
K = ans_1+S+ans_2
if mode == 1:
    print(K[::-1])
else:
    print(K)

Submission status: Accepted

Solution code:
from collections import deque

s = deque(input())
q = int(input())
top = 0

for i in range(q):
  a = list(map(str, input().split()))
  if(a[0] == "1"):
    top = (top + 1) % 2
    continue
  # a[0] == "2"
  if(top + int(a[1]) - 1 == 1):
    s.append(a[2])
  else:
    s.appendleft(a[2])
if(top == 0):
  print(''.join(s))
else:
  print((''.join(s))[::-1])

Submission status: Accepted

Solution code:
s=input()
q=int(input())
queri=[]
rv=0
forward=[]
back=[]
for i in range(q):
    queri=list(map(lambda x:x,input().split()))
    if queri[0]=="2":
        rvt=(rv+int(queri[1]))%2
        if rvt==1: forward.append(queri[2])
        else: back.append(queri[2])

    else: rv=(rv+1)%2

if rv==0:
    for i in range(len(forward)-1,-1,-1):
        print(forward[i],end="")
    print(s,end="")
    for i in range(len(back)):
        print(back[i],end="")
        
if rv==1:
    for i in range(len(back)-1,-1,-1):
        print(back[i],end="")
    print(s[::-1],end="")
    for i in range(len(forward)):
        print(forward[i],end="")

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
revers_count = 0
mae = ""
ushiro = ""

for i in range(Q):
    Q_i = list(input().split())
    if Q_i[0] == "1":
        revers_count += 1
    else:
        if (Q_i[1] == "1" and revers_count % 2 == 0) or (Q_i[1] == "2" and revers_count % 2 == 1):
            mae = Q_i[2] + mae
        else:
            ushiro = ushiro + Q_i[2]

ans = mae + S + ushiro

if revers_count % 2 == 1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
from collections import deque

DEBUG = False

def solve():
    return


def read_int_list(sep = " "):
    return [int(s) for s in sys.stdin.readline().rstrip().split(sep)]

def read_int():
    return int(sys.stdin.readline())

def read_str():
    return sys.stdin.readline().rstrip()
    
def read_str_list(sep = " "):
    return [s for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


def main():
    d = deque(list(read_str()))
    q = read_int()

    reversed_ = False
    for _ in range(0, q):
        query = read_str_list()
        if query[0] == "1":
            reversed_ = not reversed_
            continue
        # query[0] == "2"
        f, c = query[1:]
        if (f == "1" and not reversed_) or (f == "2" and reversed_):  # insert to the left
            d.appendleft(c)
        else:
            d.append(c)
    if reversed_:
        print("".join(reversed(list(d))))
    else:
        print("".join(list(d)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
a = []
f = False #さかさま
for i in range(q):
    x = list(input().split())
    if x[0]=='1':
        f = not f
    else:
        if f:
            if x[1]=='1': s.append(x[2])
            else: a.append(x[2])
        else:
            if x[1]=='1': a.append(x[2])
            else: s.append(x[2])
a.reverse()
s = a+s
if f:s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
S = input()
reverse = 0
front = []
end = []
n = int(input())
for _ in range(n):
  List = list(input().split())
  if List[0] == '1':
    reverse += 1
  else:
    if List[1] == '1':
      if reverse % 2 == 1:
        end.append(List[2])
      else:
        front.append(List[2])
    else:
      if reverse % 2 == 1:
        front.append(List[2])
      else:
        end.append(List[2])
if reverse %2 == 1:
  S = S[::-1]
  front, end = end, front
Answer = ''.join(front[::-1]) + S + ''.join(end)
print(Answer)

Submission status: Accepted

Solution code:
def main():
    S = raw_input()
    Q = int(raw_input())

    is_reversed = False
    sentou = []
    owari = []

    for _ in range(Q):
        query = raw_input().split()
        if query[0] == "1":
            is_reversed = not is_reversed
        elif query[0] == "2":
            F = query[1]
            C = query[2]
            if F == "1":
                if not is_reversed:
                    sentou.append(C)
                else:
                    owari.append(C)
            elif F == "2":
                if not is_reversed:
                    owari.append(C)
                else:
                    sentou.append(C)

    S = "".join(sentou[::-1]+list(S)+owari)


    if not is_reversed:
        print S
    else:
        print S[::-1]

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque


S = list(input())
N = int(input())

d = deque()
d.extend(S)

# Trueであれば順方向
direction = True

for i in range(N):
    query = input()
    if query[0] == "1":
        t = int(query)
    else:
        t, f, c = query.split()
        t = int(t)
        f = int(f)

    if t == 1:
        #True-Falseの逆転
        direction = not direction

    if t == 2:
        if direction == True:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)

        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if direction == True:
    print(''.join(d))
else:
    print(''.join(reversed(d)))

Submission status: Accepted

Solution code:
from collections import deque

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

for i in range(Q):
    l = str(input())
    if l[0] == '1':
        rev = (rev + 1) % 2
    else:
        two, F, C = l.split()
        if rev == 0:
            if F == '1':
                S.appendleft(C)
            else:
                S.append(C)

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

            else:
                S.appendleft(C)

if rev == 0:
    print(''.join(S))
else:
    print(''.join(list(reversed(S))))

Submission status: Accepted

Solution code:
S=input()
Q_num = int(input())
Q=[]
for _ in range(Q_num):
    Q.append(list(input().split()))
from collections import deque
sento = deque()
matsubi = deque()
mirror_num=0
for q in Q:
    t=q[0]
    if t=='1':
        mirror_num+=1
        continue

    f=int(q[1])
    c=q[2]
    # 反転が偶数回なら反転は無視できる
    if int(mirror_num%2)==0:
        if f==1:
            sento.appendleft(c)
            continue
        matsubi.append(c)
        continue

    if f==1:
        matsubi.append(c)
        continue
    sento.appendleft(c)
    continue

sento = ''.join(sento)
matsubi = ''.join(matsubi)
S_result = sento+S+matsubi
if mirror_num%2==1:
    S_result = S_result[::-1]
print(S_result)

Submission status: Accepted

Solution code:
from sys import stdin
s = list(stdin.readline().rstrip())
q = int(stdin.readline().rstrip())
t = []
k = []
for i in range(q):
    t.append([x for x in stdin.readline().rstrip().split()])
r = 0
for i in range(q):
    if int(t[i][0]) == 1:
        if r == 1:
            r = 0
        else:
            r = 1
    else:
        if (int(t[i][1]) + r) % 2 == 0:
            s.append(t[i][2])
        else:
            k.append(t[i][2])
k = list(reversed(k))
k.extend(s)
if r == 1:
    k = list(reversed(k))
k = ''.join(k)
print(k)

Submission status: Accepted

Solution code:
s = str(input())
q = int(input())
Q = [list(input().split()) for i in range(q)]
head = ''
bottom = ''
reverse = False
for i in range(q):
    if Q[i][0] == '1':
        reverse = not reverse
    else: #Ti == 2
        if Q[i][1] == '1':
            if reverse == False:
                head = Q[i][2] + head
            else:
                bottom = bottom + Q[i][2]
        if Q[i][1] == '2':
            if reverse == False:
                bottom = bottom + Q[i][2]
            else:
                head = Q[i][2] + head
s = head + s + bottom
ans = ''
if reverse == True:
    for i in range(len(s)):
        ans += s[-1 * (i + 1)]
else:
    ans = s
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

cur = 1
d = deque([S])

for _ in range(Q):
    ins = input().split()
    if len(ins) == 1:
        cur = 2 if cur == 1 else 1
    else:
        _, f, c = ins
        f = int(f)
        if cur == f == 1 or cur == f == 2:
            d.appendleft(c)
        else:
            d.append(c)

if cur == 1:
    print("".join(d))
else:
    temp = "".join(d)
    print(temp[::-1])

Submission status: Accepted

Solution code:
from collections import deque
S = list(input())
S = deque(S)
rev = False
Q = int(input())
for _ in range(Q):
    T = list(map(str,input().split()))
    if T[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    if T[0] == '2':
        if T[1] == '1':
            if rev:
                S.append(T[2])
            else:
                S.appendleft(T[2])
        if T[1] == '2':
            if rev:
                S.appendleft(T[2])
            else:
                S.append(T[2])
if rev:
    S = list(reversed(S))
    print("".join(S))
else:
    S = list(S)
    print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
Q = int(input())
c = 0
for _ in range(Q):
  q = input()
  if (q[0] == "1"):
    c += 1
  else:
    if (q[2] == "1"):
      if (c % 2 == 0):
        s.appendleft(q[4])
      else:
        s.append(q[4])
    else:
      if (c % 2 == 0):
        s.append(q[4])
      else:
        s.appendleft(q[4])
if (c % 2 == 0):
  print("".join(list(s)))
else:
  print("".join(list(s))[::-1])

Submission status: Accepted

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

reverseFlag = False
prefix = ""
suffix = ""

for i in range(NQ):
    query = input().split()
    if (int(query[0]) == 1):
        reverseFlag = not reverseFlag
    else:
        if (reverseFlag):
            if (int(query[1]) == 1):
                suffix = suffix + query[2]
            else:
                prefix = query[2] + prefix
        else:
            if (int(query[1]) == 1):
                prefix = query[2] + prefix
            else:
                suffix = suffix + query[2]

if (reverseFlag):
    print(suffix[::-1] + S[::-1] + prefix[::-1])
else:
    print(prefix + S + suffix)

Submission status: Accepted

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



front = ''
back = ''

r_flg = False
for i in range(Q):
    q = input()
    if q == '1':
        r_flg = False if r_flg else True
        continue
        
    t,f,c = q.split()
    if not r_flg:
        if f == '1':
            front += c
        else:
            back += c
    else:
        if f == '1':
            back += c
        else:
            front += c
    
if not r_flg:
    print(front[::-1]+S+back)
else:
    print(back[::-1] + S[::-1] + front)

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import deque
def resolve():
    S = deque(sr())
    Q = ir()
    rev = 1
    for i in range(Q):
        T = list(sr().split())
        if int(T[0]) == 1:
            rev *= -1
        else:
            x, F, C = T
            if int(F) == 1:
                if rev == 1:
                    S.appendleft(C)
                else:
                    S.append(C)
            else:
                if rev == 1:
                    S.append(C)
                else:
                    S.appendleft(C)
    if rev == -1:
        S.reverse()
    print(*S, sep='')
resolve()

Submission status: Accepted

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

s = deque(s)
rev = 0

for i in range(q):
    t = list(input().split())
    if len(t) == 1:
        rev += 1
        continue
    if t[1] == '1':
        if rev % 2 == 0:
            s.appendleft(t[2])
        else:
            s.append(t[2])
    else:
        if rev % 2 == 0:
            s.append(t[2])
        else:
            s.appendleft(t[2])
if rev % 2 != 0:
    s.reverse()

print(''.join(s))

Submission status: Accepted

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

s_deq = deque(s)
reversed = False

for _ in range(q_num):
    query = input().split()
    if query[0] == '1':
        reversed = False if reversed else True
    elif query[0] == '2':
        if not reversed:
            if query[1] == '1':
                s_deq.appendleft(query[2])
            elif query[1] == '2':
                s_deq.append(query[2])
        else:
            if query[1] == '2':
                s_deq.appendleft(query[2])
            elif query[1] == '1':
                s_deq.append(query[2])
if reversed:
    s_deq.reverse()
print(''.join(s_deq))

Submission status: Accepted

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


ss = list(input())
n = len(ss)-1

s = deque([])
for i in range(n):
    s.append(ord(ss[i]))

p = 1

q = int(input())
for i in range(q):
    inp = list(input())

    if inp[0] == '1':
        p *= -1
    else:
        c = ord(inp[4])
        f = inp[2]
        if (f == '1' and p == 1) or (f == '2' and p == -1):
            s.appendleft(c)
        else:
            s.append(c)


n = len(s)
if p == 1:
    for e in s:
        print(chr(e),end = '')
else:
    for i in range(n-1,-1,-1):
        print(chr(s[i]),end = '')

Submission status: Accepted

Solution code:
s=input()
q=int(input())
query=[]
for i in range(q):
    query.append(input())
rev=0
bef=""
aft=""
for i in range(q):
    if query[i][0]=="1":
        rev+=1
    else:
        n,f,c=query[i].split()
        if (rev+int(f))%2==0:
            aft=aft+c
        else:
            bef=c+bef
seqfin=bef+s+aft
if rev%2==0:
    print(seqfin)
else:
    print(seqfin[::-1])

Submission status: Accepted

Solution code:
reverseFlag = False

S = input()
Q = int(input())
S_mae = ''
S_ato = ''

for i in range(Q):
    TFC = input().split(' ')
    if TFC[0] == '1':
        reverseFlag = not reverseFlag
    else:
        if TFC[1] == '1':
            if reverseFlag:
                S_ato = S_ato + TFC[2]
            else:
                S_mae = TFC[2] + S_mae
        else:
            if reverseFlag:
                S_mae = TFC[2] + S_mae
            else:
                S_ato = S_ato + TFC[2]
if reverseFlag:
    print(S_ato[::-1] + S[::-1] + S_mae[::-1])
else:
    print(S_mae + S + S_ato)

Submission status: Accepted

Solution code:

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


is_reverse = False
pre_s = ""
suf_s = ""

for i in range(q):
  query = input()
  if query == '1':
    is_reverse = not is_reverse
  else:
    query = query.split()
    f, c = int(query[1]), query[2]
    if f == 1:
      if is_reverse:
        suf_s = suf_s + c
      else:
        pre_s = c + pre_s
    else:
      if is_reverse:
        pre_s = c + pre_s
      else:
        suf_s = suf_s + c

if is_reverse:
  print(suf_s[::-1] + s[::-1] + pre_s[::-1])
else:
  print(pre_s + s + suf_s)

Submission status: Accepted

Solution code:
from collections import deque

d = deque()
inversion = 1

s = input()

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

q = int(input())

for i in range(q):
    qn = list(input().split())
    if qn[0] == "1":
        inversion *= -1
    elif qn[0] == "2":
        if qn[1] == "1":
            if inversion == 1:
                d.appendleft(qn[2])
            elif inversion == -1:
                d.append(qn[2])
        if qn[1] == "2":
            if inversion == 1:
                d.append(qn[2])
            elif inversion == -1:
                d.appendleft(qn[2])

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

print()

Submission status: Accepted

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

Q = Q[::-1]

cnt = 0
left = []
right = []
for x in range(q):
    if len(Q[x]) == 1:
        cnt += 1
    else:
        if (int(Q[x][1]) + cnt) % 2 == 0:
            right.append(Q[x][2])
        else:
            left.append(Q[x][2])

#print(left, right)

for l in left:
    print(l, end="")
if cnt % 2 == 0:
    for s in S:
        print(s, end="")
else:
    for s in S[::-1]:
        print(s, end="")
for r in right[::-1]:
    print(r, end="")

print()

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = [list(map(str,input().split())) for i in range(Q)]
front = ""
back = ""
hanten_flag = 0
cnt = 0
for i in range(Q):
    if(Query[i][0] == "1"):
        hanten_flag ^= 1
    else:
        if(Query[i][1] == "1" and hanten_flag == 0):
            front = Query[i][2] + front
        elif(Query[i][1] == "1" and hanten_flag == 1):
            back = back + Query[i][2]
        elif(Query[i][1] == "2" and hanten_flag == 0):
            back = back + Query[i][2]
        else:
            front = Query[i][2] + front
SS = front + S + back
if(hanten_flag == 1):
    SS = SS[::-1]
print(SS)

Submission status: Accepted

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

T = [None] * Q
F = [None] * Q
C = [None] * Q
for i in range(Q):
    query = input().split()
    T[i] = int(query[0])
    if len(query) == 3:
        F[i] = int(query[1])
        C[i] = query[2]

ans = deque([S])
cnt1 = 0  # T[i]の偶奇
for i in range(Q):
    if T[i] == 1:
        cnt1 = (cnt1 + 1) % 2
    elif F[i] == 1:  # 先頭にCi追加
        if cnt1 == 0:  # そのまま
            ans.appendleft(C[i])
        else:   # 逆順にして末尾に追加
            ans.append(C[i][::-1])
    else:  # 末尾にCi追加
        if cnt1 == 0: # そのまま
            ans.append(C[i])
        else:
            ans.appendleft(C[i][::-1])

ans = ''.join(ans)

if cnt1 == 1:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

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

reverse_flag = False
s_head = list()

for i in range(n):
    line = list(map(str, input().split()))
    if line[0] == '1':
        if reverse_flag:
            reverse_flag = False
        else:
            reverse_flag = True
    elif line[0] == '2':
        if line[1] == '1':
            if reverse_flag:
                s.append(line[2])
            else:
                s_head.append(line[2])
        elif line[1] == '2':
            if reverse_flag:
                s_head.append(line[2])
            else:
                s.append(line[2])

if reverse_flag:
    ans = s[::-1]
    ans.extend(s_head)
    print(''.join(ans))
else:
    ans = s_head[::-1]
    ans.extend(s)
    print(''.join(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

s = input()
N = int(input())

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

d = deque(S)
    
back_count = 0
for i in range(N):
    Q = list(map(str, input().split()))
    Q[0] = int(Q[0])
    if Q[0] == 1:
        if back_count == 0:
            back_count = 1
        else:
            back_count = 0
    else:
        Q[1] = int(Q[1])
        if back_count == 0:
            if Q[1] == 1:
                d.appendleft(Q[2])
            else:
                d.append(Q[2])
        else:
            if Q[1] == 1:
                d.append(Q[2])
            else:
                d.appendleft(Q[2])

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

Submission status: Accepted

Solution code:
s  = str(input())
t = ""
u = ""
q = int(input())
count_inv = 0
for i in range(q):
  inlis = list(map(str,input().split())) + [0,0]
  if inlis[0] ==  "1":
    count_inv += 1
    count_inv %= 2
  if inlis[0] == "2":
    if inlis[1]  == "1":
      if count_inv == 0:
        u += str(inlis[2])
      else:
        t += str(inlis[2])
      # print(1)
    if inlis[1] == "2":
      if count_inv == 0:
        t += str(inlis[2])
      else:
        u += str(inlis[2])
      # print(2)
  # print(s)
if count_inv == 0:
  u = u[::-1]
  ans = u+s+t
else:
  t = t[::-1]
  s = s[::-1]
  ans = t+s+u
print(ans)

Submission status: Accepted

Solution code:
# input
S = input()
Q = int(input())
Query = []
for _ in range(Q):
    Query.append(input())

# process
t = True
hlist = []
flist = []
for q in Query:
    if q == "1":
        t = not t
    else:
        _, f, c = q.split()
        if f == "1":
            if t:
                hlist.append(c)
            else:
                flist.append(c)
        else:
            if t:
                flist.append(c)
            else:
                hlist.append(c)

# output
ans = "".join(hlist[::-1]) + S + "".join(flist)
if t:
    print(ans)
else:
    print("".join(list(ans)[::-1]))

Submission status: Accepted

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

s = list(inp())
q = iinp()

l = []

p = 0
o = 1

for i in range(q):
  a = inps()
  if o == 1 and a[0] == 1:
    continue
  if a[0] == "2":
    if a[1] == "1":
      if p == 0:
        l.append(a[2])
        o += 1
      else:
        s.append(a[2])
        o += 1
    else:
      if p == 0:
        s.append(a[2])
        o += 1
      else:
        l.append(a[2])
        o += 1
  if a[0] == "1":
    if p == 0:
      p = 1
    else:
      p = 0

if p == 0:
  for i in range(len(l)):
    print(l[len(l)-1-i],end="")
  for i in s:
    print(i,end="")
  print("")
else:
  for i in range(len(s)):
    print(s[len(s)-1-i],end="")
  for i in l:
    print(i,end="")
  print("")

Submission status: Accepted

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  pre=''
  post=''
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==1:
        pre = ''.join([pre,c])
      elif f==2:
        post = ''.join([post,c])

  pre = pre[::-1]
  S = pre+S+post
  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Accepted

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

from collections import deque
d = deque(s)

r = 1

for i in range(q):
  inp = input().split()
  if len(inp)==1:
    r = r*-1
  else:
    f = int(inp[1])
    if f==1:
      f=1
    else:
      f=-1
    if f*r==1:
      d.appendleft(inp[2])
    else:
      d.append(inp[2])
    #print(r,f, d)

if r==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:
from collections import deque


def main():
    S = list(input())
    dec_s = deque(S)
    flag = True  # Trueのときdec_s Falseのときre
    for _ in range(int(input())):
        t = list(input().split(' '))
        if len(t) == 1:
            flag = not flag
        else:
            if (t[1] == '1' and flag) or (t[1] == '2' and not flag):
                dec_s.appendleft(t[2])
            else:
                dec_s.append(t[2])
    if not flag: dec_s.reverse()
    print(''.join(dec_s))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
direction=0 #0:順 1:逆

for i in range(q):
    query=input()
    if query[0]=='1':
        direction=direction^1
    elif query[0]=='2':
        i,f,c=query.split()
        if f=='1' and direction==0:
            s.appendleft(c)
        elif f=='1' and direction==1:
            s.append(c)
        elif f=='2' and direction==0:
            s.append(c)
        elif f=='2' and direction==1:
            s.appendleft(c)


if direction==0:
    print(''.join(s))
else:
    x=[]
    for i in range(len(s)):
        x.append(s[-1-i])
    print(''.join(x))

Submission status: Accepted

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

before = [0] * (2 * (10**5))
after = [0] * (2 * (10**5))

count1 = True
before_count = 0
after_count = 0


for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        if count1:
            count1 = False
        else:
            count1 = True
    else:
        if int(q[1]) == 1:
            if count1:
                before[before_count] = q[2]
                before_count += 1
            else:
                after[after_count] = q[2]
                after_count += 1
        else:
            if count1:
                after[after_count] = q[2]
                after_count += 1
            else:
                before[before_count] = q[2]
                before_count += 1


for i in range(len(before)):
    if before[i] == 0:
        before = before[:i]
        break

for i in range(len(after)):
    if after[i] == 0:
        after = after[:i]
        break

before = ''.join(before)
after = ''.join(after)

before = before[::-1]
if count1:
    middle = S
    ans = before + middle + after
else:
    middle = S
    ans = before + middle + after
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

d = deque(input())
q = int(input())
reverse_flag = False

for i in range(q):
  query = input().split()
  if query[0] == "1":
    reverse_flag = not reverse_flag
  else:
    if (query[1] == "1") ^ reverse_flag:
      d.appendleft(query[2])
    else:
      d.append(query[2])

print("".join(list(d)[::-1]) if reverse_flag else "".join(d))