Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Accepted

Solution code:
from collections import deque
S = input()
ans = deque(S)
Q = int(input())
rev = False
for i in range(Q):
    a_list = input().split()
    if a_list[0] == "1":
        if rev == True:
            rev = False
        else:
            rev = True
    elif (a_list[1] == "1" and rev == False) or (a_list[1] == "2" and rev == True):
        ans.appendleft(a_list[2])
    else:
        ans.append(a_list[2])
if rev:
    ans.reverse()
ans = "".join(ans)
print(ans)

Submission status: Accepted

Solution code:
S=input()
p=1
head=""
tail=""
Q=int(input())
for _ in range(Q):
  q=input()
  if q=='1':
    p^=1
  else:
    a,f,c=q.split()
    f=int(f)%2
    if p==0:
      f=(f+1)%2
    if f:
      head+=c
    else:
      tail+=c
if p:
  S=head[::-1]+S+tail
else:
  S=tail[::-1]+S[::-1]+head
print(S)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
b = "" #reversed
a = ""
f = 0
for i in range(Q):
  q = input()
  if q == "1":
    f+=1
  else:
    _,F,C = q.split()
    if (f+int(F))%2==1:
      b+=C
    else:
      a+=C
if f%2==0:
  print(b[::-1]+S+a)
else:
  print(a[::-1]+S[::-1]+b)

Submission status: Accepted

Solution code:
S = input().rstrip()
Q = int(input().rstrip())
a = -1
before =[]
after =[]
for _ in range(Q):
    stdin = input().rstrip()
    if int(stdin[0]) == 1:
        a = ~a
    else:
        T,F,C = stdin.split()
        if int(F) == 2+a:
            before.append(C)
        else:
            after.append(C)
S = ''.join(before)[::-1] + S + ''.join(after)
if a == 0:
    S = S[::-1]
print(S)

Submission status: Accepted

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

cnt=0
left=""
right=""

for i in range(q):
  qq=[x for x in input().rstrip().split()]

  if qq[0]=="2":
    t=int(qq[0])
    f=int(qq[1])
    c=qq[2]
    
    if cnt%2==1 and f==1:
      right=right+c
    
    elif cnt%2==1 and f==2:

      left=c+left
    elif cnt%2==0 and f==2:
  
      right=right+c
    elif cnt%2==0 and f==1:

      left=c+left

  else:
    cnt+=1

s=left+s+right
if cnt%2==1:
  print(s[::-1])
else:
  print(s)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
h=0
m=''
u=''
for _  in range(q):
  d=input()
  if '2' in d:
    a,b,c=map(str,d.split())
    if b=='1':
      if h%2==0:
        m+=c
      else:
        u+=c
        
    else:
      if h%2==0:
        u+=c
      else:
        m+=c
        
  else:
    h+=1
    
M=list(m)
G=list(reversed(M))
GG=''.join(G)
s=GG+s+u
if h%2==0:
  print(s)
else:
  f=list(s)
  g=list(reversed(f))
  print(''.join(g))

Submission status: Accepted

Solution code:
from collections import deque
if __name__ == '__main__':

    s = input()
    n = int(input())

    rvflg = False

    d_st = deque()
    d_ed = deque()

    for _ in range(n):
        q = input()
        if len(q) == 1:
            if rvflg:
                rvflg = False
            else:
                rvflg = True
        else:
            i,f,c = map(str,q.split())
            if f == "1":#先頭に追加
                if rvflg:
                    #末尾に追加
                    d_ed.append(c)
                else:
                    #先頭に追加
                    d_st.appendleft(c)
            else:#末尾に追加
                if rvflg:
                    #先頭に追加
                    d_st.appendleft(c)
                else:
                    #末尾に追加
                    d_ed.append(c)

    ans = "".join(d_st) + s + "".join(d_ed)
    #最後に反転するかを決定
    if rvflg:
        ans = ans[::-1]
    print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':

	s = input()
	n = int(input())

	rvcnt = 0

	tmp = "S"
	tmp_before = ""
	tmp_after = ""
	
	for _ in range(n):
		q = input()
		if len(q) == 1:
			rvcnt += 1
		else:
			i,f,c = map(str,q.split())
			if f == "1":#先頭に追加
				if rvcnt % 2 == 0:
					#先頭に追加
					tmp_before = c + tmp_before
				else:
					#末尾に追加
					tmp_after = tmp_after + c
			else:#末尾に追加
				if rvcnt % 2 == 0:
					#末尾に追加
					tmp_after = tmp_after + c
				else:
					#先頭に追加
					tmp_before = c + tmp_before


	#仮想を元に戻す
	tmp1 = tmp.replace("S",s)
	#連結
	tmp2 = tmp_before + tmp1 + tmp_after

	#最後に反転するかを決定
	if rvcnt % 2 == 1:
		print(tmp2[::-1])
	else:
		print(tmp2)

Submission status: Accepted

Solution code:
from collections import deque
s=deque(input())
q=int(input())
flag=0
for i in range(q):
  query=input()
  if len(query)==1:
    flag+=1
  else:
    if query[2]=="1" and flag%2==0 or query[2]=="2" and flag%2==1:
      s.appendleft(query[4])
    else:
      s.append(query[4])
if flag%2==1:
  s.reverse()
print("".join(s))

Submission status: Accepted

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

ct=0
reverse=[]
string=[deque(''), deque('')]
# 先頭に付け足す->string[0]にappendleft
# 最後尾に付け足す->strint[1]にappend
# ctの偶奇が変わる毎に付け足す方を変える

for i in range(Q):
    l=list(input().split())
    if l==['1']:
        reverse.append(i)
        ct+=1
    elif (int(l[1])+ct)%2==1:
        string[0].appendleft(l[2])
    else:
        string[1].append(l[2])

# print(string)
# print(ct)
if ct%2==0:
    top=''.join(string[0])
    bottom=''.join(string[1])
else:
    top=''.join(reversed(string[1]))
    bottom=''.join(reversed(string[0]))
    S=S[::-1]
    # print(S)
print(top+S+bottom)

Submission status: Accepted

Solution code:
from collections import deque

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

dt = deque()
de = deque()

f1 = 1
f2 = 2

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        f1,f2 = f2,f1
    elif int(q[0]) == 2:
            if int(q[1]) == f1:
                dt.appendleft(q[2])
            elif int(q[1]) == f2:
                de.append(q[2])
                
st = "".join(dt)
se = "".join(de)
ans = st + s + se

if f1 == 1:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Submission status: Accepted

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

f1 = 1
f2 = 2

st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        f1,f2 = f2,f1
    elif int(q[0]) == 2:
            if int(q[1]) == f1:
                st = q[2] + st
            elif int(q[1]) == f2:
                se += q[2]

ans = st + s + se
if f1 == 1:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Submission status: Accepted

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

f1 = 1
f2 = 2
cnt = 0
st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        cnt += 1
    elif int(q[0]) == 2:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                st = q[2] + st
            elif int(q[1]) == 2:
                se += q[2]
        else:
            if int(q[1]) == 1:
                se += q[2]
            elif int(q[1]) == 2:
                st = q[2] + st


ans = st + s + se
if cnt % 2 == 0:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S=deque(input())
Q=int(input())
count = 0
for i in range(Q):
    q = list(input().split())
    #print(q)
    if q[0] == "1":
        count += 1
    else:
        if count%2 == 0:
            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 count%2 != 0:
    S = reversed(S)
print("".join(S))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase
from collections import deque as dq

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
##############################################################################
from collections import deque as dq
L=dq(list(input()))
C=0
for _ in range(int(input())):
    S=input().split()
    if S == ['1']:
        C+=1
    elif C%2==0:
        if S[1]=='1':
        	L.appendleft(S[2])
        else:
        	L.append(S[2])
    elif C%2!=0:
        if S[1]=='1':
        	L.append(S[2])
        else:
        	L.appendleft(S[2])
if C%2!=0:
    L.reverse()
print(''.join(L))

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
l1 = []
l2 = []
rev = False
for i in range(q):
    inp = input()
    if inp[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    elif inp[2] == "1":
        a = inp[4]
        if rev:
            l2.append(a)
        else:
            l1.append(a)
    else:
        a = inp[4]
        if rev:
            l1.append(a)
        else:
            l2.append(a)
l1.reverse()
ans = l1 + s + l2
if rev:
    ans.reverse()
print("".join(ans))

Submission status: Accepted

Solution code:
s = input()
n = int(input())
c = 0
left_l = ['', '']
right_l = ['', '']
s_l = [s, s[::-1]]
for _ in range(n):
  lst = [i for i in input().split()]
  if lst[0] == '1':
    if c == 1:
      s_l[0], s_l[1] = s_l[1], s_l[0]
      #print(s)
    else:
      s_l = [right_l[1] + s_l[1] + left_l[1], left_l[0] + s_l[0] + right_l[0]]
      left_l[0], left_l[1] = '', ''
      right_l[0], right_l[1] = '', ''
      c = 1
      #print(s)
  elif lst[0] == '2':
    c = 0
    if lst[1] == '1':
      left_l[0] = lst[2] + left_l[0]
      left_l[1] = left_l[1] + lst[2]
    elif lst[1] == '2':
      right_l[0] = right_l[0] + lst[2]
      right_l[1] = lst[2] + right_l[1]
w = left_l[0] + s_l[0] + right_l[0]
print(w)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
flip = 0
head = []
tail = []
for i in range(Q):
  Query = input().split()
  if len(Query) == 3:
    T,F,C = Query
    F = int(F)-1
    if F^flip == 1:
      tail.append(C)
    else:
      head.append(C)
  else:
    flip ^= 1
if flip:
  head, tail = tail, head
  S = S[::-1]
head = ''.join(head[::-1])
tail = ''.join(tail)
print(head+S+tail)

Submission status: Accepted

Solution code:
s= input();
q = int(input());
flag = 0;
fornt = "";
back = "";
while(q>0):
    q-=1;
    ls = list(map(str,input().split(" ")));
    if(ls[0]=='1'):
        flag +=1;
    else:
        if(flag%2 == 1):
            if(ls[1]=='2'):
                fornt=ls[2]+fornt;
            else:
                back=back+ls[2];
        else:
            
            if(ls[1]=='1'):
                fornt=ls[2]+fornt;
            else:
                back=back+ls[2];
s = fornt+s+back
if(flag%2==0):
    print(s);
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
s1 = ""
s2 = ""
c = 1
for i in range(q):
  a = input()
  if " " in a:
    l = list(a.split())
    c2 = 3-2*int(l[1])
    if c*c2 == 1:
      s1 = s1 + l[2]
    else:
      s2 = s2 + l[2]
  else:
    c = -c
s = s1[::-1] + s + s2
if c == 1:
  print(s)
else:
  print(s[::-1])

Submission status: Accepted

Solution code:
def main():
    S=input()
    Q=int(input())
    Left=[]
    Right=[]

    reverse_flag=0
    for i in range(Q):
        query=input().split()
        if query[0]=="1":
            if reverse_flag==1:
                reverse_flag=0
            else:
                reverse_flag=1
        else:
            if query[1]=="1":
                if reverse_flag==0:
                    Left.append(query[2])
                else:
                    Right.append(query[2])
            else:
                if reverse_flag==0:
                    Right.append(query[2])
                else:
                    Left.append(query[2])

    res=""
    if reverse_flag==0:
        Left.reverse()
        res+="".join(Left)
        res+=S
        res+="".join(Right)

    else:
        Right.reverse()
        res+="".join(Right)
        S=list(S)
        S.reverse()
        res+="".join(S)
        res+="".join(Left)
    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
S = input()
A = ["", ""]
Q = int(input())
b = 0
for _ in range(Q):
    query = input()
    if query[0] == "1":
        b ^= 1
    else:
        F = int(query[2])
        A[b^F-1] += query[4]
ans = A[0][::-1] + S + A[1]
if b:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = str(input())
Q = int(input())
flip = 0
front,back = '',''
for i in range(Q):
  j = input()
  if len(j) == 1:
    flip += 1
    continue
  else:
    if j[2] == '1' and flip % 2 == 0:
      front = j[4] + front
      continue
    elif j[2] == '2' and flip % 2 == 0:
      back = back + j[4]
      continue
    elif j[2] == '1' and flip % 2 ==1:
      back= back + j[4]
      continue
    elif j[2] == '2' and flip % 2 ==1:
      front = j[4] + front
      continue
S = front + S + back
if flip % 2 == 1:
  S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
q = int(input())

reverse = False
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        reverse = not reverse
    else:#2
        if query[1] == '1':
            if reverse:
                S.append(query[2])
            else:
                S.appendleft(query[2])
        else:
            if reverse:
                S.appendleft(query[2])
            else:
                S.append(query[2])

if reverse:
    while S:
        print(S.pop(), end='')
else:
    while S:
        print(S.popleft(), end='')

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
A=[list(input().split()) for i in range(Q)]
c=0
R=""
L=""
for i in range(Q):
    if A[i][0]=="1":
        c+=1
        continue
    if c%2==1 and A[i][0]=="2":
        if A[i][1]=="1":
            R=R+A[i][2]
        elif A[i][1]=="2":
            L=A[i][2]+L
    elif c%2==0 and A[i][0]=="2":
        if A[i][1]=="1":
            L=A[i][2]+L
        elif A[i][1]=="2":
            R=R+A[i][2]
S=L+S+R
if c%2==1:
    S=S[::-1]
print(S)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
flag = True
head = '' # 最終的に先頭に追加されている文字
end = '' # 最終的に末尾に追加されている文字

for i in range(q):
    que = [i for i in input().split()]
    if que[0] == '1':
        flag ^= True # Trueなら通常，Falseは反転状態
    elif que[0] == '2' and flag is True:
        if que[1] == '1':
            head = que[2] + head
        elif que[1] == '2':
            end = end + que[2]
    elif que[0] == '2' and flag is False:
        if que[1] == '1':
            end = end + que[2]
        elif que[1] == '2':
            head = que[2] + head
s = head+s+end
if flag is False:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
from collections import deque
S=input()
d=deque()
for i in S:
    d.append(i)
Q=int(input())
Flag=True
for i in range(Q):
    L=input().split()
    if L[0]=="1":
        Flag=not Flag
    else:
        if L[1]=="1" and Flag or L[1]=="2" and not Flag:
            d.appendleft(L[2])
        else:
            d.append(L[2])
ans=""
for i in d:
    ans+=i
if not Flag:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
hanten=False
mae=""
usiro=""
for i in range(q):
    tmp=input()
    if len(tmp)==1:
        hanten=not(hanten)
    else:
        if hanten==False:
            if tmp[2]=="1":
                mae=tmp[4]+mae
            else:
                usiro=usiro+tmp[4]
        else:
            if tmp[2]=="1":
                usiro=usiro+tmp[4]
            else:
                mae=tmp[4]+mae
                
ans=mae+s+usiro
if hanten==False:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

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

reverse = False
for _ in range(Q):
    q = sys.stdin.readline()
    if q[0] == "1":
        reverse = not reverse
    else:
        _, f, c = q.split()
        if f == "1":
            if reverse:
                S.append(c)
            else:
                S.appendleft(c)
        else:
            if reverse:
                S.appendleft(c)
            else:
                S.append(c)
if reverse:
    S.reverse()
ans = "".join(S)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

s = input()

r = False
d = deque()
d.extend(list(s))

q = int(input())
for i in range(q):
    t = input()
    if t == "1":
        r = not r
    else:
        t, f, c = list(t.split())
        f = False if f=="1" else True

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

if r:
    d.reverse()

print("".join(d))

Submission status: Accepted

Solution code:
import collections
import sys

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

r=0
for _ in range(Q):
    fs=next(sys.stdin).split()
    T=int(fs[0])
    if T==1:
        r^=1
    else:
        F,C=int(fs[1])-1,fs[2]
        if F==r:
            S.appendleft(C)
        else:
            S.append(C)

ans=''.join(reversed(S) if r else S)
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections

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

rev=0
for _ in range(Q):
    fields=next(sys.stdin).split()
    T,F,C=(fields+['',''])[:3]
    if T=='1':
        rev^=1
    elif (rev and F=='1') or (not rev and F=='2'):
        S.append(C)
    else:
        S.appendleft(C)

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

Submission status: Accepted

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

d = 0
X = [[], []]
for i in range(Q):
    q = input()
    if q[0] == "1":
        d = (d + 1) % 2
    else:
        a, f, c = q.split()
        x = (int(f) + d) % 2
        X[x].append(c)

if d == 0:
    v = "".join(X[1])[::-1] + S + "".join(X[0])
else:
    v = "".join(X[0])[::-1] + S[::-1] + "".join(X[1])
print(v)

Submission status: Accepted

Solution code:
S=input()

Q=int(input())

inst=[]
for i in range(Q):
    array=input().split()
    inst.append(tuple(array))

rev=True
front_str=[]
back_str=[]
for i in range(Q):
    if int(inst[i][0])==1:
        rev=not rev
    elif rev:
        if int(inst[i][1])==1:
            front_str.append(inst[i][2])
        else:
             back_str.append(inst[i][2])
    else:
        if int(inst[i][1])==1:
            back_str.append(inst[i][2])
        else:
            front_str.append(inst[i][2])

if rev:
    front_str.reverse()
else:
    back_str.reverse()
    S=S[::-1]

front="".join(front_str)
back="".join(back_str)

if rev:
    print(front+S+back)
else:
    print(back+S+front)

Submission status: Accepted

Solution code:
from collections import deque

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

#flag=0 でleft, flag=1でrightが先頭
flag = 0
cnt = 0
d  = deque(s)

for _ in range(q):
    lis = list(input().split())
    if lis[0] == "1":
        if flag:
            flag = 0
        else:
            flag = 1
        cnt += 1
    else:
        if lis[1] == '1':
            if flag:
                d.append(lis[2])
            else:
                d.appendleft(lis[2])
        else:
            if flag:
                d.appendleft(lis[2])
            else:
                d.append(lis[2])


d = list(d)
if cnt % 2 != 0:
    d = d[::-1]


print(''.join(d))

Submission status: Accepted

Solution code:
def solve():
    from collections import deque
    S = input()
    s = deque(S)
    Q = int(input())
    reverse = False
    for i in range(Q):
        T = input().split()
        if T[0] == '1':
            if reverse:
                reverse = False
            else:
                reverse = True
        else:
            if T[1] == '1' and not reverse:
                s.appendleft(T[2])
            elif T[1] == '1' and reverse:
                s.append(T[2])
            elif T[1] == '2' and not reverse:
                s.append(T[2])
            else:
                s.appendleft(T[2])
    if reverse:
        print(''.join(s)[::-1])
    else:
        print(''.join(s))

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    reverse_flag = False
    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one:
            reverse_flag ^= True 
            # q.reverse()
        elif T == two:
            F = query[2]
            C = query[4]
            if (F == one) ^ reverse_flag:
                q.appendleft(C)
            else:
                q.append(C)

    return bytes(reversed(q)) if reverse_flag else bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import collections
import itertools


def resolve(in_):
    one = ord(b'1')
    two = ord(b'2')
    # print(f'{one=} {two=}')

    S = in_.readline().strip()
    q = collections.deque(S)
    # print(f'{S=} {q=}')

    reverse_flag = False
    Q = int(in_.readline())
    for query in itertools.islice(in_, Q):
        T = query[0]
        if T == one:
            reverse_flag ^= True 
            # q.reverse()
        elif T == two:
            F = query[2]
            C = query[4]
            if (F == one) ^ reverse_flag:
                q.appendleft(C)
            else:
                q.append(C)
    else:
        if reverse_flag:
            q.reverse()
            reverse_flag = 0

    return bytes(q)


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer.decode('ascii'))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
a = deque([])
for i in S:
    a.append(i)

# is_reverse: 反転しているか
is_reverse = False
for i in range(Q):
    query = input().split()
    if query[0] == "1":
        is_reverse = not is_reverse
    else:
        if query[1] == "1":
            if is_reverse:
                a.append(query[2])
            else:
                a.appendleft(query[2])
        else:
            if is_reverse:
                a.appendleft(query[2])
            else:
                a.append(query[2])
if is_reverse:
    while len(a)>0:
        print(a.pop(), end="")
else:
    while len(a)>0:
        print(a.popleft(), end="")
print()

Submission status: Accepted

Solution code:

s = input()
n = int(input())

hanten=0
s1=""
s2=""
for i in range(n):
    Q = input()
    if int(Q[0])==1:
        hanten+=1 
    elif int(Q[0])==2 :
        if hanten%2 == int(Q[2])-1 :
            s1= s1+Q[4]
        else:
            s2= s2+Q[4]

s1 = s1[::-1]
s = s1+s+s2

if hanten%2 :
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
r = 0
S = ['','']
for _ in range(q):
  d = input().split()
  if d[0]=='1':
    r ^= 1
  else:
    S[(int(d[1])+r)%2] += d[2]
s = S[1][::-1] + s + S[0]
if r:
  s = s[::-1]
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
cou = 1
frt = ""
bck = ""
for i in range(q):
    t = list(input().split())
    #logging.debug(t)#
    if t[0] == "1":
        cou *= -1
    else:
        if (t[1] == "1" and cou == 1)or(t[1] == "2" and cou == -1):
            frt = t[2] + frt
        else:
            bck = bck + t[2]
#    logging.debug("s {},cou {}".format(s,cou))

s = frt + s + bck
#logging.debug(s)#
if cou == 1:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Accepted

Solution code:
s=input()
n=int(input())
a=[input() for _ in range(n)]

front=True
f=""
b=""

for q in a:
    if q=="1":front=not front;continue
    l=q.split()
    if l[1]=="1":
        if front:f=l[2]+f
        else:b=b+l[2]
    if l[1]=="2":
        if front:b=b+l[2]
        else:f=l[2]+f

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
flip = 1
right_edge = list(s)
left_edge = []
for i in range(q):
    query = input()
    if query[0] == '1':
        flip *= -1
    else:
        if query[2] == '1':
            if flip == 1:
                left_edge.append(query[-1])
            else:
                right_edge.append(query[-1])
        else:
            if flip == 1:
                right_edge.append(query[-1])
            else:
                left_edge.append(query[-1])
left_edge = left_edge[::-1]
if flip == 1:
    print(''.join(left_edge + right_edge))
else:
    print(''.join(right_edge[::-1] + left_edge[::-1]))

Submission status: Accepted

Solution code:
from collections import deque
def main():
    S = deque(map(str, input()))
    Q = int(input())
    flag = False
    for _ in range(Q):
        A = tuple(map(str, input().split()))
        if A[0] == "1":
            flag = not(flag)
        else:
            if (A[1] == "1" and not(flag)) or (A[1] == "2" and flag):
                S.appendleft(A[2])
            else:
                S.append(A[2])
    if flag:
        print("".join(S)[::-1])
    else:
        print("".join(S))
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

from collections import deque

q = deque(list(input()))
Q = int(input())

flg = 0
for _ in range(Q):
    i, *x = input().split()
    if i == "1":
        flg = 1 - flg
    else:
        f, c = x
        if flg == 0:
            if f == "1":
                q.appendleft(c)
            else:
                q.append(c)
        else:
            if f == "1":
                q.append(c)
            else:
                q.appendleft(c)

if flg == 0:
    print("".join(q))
else:
    print("".join(reversed(q)))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
q = int(input())
res = deque([S])
reverseFlag = False
for i in range(q):
  tmp = input()
  t = tmp[0]
  if t == "1":
    reverseFlag ^= True # ひっくり返す
  else:
    f,c = tmp[2:].split()
    if f == "1" and reverseFlag == False or f == "2" and reverseFlag == True:
      res.appendleft(c)
    else:
      res.append(c)
if reverseFlag:
  ans = "".join(res)[::-1]
else:
  ans = "".join(res)

print(ans)

Submission status: Accepted

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

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

queue = "".join(list(queue))
if rev_count % 2 == 0:
    print(queue)
else:
    print(queue[::-1])

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(S)
flg = True #Trueが前から,
for i in range(Q):
    t, *a = input().split()
    if t == "1":
        flg = not flg
    else:
        if (flg and a[0]=="1") or ((not flg) and a[0]=="2"):
            d.appendleft(a[1])
        else:
            d.append(a[1])
d = list(d)
if flg:
    print(''.join(d))
else:
    print(''.join(d[::-1]))