Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import collections

s = collections.deque(input().strip())
num = int(input())
query = [0] * num
for i in range(num):
    query[i] = input().strip()

reverse_flag = False
for q in query:
    if q[0] == '1':
        reverse_flag = not reverse_flag
    else:
        _, f, c = q.split()
        if (f == '1' and not reverse_flag) or (f == '2' and reverse_flag):
            s.appendleft(c)
        if (f == '2' and not reverse_flag) or (f == '1' and reverse_flag):
            s.append(c)
if reverse_flag:
    s.reverse()
print(''.join(list(s)))

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

S = read_str()
Q = read_int()[0]
Query = [list(read_str().split()) for _ in range(Q)]

d = deque([S])
# rd = deque([S])
def solve():
    reverse = False
    for q in Query:
        if q[0] == '1':
            reverse = not reverse
        else:
            if reverse:
                if q[1] == '1':
                    # rd.appendleft(q[2])
                    d.append(q[2])
                else:
                    d.appendleft(q[2])
                    # rd.append(q[2])
            else:
                if q[1] == '1':
                    d.appendleft(q[2])
                    # rd.append(q[2])
                else:
                    # rd.appendleft(q[2])
                    d.append(q[2])
        # print(d, rd, reverse)
    ans = ''
    if reverse:
        ans = ''.join(list(d))[::-1]
    else:
        ans = ''.join(list(d))
    return ans

if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
x=0
A=[]
B=[]
for i in range(0,Q):
    query=list(input().split())
    if query[0]=='1':
        x+=1
    else:
        if query[1]=='1':
            if x%2==1:
                B.append(query[2])
            else:
                A.append(query[2])
        else:
            if x%2==1:
                A.append(query[2])
            else:
                B.append(query[2])

if x%2==1:
    B=[B[len(B)-1-i] for i in range(0,len(B))]
    S=[S[len(S)-1-i] for i in range(0,len(S))]
    a=''.join(A)
    b=''.join(B)
    S=''.join(S)
    print(b+S+a)
else:
    A=[A[len(A)-1-i] for i in range(0,len(A))]
    a=''.join(A)
    b=''.join(B)
    print(a+S+b)

Submission status: Accepted

Solution code:
from collections import deque

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

    Q = deque(s)
    rev = False
    for i in range(q):
        tfc = input().split()

        if tfc[0] == '1':
            rev = not rev
        else:
            f = tfc[1]
            c = tfc[2]
            if (f == '1' and not rev) or (f != '1' and rev):
                Q.appendleft(c)
            else:
                Q.append(c)

    if rev:
        print(''.join(list(Q)[::-1]))
    else:
        print(''.join(Q))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import collections
s=input()
q=int(input())
qv=[list(input().split()) for _ in range(q)]
cnt=0
ans=collections.deque()
for i in range(len(s)):
  ans.append(s[i])
for i in range(q):
  if qv[i][0]=="1":
    cnt+=1
  else:
    t,f,c = qv[i]
    if f=="1":
      if cnt%2==0:
        ans.appendleft(c)
      else:
        ans.append(c)
    elif f=="2":
      if cnt%2==0:
        ans.append(c)
      else:
        ans.appendleft(c)
if cnt%2!=0:
  ans.reverse()
print("".join(ans))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s=input().replace('\n','')
q=int(input())
num1=0
sento=''
matsubi=''

for i in range(q):
    query=input()
    if query[0]=='1':
        num1+=1
    else:
        query=query.replace('\n','')
        _,f,c=map(str,query.split())
        if f=='1':
            if num1%2==0:
                sento=c+sento
            else:
                matsubi+=c
        else:
            if num1%2==0:
                matsubi+=c
            else:
                sento=c+sento
ans=sento+s+matsubi
if num1%2==0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
S = str(input())
Q = int(input())
s1 = ''
s2 = ''
reverse = False
for i in range(Q):
    query = list(map(str,input().split()))
    if query[0] == '1':
        if reverse == True:
            reverse = False
        else:
            reverse = True
    if query[0] == '2':
        if query[1] == '1':
            if reverse == False:
                s1 = query[2] + s1
            else:
                s2 = s2 + query[2]
        else:
            if reverse == True:
                s1 = query[2] + s1
            else:
                s2 = s2 + query[2]
S = s1 + S + s2
if reverse == True:
    S = S[::-1]
print(S)

Submission status: Accepted

Solution code:
def main():
    s=input()
    q=int(input())
    rot = 0
    A = ''
    B = ''
    for _ in range(q):
        t=input()
        if t=='1':
            rot += 1
        else:
            t = t.split()
            if (int(t[1])+rot)%2==1:
                A += t[2] 
            else:
                B  += t[2]
    if not rot%2:
        print(''.join(A[::-1]+s+B))
    else:
        print(''.join(B[::-1]+s[::-1]+A))        
main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
li=[list(map(str,input().split())) for _ in range(q)]

moji = "0"
frag = True

for i in range(q):
    que = li[i]
    if que[0] == "1":
        if frag:
            frag = False
        else:
            frag = True
    else:
        f = que[1]
        c = que[2]
        if frag:
            if f == "1":
                moji = c+moji
            else:
                moji = moji+c
        else:
            if f == "1":
                moji = moji+c
            else:
                moji = c+moji


if frag:
    print(moji.replace("0",s))
else:
    moji = moji[::-1]
    print(moji.replace("0",s[::-1]))

Submission status: Accepted

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

is_reverse = False
b = ''
a = ''

for i in range(Q):
    q = input().split()
    if len(q) == 1:
        is_reverse = not is_reverse

    else:
        if (q[1] == '2' and not is_reverse) or (q[1] == '1' and is_reverse):
            a += q[2]
        else:
            b += q[2]

if is_reverse:
    print(a[::-1] + S[::-1] + b)

else:
    print(b[::-1] + S + a)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
seq1 = ''
seq2 = ''
flg = 1
for i in range(q):
    ii = input().split()
    if ii[0] == '1':
        seq1, seq2 = seq2, seq1
        flg *= -1
    else:
        if ii[1] == '1':
            seq1 += ii[2]
        else:
            seq2 += ii[2]
print(seq1[::-1] + s[::flg] + seq2)

Submission status: Accepted

Solution code:
ant = []
post = []
s = list(input())
q = int(input())
c = 0

for _ in range(q):
    l = input().split()
    if l == ['1']:
        c += 1
    elif l[1] == '1':
        if c % 2 == 0:
            ant.append(l[2])
        else:
            post.append(l[2])
    else:
        if c % 2 == 0:
            post.append(l[2])
        else:
            ant.append(l[2])

ans = ''
if c % 2 == 0:
    ant.reverse()
    for i in ant:
        ans += i
    for k in s:
        ans += k
    for j in post:
        ans += j
else:
    post.reverse()
    s.reverse()
    for i in post:
        ans += i
    for k in s:
        ans += k
    for j in ant:
        ans += j
        
print(ans)

Submission status: Accepted

Solution code:
from collections import deque


s = list(input())
q = int(input())
ans = deque(s)
cnt = 0
for i in range(q):
    arr = list(map(str, input().split()))
    if arr[0] == '1':
        cnt += 1
        cnt %= 2
    else:
        if (arr[1] == '1' and cnt == 0) or (arr[1] == '2' and cnt == 1):
            ans.appendleft(arr[2])
        else:
            ans.append(arr[2])
if cnt == 0:
    print(*ans, sep='')
else:
    ans.reverse()
    print(*ans, sep='')

Submission status: Accepted

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

reverseNum = 0
initialTopAppend = ""
initialLastAppend = ""
for i in range(Q):
    l = input().split()
    if l[0] == "1":
        reverseNum += 1
        continue
    if reverseNum % 2 == 0:
        if l[1] == "1":
            initialTopAppend = l[2] + initialTopAppend
        else:
            initialLastAppend += l[2]
    else:
        if l[1] == "1":
            initialLastAppend += l[2]
        else:
            initialTopAppend = l[2] + initialTopAppend

ans = initialTopAppend + S + initialLastAppend

if reverseNum % 2 != 0:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
s = input()
n = int(input())
x = []
y = []
r = 1
for i in range(n):
    q = input().split()
    if q[0] == '1':
        r = -r
    elif q[1] == '1':
        z = q[2][0]
        if r == 1:
            x.append(z)
        else:
            y.append(z)
    else:
        z = q[2][0]
        if r == -1:
            x.append(z)
        else:
            y.append(z)
if r == 1:
    x.reverse()
    print("".join(x) + s + "".join(y))
else:
    y.reverse()
    print("".join(y) + s[::-1] + "".join(x))

Submission status: Accepted

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
query = [input().split() for _ in range(Q)]
reverse = False
for q in query:
    if q[0] == '1':
        reverse = not reverse
    else:
        if q[1] == '1':
            if not reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])
if reverse:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
mae = ''
ushiro = ''
rotate = False
for _ in range(Q):
  query = input()
  if query[0] == '1':
    rotate = not rotate
  else:
    if query[2] == '1':
      if rotate:
        ushiro = ushiro + query[4]
      else:
        mae = query[4] + mae
    else:
      if rotate:
        mae = query[4] + mae
      else:
        ushiro = ushiro + query[4]
print(mae + S + ushiro if not rotate else (mae + S + ushiro)[::-1])

Submission status: Accepted

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

mae = ""
usiro =""

r_num=0

for i in range(Q):
    query = (input())
    if query=="1":
        #kari = mae
        #mae=usiro[::-1]
        #usiro=kari[::-1]
        r_num+=1
    else:
        query = query.split(" ")
        if query[1] == "1":
            if r_num%2==0:
                mae = query[2]+mae
            else:
                usiro += query[2]
        else:
            if r_num %2 ==0:
                usiro += query[2]
            else:
                mae = query[2]+mae
if r_num%2 == 1:
    print(usiro[::-1]+S[::-1]+mae[::-1])
else:
    print(mae+S+usiro)

Submission status: Accepted

Solution code:
from collections import deque

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

reverse = False
count = 0
mae = deque()
ushiro = deque()
for i in range(q):
    qu = input()
    if qu[0] == "1":
        count += 1
        if reverse:
            reverse = False
        else:
            reverse = True
    else:
        _, f, c = qu.split()
        if reverse:
            if f == "1":
                ushiro.append(c)
            else:
                mae.appendleft(c)
        else:
            if f == "1":
                mae.appendleft(c)
            else:
                ushiro.append(c)

s = "".join(mae) + s + "".join(ushiro)

if count % 2:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
s = input()
qq = int(input())
qs = []
flag = True
for i in range(qq):
    l = input().split()
    qs.append(l)
    if l[0] == "1":
        #print("pre")
        flag = not(flag)


from collections import deque

preflag = flag
res = ""
pre = deque([])
suf = deque([])
for i in range(qq-1, -1, -1):
    q = qs[i]
    if q[0] == "1":
        flag = not(flag)

    elif q[0] == "2":
        if q[1] == "1":
            if flag:
                pre.append(q[2])
            else:
                suf.appendleft(q[2])

        elif q[1] == "2":
            if flag:
                suf.appendleft(q[2])
            else:
                pre.append(q[2])

#print(pre)
#print(s)
#print(suf)
#print(preflag)
if preflag:
    print("".join(pre) + s + "".join(suf))
else:
    print("".join(suf)[::-1] + s[::-1] + "".join(pre)[::-1])

Submission status: Accepted

Solution code:
s = input()
n = int(input())
query = [0]*n
rev = 0
for i in range(n):
    query[i] = list(map(str, input().split()))
    if query[i][0] == '1':
        rev = rev + 1

begin = ''
end = ''
pal = 0

for i in range(n):
    if int(query[i][0]) == 1:
        pal = (pal+1)%2
    else:
        if pal == 0:
            if int(query[i][1]) == 1:
                begin = query[i][2] + begin 
            else:
                end = end + query[i][2]
        else:
            if int(query[i][1]) == 1:
                end = end + query[i][2] 
            else:
                begin = query[i][2] + begin
#    print('--------'+str(i+1)+'th try-----')
#    print('begin:'+begin)
#    print('end:'+end)
#    print('pal:'+str(pal))
if rev%2 == 1:
    print(end[::-1]+s[::-1]+begin[::-1])
else:
    print(begin+s+end)

Submission status: Accepted

Solution code:
import sys

S = input()

Q = int(input())
flag=0
mae = ''
matsu = ''

for i in range(Q):
    r = sys.stdin.readline().rstrip('\n')
    if r == '1':
        flag = 1 - flag
    else:
        ni, f, c = r.split()
        if f=='1':
            if flag == 0:
                mae += c
            else:
                matsu += c
        else:
            if flag == 0:
                matsu += c
            else:
                mae += c


if flag ==0:
    print(mae[::-1]+S+matsu)
else:
    print(matsu[::-1]+S[::-1]+mae)

Submission status: Accepted

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

sq = deque(S)
cnt = 0

for i, lst in enumerate(que):
    if lst[0]=='1':
        cnt += 1
        continue
    if cnt%2==1:
        if lst[1]=='1':sq.append(lst[2])
        else:sq.appendleft(lst[2])
    else:
        if lst[1]=='1':sq.appendleft(lst[2])
        else:sq.append(lst[2])

ans = list(sq)
if cnt%2==1:
    ans.reverse()
print(''.join(ans))

Submission status: Accepted

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

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

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

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


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

main()

Submission status: Accepted

Solution code:
Slist = list(input())
Q = int(input())

hanten = 0
mae = [] # 後で反転
ushiro = []

for q in range(Q):
    query = input()
    if query == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    else:
        T,F,C = query.split()
        if F == "1":
            if hanten == 0:
                mae.append(C)
            else:
                ushiro.append(C)
        elif F =="2":
            if hanten == 0:
                ushiro.append(C)
            else:
                mae.append(C)
# print("hanten:",hanten,"mae:",mae,"ushiro:",ushiro)            
if hanten == 0:
    rmae = mae[::-1]
    goukei = rmae + Slist + ushiro
    print(''.join(goukei))
else:
    goukei =ushiro[::-1] + Slist[::-1] + mae
    print(''.join(goukei))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
t1counter=0
s1=''
for i in range(q):
    tfc=input().split()
    if(tfc[0]=='1'):
        t1counter+=1
    else:
        if(t1counter%2==0 and tfc[1]=='1'):
            s1=s1+tfc[2]
        elif(t1counter%2==1 and tfc[1]=='2'):
            s1=s1+tfc[2]
        elif(t1counter%2==0 and tfc[1]=='2'):
            s=s+tfc[2]
        elif(t1counter%2==1 and tfc[1]=='1'):
            s=s+tfc[2]
        else:
            print('error')
s=s1[::-1]+s
if(t1counter%2==0):
    print(s)
elif(t1counter%2==1):
    print(s[::-1])

Submission status: Accepted

Solution code:
from queue import deque
def main():
    que=deque()
    que.append(input())
    head = True
    for i in range(int(input())):
        q=input().split()
        if q[0]=="1":
            head = not head
        else:
            h = q[1]=="1"
            if (head and h) or (not head and not h):
                que.appendleft(q[2])
            else:
                que.append(q[2])      
    string = "".join(que)
    print(string if head else string[::-1])
main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

s = input().split()[0]
q = int(input())

left, right = [], []
state = 1
for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        state = (state)%2 + 1
    else:
        _, f, c = q_i
        f = int(f)
        if (f+state)%2 == 0:
            left += [c]
        else:
            right += [c]
if state == 1:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s[::-1] + ''.join(left))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
	S = input()
	Q = int(input())

	c = 1

	A = ''
	B = ''

	for i in range(Q):
		I = input()
		if len(I) == 1:
			c = c * (-1)
		else:
			T, F, C = I.split()
			f = int(F)
			if f == 1 and c == 1:
				A = C + A
			elif f == 1 and c == -1:
				B = B + C
			elif f == 2 and c == -1:
				A = C + A
			else:
				B = B + C

	S = A + S + B

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

	print(S)

Submission status: Accepted

Solution code:
import collections

s = collections.deque(input())
q = int(input())
rev = 0
for i in range(q):
  tmp = input().split()
  if len(tmp) == 1:
    rev += 1
  else:
    if tmp[1] == '1':
      if rev % 2 == 0:
        s.appendleft(tmp[2])
      else:
        s.append(tmp[2])
    else:
      if rev % 2 == 0:
        s.append(tmp[2])
      else:
        s.appendleft(tmp[2])
if rev % 2:
  while True:
    try:
      print(s.pop(), end='')
    except IndexError:
      break
else:
  while True:
    try:
      print(s.popleft(), end='')
    except IndexError:
      break

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
Query=[input().split() for i in range(Q)]

m=1
A=[]
B=[]
for i in range(Q):
  if int(Query[i][0])==1:
    m*=-1
  else:
    
    if m*((-2*int(Query[i][1]))+3)==1:
      A.append(Query[i][2])
    else:
      B.append(Query[i][2])
if m==1:
  print(''.join(A)[::-1]+S+''.join(B))
else:
  print(''.join(B)[::-1]+S[::-1]+''.join(A))

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
reverse=0
head=[]
tail=[]
for i in range(Q):
    q=input().split()
    if int(q[0])==1:
        reverse=(reverse+1)%2
    else :      
        if (reverse+(int(q[1])-1))%2:
            tail.append(q[2])
        else:
            head.append(q[2])

            
head.reverse()
if reverse:
    tmp=head
    head=tail
    tail=tmp
    S.reverse()
    head.reverse()
    tail.reverse()
    
    
print("".join(head+S+tail))

Submission status: Accepted

Solution code:
def m():
    s = input()
    q = int(input())
    inv = 0
    pre = []
    sub = []

    for i in range(q):
        raw = input().split()
        if len(raw) == 1:
            inv = inv ^ 1
        else:
            f = int(raw[1])-1
            c = raw[2]
            if inv==f:
                pre.append(c)
            else:
                sub.append(c)
    
    if inv==0:
        print(''.join(pre[::-1])+s+''.join(sub))
    else:
        print(''.join(sub[::-1])+s[::-1]+''.join(pre))

m()

Submission status: Accepted

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

num = 0
mae = ""
usi = ""
for i in range(Q):
    tmp = input().split()

    if tmp[0] == "1":
        num += 1

    elif tmp[0] == "2":
        num = num % 2
        dada = num + int(tmp[1])

        if dada == 2:
            usi += tmp[2]
            # S = S + tmp[2]
            # print("usi: "+usi)
        else:
            mae = tmp[2] + mae
            # S = tmp[2] + S
            # print("mae: "+mae)


if num % 2 == 1:
    S = S[::-1]
    S = usi[::-1] + S + mae[::-1]
else:
    S = mae + S + usi
print(S)

Submission status: Accepted

Solution code:
def solve():
    S = input()
    q = int(input())
    flg = 0
    li = ["", ""]
    for _ in range(q):
        Q = input().split()
        if Q[0] == "1":
            flg = 1 - flg
        else:
            li[(int(Q[1]) - 1 + flg) % 2] += Q[2]
    if flg:
        S = S[::-1]
    print(li[flg][::-1] + S + li[1 - flg])
solve()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
que = [list(map(str, list(input().split()))) for i in range(q)]

hanten = 0
left = ""
right = ""
for i in range(q):
    if que[i][0] == "1":
        if hanten == 0:
            hanten = 1
        else:
            hanten = 0
    elif que[i][0] == "2":
        if hanten == 0:
            if que[i][1] == "2":
                sub = [right, que[i][2]]
                right = ''.join(sub)
            elif que[i][1] == "1":
                sub = [que[i][2], left]
                left = ''.join(sub)
        else:
            if que[i][1] == "2":
                sub = [que[i][2],left]
                left = ''.join(sub)
            elif que[i][1] == "1":
                sub = [right,que[i][2]]
                right = ''.join(sub)

sub = [left, s, right]
s = ''.join(sub)
if hanten == 1:
    s = s[::-1]
print(s)

Submission status: Accepted

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

S = input()
S_list = list(S)

tmp = [''] * (2 * 10**5)
tmp.extend(S_list)
tmp.extend([''] * (2 * 10**5))

Q = int(input())

rev = False
front_i = 2 * 10**5
end_i = front_i + len(S_list)
for i in range(Q):
    query = input()
    if len(query) == 1:
        # Type 1
        rev = not rev
    else:
        # Type 2
        [N, F, C] = [i for i in query.split()]
        F = int(F)
        front_flag = True
        if F == 2:
            front_flag = False
        if rev:
            front_flag = not front_flag
        
        if front_flag:
            front_i -= 1
            tmp[front_i] = C
        else:
            tmp[end_i] = C
            end_i += 1

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

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
f = list()
b = list()
r = False
for i in range(Q):
    q = input()
    if q[0] == '1':
        r = not r
    else:
        T, F, C = q.split()
        if (F == '1') ^ r:
            f.append(C)
        else:
            b.append(C)
if r:
    S = list(reversed(S + b)) + f
else:
    S = list(reversed(f)) + S + b

print(''.join(S))

Submission status: Accepted

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

isreverse = False
head=[]
tail=[]

for i in range(Q):
    query = input()
    if str(query)[0]=="1":
        isreverse = isreverse^True
    else:
        F = int(query.split()[1])
        C = str(query.split()[2])
        if (F==1 and isreverse==False) or (F==2 and isreverse==True):
            head.append(C)
        else:
            tail.append(C)


if isreverse==False:
    print("".join(reversed(head))+S+"".join(tail))
else:
    print("".join(reversed(tail))+S[::-1]+"".join(head))

Submission status: Accepted

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

# for query in queries:
#     if query == "1":
#         S = S[::-1]
#     else:
#         _, F, C = list(query.split())
#         if F == "1":
#             S = C + S
#         else:
#             S = S + C
# print(S)

left = ""
right = ""
target = 0

for query in queries:
    if query == "1":
        target = 1 - target
    else:
        _, F, C = list(query.split())
        if F == "1":
            if target == 0:
                left += C
            else:
                right += C
        else:
            if target == 0:
                right += C
            else:
                left += C
if target == 0:
    print("{}{}{}".format(left[::-1], S, right))
else:
    print("{}{}{}".format(right[::-1], S[::-1], left))

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
right_lst = ["0"]*(2*10**5)
left_lst = ["0"]*(2*10**5)
S = left_lst + s + right_lst
left = 2*10**5
right = 2*10**5+len(s)-1
isReverse=False
#print(left,right)
for i in range(n):
  A = input().split(" ")
  if A[0]=="1":
    isReverse = not isReverse
    #print(S[left:right+1])
    #print(left,right)
    continue
  else:
    if (A[1]=="1" and isReverse) or (A[1]=="2" and not isReverse):
      right +=1
      S[right] = A[2]
      
    else:
      left-=1
      S[left] = A[2]
      
    #print(S[left:right+1])
    #print(left,right)

ans = S[left:right+1] 
#print(ans)    
if isReverse:
  ans = list(reversed(ans))

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

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
hanten = 0
for i in range(q):
  nyuuryoku = input().split()
  n = nyuuryoku[0]
  if len(nyuuryoku) == 1:
    hanten += 1
    continue
  else:
    f = int(nyuuryoku[1])
    c = nyuuryoku[2]
    if hanten%2 == 0:
      if f ==1:
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f == 1:
        s.append(c)
      else:
        s.appendleft(c)
if hanten%2 == 1:
  s.reverse()
ans = "".join(s)
print(ans)

Submission status: Accepted

Solution code:
S = list([i for i in input().split()][0])
Q = [int(i) for i in input().split()][0]

r = 0
firstS = []
lastS = []
for q in range(Q):
  row = input().split()
  if len(row) == 1:
    r = (r+1) % 2
  else:
    f = int(row[1])
    c = row[2]
    if r == 0:
      if f == 1:
        firstS.append(c)
      else:
        lastS.append(c)
    else:
      if f == 2:
        firstS.append(c)
      else:
        lastS.append(c)
        
if r % 2 == 0:
  S = firstS[::-1] + S + lastS
else:
  S = lastS[::-1] + S[::-1] + firstS 
print("".join(S))

Submission status: Accepted

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

R=0
S1=""
S2=""
for i in range(Q):
    temp=list(input().split())

    if int(temp[0])==1:
        S=S1+S+S2
        S1=""
        S2=""
        R+=1
    elif int(temp[0])==2:
        F=int(temp[1])
        c=temp[2]
        if F==1:
            if R%2==0:
                S1=c+S1
            else:
                S2=S2+c
        elif F==2:
            if R%2==0:
                S2=S2+c
            else:
                S1=c+S1
S=S1+S+S2

if R%2==1:
    ans=S[-1::-1]
    print(ans)
else:
    print(S)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
flg=1
mae,ushiro='',''
for i in range(q):
  inp=input()
  if inp=='1':
    flg*=-1
  else:
    f,c=inp[2],inp[4]
    if f=='1'and flg==1:
      mae+=c
    elif f=='2' and flg==-1:
      mae+=c
    else:
      ushiro+=c

s=mae[::-1]+s+ushiro
if flg==-1:
  s=s[::-1]
print(s)

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
rev = 1
head = []
tail = []
for _ in range(Q):
    lst = input().split() 
    if (lst[0] == '1'):
        rev *= -1
    else:
        if (lst[1] == '1'):
            pos = rev
        else:
            pos = -1 * rev
        if (pos == 1):
            head.append(lst[2])
        else:
            tail.append(lst[2])
ans = ''.join(head[::-1]) + S + ''.join(tail)
print(ans[::rev])

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Atama = []
Osiri = []
rev = [0]*Q
for i in range(Q):
    a = list(input().split())
    if len(a) == 1:##T_i=1
        rev[i] = 1
    else:#T_i == 2
        if a[1] == "1":
            Atama.append([i,a[2],1])
        else:
            Osiri.append([i,a[2],-1])

##revの累積和
Rev = []
tmp = 0
r = sum(rev)
for i in range(len(rev)):
    r -= rev[i]
    Rev.append(r)
##print(Rev)
for i in range(len(Atama)):
    Atama[i][2] *= (-1)**Rev[Atama[i][0]]
for i in range(len(Osiri)):
    Osiri[i][2] *= (-1)**Rev[Osiri[i][0]]

Ans = Atama+Osiri
Ans.sort()
##print(Ans)
fw = ""
aw = ""
N = len(Ans)
for i in range(N):
    if Ans[i][2] == 1:
        fw += Ans[i][1]
    else:
        aw += Ans[i][1]

if sum(rev)%2 == 1:
    S = S[::-1]
fw = fw[::-1]

print(fw+S+aw)

Submission status: Accepted

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

answer = s
s1 = ""
s2 = ""
state = 0


for _ in range(q):
    Q = input()
    if Q == "1":
        if state == 0:
            state = 1
        else:
            state = 0
        continue

    _,F,C = Q.split()
    if F == "1":
        if state == 0:
            s2 = s2 + C
        else:
            s1 = s1 + C
    else:
        if state == 0:
            s1 = s1 + C
        else:
            s2 = s2 + C





if state == 0:
    answer = s2[::-1] + s + s1
else:
    answer = s1[::-1] + s[::-1] + s2



print(answer)