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:
def main():
    from collections import deque
    import sys
    input = sys.stdin.readline

    S = input().rstrip()
    Q = int(input())

    dq = deque(S)

    reverse = 0

    for _ in range(Q):
        Query = iter(input().rstrip().split())
        T = next(Query)
        if T == '1':
            reverse = 1 - reverse
            # # Sの前後を反転する
            # if len(dq) >= 2:
            #     head = dq.popleft()
            #     tail = dq.pop()
            #     dq.appendleft(tail)
            #     dq.append(head)

        else:
            F, C = Query
            if F == '1':
                # Sの先頭にC
                if reverse:
                    dq.append(C)
                else:
                    dq.appendleft(C)

            else:
                # Sの末尾にC
                if reverse:
                    dq.appendleft(C)
                else:
                    dq.append(C)

    if reverse:
        dq = reversed(list(dq))

    res = ''.join(dq)
    print(res)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
from collections import deque
d = deque(input())

reverse = -1
n = int(input())
for i in range(n):
    query = input().split()
    if len(query) == 1:
        reverse *= (-1)
    else:
        f = int(query[1])
        c = query[2]
        if reverse == -1:
            if f == 1:
                d.appendleft(c)
            else:
                d.append(c)
        else:
            if f == 1:
                d.append(c)
            else:
                d.appendleft(c)

if reverse == 1:
    d.reverse()

print("".join(d))

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
direction = 1
mae = []
usiro = []
for i in range(q):
    qi = input()
    if qi == "1":
        direction *= -1
    else:
        tfq = list(qi.split())
        t = tfq[0]
        f = tfq[1]
        q = tfq[2]

        if f == "1":
            if direction == 1:
                mae.append(q)
            else:
                s.append(q)
        else:
            if direction == 1:
                s.append(q)
            else:
                mae.append(q)
# print(mae)
if direction == -1:
    for i in s[::-1]:
        print(i, end = "")
    for i in mae:
        print(i, end = "")
else:
    for i in mae[::-1]:
        print(i, end = "")
    for i in s:
        print(i, end = "")

Submission status: Accepted

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

reverse=False
lst=["" for _ in range(500000+10)]
sp=200000+5
fp=200000+5

for i in range(len(S)):
    lst[sp+i]=S[i]
    fp+=1

for q in query:
    if q[0]=="1":
        reverse = not reverse
    else:
        if q[1]=="1":
            if reverse:
                #S=S+q[2]
                lst[fp]=q[2]
                fp+=1
            else:
                #S=q[2]+S
                sp-=1
                lst[sp]=q[2]
        else:
            if reverse:
                #S=q[2]+S
                sp-=1
                lst[sp]=q[2]
            else:
                #S=S+q[2]
                lst[fp]=q[2]
                fp+=1

if reverse:
    #print(S[::-1])
    print("".join(lst[::-1]))
else:
    #print(S)
    print("".join(lst))

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(s, Q, queries):
    l = ['']
    r = ['']
    rev = 0
    for q in queries:
        if q[0] == 1:
            rev += 1
            continue
        f = q[1]
        c = q[2]
        if rev % 2 == 0 and f == 2 or rev % 2 == 1 and f == 1:
            r.append(c)
        else:
            l.append(c)
    if rev % 2 == 0:
        l.reverse()
        ret = ''.join(l) + s + ''.join(r)
    else:
        r.reverse()
        ret = ''.join(r) + s[::-1] + ''.join(l)
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = str(next(tokens))
    Q = int(next(tokens))
    q = []
    for i in range(Q):
        t = int(next(tokens))
        if t == 1:
            q.append([1, None, None])
        else:
            f = int(next(tokens))
            c = str(next(tokens))
            q.append([2, f, c])
    solve(s, Q, q)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
S = input()
S_deque = deque(S)
# print(S_deque)

Q = int(input())

reverse = 0

for i in range(Q):
    q = input().split()
    # print(q)

    if q[0] == "2":
        if not reverse:
            if q[1] == "1":
                S_deque.appendleft(q[2])
            elif q[1] == "2":
                S_deque.append(q[2])
        if reverse:
            if q[1] == "1":
                S_deque.append(q[2])
            elif q[1] == "2":
                S_deque.appendleft(q[2])
    else:
        if not reverse:
            reverse = 1
        else:
            reverse = 0

if reverse:
    print("".join(reversed(S_deque)))
else:
    print("".join(S_deque))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
mod=10**9+7
inf=float("inf")
from math import sqrt
from collections import deque
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#######ここから天ぷら########
s=deque(list(input()))
q=int(input())
rev=0
for _ in range(q):
    a=input()
    if a=="1": 
        rev=1-rev
        continue
    n,m,p=map(str,a.split())
    if m=="1":
        if rev==1:
            s.append(p)
        else:
            s.appendleft(p)
    else:
        if rev==1:
            s.appendleft(p)
        else:
            s.append(p)
if rev==1:
    print("".join(list(s)[::-1]))
else:
    print(("".join(list(s))))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


if __name__ == "__main__":

    S = input()
    Q = int(input())
    rev = False
    head = []
    tail = []
    for _ in range(Q):
        task = input()
        if task == "1":
            rev = not rev
        else:
            _, f, c = task.split()
            if f == "1":
                if rev:
                    tail.append(c)
                else:
                    head.append(c)
            else:
                if rev:
                    head.append(c)
                else:
                    tail.append(c)

        # print(task, head, tail, rev)

    ans = ''.join(reversed(head)) + S + ''.join(tail)
    if rev:
        ans = ''.join(reversed(ans))

    print(ans)

Submission status: Accepted

Solution code:

from collections import deque


def resolve():
    S=deque(list(input()))
    Q=int(input())
    querys=[input() for i in range(Q)]
    rev=False
    for qq in querys:
        if qq=="1":
            rev = not rev
            continue
        _,f,c=qq.split()
        if rev==False and f=="1":
            S.appendleft(c)
        elif rev == True and f=="1":
            S.append(c)
        elif rev==False and f=="2":
            S.append(c)
        else:
            S.appendleft(c)
    ans=list(S)
    if rev ==False:
        print("".join(ans))
    else:
        print("".join(reversed(ans)))



    


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
s = input()
d.append(s)
rev = 0

Q = int(input())

for q in range(Q):
    ls = input().split()
    #print (ls)
    if ls[0] == '1':
        rev = (rev + 1) % 2
    else:
        if (int(ls[1]) + rev) % 2 == 0:
            d.append(ls[2])
        else:
            d.appendleft(ls[2])

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

Submission status: Accepted

Solution code:
s = input()
q = int(input())
add_head = ""
add_tail = ""
rev = False
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        rev = not rev
    else:
        if query[1] == "1":
            if rev:
                add_tail = add_tail + query[2]
            else:
                add_head = add_head + query[2]
        else:
            if rev:
                add_head = add_head + query[2]
            else:
                add_tail = add_tail + query[2]
                
if not rev:
    print(add_head[::-1] + s + add_tail)
else:
    print(add_tail[::-1] + s[::-1] + add_head)

Submission status: Accepted

Solution code:
from collections import deque



def main():
    S = input()
    Q = int(input())

    order = 1

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

    for i in range(Q):
        tfc = list(map(str, input().split()))

        if tfc[0] == "1":
            order *= -1
        else:
            if order == 1:
                if tfc[1] == "1":
                    #ans = tfc[2] + ans
                    ans.appendleft(tfc[2])
                else:
                    #ans = ans + tfc[2]
                    ans.append(tfc[2])

            else:
                if tfc[1] == "2":
                    #ans = tfc[2] + ans
                    ans.appendleft(tfc[2])
                else:
                    #ans = ans + tfc[2]
                    ans.append(tfc[2])


    ans = "".join(ans)

    if order == 1:
        print(ans)
    else:
        print(ans[::-1])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

s = s_in()
q = n_in()

d = True

pre = []
suf = []

for _ in range(q):
    query = s_in()
    if len(query) == 1:
        d = not d
    else:
        _, f, c = query.split()
        if d:
            if f == '1':
                pre.append(c)
            else:
                suf.append(c)
        else:
            if f == '1':
                suf.append(c)
            else:
                pre.append(c)

s = "".join(pre[::-1]) + s + "".join(suf)

if d:
    print(s)
else:
    print(s[::-1])

Submission status: Accepted

Solution code:
s=list(input())
q=int(input())
cnt=1
sen=[]
matu=[]
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        cnt*=-1
    else:
        if a[1]=='1':
            if cnt==1:
                sen.append(a[2])
            else:
                matu.append(a[2])
        else:
            if cnt==1:
                matu.append(a[2])
            else:
                sen.append(a[2])
#print(cnt)
if cnt==-1:
    s=s[::-1]
ss=''.join(s)
if cnt==1:
    sen=sen[::-1]
sensen=''.join(sen)
if cnt==-1:
    matu=matu[::-1]
matumatu=''.join(matu)
if cnt==1:
    print(sensen+ss+matumatu)
else:
    print(matumatu+ss+sensen)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
reverse = False
front = []
back = []
for i in range(q):
    inp = list(input().split())
    if len(inp) == 1:
        if reverse:
            reverse = False
        else:
            reverse = True
        continue
    t, f, c = inp[0], int(inp[1]), inp[2]
    if not reverse:
        if f == 1:
            front.append(c)
        else:
            back.append(c)
    else:
        if f == 2:
            front.append(c)
        else:
            back.append(c)
s = "".join(front[::-1]) + s + "".join(back)
if reverse:
    print(s[::-1])
else:
    print(s)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
left = ""
right = ""


def s_add(r, f):
    if r % 2 == 0:
        if f == 1:
            return True
        else:
            return False
    else:
        if f == 1:
            return False
        else:
            return True


cnt = 0
for _ in range(q):
    query = input()
    if query == "1":
        cnt += 1
    else:
        z, f, c = query.split()
        if s_add(cnt, int(f)):
            left += c
        else:
            right += c

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

Submission status: Accepted

Solution code:
# ABC158d


def main():
    import sys
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    def dump(*args):
        sys.stderr.write(str(args))

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]

    mode = 0
    from collections import deque
    que = deque(list(s))
    for i in query:
        if len(i) == 1:
            mode = 1-mode
            continue
        f = int(i[1])
        if mode == 0:
            if f == 1:
                que.appendleft(i[2])
            else:
                que.append(i[2])
        else:
            if f == 1:
                que.append(i[2])
            else:
                que.appendleft(i[2])
    mode = 1 if mode == 0 else - 1
    # print(mode)
    qq = list(que)
    # print(qq[::-1])
    print(*(qq[::mode]), sep='')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
s=list(input())
s=deque(s)
q=int(input())
a=0
for i in range(q):
  A=list(input().split())
  if A[0]=="1":
    a=(a+1)%2
  else:
    if A[1]=="1":
      if a==0:
        s.appendleft(A[2])
      else:
        s.append(A[2])
    else:
      if a==0:
        s.append(A[2])
      else:
        s.appendleft(A[2])
if a==1:
  S=[]
  c=len(s)
  for i in range(c):
    S.append(s[-1])
    s.pop()
else:
  S=s
for i in range(len(S)):
  print(S[i],end="")
print()

Submission status: Accepted

Solution code:
from collections import deque

def main():
    s=deque(input())
    q=int(input())
    
    right=True
    for _ in range(q):
        l=list(map(str,input().split()))
        if l[0]=='1':
            if right:
                right=False
            else:
                right=True
        else:
            if l[1]=='1':
                if right:
                    s.appendleft(l[2])
                else:
                    s.append(l[2])
            else:
                if right:
                    s.append(l[2])
                else:
                    s.appendleft(l[2])
    
    if right:
        print(''.join(s))
    else:
        print(''.join(list(s)[::-1])) 

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
Q = ir()
dir = 0 #最初の向きを0、逆向きを1
head_tail = ['', '']
for _ in range(Q):
    x = sr().split()
    if x[0] == '1':
        dir = (1 - dir)
    else:
        t, f, c = x
        h_d = dir
        if f == '2':
            h_d = (1 - h_d)
        head_tail[h_d] += c

if dir == 0:
    answer = head_tail[0][::-1] + S + head_tail[1]
else:
    answer = head_tail[1][::-1] + S[::-1] + head_tail[0]

print(answer)

Submission status: Accepted

Solution code:
from collections import deque

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

inv = False
for _ in range(q):
    com, *query = input().split()
    if com == "1":
        if not inv:
            inv = True
        else:
            inv = False
    else:
        f, c = query
        if not inv:
            if f == "1":
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if f == "1":
                s.append(c)
            else:
                s.appendleft(c)


s = "".join(s)
print(s if not inv else s[::-1])

Submission status: Accepted

Solution code:
from collections import deque

s = input()

q = int(input())

d = deque(list(s))

inversed = -1

for i in range(q):
    query = list(input().split())
    if int(query[0]) == 1:
        inversed *= -1
    else:
        if int(query[1]) == 1:
            if inversed < 0:
                d.appendleft(query[2])
            else:
                d.append(query[2])
        else:
            if inversed < 0:
                d.append(query[2])
            else:
                d.appendleft(query[2])

if inversed < 0:
    print(''.join(list(d)))
else:
    print(''.join(list(d)[::-1]))

Submission status: Accepted

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

S = input()[:-1]
Q = int(input())
que = []
acc = [0]

for i in range(Q):
    l = list(input().split())
    ty = int(l[0])
    
    if ty==1:
        acc.append(acc[-1]+1)
    else:
        acc.append(acc[-1]+0)
        
    if ty==1:
        continue
        
    f = int(l[1])
    que.append((i, f, l[2]))
    
ans = deque([])

if (acc[-1]-acc[0])%2==0:
    for i in range(len(S)):
        ans.append(S[i])
else:
    for i in range(len(S)-1, -1, -1):
        ans.append(S[i])

for i, f, c in que:
    cnt = acc[-1]-acc[i]
    
    if cnt%2==0:
        if f==1:
            ans.appendleft(c)
        else:
            ans.append(c)
    else:
        if f==1:
            ans.append(c)
        else:
            ans.appendleft(c)

ans = list(ans)
print(''.join(ans))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
cnt=0 #反転の回数の偶奇を記憶
right=""
left=""
for i in range(q):
	query=list(input().split())
	if query[0]=="1":
		cnt+=1
	else:
		if (cnt+int(query[1]))%2==0:
			right+=query[2]
		else:
			left+=query[2]
if cnt%2==0:
	print(left[::-1]+s+right)
else:
	print(right[::-1]+s[::-1]+left)

Submission status: Accepted

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

muki = 1
a = ''
b = ''
for i in Query:
    if i[0] == '1':
        muki *= -1
    else:
        if i[1] == '1':
            if muki == 1:
                a += i[2]
                # S = i[2] + S
            else:
                # S = S + i[2]
                b += i[2]
        else:
            if muki == -1:
                # S = i[2] + S
                a += i[2]
            else:
                # S = S + i[2]
                b += i[2]
S = a[::-1] + S + b
if muki == -1:
    print(S[::-1])
else:
    print(S)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
# input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return map(int,input().split())
def LI(): return list(map(int,input().split()))

from collections import deque
def main():
    S = input()
    queue = deque(list(S))
    Q = INT()
    reverse_flag = False

    for _ in range(Q):
        query = list(input().split())
        if len(query) == 3:
            F = int(query[1])
            C = query[2]
            if F == 1:
                if reverse_flag:
                    queue.append(C)
                else:
                    queue.appendleft(C)
            elif F == 2:
                if reverse_flag:
                    queue .appendleft(C)
                else:
                    queue.append(C)
        else:
            if reverse_flag:
                reverse_flag = False
            else:
                reverse_flag = True
    answer = ''.join(queue)
    if reverse_flag:
        answer = answer[::-1]
    print(answer)
    return
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

is_reversed = False
prefix = ""
suffix = ""

for query in Query:
    if query == "1":
        is_reversed = not is_reversed
    else:
        _, f, c = query.split()
        if is_reversed ^ (f == "1"):
            prefix += c
        else:
            suffix += c

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

Submission status: Accepted

Solution code:
from collections import deque

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


rev=0
for i in range(q):
  l=list(input().split())
  if len(l)==1:
#    s.reverse()
    rev+=1
  else:
    t,f,c=l[0],l[1],l[2]

    if rev%2==0:
      if f=='1':
        s.appendleft(c)
      else:
        s.append(c)
    else:
      if f=='1':
        s.append(c)
      else:
        s.appendleft(c)

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

Submission status: Accepted

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

S = deque(readline().rstrip().decode())
Q = int(readline())
query = readlines()

rev = False
for q in query:
    if q[0] == ord('1'):
        rev = not rev
    else:
        _, f, c = q.decode().split()
        f = int(f)
        if rev ^ (f == 1):
            S.appendleft(c)
        else:
            S.append(c)

T = ''.join(S)
if rev:
    T = T[::-1]
print(T)

Submission status: Accepted

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

is_reverse = False
sento = []
matubi = []
for i in range(Q):
    s = input().split()
    if s[0] == '1':
        is_reverse = not is_reverse
    else:
        if (s[1] == '1' and not is_reverse) or (s[1] == '2' and is_reverse):
            sento.append(s[2])
        else:
            matubi.append(s[2])

ans = ''.join(sento[::-1]) + S + ''.join(matubi)
if is_reverse:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
appendix = [[], []]
reverse = False
for _ in range(q):
  query = input().split()
  if query[0] == '1':
    reverse ^= True
  else:
    f = bool(int(query[1]) - 1)
    appendix[int(f ^ reverse)].append(query[2])
if reverse:
  print(''.join(appendix[1][::-1]) + s[::-1] + ''.join(appendix[0]))
else:
  print(''.join(appendix[0][::-1]) + s + ''.join(appendix[1]))

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
mode=0
sento=''
matubi=''
for i in range(Q):
  query=list(input().split())
  if query[0]=='1':
    mode=(mode+1)%2
  if query[0]=='2':
    if query[1]=='1':
      if mode==0:
        sento=query[2]+sento
      else:
        matubi=matubi+query[2]
    else:
      if mode==0:
        matubi=matubi+query[2]
      else:
        sento=query[2]+sento
if mode==1:
  S=matubi[::-1]+S[::-1]+sento[::-1]
else:
  S=sento+S+matubi
print(S)

Submission status: Accepted

Solution code:
'''
　 ∧_,,∧    究極奥義「WAがACになーれ！！」
　（`・ω・)つ━☆・*。
　⊂　　 ノ 　　　・゜+.
　 し’´Ｊ　　*・ 
'''
s=input()
a=[]
b=[]
now=1
q=int(input())
for i in range(q):
    x=list(input().split())
    if x[0]=="1":
        now*=-1
    else:
        if now==1 and x[1]=="2" or now==-1 and x[1]=="1":
            b.append(x[2])
        else:
            a.append(x[2])
if now==-1:
    ans=b[::-1]+list(s[::-1])+a
    print(*ans,sep="")
else:
    ans=a[::-1]+list(s)+b
    print(*ans,sep="")

Submission status: Accepted

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

    ans = deque(list(S))
    turn = False
    for q in Query:
        if q[0] == '1':
            turn = not turn
        else:
            _, f, c = q
            if f == '1':
                if turn:
                    ans.append(c)
                else:
                    ans.appendleft(c)
            else:
                if turn:
                    ans.appendleft(c)
                else:
                    ans.append(c)
    return ''.join(ans) if not turn else ''.join(reversed(ans))

print(d_string_formation())

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
mae = []
usi = []
R = 0
for _ in range(Q):
    q = list(map(str,input().split()))
    if q[0] == '1':
        R += 1
        R %= 2
    elif q[0] == '2':
        if R == 0:
            if q[1] == '1':
                mae.append(q[2])
            elif q[1] == '2':
                usi.append(q[2])
        elif R == 1:
            if q[1] == '1':
                usi.append(q[2])
            elif q[1] == '2':
                mae.append(q[2])
mae.reverse()
ans = mae + S + usi
if R == 1:
    ans.reverse()
print(''.join(ans))

Submission status: Accepted

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

# ex
#2 1 p
#1
#2 2 c
#1
rev=False
head=[] # 後ろが先頭
tail=[] 

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

    if query[0] == "1":
        # 反転
        rev = not rev
    else:
        # T=2
        F = query[1]
        char = query[2]
        if F == "1":
            # 先頭につける
            if rev:
                # 先頭はtail
                tail.append(char)
            else:
                head.append(char)
        else:
            # 末尾につける
            if rev:
                # 先頭はtail
                head.append(char)
            else:
                tail.append(char)

head_str=''.join(head)
tail_str=''.join(tail)

if rev:
    # Sを反転
    print(tail_str[::-1]+S[::-1]+head_str)
else:
    print(head_str[::-1]+S+tail_str)

Submission status: Accepted

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

tep = 1
top = ""
bottom = ""
for t in range(Q):
    query = [i for i in input().split()]
    if query[0]=="1":
       tep =tep * (-1) 
    if query[0]=="2":
        F = int(query[1])
        C = query[2]
        if F==1:
            if tep ==1:
                top = C + top
            else:
                bottom = bottom + C
        if F==2:
            if tep ==-1:
                top = C + top
            else:
                bottom = bottom + C


ans = top + S + bottom
if tep==1:
    print(ans)

else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

from collections import deque
ss = ins()
s = deque([ss[i] for i in range(len(ss))])
dir = 0
q = inn()
for qq in range(q):
    a = input().split()
    if a[0]=='1':
        dir = 1-dir
    else:
        f = a[1]
        c = a[2]
        if f=='1' and dir==0 or f=='2' and dir==1:
            s.appendleft(c)
        else:
            s.append(c)
    #ddprint(f"s {s} dir {dir}")
if dir==1:
    s.reverse()
print(''.join(s))

Submission status: Accepted

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

def main():
    s = list(input())
    s = deque(s)
    q = int(input())
    count,b = 0,1
    for _ in range(q):
        x = list(input().split())
        if x[0] == "1":
            b *= -1
            count += 1
        else:
            if x[1] == "1":
                if b == 1:
                    s.appendleft(x[2])
                else:
                    s.append(x[2])
            else:
                if b == 1:
                    s.append(x[2])
                else:
                    s.appendleft(x[2])
    
    if count%2 != 0:
        s.reverse()
        
    print("".join(s))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

t = deque(S)
reverse = False
for _ in range(Q):
    query = input()
    if query[0] == '1':
        reverse = not reverse
    elif query[0] == '2':
        _, F, C = query.split()
        if F == '1':
            if reverse:
                t.append(C)
            else:
                t.appendleft(C)
        elif F == '2':
            if reverse:
                t.appendleft(C)
            else:
                t.append(C)
result = ''.join(t)
if reverse:
    result = result[::-1]
print(result)

Submission status: Accepted

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

d=deque(S)
rev=False
for q in range(Q):
  q=input()
  if q=="1":
    rev=not rev
  else:
    qlist=q.split()
    if qlist[1]=="1":
      if not rev:
        d.appendleft(qlist[2])
      else:
        d.append(qlist[2])
    elif qlist[1]=="2":
      if not rev:
        d.append(qlist[2])
      else:
        d.appendleft(qlist[2])

d2=deque()
if not rev:
  d2=deque(d)
else:
  d2=deque(reversed(d))
  
print("".join(d2))

Submission status: Accepted

Solution code:
S=input()
n=int(input())
t=0
L=""
R=""
for i in range(n):
    Q=list(input().split())
    if Q[0]=="1":
        if t==0:
            t=1
        else:
            t=0
    else:
        if int(Q[1])+t==2:
            R=R+Q[2]
        else:
            L=Q[2]+L
            
ans=L+S+R
if t==0:
    print(ans)
else:
    print(ans[::-1])