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:
s = input()
q = int(input())
ans = ''
ans1 = ''
ans2 = ''
zengo = 1 #表は1、裏は-１
front = []
back = []
for i in range(q):
    a = list(input().split())
    if a[0] == '1':
        zengo *= -1
    else:
        if a[1] == '1':
            if zengo == 1:
                front.append(a[2])
            else:
                back.append(a[2])
        else:
            if zengo == 1:
                back.append(a[2])
            else:
                front.append(a[2])
front = list(reversed(front))
for i in range(len(front)):
    ans1 += front[i]
for i in range(len(back)):
    ans2 += back[i]
    
ans = ans1 + s + ans2
if zengo == -1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import os
from collections import deque

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


# 実装を行う関数
def resolve(test_def_name=""):
    que = deque()
    que.extend(list(input()))
    q = int(input())
    query_s = [list(input().split()) for i in range(q)]

    isReverse = False
    for query in query_s:
        if query[0] == "1":
            isReverse = True if not isReverse else False
        else:
            # 先頭に追加
            if (isReverse is False and query[1] == "1") or (isReverse and query[1] == "2"):
                que.appendleft(query[2])
            # 末尾に追加
            else:
                que.append(query[2])

    ans = ""
    if isReverse:
        ans = "".join(list(reversed(que)))
    else:
        ans = "".join(que)

    print(ans.replace("\r", ""))


# パターン
# !isReverse 1(先頭)　= 先頭に追加
#  isReverse 1(先頭)　= 末尾に追加
# !isReverse 2(末尾)　= 先頭に追加
#  isReverse 2(末尾)　= 先頭に追加

# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """a
4
2 1 p
1
2 2 c
1"""
        output = """cpa"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """a
6
2 2 a
2 1 b
1
2 2 c
1
1"""
        output = """aabc"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """y
1
2 1 x"""
        output = """xy"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
QL = []

for _ in range(Q):
    QL.append(input())

inversed = False
invc = 0

prefix = []
suffix = []

for query in reversed(QL):
    # print("qi:", query)
    if query == '1':
        #1
        inversed = not inversed
        invc += 1
    
    else:
        # print("2:inv:", inversed)
        #2
        __, f, c = query.split()

        if f == '1':
            if inversed:
                suffix.append(c)
            else:
                prefix.append(c)
        else:
            if not inversed:
                suffix.append(c)
            else:
                prefix.append(c)

# print("prefix:", prefix)
# print("suffix:", suffix)

if inversed:
    S = list(reversed(S))

print("".join(prefix) + "".join(S) + "".join(list(reversed(suffix))))

Submission status: Accepted

Solution code:
from collections import deque
s = deque(input())
q = int(input())
query = [list(input().replace(' ','')) for _ in range(q)]

r = False
for i in range(q):
    if r:
        if len(query[i]) == 1:
            r = False
        elif query[i][1] == '1':
            s.append(query[i][2])
        else:
            s.appendleft(query[i][2])
    else:
        if len(query[i]) == 1:
            r = True
        elif query[i][1] == '1':
            s.appendleft(query[i][2])
        else:
            s.append(query[i][2])

if r:
    s = reversed(s)
    t = ''.join(s)
    print(t)
else:
    t = ''.join(s)
    print(t)

Submission status: Accepted

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

rev = False
l, r = "", ""

for _ in range(Q):
    query = input()
    if query[0] == "1":
        rev = not rev
    else:
        T, F, C = query.split()
        if F == "1":
            if rev:
                r += C
            else:
                l = C + l
        else:
            if rev:
                l = C + l
            else:
                r += C

res = l + S + r
if rev:
    res = res[::-1]

print(res)

Submission status: Accepted

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

q=[0]*Q
turn=0
houkou=['']*2
for i in range(Q):
    q[i]=input()

    if(q[i][0]=='1'):
        turn=not turn

    else:
        if(turn==0):
            if(q[i][2]=='1'):
                S.appendleft(q[i][4])

            else:
                S.append(q[i][4])
        else:
            if(q[i][2]=='1'):
                S.append(q[i][4])

            else:
                S.appendleft(q[i][4])



if(turn==0):
    print(''.join(S))
else:
    S.reverse()
    print(''.join(S))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 12:53:41 2020

@author: reira
"""

S = input()
Q = int(input())
flag = 0
l = ""
r = ""
for i in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag = (flag+1)%2
    else:
        if (int(q[1]) + flag)%2:
            l += q[2]
        else:
            r += q[2]
if flag:
    print(r[::-1]+S[::-1]+l)
else:
    print(l[::-1]+S+r)

Submission status: Accepted

Solution code:
#

import sys
input=sys.stdin.readline

def main():
    S=input().strip("\n")
    Q=int(input())
    sign=True
    head=[]
    tail=[]
    for i in range(Q):
        q=tuple(input().split())
        if q[0]=="1":
            sign^=True
        else:
            if (q[1]=="1" and sign==True) or (q[1]=="2" and sign==False):
                head.append(q[2])
            else:
                tail.append(q[2])
    if sign==True:
        print("".join(head[::-1])+S+"".join(tail))
    else:
        print("".join(tail[::-1])+S[::-1]+"".join(head))
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
s = deque(list(input()))
q = int(input())
cur = 0
for i in range(q):
    ls = list(map(str, input().split()))
    if len(ls) == 1:
        cur += 1
    else:
        if ls[1] == '1':
            if cur%2 == 0:
                s.appendleft(ls[2])
            else:
                s.append(ls[2])
        else:
            if cur%2 == 0:
                s.append(ls[2])
            else:
                s.appendleft(ls[2])
if cur%2 == 0:
    print(*s, sep='')
else:
    print(*list(s)[::-1], sep='')

Submission status: Accepted

Solution code:
S=input()
Q=int(input())
reverse=False
front=""
rear=""
for _ in range(Q):
    inp=input()
    if inp[0]=="1":
        reverse= not reverse
    elif inp[0]=="2":
        if (inp[2]=="1" and not reverse) or (inp[2]=="2" and reverse):
            front=inp[4]+front
        else:
            rear=rear+inp[4]
ans=front+S+rear
if reverse:
    ans=ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
s = input()
q = int(input())
 
head = []
tail = []
flip = 0
 
for i in range(q):
    query = input().split()
    if query[0] == "1":
        flip ^=1
    else:
        f = int(query[1]) - 1
        c = query[2]
        if (f ^ flip) == 0:
            head.append(c)
        else:
            tail.append(c)
 
if flip:
    head, tail = tail, head
    s = s[::-1]
 
head = "".join(head)
tail = "".join(tail)
 
print(head[::-1] + s + tail)

Submission status: Accepted

Solution code:
from collections import deque
from itertools import islice

S = input()

result = deque(S)


def input_str(line):
    r = line.split()
    if len(r) == 1:
        return int(r[0]), None, None
    else:
        return int(r[0]), int(r[1]), r[2]


reverse_flag = False

for _ in range(int(input())):
    Q, F, C = input_str(input())
    if Q == 1:
        reverse_flag ^= True
    if Q == 2:
        if reverse_flag:
            if F == 1:
                result.append(C)
            else:
                result.appendleft(C)
        else:
            if F == 1:
                result.appendleft(C)
            else:
                result.append(C)

if reverse_flag:
    result.reverse()
print(''.join(result))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

def reversedeq(deq):
    newdeq = deque()
    for _ in range(len(deq)):
        c = deq.pop()
        newdeq.append(c)
    
    return newdeq


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

for _ in range(Q):
    Query = [x for x in input().split()]
    if len(Query) == 1:
        reverse_state = reverse_state ^ 1
    else:
        if reverse_state:
            if Query[1] == '1':
                S.append(Query[2])
            else:
                S.appendleft(Query[2])
        else:
            if Query[1] == '1':
                S.appendleft(Query[2])
            else:
                S.append(Query[2])

if reverse_state:
    S = reversedeq(S)

print("".join(S))

Submission status: Accepted

Solution code:
S=input()
q=int(input())
s=[]
r=[]
for i in S:
    s.append(i)
t=1
for i in range(q):
    qv=list(input().split())
    if qv[0]=='1':
        t^=1
    else:
        if int(qv[1])^t==0 or int(qv[1])^t==2:
            r.append(qv[2])
        else:
            s.append(qv[2])
            
if t==0:
    ss=s.copy()
    s=[]
    for j in range(len(ss)):
        s.append(ss[-1-j])
    ans=s
    ans.extend(r)
else:
    rr=r.copy()
    r=[]
    for j in range(len(rr)):
        r.append(rr[-1-j])
    ans=r
    ans.extend(s)
answer=''.join(ans)
print(answer)

Submission status: Accepted

Solution code:
from collections import deque
s = input()
Q = int(input())
que = deque(s)
cnt = 0
for i in range(Q):
    q = input().split()
    if q[0] == '1':
        cnt += 1
    else:
        reverse = cnt % 2
        if q[1] == '1':
            if reverse == 0:
                s = que.appendleft(q[2])
            else:
                s = que.append(q[2])
        else:
            if reverse == 0:
                s = que.append(q[2])
            else:
                s = que.appendleft(q[2])

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

Submission status: Accepted

Solution code:
from collections import deque
S = input()
Q = int(input())
t = []
f = []
c = []
for i in range(Q):
    in_ = list(input().split())
    if in_[0] == "1":
        t.append(1)
        f.append(0)
        c.append(0)
    else:
        t.append(2)
        f.append(int(in_[1]))
        c.append(in_[2])
acc = [0 for _ in range(Q)]
if t[-1] == 1:  acc[-1] = 1
for i in reversed(range(Q - 1)):
    if t[i] == 1:
        acc[i] = acc[i + 1] + 1
    else:
        acc[i] = acc[i + 1]
        
if acc[0] % 2 == 0:
    ans = deque(S)
else:
    ans = deque(S[::-1])
    
for i in range(Q):
    if t[i] == 2:
        if (f[i] + acc[i]) % 2 == 1:
            ans.appendleft(c[i])
        else:
            ans.append(c[i])
print("".join(ans))

Submission status: Accepted

Solution code:
s = input()

x = ""
y = ""

Q = int(input())

rev = 0
for i in range(Q):
    q = list(input().split())
    
    if q[0] == "2":
        q[1] = int(q[1])
        q[1] -= 1 
        
        if rev:
            q[1] = 1 - q[1]
        
        if q[1] == 0:
            x += q[2]
        else:
            y += q[2]
    else:
        rev = 1 - rev
        
x = x[::-1]
ans = x + s + y

if rev:
    ans = ans[::-1]

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
s=input()
q=int(input())
n=0
que=deque(s)
for i in range(q):
    l=input()
    if l[0]=='1':
        n+=1
    else:
        if (l[2]=='1' and n%2==0) or (l[2]=='2' and n%2==1):
            que.appendleft(l[4])
        else:
            que.append(l[4])

ans=''.join(que)
if n%2==0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Accepted

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations

def CHK():
    S=list(input())
    Q=int(input())
    H=""
    B=""
    rflg = 0
    for i in range(Q):
        Que = input()
        if Que == '1':
            if rflg == 0:
                rflg = 1
            else:
                rflg = 0
            #S= list(reversed(S))
        else:
            T,F,C=map(str,Que.split())
            if F == '1' and rflg == 0 or (F != '1' and rflg == 1):
                #S=list(C)+S
                H = C + H
            else:
                #S.append(C)
                B = B + C

    S = list(H) + S + list(B)
    if rflg == 1:
        S = list(reversed(S))
    print("".join(S))

CHK()

Submission status: Accepted

Solution code:
from collections import deque

S=input()
Q=int(input())
r_flag=False
ans=deque((S))
for i in range(Q):
  Qu=input().split()
  if Qu[0]=="1":
    r_flag=not r_flag
  else:
    if Qu[1]=="1":
      if r_flag==True:
        ans.append(Qu[2])
      else:
        ans.appendleft(Qu[2])
    else:
      if r_flag==True:
        ans.appendleft(Qu[2])
      else:
        ans.append(Qu[2])
ans=list(ans)
print("".join(ans) if r_flag==False else "".join(ans[::-1]))

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
A = []
a = 0
for i in range(Q):
  q = list(input().split())
  if q[0] == '1':
    a += 1
  else:
    if (q[1] == '1' and a % 2 == 0) or (q[1] == '2' and a % 2 == 1):
      A.append(q[2])
    else:
      S.append(q[2])
A = A[::-1]
S = A + S
if a % 2 == 1:
  S = S[::-1]
ans = ''.join(S)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
S = deque(list(input().strip()))
Q = int(input())
P = 0
for _ in range(Q):
    q = list(input().split())
    if q[0]=="1":
        P += 1
        P = P%2
    else:
        c = q[2]
        if q[1]=="1":
            if P==0:
                S.appendleft(c)
            else:
                S.append(c)
        else:
            if P==0:
                S.append(c)
            else:
                S.appendleft(c)
if P==1:
    S = list(S)
    S = S[::-1]
print("".join(S))

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
s = input()
q = I()
rev = False
ans = deque((s))
for i in range(q):
    A = input().split()
    if A[0] == '1':
        if rev:
            rev = False
        else:
            rev = True
    else:
        if A[1] == '1':
            if rev:
                ans.append(A[2])
            else:
                ans.appendleft(A[2])
        else:
            if rev:
                ans.appendleft(A[2])
            else:
                ans.append(A[2])
if rev:
    ans.reverse()
    print(*ans,sep='')
else:
    print(*ans,sep='')

Submission status: Accepted

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

ans = deque(s)
ok = True
for v in Q:
    if v[0] == "1":
        if ok: ok = False
        else: ok = True
    else:
        if v[1] == "1":
            if ok: ans.appendleft(v[2])
            else: ans.append(v[2])
        else:
            if ok: ans.append(v[2])
            else: ans.appendleft(v[2])
ans = list(ans)
if not ok: ans = ans[::-1]
print("".join(ans))

Submission status: Accepted

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

from collections import deque
s = deque(s)

cnt = 0

for i in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        cnt += 1
    else:
        t, f, c = query
        if f == '1':
            if cnt%2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if cnt%2 == 0:
                s.append(c)
            else:
                s.appendleft(c)
s = list(s)
if cnt%2 == 1:
    s.reverse()
print(''.join(s))

Submission status: Accepted

Solution code:
S=input()
# Python3 program to Split string into characters 
def split(word): 
    return [char for char in word]  
word = S
splits=split(S)
Q=int(input())
flipcounter=0
prestring=[]
poststring=[]
for i in range(0,Q):
  inputforround=input()
  if inputforround=='1':
    flipcounter=flipcounter+1
  else:
    T,F,C=map(str,inputforround.split())
    if (int(F)+flipcounter)%2==1:
      prestring.append(C)
    else:
      poststring.append(C)
prestring.reverse()
for j in splits:
  prestring.append(j)
for k in poststring:
  prestring.append(k)
if flipcounter%2==1:
  prestring.reverse()
ans=''.join(prestring)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np
 
S=deque(input())
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])
 
for i in range(Q):
  l=L[i]
  if len(l)==1:
    count+=1
    continue
  elif len(l)==3:
    F=int(l[1])
    C=l[2]
    if (count%2==1) ^ (F==1):
      S.appendleft(C)
    else:
      S.append(C)
 
if count%2==1:
  S.reverse()
print(''.join(S))

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np

S=deque(input())
Q=int(input())
count = 0
L=np.array([input().split() for _ in range(Q)])

for i in range(Q):
  l=L[i]
  if len(l)==1:
    count+=1
    continue
  elif len(l)==3:
    F=int(l[1])
    C=l[2]
    if count%2==0:
      if F==1:
        S.appendleft(C)
      else:
        S.append(C)
    else:
      if F==2:
        S.appendleft(C)
      else:
        S.append(C)

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

Submission status: Accepted

Solution code:
# AtCoder
from collections import deque
S = input()
Q = int(input())
qs = [input().split() for i in range(Q)]

ans = deque(S)
flag = 0
count = 0
for q in qs:
    if q[0] == '1':
        if flag == 0:
            flag = 1
        else:
            flag = 0
        count += 1
    else:
        if flag == 0:
            if q[1] == '1':
                ans.appendleft(q[2])
            else:
                ans.append(q[2])
        else:
            if q[1] == '1':
                ans.append(q[2])
            else:
                ans.appendleft(q[2])

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

print(*ans, sep='')

Submission status: Accepted

Solution code:
from collections import deque
alfabet=deque(input())
n=int(input())
reverse=0
for i in range(n):
  operation_list=list(input().split())
  if int(operation_list[0])==1:
    reverse+=1
  else:
    if int(operation_list[1])==2 and reverse%2==0:
      alfabet.append(operation_list[2])
    elif int(operation_list[1])==2 and reverse%2==1:
      alfabet.appendleft(operation_list[2])
    elif int(operation_list[1])==1 and reverse%2==0:
      alfabet.appendleft(operation_list[2])
    elif int(operation_list[1])==1 and reverse%2==1:
      alfabet.append(operation_list[2])
if reverse%2!=0:
  alfabet.reverse()
print(''.join(alfabet))

Submission status: Accepted

Solution code:
# D - String Formation
from collections import deque
S = input()
Q = int(input())
now = 1
tmp = [deque([]),deque([])]
for _ in range(Q):
    t = list(input().split())
    if t[0]=='1':
        now = 1+(now==1)
    else:
        if int(t[1])==now:
            tmp[0].append(t[2])
        else:
            tmp[1].append(t[2])
ans = ''
if now==1:
    i,j = 0,1
else:
    i,j = 1,0
while len(tmp[i])>0:
    ans += tmp[i].pop()
if now==2:
    S = S[::-1]
ans += S
while len(tmp[j])>0:
    ans += tmp[j].popleft()
print(ans)

Submission status: Accepted

Solution code:
s=input()
q=int(input())
mae=""
usr=""
flg=True
for i in range(q):    
    a=list(map(str,input().split()))
    if a[0]=="1":
        if flg:
            flg=False
        elif flg==False:
            flg=True
    if a[0]=="2":
        if a[1]=="1":
            if flg:
                mae=a[2]+mae
            else:
                usr=usr+a[2]
        if a[1]=="2":
            if flg:
                usr=usr+a[2]
            else:
                mae=a[2]+mae
if flg:
    ans=mae+s+usr
else:
    ans=usr[::-1]+s[::-1]+mae[::-1]
print(ans)

Submission status: Accepted

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

c = 0
w = []

for i in range(Q) :
    a = input()
    if a[0] == "1" :
        c ^= 1
    else :
        if a[2] == "1" :
            if c == 0 :
                w.append(a[4])
            else :
                S.append(a[4])
        else :
            if c == 0 :
                S.append(a[4])
            else :
                w.append(a[4])
S = w[::-1] + S
print("".join(S) if c == 0 else "".join(S[::-1]))

Submission status: Accepted

Solution code:
s=input()
q=int(input())
m=0
from collections import deque
s=deque(s)
for i in range(q):
  l=list(map(str,input().split()))
  if l[0]=="1":
    m+=1
    m%=2
  else:
    if m==0:
      if l[1]=="1":
        s.appendleft(l[2])
      else:
        s.append(l[2])
    else:
      if l[1]=="1":
        s.append(l[2])
      else:
        s.appendleft(l[2])
s=list(s)
if m==1:
  s=s[::-1]
print("".join(s))

Submission status: Accepted

Solution code:
s = input()
q = int(input())
h,t = [],[]
p = 0

for i in range(q):
    c = input().split()
    if c[0] =='1':
        h,t = t,h
        p = 1-p
    else:
        if c[1] =='1':
            h.append(c[2])
        else:
            t.append(c[2])
if p ==1:
    s = s[::-1]
print("".join(h[::-1])+s+"".join(t))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip()
q = int(input())
query = []
rev = 0
for i in range(q):
    query.append(list(map(str, input().split())))
sa = []
sb = []
for i in range(q):
    if len(query[i]) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if query[i][1] == '1':
                sa.append(query[i][2])
            else:
                sb.append(query[i][2])
        else:
            if query[i][1] == '2':
                sa.append(query[i][2])
            else:
                sb.append(query[i][2])
ssa = ''.join(sa)
ssa = ssa[::-1]
ssb = ''.join(sb)
ss = ssa + s + ssb
if rev == 1:
    ss = ss[::-1]
print(ss)

Submission status: Accepted

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

for i in range(Q):
    Query.append(list(input().split()))


turning = 1
# 1なら正常　-1なら反転
ans = deque(S)
for i in Query:
    if i[0] == "1":
        turning *= -1
    else:
        if i[1] == "1":
            if turning == 1:
                ans.appendleft(i[2])
            else:
                ans.append(i[2])
        else:
            if turning == 1:
                ans.append(i[2])
            else:
                ans.appendleft(i[2])


print("".join(list(ans)) if turning == 1 else "".join(list(ans)[::-1]))

Submission status: Accepted

Solution code:
from collections import deque
d=deque()
d.append(input())
q=int(input())
r=1
for i in range(q):
  q2=input().split()
  if int(q2[0])==1:
    r*=-1
  else:
    if (r==1 and int(q2[1])==1) or (r==-1 and int(q2[1])==2):
        d.appendleft(q2[2])
    else:
        d.append(q2[2])

print("".join(d) if r==1 else "".join(d)[::-1])

Submission status: Accepted

Solution code:
s = list(input())
q = int(input())
r = 0
first = []
last = []

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

    if query[0] == "1":
        r = (r+1) % 2
        continue

    if r == 0:
        if query[1] == "1":
            first.append(query[2])
        else:
            last.append(query[2])
    else:
        if query[1] == "1":
            last.append(query[2])
        else:
            first.append(query[2])

first.reverse()
ans = first + s + last

if r == 1:
    ans.reverse()

print("".join(ans))

Submission status: Accepted

Solution code:
from collections import deque
def main():
    s = deque(input())
    q = int(input())
    qs = [tuple(input().split()) for _ in range(q)]
    flag = 1
    for qse in qs:
        if qse[0] == '1':
            flag ^= 1
        else:
            f = int(qse[1]) - 1
            f ^= flag
            if f:
                s.appendleft(qse[2])
            else:
                s.append(qse[2])
    if flag:
        print("".join(s))
    else:
        ss = list(s)
        print("".join(ss[::-1]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=str(input())
s=list(s)
q=int(input())
sb=[0]*(q+1)
sa=[0]*(q+1)
sbi=0
sai=0
ta=0
for i in range(q):
  temp=str(input())
  temp=list(temp)
  if temp[0]=="1":
    ta=ta+1
  else:
    if (temp[2]=="1" and ta%2==0) or (temp[2]=="2" and ta%2!=0):
      sb[sbi]=temp[4]
      sbi=sbi+1
    else:
      sa[sai]=temp[4]
      sai=sai+1
lsb=sb.index(0)
lsa=sa.index(0)
sb.reverse()
ans=sb[len(sb)-lsb:len(sb)]+s+sa[0:lsa]
if ta%2!=0:
  ans.reverse()
print("".join(ans))

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())
cnt = 0
start = ''
end = ''
for i in range(Q):
    A = list(map(str,readline().decode().split()))
    if A[0] == '1':
        cnt +=1
    else:
        if (cnt + int(A[1])) % 2:
            start += A[2]
        else:
            end += A[2]
ans = start + S + end
if cnt % 2 == 1:
    print(end[::-1] + S[::-1] + start)
else:
    print(start[::-1] + S + end)

Submission status: Accepted

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

for i in range(Q):
    a = input().split()
    if a[0]=="1":
        flag = not flag

    else:
        f = a[1]
        c = a[2]
        if f=="1":
            if flag:
                que.appendleft(c)
            else:
                que.append(c)
        else:
            if flag:
                que.append(c)
            else:
                que.appendleft(c)

if not flag:
    que.reverse()

print("".join(que))

Submission status: Accepted

Solution code:
from collections import deque

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

d = deque()
d.append(S)

for _ in range(Q):
    A = list(input().split())
    if A[0] == "1":
        if t == 0:
            t = 1
        else:
            t = 0

    elif (A[1] == "1" and t == 0) or (A[1] == "2" and t == 1):
        d.appendleft(A[2])
    else:
        d.append(A[2])

l = list(d)
ans = "".join(l)
if t == 1:
    ans = ans[::-1]
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
Q = int(input())
a = 0
A = []
B = []
for i in range(Q):
  q = list(input().split())
  q[0] = int(q[0])
  if q[0] == 1:
    if a == 0:
      a = 1
    else:
      a = 0
  else:
    q[1] = int(q[1])
    if (q[1] == 1 and a == 0) or (q[1] == 2 and a == 1):
      B.append(q[2])
    else:
      A.append(q[2])
B = B[::-1]
ans = B + S + A
if a == 1:
  ans = ans[::-1]
ans = ''.join(ans)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())

q = int(input())

before = []

after = []

cnt = 0

for i in range(q):
    t = list(input().split())
    
    if t[0] == "1":
        cnt += 1
    else:
        f = t[1]
        c = t[2]
        if f == "1":
            if cnt%2 == 0:
                before.append(c)                      
            else:
                after.append(c)
        else:
            if cnt%2 == 0:
                after.append(c)
            else:
                before.append(c)
                
if cnt%2 == 1:
    s.reverse()
    after.reverse()
    #before.reverse()
    for i in after:
        print(i,end = "")
    for i in s:
        print(i,end = "")
    for i in before:
        print(i,end = "")
else:
    before.reverse()
    before.extend(s)
    before.extend(after)
            
    for i in before:
        print(i,end = "")

Submission status: Accepted

Solution code:
from collections import deque
S= deque(input())
Q = int(input())
ba = 1
for i in range(Q):
    Query =list(map(str,input().split()))
    Query0 = Query[0]
    if Query[0] == '1':
        ba *= -1
    else:
        if Query[1] == '1' and ba ==1:
            S.appendleft(Query[2])
        elif Query[1] == '1' and ba ==-1:
            S.append(Query[2])
        elif Query[1] == '2' and ba ==1:
            S.append(Query[2])
        elif Query[1] == '2' and ba ==-1:
            S.appendleft(Query[2])
if ba == -1:
    S.reverse()
mojieru = ''.join(S)
print(mojieru)