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:
#!/usr/bin/env python3
from collections import deque


def main():
    d = deque()
    d.extend(list(input()))

    Q = int(input())
    reflag = False

    for i in range(Q):
        query = list(input().split())
        if query[0] == '1':
            reflag = not reflag
        elif (query[1] == '1' and not reflag) or (query[1] == '2' and reflag):
            d.appendleft(query[2])
        else:
            d.append(query[2])

    if reflag:
        print("".join(reversed(d)))
    else:
        print("".join(d))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

s = deque(input().strip())
q=int(input())
count = 0
for i in range(q):
    L = list(input().rstrip().split())    
    if L[0]=='1':
        count += 1
    else:
        a,b,c = L
        b = int(b)
        if count&1==1:
            b=3-b
        if b==2:
            s.append(c)
        else:
            s.appendleft(c)
if count&1==1:
    print("".join(list(s)[::-1])) 
else:
    print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

S = input()
d = deque(S)
Q = int(input())
is_order = True
for i in range(Q):
    tmp = input()
    if len(tmp) == 1:
        t = 1
        is_order = not is_order
    else:
        t,f,c = tmp.split()
        f = int(f)
        if f == 1:
            if is_order:
                d.appendleft(c)
            else:
                d.append(c[::-1]) 
        else:
            if is_order:
                d.append(c)
            else:
                d.appendleft(c[::-1])
if is_order:
    print("".join(d))
else:
    print("".join(reversed(d)))

Submission status: Accepted

Solution code:
from collections import deque

s=deque(list(input()))
flag=0
for i in range(int(input())):
  dir=input().split()
  if int(dir[0])==1:flag+=1
  else:
    if int(dir[1])==1:
      if flag%2==0:s.appendleft(dir[2])
      else:s.append(dir[2])
    else:
      if flag%2==0:s.append(dir[2])
      else:s.appendleft(dir[2])
s=list(s)
if flag%2==1:s.reverse()
print(*s,sep='')

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


s = readline().decode().rstrip()
q = int(readline())

cnt1 = 0
t2 = []
for _ in range(q):
    t = list(readline().decode().rstrip().split())
    t2.append(t)
    if t[0] == '1':
        cnt1 += 1

if cnt1 % 2 != 0:
    s = s[::-1]
s1 = []
s2 = []

for t in t2:
    if t[0] == '1':
        cnt1 -= 1
    else:
        if cnt1 % 2 == 0 and t[1] == '1':
            s1.append(t[2])
        elif cnt1 % 2 != 0 and t[1] == '2':
            s1.append(t[2])
        elif cnt1 % 2 == 0 and t[1] == '2':
            s2.append(t[2])
        else:
            s2.append(t[2])

print(''.join(s1[::-1])+s+''.join(s2))

Submission status: Accepted

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

st = True
deq = deque(S)
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        st = not st
    else:
        if (Query[1] == '1') ^ st:
            deq.append(Query[2])
        else:
            deq.appendleft(Query[2])
if st:
    print(''.join(deq))
else:
    print(''.join(reversed(deq)))

Submission status: Accepted

Solution code:
from collections import deque

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

deq = deque(s)
pol = 0
for _ in range(q):
    query = input()
    if query[0] == '1':
        pol ^= 1
        continue
    _, f, c = query.split()
    op = int(f) - 1
    if op ^ pol:
        deq.append(c)
    else:
        deq.appendleft(c)

if pol:
    print(''.join(reversed(deq)))
else:
    print(''.join(deq))

Submission status: Accepted

Solution code:
from collections import deque

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

queue = deque(list(S))

r = False
for _ in range(Q):
    query = input()
    if query == '1':
        r ^= True
    else:
        _, f, c = query.split()
        front = f == '1'

        if r ^ front:
            queue.appendleft(c)
        else:
            queue.append(c)

if r:
    ans = ''.join(list(queue)[::-1])
else:
    ans = ''.join(queue)

print(ans)

Submission status: Accepted

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

r = False
prefix = []
suffix = []
for _ in range(Q):
    query = input()
    if query == '1':
        r ^= True
    else:
        _, f, c = query.split()
        front = f == '1'

        if r ^ front:
            prefix.append(c)
        else:
            suffix.append(c)

if r:
    S = ''.join(suffix[::-1]) + S[::-1] + ''.join(prefix)
else:
    S = ''.join(prefix[::-1]) + S + ''.join(suffix)

print(S)

Submission status: Accepted

Solution code:
from collections import deque
# 両端を扱うならdequeが速い
# 内側を扱うならリストが速い

S = input()

q = deque()
for s in list(S):
    q.append(s)

Q = int(input())

direction = True

for _ in range(Q):
    line = input()

    # TrueならFalseに、FalseならTrueに変える
    if line == "1":
        direction = not direction

    else:
        if (direction and line[2] == '1') or (not direction and line[2] == '2'):
            # 左端に追加する場合
            q.appendleft(line[4])
        else:
            # 右端に追加する場合
            q.append(line[4])

if direction:
    print("".join(q))
elif not direction:
    q.reverse()
    print("".join(q))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
now = 1
head = ''
last = ''
for _ in range(Q):
    query = input().split()
    if query[0] == '1':
        now = now * -1
    if query[0] == '2':
        if now * (int(query[1])-1.5) == 0.5:
            last += query[2]
        else:
            head += query[2]

ans = head[::-1] + S + last
if now == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
s = input()
q = int(input())
from collections import deque
f_s = deque([])
b_s = deque([])
fwd = 1
for i in range(q):
    qs = list(input().split())
    if qs[0] == '1':
        fwd ^= 1
    elif qs[1] == '1':
        if fwd:
            f_s.appendleft(qs[2])
        else:
            b_s.append(qs[2])
    else:
        if fwd:
            b_s.append(qs[2])
        else:
            f_s.appendleft(qs[2])
print("".join(f_s)+s+"".join(b_s) if fwd else ("".join(f_s)+s+"".join(b_s))[::-1])

Submission status: Accepted

Solution code:
from collections import deque
S = input()
q = deque()
for s in list(S):
    q.append(s)
Q = int(input())
direction = True
for _ in range(Q):
    line = input()
    if line[0] == '1':
        direction = not direction
    else:
        if (direction and line[2] == '1') or (not direction and line[2] == '2'):
            q.appendleft(line[4])
        else:
            q.append(line[4])
print("".join(q) if direction else "".join(reversed(q)))

Submission status: Accepted

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

s = input().strip()
qq = int(input())

rev = False
pre = ''
suf = ''
for _ in range(qq):
    q = input().strip()
    if q[0] == '1':
        rev = not rev
    else:
        c = q[4]
        if not rev:
            if q[2] == '1':
                pre += c
            else:
                suf += c
        else:
            if q[2] == '1':
                suf += c
            else:
                pre += c
ans = pre[::-1] + s + suf
if rev:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

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

c = input()
c = c[:len(c)-1]
q = int(input())

num = len(c)
c_mae = ''
c_ato = ''

hantei = 1

for i in range(q):
    q_i= list(input())
    #print(q_i)
    if q_i[0] == '1':
        hantei *= -1
    elif (q_i[2] =='1' and hantei == 1) or (q_i[2] == '2' and hantei == -1):
        c_mae = q_i[4] + c_mae
    else:
        c_ato += q_i[4]

print(c_mae+c+c_ato if hantei == 1 else (c_mae+c+c_ato)[::-1])

Submission status: Accepted

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

SS =deque(list(s))
rev_count = 0

for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            SS.append(que[4])
        else:
            SS.appendleft(que[4])

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

Submission status: Accepted

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

rev_count = 0
sbf=''
saf=''
for i in range(Q):
    que = input()
    if que[0] == '1':
        rev_count +=1
    elif que[0] == '2':
        if (rev_count + int(que[2]))%2 == 0:
            saf = saf + que[4]
        else:
            sbf = que[4] + sbf

print(sbf+s+saf if rev_count %2 == 0 else (sbf+s+saf)[-1::-1])

Submission status: Accepted

Solution code:
#158_D
s = input()
q = int(input())

from collections import deque

s = deque(s)

rev = 1
for _ in range(q):
    Q = list(input().split())
    if Q[0] == "1":
        rev *= -1
    elif Q[0] == "2":
        if (Q[1]=="1" and rev==1) or (Q[1]=="2" and rev==-1):
            s.appendleft(Q[2])
        elif (Q[1]=="2" and rev==1) or (Q[1]=="1" and rev==-1):
            s.append(Q[2])
            
s = "".join(s)
            
if rev == -1:
    s = s[::-1]

print(s)

Submission status: Accepted

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

is_forward = True

q = int(input())
for i in range(q):
    query = input()
    if query[0] == "1":
        is_forward = is_forward ^ 1
    elif query[0] == "2":
        _, f, c = query.split()
        f = int(f)
        f %= 2
        is_end = f ^ is_forward
        if is_end:
            moji.append(c)
        else:
            moji.appendleft(c)

if is_forward == False:
    moji = list(moji)[::-1]
print("".join(moji))

Submission status: Accepted

Solution code:
def main():
    s = input()
    q = int(input())
    is_fwd = True
    before = ''
    after = ''

    for i in range(q):
        fields = [i for i in input().split()]
        t = int(fields[0])
        if t == 1:
            is_fwd = not is_fwd
        elif t == 2:
            f = int(fields[1])
            c = fields[2]
            if (f == 1) == is_fwd:
                before += c
            else:
                after += c

    res = before[::-1] + s + after
    if is_fwd:
        print(res)
    else:
        print(res[::-1])

main()

Submission status: Accepted

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

from collections import*
D=deque(S)
count=0
for _ in range(N):
    *Q,=input().split()
    if len(Q)==1:
        count+=1
    else:
        if (count+int(Q[1]))%2:
            D.appendleft(Q[2])
        else:
            D.append(Q[2])
ans=''.join(D)
print(ans[::-1] if count%2 else ans)

Submission status: Accepted

Solution code:
s=input()
l1=[]
l2=[]
num1=0 #0は反転なし　1はあり
def ap1(str1):
    l1.append(str1)
def ap2(str2):
    l2.append(str2)
nq=int(input())
for i in range(nq):
    q=list(input().split())
    if q[0]=="1":
        num1=(num1+1)%2
    else:
        f=int(q[1])
        if (num1+f)%2==1: #1で先頭 2で末尾
            ap1(q[2])
        else:
            ap2(q[2])
if num1==0:
    ans=""
    for i in range(len(l1)):
        ans+=l1[-1-i]
    ans+=s
    for i in range(len(l2)):
        ans+=l2[i]
else:
    ans=""
    for i in range(len(l2)):
        ans+=l2[-1-i]
    for i in range(len(s)):
        ans+=s[-1-i]
    for i in range(len(l1)):
        ans+=l1[i]
print(ans)

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
rev = 0
front, back=[],[]
for i in range(Q):
  q = input()
  if len(q)==1:rev = 1- rev
  else:
    n,f,c=q.split()
    if f=='1' and rev==0:
      front.append(c)
    elif f=='2' and rev==1:
      front.append(c)
    elif f=='1' and rev==1:
      back.append(c)
    elif f=='2' and rev == 0:
      back.append(c)

if rev==0:
  S = ''.join(front[::-1]) + S + ''.join(back)
else:
  S = ''.join(back[::-1]) + S[::-1] + ''.join(front)
print(S)

Submission status: Accepted

Solution code:
s=input()
n = int(input())
s1=""
s2=""
t=0
for i in range(n):
    m=input().split()
    if len(m)==1:
        t=1-t
    else:
        if t==0:
            if int(m[1])==2:
                s2+=m[2]
            else:
                s1+=m[2]
        else:
            if int(m[1])==2:
                s1+=m[2]
            else:
                s2+=m[2]
    #print(s1,s2)
if t==0:
    s3=s1[::-1]
    print(s3+s+s2)
else:
    s=s[::-1]
    s2=s2[::-1]
    print(s2+s+s1)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
f=""
b=""
r=0
for i in range(q):
  query=input().split()
  if int(query[0])==1:
    r=1-r
  else:
    if (r^(int(query[1])-1))==0:
      f+=query[2]
    else:
      b+=query[2]
ret=f[::-1]+s+b
if r:
  ret=ret[::-1]
print(ret)

Submission status: Accepted

Solution code:
import sys
from collections import deque

input = sys.stdin.readline

S = deque(input()[:-1])
reverse_flg = 0
Q = int(input())
qs = [input().split() for _ in range(Q)]
for q in qs:
    if q[0] == '1':
        reverse_flg = reverse_flg ^ 1
    else:
        flg = (int(q[1]) + reverse_flg) % 2
        if flg == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])

if reverse_flg:
    S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    Q = int(readline())
    
    dq = deque(S)
    flipped = False
    for _ in range(Q):
        A = readline().split()
        if A[0] == '1':
            flipped = not flipped
        elif A[1] == '1' and not flipped or A[1] == '2' and flipped:
            dq.appendleft(A[2])
        else:
            dq.append(A[2])
    
    ans = ''.join(reversed(dq) if flipped else dq)
    
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
T_REVERT = "1"
T_APPEND = "2"
F_BEFORE = "1"
F_AFTER = "2"

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

before = []
after = []
is_revert = False


for i in range(Q):
  q = input().split(" ")
  if q[0] == T_REVERT:
    is_revert = not is_revert
    continue
  if (q[1] == F_BEFORE) ^ is_revert:
    before.append(q[2])
  else:
    after.append(q[2])


ans_s = ""
if not is_revert:
  print("".join(before[::-1]) + S + "".join(after))
else:
  print("".join(after[::-1]) + S[::-1] + "".join(before))

Submission status: Accepted

Solution code:
import sys

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())

flag = False
s = rr()
q = ri()
front = ''
end = ''
for _ in range(q):
    query = list(rs())
    if query[0] == '1':
        flag = not flag
        continue
    else:
        f = int(query[1])
        c = query[-1]
        if flag == True:
            if f == 1:
                end += c
            else:
                front = c+front
        else:
            if f == 1:
                front = c+front
            else:
                end += c
if flag:
    print((front+s+end)[::-1])
else:
    print(front+s+end)

Submission status: Accepted

Solution code:
import sys
from collections import deque

sys.setrecursionlimit(10**7)
input = sys.stdin.readline

s = input().rstrip()
q = int(input())
d = deque()
d.append(s)

flag = False
for i in range(q):
    qq = input().split()
    if qq[0] == '1':
        flag = not(flag)
    else:
        if flag:
            if qq[1] == '1':
                d.append(qq[2])
            else:
                d.appendleft(qq[2])
        else:
            if qq[1] == '1':
                d.appendleft(qq[2])
            else:
                d.append(qq[2])

ans = ''.join(list(d))
if flag:
    print(ans[::-1])
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque(S)
R = 0

for i in range(Q):
    Query = str(input())
    if len(Query) == 1:
        R = (R+1)%2
    else:
        T,F,C = map(str,Query.split())

        if (R + int(F)) % 2 == 1:
            d.appendleft(C)
        else:
            d.append(C)

if R == 1:
    d.reverse()

for i in range(len(d)):
    print(d[i],end="")

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
Query = [input() for _ in range(Q)]
inv = False
add = ['','']
for query in Query:
    if query[0]=='1':
        inv = not inv
    else:
        add[(query[2] == '1') ^ inv] += query[-1]
ans = add[1][::-1] + S + add[0]
if inv:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

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

from collections import deque


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

cnt = 0
for _ in range(q):
    query = input().strip()
    if query == "1":
        cnt += 1
    else:
        t, f, c = query.split()
        if cnt % 2 == 0:
            if f == "1":
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if f == "1":
                S.append(c)
            else:
                S.appendleft(c)
ans = "".join(S)

print(ans if cnt % 2 == 0 else ans[::-1])

Submission status: Accepted

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

cnt = 0
for _ in range(q):
    i = input().split()
    t = int(i[0])

    if t == 1:
        cnt += 1
    else:
        f = int(i[1])
        c = i[2]
        if (cnt % 2 == 0 and f == 1) or (cnt % 2 == 1 and f == 2):
            s.appendleft(i[2])
        else:
            s.append(i[2])

if cnt % 2 == 1:
    s.reverse()
print(''.join(s))

Submission status: Accepted

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

def main():
    
    s = str(input().rstrip())
    q =int(input())
    ten = 0
    r = []
    l = []
    la = l.append
    ra =r.append
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    la(ls[2])
                else:
                    ra(ls[2])
            else:
                if ten%2 ==1:
                    la(ls[2])
                else:
                    ra(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    s = str(input().rstrip())
    q =int(input())
    ten = 0
    r = []
    l = []
    la = l.append
    ra =r.append
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    la(ls[2])
                else:
                    ra(ls[2])
            else:
                if ten%2 ==1:
                    la(ls[2])
                else:
                    ra(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    s = input()
    q =int(input())
    ten = 0
    r = []
    l = []
    for _ in range(q):
        ls = tuple(map(str, sys.stdin.readline().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
            else:
                if ten%2 ==1:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input()
    q =int(input())
    ten = 0
    r = []
    l = []
    for _ in range(q):
        ls = tuple(map(str, input().split()))
        if len(ls)==1:
            ten +=1
        else:
            if ls[1] =='1':
                if ten%2 ==0:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
            else:
                if ten%2 ==1:
                    l.append(ls[2])
                else:
                    r.append(ls[2])
    s = ''.join(reversed(l))+s+''.join(r)
    if ten%2 ==1:
        s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
q =int(input())
ten = 0
r = []
l = []
for _ in range(q):
    ls = tuple(map(str, input().split()))
    if len(ls)==1:
       ten +=1
    else:
        if ls[1] =='1':
            if ten%2 ==0:
                l.append(ls[2])
            else:
                r.append(ls[2])
        else:
            if ten%2 ==1:
                l.append(ls[2])
            else:
                r.append(ls[2])
s = ''.join(reversed(l))+s+''.join(r)
if ten%2 ==1:
    s = s[::-1]
print(s)

Submission status: Accepted

Solution code:

def main():
    s = input()
    q = int(input())
    bottom = ''
    top = ''
    swapped = 0
    for _ in range(q):
        query = input()
        if len(query) == 1:
            swapped = 1 - swapped
            continue
        _, f, c = query.split()
        if swapped:
            if f == '1':
                bottom += c
            else:
                top += c
        else:
            if f == '1':
                top += c
            else:
                bottom += c

    if swapped:
        print(bottom[::-1] + s[::-1] + top)
    else:
        print(top[::-1]+s+bottom)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
q=int(input())
cnt=0
front=[]
rear=[]
for qq in range(q):
  l=list(input().split())
  if l[0]=='1':
    cnt+=1
  else:
    if (cnt+int(l[1]))%2==1:
      front.append(l[2])
    else:
      rear.append(l[2])
front=''.join(front[::-1])
rear=''.join(rear)
s=front+s+rear
print(s if cnt%2==0 else s[::-1])

Submission status: Accepted

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

# 持っているビスケットを叩き、1枚増やす
# ビスケット A枚を 1円に交換する
# 1円をビスケット B枚に交換する
def main():
    s = input().strip()
    q = int(input())
    flg = True
    deq = collections.deque(s)
    for i in range(q):
        t = input_list_str()
        if len(t) == 1:
            n = t[0]
            flg = False if flg is True else True
        else:
            n, f, c = t
            c = str(c).strip()
            if f == '1':
                if flg:
                    deq.appendleft(c)
                else:
                    deq.append(c)
            else:
                if flg:
                    deq.append(c)
                else:
                    deq.appendleft(c)
    if flg is False:
        deq.reverse()
    print(''.join(deq))


def bi(num, a, b, x):
    print((a * num) + (b * len(str(b))))
    if (a * num) + (b * len(str(b))) <= x:
        return False
    return True

def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
q = int(input())
count = 0
a = []
b = []

for _ in range(q):
    Q = list(map(str, input().split()))
    if Q[0] == "1":
        count += 1
    else:
        if (int(Q[1])+count)%2 == 1:
            a.append(Q[2])
        else:
            b.append(Q[2])
    
        
a = "".join(a[::-1])
b = "".join(b)
s = a + s + b
if count%2 == 1 and count != 0:
    s = s[::-1]
print("".join(s))

Submission status: Accepted

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
q = INT()
p = [0]*q
flag = 0
c0 = c1 = ''

for i in range(q):
        p[i] = input()
        if p[i][0] == '1':
                flag = 1 - flag
        else:
                t, f, c = p[i].split()
                if (f == '1' and flag == 0) or (f == '2' and flag == 1):
                        c0 = c + c0
                else:
                        c1 = c1 + c

s = c0 + s + c1
print(s if flag == 0 else s[::-1])

Submission status: Accepted

Solution code:
s = ["", input()]
q = int(input())

for _ in range(q):
  que = input().split()
  if que[0] == "1":
    s = s[::-1]
  else:
    s[int(que[1])-1] += que[2]

print("".join(list(s[0])[::-1]) + s[1])

Submission status: Accepted

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

prefix = ''
surfix = ''
cnt_reverse = 0

for i in range(Q):
    q = input()
    if q == '1':
        cnt_reverse += 1
    else:
        _, f, c = q.split()
        if (f=='1' and cnt_reverse%2==0):
            prefix = c + prefix
        elif (f=='2' and cnt_reverse%2==1):
            prefix = c + prefix
        elif (f=='1' and cnt_reverse%2==0):
            surfix = surfix + c
        else:
            surfix = surfix + c
if cnt_reverse % 2 == 0:
    S = prefix + S + surfix
else:
    S = surfix[::-1] + S[::-1] + prefix[::-1]
print(S)

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
# -1がnotReverse, 1がReverser
isR = -1
for i in range(q):
    line = list(input().split())
    t = int(line[0])
    if t == 1:
        isR *= -1
    else:
        f = int(line[1])
        if isR + f == 0 or isR + f == 3:
            s.appendleft(line[2])
        else:
            s.append(line[2])
if isR == 1:
        s.reverse()
print("".join(s))

Submission status: Accepted

Solution code:
from collections import deque

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

S = deque(S)
rev = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        rev *= -1
    else:
        T, F, C = query.split()
        if rev == 1:
            if F == "1":
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if F == "1":
                S.append(C)
            else:
                S.appendleft(C)

if rev == -1:
    S = reversed(S)

print("".join(S))

Submission status: Accepted

Solution code:
S = input()
Q = int(input())
reverse = 0
left = ""
right = ""
for _ in range(Q):
    q = input().split()
    if q[0]=="1":
        reverse += 1
        left, right = right, left
    if q[0]=="2" and q[1]=="1":
        if reverse%2==0: left = q[2]+left
        else: left = left+q[2]
    if q[0]=="2" and q[1]=="2":
        if reverse%2==0: right = right+q[2]
        else: right = q[2]+right
if reverse%2==1:
    left = left[::-1]
    right = right[::-1]
    S = S[::-1]
print(left+S+right)

Submission status: Accepted

Solution code:
from collections import deque


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        is_reversed = not is_reversed
    else:
        F, C = int(Query[1]), Query[2]
        if is_reversed:
            F = 3 - F
        if F == 1:
                S.appendleft(C)
        else:
                S.append(C)

if is_reversed:
    S.reverse()
print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque


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

for _ in range(Q):
    Query = input().split()
    if Query[0] == "1":
        reverse_count += 1
    else:
        F, C = Query[1], Query[2]
        if F == "1":
            if reverse_count % 2 == 0:
                S.appendleft(C)
            else:
                S.append(C)
        else:
            if reverse_count % 2 == 0:
                S.append(C)
            else:
                S.appendleft(C)

if reverse_count % 2 == 1:
    S.reverse()
print("".join(S))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
q = int(input())
d = deque()
for i in s:
  d.append(i)
rv = 0
lst = [1, 0]
for i in range(q):
  z = input()
  if z[0] == '1':
    rv = lst[rv]
  elif z[2] == '1':
    if rv == 1:
      d.append(z[4])
    else:
      d.appendleft(z[4])
  else:
    if rv == 0:
      d.append(z[4])
    else:
      d.appendleft(z[4])
if rv == 1:
  print(''.join(d)[::-1])
else:
  print(''.join(d))

Submission status: Accepted

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
reverse_index = -1

for i in range(Q):
    query = input()
    if query[0] == '1':
        reverse_index *= -1
    elif query[0] == '2':
        if query[2] == '1':
            if reverse_index == -1:
                S.appendleft(query[4])
            else:
                S.append(query[4])
        else:
            if reverse_index == -1:
                S.append(query[4])
            else:
                S.appendleft(query[4])

if reverse_index == 1:
    S.reverse()
    print(*S, sep='')
else:
    print(*S, sep='')