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: Wrong Answer

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

jun = 1
for _ in range(q):
  
  tmp = input()
  if len(tmp)==1:
    jun = int(jun*(-1/2)+1/2)
  else:
    _,f,c = tmp.split()
    f = int(f)
    if jun+f ==2:
      s = c+s
    else:
      s += c
if jun==1:
  print(s)
else:
  print(s[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
import bisect as bi
from operator import mul
from functools import reduce
import math

def fast_comb(a,b,mod):
    up = reduce(lambda x,y: x*y%mod, range(a,a-b,-1))
    down = reduce(lambda x,y: x*y%mod, range(1,b+1))
    return (up * pow(down, mod-2, mod)) % mod

def prime(n):
    is_prime = [True] * (n+1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]:
            continue
        for j in range(i*2, n+1, i):
            is_prime[j] = False
    return is_prime

def readlines(n):
    for _ in range(n):
        query = input().split()
        yield iter(query)

def main():
    a = input().strip()
    deq = deque()
    deq.append(a)
    q = int(input())

    initial = True
    for query in readlines(q):
        t = next(query)
        if t == '1':
            initial = not initial
            continue

        f, c = query
        if initial:
            if f == '1':
                deq.appendleft(c)
            else:
                deq.append(c)
        else:
            if f == '1':
                deq.append(c)
            else:
                deq.appendleft(c)

    print("".join(deq) if initial else "".join(reversed(deq)))



main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	S = input()
	S = S[:-1]
	Q = inputInt()

	save = []

	flg = True
	for _ in range(Q):
		inp = input()
		inp = inp[:-1]
		inpt = inp.split()

		if len(inpt) == 1:
			if flg == True:
				flg = False
			else:
				flg = True
		else:
			oder = inpt[1]
			if oder == "1":
				if flg == True:
					S = inpt[2] + S
				else:
					S = S + inpt[2]
			else:
				if flg == True:
					S = S + inpt[2]
				else:
					S = inpt[2] + S

		if len(S) > 10000:
			save.append(S[1:-1])
			#print(save)
			S = S[0] + S[-1]
			#print(S)

	if flg == True:
		if len(save) > 0:
			tmp = "".join(save)
			ind = len(S) // 2
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S)
	else:
		if len(save) > 0:
			S = S[::-1]
			tmp = "".join(save)
			tmp = tmp[::-1]
			ind = len(S) // 2
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S[::-1])


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
que = []
for _ in range(q):
    que.append(list(input().split()))
d = deque([s])
cnt = 0
flag = False
for i in range(q):
    if len(que[i]) == 1:
        cnt += 1
        if i == q - 1 and cnt % 2 == 1:
            flag = True
    else:
        f = int(que[i][1])
        c = que[i][2]
        if (f + cnt) % 2 == 1:
            d.appendleft(c)
        else:
            d.append(c)
if flag:
    d = reversed(list(d))
res = ""
for i in d:
    res += i
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s=input().strip()
Q=int(input())

flag=1
before=()
after=()
for i in range(Q):
    a_list=list(input().split())

    if a_list[0]=="1":
        flag*=-1
    else:
        if flag==1:
            if a_list[1]=="1":
                before+=(a_list[2],)
            else:
                after+=(a_list[2],)
        else:
            if a_list[1]=="1":
                after+=(a_list[2],)
            else:
                before+=(a_list[2],)

if flag:
    ans="".join(list(before[::-1]))+s+"".join(after)
else:
    ans="".join(list(after[::-1]))+s[::-1]+"".join(before)

print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
rev=1
for i in range(Q):
    t=input()
    if t=='1':
        rev+=1
        rev=rev%2
    else:
        _,F,C=t.split(" ")
        if (int(F)+rev)%2==1:
            S=C+S
        else:
            S=S+C

if rev==1:
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
o = S
r = S
d = 1
dcnt = 0

for _ in range(Q):
  q = input().split()
  
  if q[0] == '1':
    dcnt += 1
    d *= -1 
  else:
    if (q[1] == '1' and d == 1) or (q[1] == '2' and d == -1):
      o = q[2] + o
      r = r + q[2]
    else:
      o = o + q[2]
      r = q[2] + r
      
if dcnt % 2 == 0:
  print(o)
else:
  print(r)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

cur = 1
d = deque([S])

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

    if len(xs) == 1:
        cur = 2 if cur == 1 else 1
    else:
        _, f, c = xs
        if f == cur == 1 or f == cur == 2:
            d.append(c)
        else:
            d.appendleft(c)

if cur == 1:
    ans = "".join(d)
else:
    d.reverse()
    ans = "".join(d)

print(ans)

Submission status: Wrong Answer

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

ans = [S]
for k in range(Q):
    if Query[k][0]=="1":
        ans.reverse()
    elif Query[k][0]=="2":
        if Query[k][1]=="1":
            ans.insert(0, Query[k][2])
        else:
            ans.append(Query[k][2])

print("".join(ans))

Submission status: Wrong Answer

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

rev_flag = False

heads = []
tails = []

for i in range(Q):
  in_ = input()
  if len(in_) > 1:
    _, f, c = in_.split()
    f = int(f)
    
    if rev_flag:
      if f==1:
        #S = S + c
        tails.append(c)
      elif f==2:
        #S = c+S
        heads.append(c)
    else:
      if f==2:
        #S = S + c
        tails.append(c)
      elif f==1:
        #S = c + S
        heads.append(c)
    
  else:
    # reverse    
    rev_flag = not rev_flag

if rev_flag:
  ans = "".join(tails) + S[-1::-1] + "".join(heads)
else:
  ans = "".join(heads[-1::-1]) + S + "".join(tails)

print(ans)

Submission status: Wrong Answer

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

ans=[S]
for q in query:
  if len(q)==1:
    ans=ans[::-1]
  else:
    _,f,c=q
    if f=='1':
      ans.insert(0,c)
    else:
      ans.append(c)
print(*ans,sep='')

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
s = list(readline().strip())
n = int(readline())
q = sys.stdin.readlines()
for qi in q:
    if qi[0] == '1':
        s[0], s[-1] = s[-1], s[0]
    else:
        c = qi[4]
        if qi[2] == '2':
            s.append(c)
        else:
            s = [c] + s
print(''.join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque([x for x in input()])
Q = int(input())
for _ in range(Q):
    inp = input()
    if len(inp) == 1:
        T = int(inp)
        if len(S) != 1:
            left_val = S.popleft()
            right_val = S.pop()
            S.append(left_val)
            S.appendleft(right_val)
        else:
            pass
    else:
        T, F = map(int, inp.split()[:2])
        C = inp.split()[-1]
        if F == 1:
            S.appendleft(C)
        else:
            S.append(C)

print("".join(S))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
for _ in range(q):
    Q = list(map(str,input().split()))
    if int(Q[0]) == 1:
        s = s[-1]+s[1:len(s)-1]+s[0]
    else:
        if int(Q[1]) == 1:
            s = Q[2]+s
        else:
            s = s+Q[2]
            
print(s)

Submission status: Wrong Answer

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

rev_count = 0
top_add, tail_add = [], []
for i in range(Q):
  q = input()
  if(len(q) == 1): t = int(q[0])
  else: t,f,c = int(q[0]),int(q[2]),q[4]
  
  if(t == 1):
    rev_count += 1
  else:
    #先頭に追加
    if(f == 1):
      #そのまま追加
      if(rev_count%2 == 0): top_add.append(c)
      #末尾に追加
      else: tail_add.append(c) 
    #末尾に追加
    else:  
      #そのまま追加
      if(rev_count%2 == 0): tail_add.append(c)
      #先頭に追加
      else: top_add.append(c) 

ans = ''
#先頭追加
for i in range(len(top_add)):
 #逆からくっつける
 ans += top_add[-(i+1)] 
#開始文字
ans += S
#末尾追加
for i in range(len(tail_add)):
 #逆からくっつける
 ans += tail_add[-(i+1)] 
  
#反転回数 奇数なら
if(rev_count%2 == 1):
  ans = ans[::-1]
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

s = list(sys.stdin.readline().rstrip())
q = int(sys.stdin.readline())

for _ in range(q):
    query = sys.stdin.readline().split()
    if len(query) == 1:
        s[0], s[-1] = s[-1], s[0]
    else:
        if query[1] == '1':
            s = [query[2]] + s
        else:
            s.append(query[2])

print(''.join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
d=deque()
S=input()
Q=int(input())
query=[list(map(str,input().split())) for _ in range(Q)]
flag=0
#print(query)
s=S
s2=""
d.append(S)
"""
for i in range(Q):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
        else:
            if query[i][1]=="2":
                s="".join((query[i][2],s))
            else:
                s = "".join((s,query[i][2]))
if flag==0:
    print(s)
else:
    for i in range(len(s)):
        s2="".join(())
    print(s[::-1])
"""
for i in range(Q):
    if len(query[i])==1:
        flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="2":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    #print(d)
if flag==0:
    print("".join(list(d)))
else:
    d.reverse()
    print("".join(list(d)))

Submission status: Wrong Answer

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

query = [input() for _ in range(q)]
count = 0
for qu in query:
    if len(qu.split()) == 1:
        count += 1
        continue
    num, f, c = qu.split()
    num = int(num)
    if f == 1:
        s = c + s
    else:
        s = s + c

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

Submission status: Wrong Answer

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

for i in range(Q):
    tmp = list(input().split())
    lst.append(tmp)

reverse = 0
c = 1
for i in range(Q):

    if int(lst[i][0]) == 1:
        reverse += 1
        c += 1
    
    if int(lst[i][0]) == 2:
        if c%2 == 1:
            c = 1
            if int(lst[i][1]) == 1:
                S.insert(0,lst[i][2])
            else:
                S.append(lst[i][2])
        else:
            c = 1
            if int(lst[i][1]) == 2:
                S.insert(0,lst[i][2])
            else:
                S.append(lst[i][2])

if reverse%2 == 1:
    S.reverse()

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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
s=input()
q=int(input())
s1="";s2=""
sta=0
for i in range(q):
    a=tuple(input())
    if a[0]=="1":
        sta^=1
    else:
        if a[2]=="1":
            if not sta:
                s1=a[4]+s1
            else:
                s2+=a[4]
        if a[2]=="2":
            if sta:
                s1=a[4]+s1
            else:
                s2+=a[4]                
ans=s1+s+s2
if sta:
    print(ans[::-1])
else:print(ans)

Submission status: Wrong Answer

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

s=input()
q=int(input())
cnt=0
qq=[tuple(input().split()) for _ in [0]*q]
for j in qq:
    if  len(j)!=1 :
        if (cnt+int(j[1]))%2==1:
            s=j[2]+s
        else:
            s=s+j[2]
    else:
        cnt+=1
if cnt%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Wrong Answer

Solution code:
s = list(input())
Q = int(input())
for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        s[0], s[-1] = s[-1], s[0]
    else:
        if int(q[1]) == 1:
            s = list(q[2]) + s
        else:
            s = s + list(q[2])
print("".join(s))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
swc = 0
top = ""
bot = ""
for i in range(q):
	a = list(map(str, input().split()))
	if a[0] == "1":
		if swc == 0:
			swc = 1
		else:
			swc = 0
	else:
		if swc == 0:
			if a[1] == "1":
				top += a[2]
			else:
				bot += a[2]
		else:
			if a[1] == "1":
				bot += a[2]
			else:
				top += a[2]
if swc == 0:
	top = ''.join(list(reversed(top)))
	print (top + s + bot)
else:
	bot = ''.join(list(reversed(bot)))
	print (bot + s + top)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
for _ in range(q):
    query = input().split()
    if query[0] == '1':
        s = s[-1] + s[1:-1] + s[0]
    else:
        if query[1] == '1':
            s = query[2] + s
        else:
            s = s + query[2]
print(s)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.buffer.readline
from collections import deque
s = readline().rstrip().decode('utf-8')
q = int(readline())
flag = 0
lst1 = deque([])
lst2 = deque([])
for i in range(q):
    t = readline().rstrip().decode('utf-8')
    if t[0] == "1":
        #reverse
        flag = 1-flag
    else:
        com,f,c = t.split()
        if f == "1": #先頭にc追加
            if not flag:
                lst1.appendleft(c)
            else:
                lst2.append(c)
            

        else: #末尾にc追加
            if not flag:
                lst2.append(c)
            else:
                lst1.appendleft(c)

if not flag:
    s = "".join(lst1) + s + "".join(lst2)
else:
    s = "".join(lst2) + s + "".join(lst1)

print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
is_turned = False
head = []
tail = []
for _ in range(Q):
    tmp = input().split()
    if len(tmp) == 1:
        is_turned = not is_turned
        continue
    f, c = int(tmp[1]), tmp[2]
    # print('f c ', f, c)
    if not is_turned:
        if f == 1:
            head.append(c)
        else:
            tail.append(c)
    else:
        if f == 1:
            tail.append(c)
        else:
            head.append(c)
if is_turned:
    S = ''.join(reversed(S))
    # head = ''.join(reversed(head))
    head = ''.join(head)
    tail = ''.join(tail)
    ans = tail + S + head
else:
    S = ''.join(S)
    head = ''.join(reversed(head))
    # tail = ''.join(reversed(tail))
    tail = ''.join(tail)
    ans = head + S + tail
# print('head tail', head, tail, is_turned, S)
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
queries = []
rev = [0]*q
flag=0
for i in range(q):
    query = list( input().split() )
    queries.append( query )
    if query==["1"]:
        flag+=1
    rev[i]=flag

n_rev = queries.count(["1"])
rev = [rr+n_rev for rr in rev]
left=""
right=""
for i,query in enumerate(queries):
    if query!=["1"]:
        if rev[i]%2==0:
            if query[1]=="2":
                right=right+query[2]
            else:
                left=query[2]+left
        else:
            if query[1]=="1":
                right=right+query[2]
            else:
                left=query[2]+left 
print(left+s+right)

Submission status: Wrong Answer

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

forward = [] # リバース回数が2で割り切れるときに前に来る
backend = [] # リバース回数が2で割り切れないときに前に来る
reverse = 0 # リバース回数

for i in range(Q):
    #各クエリの処理（両方を許容）
        s = input()
        if s.count(" ") == 0:
            reverse += 1
        else:
            s2 = s.split(" ")
            if s2[1] == '1':
                if reverse % 2 == 0:
                    forward.append(s2[2])
                else:
                    backend.append(s2[2])
            else:
                if reverse % 2 == 0:
                    backend.append(s2[2])
                else:
                    forward.append(s2[2])

#文字列の出力
if reverse % 2 == 0:
    for i in forward[::-1]:
        print(i,end="")
    
    print(S,end="")

    for i in backend:
        print(i,end="")
else:
    for i in backend[::-1]:
        print(i,end="")
    
    print(S,end="")

    for i in forward:
        print(i,end="")

Submission status: Wrong Answer

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

d = deque()

d.append(S)

for i in range(Q):
  List = list(input().split())
  a = int(List[0])
  if a == 1:
    d.reverse()
  else:
    b = int(List[1])
    if b == 1:
      d.appendleft(List[2])
    else:
      d.append(List[2])

string = "".join(list(d))
print(string)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

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

    reverse = False
    header = []
    footer = []
    for _ in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            a, f, c = query.split()
            if reverse:
                if f == '1':
                    footer.append(c)
                else:
                    header = [c] + header
            else:
                if f == '1':
                    header.append(c)
                else:
                    footer = [c] + footer
    if reverse:
        print("".join(footer[::-1] + list(S)[::-1] + header[::-1]))
    else:
        print("".join(header) + S + "".join(footer))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
S=input()
Q=int(input())
d = deque([S])
flag=0
for i in range(Q):
    x=input().split()
    if len(x)==1:
        if int(x[0])==1:
            flag=(flag+1)%2
    else:
        if int(x[1])%2==0:
            if flag==0:
                d.appendleft(x[2])
            else:
                d.append(x[2])
        else:
            if flag==0:
                d.append(x[2])
            else:
                d.appendleft(x[2])
        

if flag==1:
    print(*d, sep="")
    

else:
    d=list(reversed(d))
    print(*d, sep="")

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
rev = 0
rev_check = False
first = list()
last = list()
for i in range(n):
    q = list(input().split())
    if q[0] == "1":
        rev += 1
        rev_check = not rev_check
    else:
        if rev_check ==  True:
            if q[1] == "1":
                last.append(q[2])
            else:
                first.append(q[2])
        else:
            if q[1] == "1":
                first.append(q[2])
            else:
                last.append(q[2])
if rev_check == False:
    print("".join(reversed(first)) + s + "".join(last))
else:
    print("".join(reversed(last)) + s + "".join(first))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
re_flag = False 
start = deque()
finish = deque()
for i in range(Q):
    l = input()
    if l == "1":
        re_flag = not re_flag
    else:
        t, f, c = l.split()
        if f == "1":
            if re_flag:
                finish.append(c)
            else:
                start.append(c)
        else:
            if re_flag:
                start.append(c)
            else:
                finish.append(c)

S.extend(finish)
S.extendleft(start)

if re_flag:
    S.rotate(1)

print("".join(list(S)))

Submission status: Wrong Answer

Solution code:
L = []
s = input()
L.append(s)
q = int(input())
c = 0
d = 0

for _ in range(q):
    l = input().split()
    if l == ['1']:
        c += 1
        d += 1
    elif l[1] == '1':
        if c % 2 == 0:
            L.insert(0,l[2])
            c = 0
        else:
            L.append(l[2])
            c = 0
    else:
        if c % 2 == 0:
            L.append(l[2])
            c = 0
        else:
            L.insert(0,l[2])
            c = 0
            
if d % 2 == 0:
    ansl = L
else:
    ansl = L.reverse()

ans = ''
for i in L:
    ans += i
    
print(ans)

Submission status: Wrong Answer

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

top = ""
tail = ""

rev = 1
#プラスで順列

for i in range(q):
    query = input()
    if query == "1":
        rev *= -1
    elif query[2] == "1":
        if rev == 1:
            top += query[4]
        else:
            tail += query[4]
    else:
        if rev != 1:
            top += query[4]
        else:
            tail += query[4]

if rev == 1:
    print(top[::-1] + s + tail)
else:
    print(tail[::-1] + s + top)

Submission status: Wrong Answer

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

count=0
f=""
b=""

for i in range(Q):
    if q[i][0]=="1":
        count+=1
    else:
        if q[i][1]=="1":
            if count%2==0:
                f=q[i][2]+f
            else:
                b+=q[i][2]
        elif q[i][1]=="2":
            if count%2==1:
                f=q[i][2]+f
            else:
                b+=q[i][2]

if count%2==1:
    print(b[::-1]+S+f[::-1])
else:
    print(f+S+b)

Submission status: Wrong Answer

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

before = ""
after= ""

count1 = True


for i in range(Q):
    q = input().split()
    if int(q[0]) == 1:
        if count1:
            count1 = False
        else:
            count1 = True
    else:
        if int(q[1]) == 1:
            if count1:
                before += q[2]
            else:
                after += q[2]
        else:
            if count1:
                after += q[2]
            else:
                before += q[2]


before = before[::-1]
if count1:
    middle = S
    ans = before + middle + after
else:
    middle = S
    middle = middle[::-1]
    ans = before + middle + after
    ans = ans[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
# list(input())
# list(map(int, input().split()))
# int(input())
from collections import deque
S = deque(list(input()))
Q = int(input())
queries = []
for _ in range(Q):
    q = list(input().split())
    if len(q) == 1:
        S[0], S[-1] = S[-1], S[0]
        continue
    _, f, c = q
    if f == '1':
        S.appendleft(c)
    else:
        S.append(c)
print("".join(S))

Submission status: Wrong Answer

Solution code:
S=input()
q=int(input())
RV=0
for i in range(q):
    IN=input()
    if IN=="1":
        RV+=1
    else:
        t,f,c=IN.split()
        if RV%2:
            if f=="1":
                S=c+S                
            else:
                S=S+c
        else:
            if f=="1":
                S=S+c
            else:
                S=c+S
if RV%2:
    print(S)
else:
    S=S[::-1]
    print(S)

Submission status: Wrong Answer

Solution code:
S = input().split()
N = int(input())
Q = [[] for _ in range(N)]
for i in range(N):
    line = input().split()
    Q[i] = line
reverse = 0
for i in range(N):
    if Q[i][0] == '1' :
        reverse = (reverse + 1) % 2
    else :
        if reverse == 0 :
            if Q[i][1] == '1' :
                S = [Q[i][2]] + S
            else :
                S = S + [Q[i][2]]
        else:
            if Q[i][1] == '1':
                S = S + [Q[i][2]]
            else:
                S = [Q[i][2]] + S
if reverse == 0 :
    print("".join(S))
else:
    S.reverse()
    print("".join(S))

Submission status: Wrong Answer

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''

'''

def solve(l):
	n = len(l)
	

S = input()
a,b = S,S
Q = int(stdin.readline())
zz = 0
for _ in range(Q):
	l = list(map(str, stdin.readline().rstrip().split()))
	if(l[0] == '1'):
		a,b = b,a
		zz += 1
	else:
		q,f,c = l
		f = int(f)
		if(f == 1):
			a += c
			b = c + b
		else:
			a = c + a
			b += c
	# print("*",a,b)

print(b)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


def main():
    s = list(input())
    q = int(input())
    s.pop(-1)
    for i in range(q):
        query = input().split()
        if query[0] == '1':
            tmp = s[0]
            s[0] = s[-1]
            s[-1] = tmp
        elif query[0] == '2':
            if query[1] == '1':
                s.insert(0, query[2])
            elif query[1] == '2':
                s.append(query[2])
    print(''.join(s))


main()

Submission status: Wrong Answer

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

S = input()
Q = int(input())
W = 0
ans = S.rstrip()

for _ in range(Q):
#    print(W,ans)
    qry = list(input().rstrip().split())
    if qry[0] == "1":
        W = 1-W
        continue
    T,F,C = qry
    T = int(T)
    F = int(F)
    C.rstrip()
    if (W == 0 and F=="2") or (W==1 and F=="1"):
        ans = ans+C
    else:
        ans = C+ans
if W == 1:
    ans = ans[::-1]
print(ans)