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:
x = input()
s = []
n = int(input())
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
    
a = ""
b = ""
c = 0
for i in range(n):
    if s[i][0] == "1":
        c += 1
        c = c%2
    else:
        if c == 0:
            if s[i][1] == "1":
                a = s[i][2] + a
            else:
                b = b + s[i][2]
        else:
            if s[i][1] == "1":
                b = b + s[i][2]
            else:
                a = s[i][2] + a
if c == 0:
    print(a + x + b)
else:
    print(''.join(list(reversed(b))) + ''.join(list(reversed(x))) + ''.join(list(reversed(a))))

Submission status: Accepted

Solution code:
def main():
    S = input()
    Q = int(input())
    queries = [input() for _ in range(Q)]

    reverse = False
    front = ""

    for e in queries:
        if e[0] == '1':
            reverse = not reverse
        else:
            T, F, C = e.split()
            if F == '2': F = True
            else: F = False

            if reverse ^ F:
                # 末尾に追加
                S += C
            else:
                # continue
                # 先頭に追加，ここの計算がボトルネック
                front += C
        # print(S)

    if reverse:
        S = S[::-1]
        S = S + front
    else:
        S = front[::-1] + S

    print(S)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

tmp = deque(S)
houkou = 1 #1:foward 2:reverse

for i in range(Q):
    inp = input().split()
    if inp[0] == '1':
        if houkou == 1:
            houkou = 2
        else:
            houkou = 1
    else:
        if houkou == 1 and inp[1] == '1':
            tmp.appendleft(inp[2])
        elif houkou == 1 and inp[1] == '2':
            tmp.append(inp[2])
        elif houkou == 2 and inp[1] == '1':
            tmp.append(inp[2])
        else:
            tmp.appendleft(inp[2])
    
#    print(houkou, tmp)

output = ''.join(list(tmp))
if houkou == 2:
    output = output[::-1]

print(output)

Submission status: Accepted

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

ok = True
for i in range(q):
    query = input()
    if query[0] == '1':
        if ok:
            ok = False
        else:
            ok = True
    else:
        query = query.split()
        if ok:
            if query[1] == '1':
                s.appendleft(query[2])
            else:
                s.append(query[2])
        else:
            if query[1] == '1':
                s.append(query[2])
            else:
                s.appendleft(query[2])
if not ok:
    s.reverse()
print(''.join(list(s)))

Submission status: Accepted

Solution code:

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

is_reverse = False
before = ""
after = ""

for _ in range(q):
  lst = input().split()
  if lst[0] == "1":
    is_reverse = not is_reverse
  else:
    _, f, c = lst
    if f == "1":
      if not is_reverse:
        before = c + before
      else:
        after = after + c
    else:
      if not is_reverse:
        after = after + c
      else:
        before = c + before

if is_reverse:
  s = s[::-1]
  before, after = after[::-1], before[::-1]

print(before + s + after)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
lenq = int(input())
Q = [input() for _ in range(lenq)]

# len(S) <= 100000
# lenq <= 2 * 10 ** 5

# q == '1' のとき、実際に反転すると O(N)かかるので反転しないで、
# どちらが先頭かだけの情報を持っておく
head = 1 # or -1
for q in Q:
    if q == '1':
        head *= -1
    else:
        _, f, c = q.split()
        if f == '1':
            if head == 1:
                S.appendleft(c)
            else:
                S.append(c)
        else:  # f == '2'
            if head == 1:
                S.append(c)
            else:
                S.appendleft(c)
S = ''.join(list(S))
if head == -1:
    S = S[::-1]
print(S)

Submission status: Accepted

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

ok = True #Falseで反転させる
for i in range(Q):
    l = q[i]
    if l[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if ok:
            if l[1] == "1": S.appendleft(l[2])
            else: S.append(l[2])
        else:
            if l[1] == "1": S.append(l[2])
            else: S.appendleft(l[2])
if not ok:
    S = list(S)[::-1]
print("".join(list(S)))

Submission status: Accepted

Solution code:
from collections import deque
 
S = deque(list(input()))
Q = int(input())
rvs = False
 
for q in range(Q):
    query = list(input().split())
    if len(query) > 1:
        if query[1] == "1":
            if rvs:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        elif query[1] == "2":
            if rvs:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    else:
        rvs = not (rvs)
 
if rvs:
    S.reverse()
 
print("".join(S))

Submission status: Accepted

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

def main():
    from itertools import chain
    S = input()
    Q = int(input())
    Queries = [input().split() for _ in range(Q)]
    H_T = H, T = [], []
    flag = 0
    for query in Queries:
        if len(query) == 1:
            flag ^= 1
            continue
        _, f, c = query
        H_T[(int(f) + flag - 1) % 2].append(c)
    t = ''.join(chain(reversed(H), S, T))
    if flag:
        t = reversed(t)
    print(''.join(t))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
mode = False
mae = ""
usiro = ""

for i in range(q):
  q_list = list(input().split())
  if q_list[0] == "1":
    mode = not mode
  else:
    if mode:
      if q_list[1] == "1":
        usiro = usiro + q_list[2]
      else:
        mae = q_list[2] + mae
    else:
      if q_list[1] == "1":
        mae = q_list[2] + mae  
      else:
        usiro = usiro + q_list[2]
if mode:
  print(usiro[::-1]+s[::-1]+mae[::-1])
else:
  print(mae+s+usiro)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flag = 1
left = ""
right = ""

for i in range(Q):
    q = input().split()
    if q==["1"]:
        flag *= -1
    else:
        if flag==1 and q[1]=="1":
            left = q[2]+left
        elif flag==1 and q[1]=="2":
            right += q[2]
        elif flag==-1 and q[1]=="1":
            right += q[2]
        else:
            left = q[2]+left
# print(S)

if flag==1:
    print(left+S+right)
else:
    print(right[::-1]+S[::-1]+left[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

if count % 2 == 1:
    s.reverse()

for i in s:
    print(i, end="")

Submission status: Accepted

Solution code:
from collections import deque


def main():
    side = 0
    S = input()
    len_s = len(S)
   
    d = deque()
    for s in S:
        d.append(s)
   
    n = int(input())
    for i in range(n):
        q = input().split()
        if len(q) == 1:
            side += 1
        elif len(q) == 3:
            len_s += 1
            if side % 2 == 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 = ""
    if side % 2 == 0:
        for i in range(len_s):
            S += d.popleft()
    else:
        for i in range(len_s):
            S += d.pop()
 
    print(S)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
quest = [0]*q
state = 0
former, latter = '', ''
for i in range(q):
    que = input().split()
# for que in quest:
    if (que[0] == '1'):
        # 反転は計算量が多いので一度だけする。
        state = 1 - state
        continue
    else:
        if (state == 0 and que[1] == '1'):
            former = que[2] + former
        elif(state == 0 and que[1] == '2'):
            latter = latter + que[2]
        elif (state == 1 and que[1] == '1'):
            latter = latter + que[2]
        else:
            former = que[2] + former

if (state == 1):
    print(latter[::-1] + s[::-1] + former[::-1])
else:
    print(former + s + latter)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
rev = False
ends = [[], []]
for _ in range(Q):
    parts = input().split()
    if parts[0] == "1":
        rev = not rev
    else:
        _, end, s = parts
        ends[(int(end) - 1) ^ rev].append(s)
res = list(reversed(ends[0])) + list(S) + ends[1]
if rev:
    res = reversed(res)
print("".join(res))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
tbl = [input().split() for _ in range(q)]
cnt = 0
r = []
l = []
for i in range(q-1,-1,-1):
    if tbl[i][0] == '1':
        cnt += 1
    else:
        if (cnt + int(tbl[i][1]))%2 == 1:
            l.append(tbl[i][2])
        else:
            r.append(tbl[i][2])
l = ''.join(l)
if cnt%2 == 1:
    s = s[::-1]
r = ''.join(r[::-1])
print(l+s+r)

Submission status: Accepted

Solution code:
from collections import deque

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

reverse = False
cnt = 0
for i in range(Q):
    inp = input().split()
    if inp[0] == "1":
        cnt += 1
        reverse = (cnt % 2 != 0)
        #S = S[::-1]
    else:
        f, c = inp[1:]
        if int(f) == 1:
            if reverse:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if reverse:
                S.appendleft(c)
            else:
                S.append(c)
S = "".join(S)
print(S[::-1] if reverse else S)

Submission status: Accepted

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

direction = True
front = ""
back = ""

for i in range(q):
    q_i = input().split()
    if q_i[0] == "1":
        if direction:
            direction = False
        else:
            direction = True

    else:
        if direction:
            if q_i[1] == "1":
                front += q_i[2]
            else:
                back += q_i[2]
        else:
            if q_i[1] == "1":
                back += q_i[2]
            else:
                front += q_i[2]

if direction:
    print(front[::-1] + s + back)
else:
    print(back[::-1] + s[::-1] + front)

Submission status: Accepted

Solution code:
from collections import deque
S=input()
st="X"
Q=int(input())
q=[]
for _ in range(Q):
  q.append(input())#qは文字列のリストだよ
t=0#totalひっくり返し
for i in range(Q):
  if q[i]=="1":
    t+=1
  else:
    T,F,C=q[i].split()
    if F=="1" and t%2==0:
      st=C+st
    elif F=="2" and t%2==0:
      st=st+C
    elif F=="1" and t%2==1:
      st=st+C
    else:
      st=C+st
if t%2==0:
  print(st.replace("X",S))
else:
  print(st.replace("X",S)[::-1])

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
n = int(input())
re = 0
st = ''
en = ''
for i in range(n):
    a = list(input().split())
    if a[0]=='1':
        re = 1 if re==0 else 0
    else:
        if a[1]=='1':
            if re==0:
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if re==0:
                s.append(a[2])
            else:
                s.appendleft(a[2])
if re==1:
    s.reverse()
res = list(s)
print(''.join(res))

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    l,r = '',''
    rev = 0
    for i in range(q):
        Q = input().split()
        if Q[0]=="1":
            rev = (rev+1)%2
        else:
            if Q[1]=='1':
                if rev==0:
                    l = Q[2]+l
                else:
                    r = r+Q[2]
            else:
                if rev==0:
                    r = r+Q[2]
                else:
                    l = Q[2]+l
    s = l+s+r
    if rev==0:
        print(s)
    else:
        print(s[::-1])

if __name__ == "__main__":
    main()

Submission status: Accepted

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

add_s = ['', ''] #pre, post
rev_flag = 0

for i in range(q):
    query = input()
    if query == '1':
        rev_flag = (rev_flag+1)%2
    else:
        _, f, c = query.split()
        if f == '1':
            add_s[rev_flag] += c
        else:
            add_s[(rev_flag+1)%2] += c

if rev_flag == 1:
    s = s[::-1]

print(add_s[rev_flag][::-1]+s+add_s[(rev_flag+1)%2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

S = deque(list(input()))
Q = int(input())
rvs = False

for q in range(Q):
    query = list(input().split())
    if len(query) > 1:
        if query[1] == "1":
            if rvs:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        elif query[1] == "2":
            if rvs:
                S.appendleft(query[2])
            else:
                S.append(query[2])
    else:
        rvs = not (rvs)

if rvs:
    S.reverse()

print("".join(S))

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    S=input()
    Q=int(input())
    A=[input().split() for _ in range(Q)]
    d=deque(S)
    
    d_left=d.appendleft
    d_right=d.append
    j=["1","2"]
    k=0
    j_re=j.reverse
    for x in A:
        if x[0]=="1":
            j_re()
            k+=1
        else:
            if x[1]==j[0]:
                d_left(x[2])
            else:
                d_right(x[2])
    if k%2==1:
        d.reverse()
    print("".join(d))    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#前・左につける変数、後ろ・右につける変数を用意して最後に足す
#前・左は最後にひっくり返す（abc ⇒cba)

#初期入力
import sys
input = sys.stdin.readline
S = input()
S=S.strip("\n")
Q = int(input())
FQ_dic={}
for i in range(Q):
    FQ_dic[i] =input().split()

rev =0
front =""
back =""

for i in range(Q):
   # print(S) 

    if FQ_dic[i][0] =="1": #逆転する
                rev +=1
    else:                   #逆転なし  
        if FQ_dic[i][1]=="1":     #前=左につける 
            if rev %2 ==0:
                front += FQ_dic[i][2]
            else:
                back  += FQ_dic[i][2]  
        else: #                      #後ろ=右につける
            if rev %2 ==0:
                back  += FQ_dic[i][2]  
            else:        
                front += FQ_dic[i][2]
front =front[::-1]
S =front + S + back

if rev %2 ==0:
    print(*S,sep="")
else:
    print(*S[::-1],sep="")

Submission status: Accepted

Solution code:
from collections import deque

def main():
    S = deque(input())
    
    Q = int(input())
    count = 1

    for i in range(Q):
        n = input()
        if n == "1":
            #qi[i][0] = 1
            count += 1
        else:
            q= list(n.split())
            if (count % 2 == 1 and q[1] == "1") or(count % 2 == 0 and q[1] == "2"):
                S.appendleft(q[2])
            else:
                S.append(q[2]) 

    if count % 2 == 0:
        S.reverse()
    print(''.join(S))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
s = list(S)
d = deque()
for i in range(len(s)):
    d.append(s[i])
Q = int(input())
p = [[0 for i in range(3)]for j in range(Q)]
x = 1
for i in range(Q):
    p[i] = list(map(str,input().split()))
def add(s,x,f,c):
    if x ==1:
        if f == 1:
            s.appendleft(str(c))
        else:
            s.append(str(c))
    else:
        if f == 1:
            s.append(str(c))
        else:
            s.appendleft(str(c))
for i in range(Q):
    if int(p[i][0]) == 1:
        x = -x
    else:
        add(d,x,int(p[i][1]),p[i][2])
if x == 1:
    print(''.join(d))
else:
    d.reverse()
    print(''.join(d))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
q = [input() for i in range(Q)]
reverse = False
p =[]
f =[]
appP = p.append
appF = f.append

for i in range(0,Q,1):
    if q[i][0]=="1":
        reverse=not reverse

    elif q[i][0]=="2":
        num,F,C = q[i].split(sep=" ")
        if F=="1":
            if reverse == False:
                appP(C)
            else:
                appF(C)
        elif F=="2":
            if reverse == False:
                appF(C)
            else:
                appP(C)
if reverse:
    tmpP="".join(p)
    tmpF="".join(f[::-1])
    print(tmpF+S[::-1]+tmpP)
else:
    tmpF="".join(f)
    tmpP="".join(p[::-1])
    print(tmpP+S+tmpF)

Submission status: Accepted

Solution code:
import numpy as np
s = input()
q = int(input())
rbef = []
aft = []
sgn = 1
for i in range(q):
    qi = np.array(input().split(),dtype=str)
    if qi[0] == "1":
        sgn *= -1
    else:
        if (qi[1] == "1" or sgn == -1) and (qi[1] == "2" or sgn == 1):
            rbef.append(qi[2])
        else:
            aft.append(qi[2])

bef = "".join(reversed(rbef))
taft = "".join(aft)
ans = bef + s + taft

if sgn == 1:
    print(ans)
else:
    rans = ''.join(list(reversed(ans)))
    print(rans)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
rev = False
ad = ''
for _ in range(Q):
    A = input().split()
    if len(A) == 1:
        rev ^= True
    else :
        if (A[1] == '1' and rev == False) or (A[1] == '2' and rev == True):
            ad += A[2]
        else :
            S += A[2]

ans = (ad[::-1] + S)[::1 - 2 * rev]
print(ans)

Submission status: Accepted

Solution code:
import collections as co
S=input()
Q=int(input())
count=0
back=str()
front=str()
judge=True
for i in range(Q):
    a=list(map(str,input().split()))   
    if  int(a[0])==1:
        if judge==True:
            judge=False
        else:
            judge=True
    elif int(a[0])==2 and int(a[1])==1:
        if judge==True:
            front=a[2]+front
        if judge==False:
            back=back+a[2]
    else:
        if judge==True:
            back=back+a[2]
        if judge==False:
            front=a[2]+front
    a.clear
if judge==True:
    S=front+S+back
if judge==False:
    S=front+S+back
    S=S[::-1]
print(S)

Submission status: Accepted

Solution code:
s=input()
n=int(input())
str=[""]*(n*2+1)
str[n]=s
start=n-1
end=n+1
dir=1


for _ in range(n):
    h=input()
    if h[0]=="1":
        if end-start!=2:
            dir*=-1
    else:
        if h[2]=="1":
            if dir==1:
                str[start]=h[4]
                start-=1
            else:
                str[end]=h[4]
                end+=1
        else:
            if dir==1:
                str[end]=h[4]
                end+=1
            else:
                str[start]=h[4]
                start-=1
if dir==-1:
    str.reverse()
    str[n]="".join(list(reversed(str[n])))
print("".join(str))

Submission status: Accepted

Solution code:
import collections
s = input()
q = int(input())
back = 1
t = collections.deque()
for ss in s:
    t.append(ss)
for i in range(q):
    query = input().split()
    if query[0] == '1':
        back *= -1
    else:
        if query[1] == '1':
            if back == 1:
                t.appendleft(query[2])
            else:
                t.append(query[2])
        else:
            if back == 1:
                t.append(query[2])
            else:
                t.appendleft(query[2])
ans = ''
if back == -1:
    for i in range(len(t)):
        ans += t.pop()
else:
    for i in range(len(t)):
        ans += t.popleft()
print(ans)

Submission status: Accepted

Solution code:
s = input()
T = ""
n = int(input())
for i in range(n):
    # print(s,T)
    t = list(map(str,input().split()))
    if t[0] == "1":
        s,T = T,s
    else:
        if t[1] == "1":
            T += t[2]
        else:
            s += t[2]

print(T[::-1]+s)

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
judge = 1
front = []
back = []
for i in range(q):
  a = list(map(str,input().split()))
  if int(a[0]) == 1:
    judge *= -1
  else:   
    if (int(a[1]) == 1 and judge == 1) or (int(a[1]) == 2 and judge == -1):
      front.append(a[2])
    else:
      back.append(a[2])
ans = front[::-1] + s + back
if judge == -1:
  ans.reverse()
print(*ans, sep='')

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
Q = int(input())
a = True
for i in range(Q):
    t = list(input().split())
    if t[0] == "1":
        a = not(a)
    if t[0] == "2":
        if ((t[1] == "1") and not a ) or ((t[1]=="2") and a ):
            s.append(t[2])
        else:
            s.appendleft(t[2])
if not a :
    s.reverse()
print(*s,sep = "")

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
reverse_mode = False #反転毎に全体を反転しているとTLEする
reversed_count = 0
 
for i in range(Q):
    query = [str(e) for e in input().split()]
    if len(query)==1:
        reversed_count += 1
        #反転のモードを切り替える
        if reverse_mode == False:
            reverse_mode = True
        elif reverse_mode == True:
            reverse_mode = False
    
    else:
        if reverse_mode == False:
            if query[1]=="1":
            #先頭に追加
                S.appendleft(query[2])
            else:
            #末尾に追加
                S.append(query[2])
        elif reverse_mode == True:
            if query[1]=="1":
            #反転した先頭に追加＝末尾に追加
                S.append(query[2])
            else:
            #反転した末尾に追加＝先頭に追加
                S.appendleft(query[2])

#終了時に反転回数に応じて逆転orそのままにする
if reversed_count%2 != 0:
    S.reverse()
    
#print(S)
print(''.join(map(str,S)))

Submission status: Accepted

Solution code:
from math import floor
import sys
from copy import copy

from collections import deque
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  # 1e17
mod = 998244353




S = input().strip()
D = deque(S)


n = int(input())
reverse = False
for operation in range(n):
    data = input().strip()
    if data[0] == "1":
        if reverse is False:
            reverse = True
        else:
            reverse = False
    else:
        f = data[2]
        ele = data[4]
        if (f == "1" and reverse is False) or (f == "2" and reverse == True):
            D.appendleft(ele)
        else:
            D.append(ele)
#    print(S)
# print(reverse)
if reverse is True:
    D.reverse()
for d in D:
    print(d,end="")

Submission status: Accepted

Solution code:
from math import floor
import sys
from copy import copy

from collections import deque
input = sys.stdin.readline
'''
n, m = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
S = input().strip()
for test in range(int(input())):
'''
inf = 100000000000000000  # 1e17
mod = 998244353




S = input().strip()
D = deque(S)


n = int(input())
reverse = False
for operation in range(n):
    data = input().strip()
    if data[0] == "1":
        if reverse is False:
            reverse = True
        else:
            reverse = False
    else:
        f = data[2]
        ele = data[4]
        if (f == "1" and reverse is False) or (f == "2" and reverse == True):
            D.appendleft(ele)
        else:
            D.append(ele)
#    print(S)
# print(reverse)
if reverse is True:
    D.reverse()
# print(*D,end="")
print("".join(D))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    s=input()
    q=int(input())
    s=deque(s)
    inv=False
    for i in range(q):
        que=input()
        if que[0]=='1':
            inv=not(inv)
        else:
            f=que[2]
            if (f=='1' and not(inv)) or (f=='2' and inv):
                s.appendleft(que[4])
            else:
                s.append(que[4])
    if inv:s.reverse()
    print(*s,sep='')



if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import deque
def main():
    s=input()
    q=int(input())
    s=deque(s)
    inv=False
    for i in range(q):
        que=input()
        if que[0]=='1':
            inv=not(inv)
        else:
            f=que[2]
            if (f=='1' and not(inv)) or (f=='2' and inv):
                s.appendleft(que[4])
            else:
                s.append(que[4])
    if inv:s.reverse()
    print(*s,sep='')

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

rev = 0
for i in range(q):
    inp = list(input().split())
    if inp[0] == '1':rev = rev^1
    else:
        if (inp[1] == '1' and rev == 0) or (inp[1] == '2' and rev == 1):
            s.appendleft(inp[2])
        else:
            s.append(inp[2])

s = list(s)
if rev == 1:s = s[::-1]
print(*s, sep = '')

Submission status: Accepted

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

S = deque(S)

rev = False
for i in range(Q):
  query = list(input())
  if query[0] == '1':
    rev = not rev
  else:
    c = query[4]
    if query[2] == '1' and not rev or (rev and query[2] == '2'):
      S.appendleft(c)
    else:
      S.append(c)
      
if rev:
  S.reverse()


print(''.join(S))

Submission status: Accepted

Solution code:
import sys

s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline().rstrip())
query = [sys.stdin.readline().rstrip().split() for i in range(q)]
top = 1
s = list(s)
s_front = []
s_back = []
for i in range(q):
    que = query[i]
    if que[0] == '1':
        top *= -1
    else:
        if que[1] == '1':
            if top > 0:
                s_front.append(que[2])
                # s = ''.join([que[2], s])
                # s = que[2] + s
            else:
                s_back.append(que[2])
                # s = ''.join([s, que[2]])
                # s = s + que[2]
        else:
            if top > 0:
                s_back.append(que[2])
                # s = ''.join([s, que[2]])
                # s = s + que[2]
            else:
                s_front.append(que[2])
                # s = ''.join([que[2], s])
                # s = que[2] + s
s_front.reverse()
s_front.extend(s)
s_front.extend(s_back)
s = s_front
s = ''.join(s)
if top < 0:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
cnt1=0
s_mae=[]
s_ushiro=[]
for i in range(q):
    query=list(input().split())
    if int(query[0])==1:
        cnt1+=1
    else:
        if (int(query[1])+cnt1)%2==0:
            s_ushiro.append(query[2])
        else:
            s_mae.append(query[2])
s1=""
for i in range(len(s_mae)):
    s1=s1+s_mae[-1-i]
s2="".join(s_ushiro)
ans=s1+s+s2
if cnt1%2==0:
    print(ans)
else:
    ans2=""
    for i in range(len(ans)):
        ans2=ans2+ans[-1-i]
    print(ans2)

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(list(S))
reverse = False

for i in range(Q):
    query = input().split()

    if query[0] == '1':
        reverse = not reverse

    elif query[0] == '2':
        if query[1] == '1' and not reverse:
            d.appendleft(query[2])
        elif query[1] == '2' and not reverse:
            d.append(query[2])
        elif query[1] == '1' and reverse:
            d.append(query[2])
        elif query[1] == '2' and reverse:
            d.appendleft(query[2])

if reverse:
    d.reverse()

print(''.join(d))

Submission status: Accepted

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

inverse = 1
left = ''
right = ''
for i in range(q):
    query = list(map(str, input().split()))
    if query[0] == '1':
        inverse *= -1
    if query[0] == '2':
        if query[1] == '1' and inverse == 1:
            left += query[2]
        elif query[1] == '2' and inverse == 1:
            right += query[2]
        elif query[1] == '1' and inverse == -1:
            right += query[2]
        elif query[1] == '2' and inverse == -1:
            left += query[2]
s = left[::-1]+s+right
if inverse == 1:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readlines = sys.stdin.readlines

s = readline().rstrip()
q = readline()
queries = readlines()
s = deque(s)
tot_1 = 0
for i in queries:
    if i[0] == '1':
        tot_1 += 1
        continue
    c = i[4]
    if (tot_1 + int(i[2])) % 2 == 0:
        s.append(c)
    else:
        s.appendleft(c)
ans = "".join(s) if tot_1 % 2 == 0 else "".join(reversed(s))
print(ans)