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:
from collections import deque

S = input()
D = deque(S)
C = deque(S)
Q = int(input())

conr = 0

for i in range(Q):
  q = list(input())
  if q[0] == "1":
    conr += 1
  else:
    s = q[-1]
    f = q[1]
    if f == "1":
      D.appendleft(s)
      C.append(s)
    else:
      C.appendleft(s)
      D.append(s)

D = list(D)
C = list(C)
total = len(D)

if conr % 2 == 1:
  ans = "".join(D)
else:
  ans = "".join(C)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
q = int(input())
tmp = deque()
tmp.append(s)
flag = 0
for _ in range(q):
    _s = input()
    if _s == "1":
        flag += 1
    else:
        f = _s[2]
        c = _s[4]
        r = flag % 2
        if f == "1" and r == 0:
            #tmp = c + tmp
            tmp.appendleft(c)
        elif f == "1" and r == 1:
            #tmp = tmp + c
            tmp.append(c)
        elif f == "2" and r == 0:
            #tmp = tmp + c
            tmp.append(c)
        elif f == "2" and r == 1:
            #tmp = c + tmp
            tmp.appendleft(c)
if flag % 2 == 0:
    for i in range(len(tmp)):
        print(tmp.popleft(), end="")
else:
    for i in range(len(tmp)):
        print(tmp.pop(), end="")

Submission status: Wrong Answer

Solution code:
S=[]
s=input()
S.append(s)
Q=int(input())
for i in range(Q):
    r=input()
    if len(r.split()) == 3:
        T,F,C=r.split()
    else:
        T='1'
    if T == '1':
        tmp=S[0]
        S[0]=S[len(S)-1]
        S[len(S)-1]=tmp
        continue
    if T == '2':
        if F == '1':
            S.insert(0,C)
        if F == '2':
            S.append(C)

print("".join(S))

Submission status: Wrong Answer

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

for i in range(Q):
    Query = input()
    if Query[0] == "1":
        Flag = not Flag
    else:
        QueryList = Query.split()
        if Flag:
            if QueryList[1] =="1":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]
        else:
            inv_add = QueryList[2][::-1]
            if QueryList[1] =="2":
                S = QueryList[2]+S
            else:
                S = S+QueryList[2]

if Flag:
    print(S)

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) > 30000:
			save.append(S[1:-1])
			#print(save)
			S = S[0] + S[-1]
			#print(S)

	if flg == True:
		if len(save) > 0:
			#print("ss")
			#print(save)
			#print(S)
			tmp = "".join(save)
			ind = len(S) // 2
			if len(S) % 2 != 0:
				#ind += 1
				pass
			print(S[:ind] + tmp + S[ind:])
		else:
			print(S)
	else:
		if len(save) > 0:
			print("SS")
			print(save)
			print(S)
			#S = S[::-1]
			tmp = "".join(save)
			#tmp = tmp[::-1]
			ind = len(S) // 2
			if len(S) % 2 != 0:
				#ind += 1
				pass
			ans = S[:ind] + tmp + S[ind:]
			print(ans[::-1])
		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:
import sys
input = sys.stdin.readline

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


for i in range(q):
  query = list(input().split())
  if (int(query[0]) == 1):
    s.reverse()
  else:
    if (int(query[1]) == 1):
      s.insert(0, query[2])
    else:
      s.append(query[2])
  
print(''.join(s))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
q = sys.stdin.readlines()
front = s[0]
if len(s) == 1:
    back = ''
else:
    back = s[-1]
s = s[1:-1]
for qi in q:
    if qi[0] == '1':
        front, back = back, front
    else:
        c = qi[4]
        if qi[2] == '1':
            if not back:
                back = front
            else:
                s = front + s
            front = c
        else:
            s += back
            back = c

print(front + s + back)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

s = list(s)
# s = s[::-1]
reverce_counter = 0
for _ in range(q):
    q = input()
    if q[0] == '1':
        reverce_counter += 1
    else:
        q = q.split(' ')
        if q[1] == '1':
            if reverce_counter % 2 == 0:
                idx = 0
            else:
                idx = len(s) - 1
            s.insert(idx,q[-1])
        else:
            if reverce_counter % 2 == 0:
                idx = len(s) - 1
            else:
                idx = 0
            s.insert(idx,q[-1])
    # print(s)
if reverce_counter % 2 == 1:
    s = s[::-1]
print(''.join(s))

Submission status: Wrong Answer

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

for i in range(Q):
  Q_list = list(map(str, input().split()))
  
  if len(Q_list) == 1:
    if len(S) >= 2:
      S_list = list(S)
      S_list[0], S_list[-1] = S_list[-1], S_list[0]
      S = "".join(S_list)
  
  else:
    F = Q_list[1]
    C = Q_list[2]
    
    if int(F) == 1:
      S = C + S
    else:
      S = S + C
      
print(S)

Submission status: Wrong Answer

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

S = str(input())
Q = int(input())
print(S, Q)

S_copy = S
for i in range(Q):
    query = list(input().split())
    # print(query)
    if int(query[0]) == 1:
        # print("reverse")
        # Sを反転
        S_copy = S_copy[::-1]
    else:
        if int(query[1]) == 1:
            # Sの先頭にC_iを追加
            # print("prepend C_i")
            S_copy = query[2] + S_copy
        else:
            # Sの末尾にC_iを追加
            # print("append C_i")
            S_copy = S_copy + query[2]

print(S_copy)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
c=0
for _ in range(q):
  L=list(input().split())
  if L[0]=='1':
    c+=1
  else:
    if L[1]=='2':
      s+=L[2]
    else:
      t=L[2]+s
      s=t

if c//2==0:
  print(s)
else:
  s=s[::-1]
  print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for _ in range(Q):
  q = list(input())
  if len(q) == 1:
    r = S[0]
    l = S[-1]
    S[0] = l
    S[-1] = r
  else:
    if q[2] == "1": S.insert(0, q[4])
    else: S.append(q[4])

print("".join(S))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def main():
    a = list(input())
    a.pop(-1)

    a_append = a.append
    # b = deque()
    # b_appendleft = b.appendleft

    Q = int(input())
    count = 0
    for _ in range(Q):
        q = list(input().split())
        if q[0] == "1":
            count += 1
            # a.reverse()
        else:
            if count % 2 == 0:
                if q[1] == "1":
                    # a.insert(0, q[2])
                    b = deque(a)
                    b.appendleft(q[2])
                    a = list(b)
                else:
                    a_append(q[2])
            else:
                if q[1] == "1":
                    a_append(q[2])
                else:
                    # a.insert(0, q[2])
                    b = deque(a)
                    b.appendleft(q[2])
                    a = list(b)

    if count % 2 != 0:
        a.reverse()
    print("".join(a))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

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

S= one_str()

Q = one_int()

is_not_ac=False
head = ""
tail = ""
for i in range(Q):
    temp = input()
    if len(temp)>=2:
        T, F, C = temp.split()
        if F=="1":
            if is_not_ac:
                tail += C
            else:
                head += C
        else:
            if is_not_ac:
                head += C
            else:
                tail += C
    else:        
        is_not_ac = not is_not_ac

if is_not_ac:
    print(tail[::-1] + S + head)
else:
    print(head[::-1] + S + tail)

Submission status: Wrong Answer

Solution code:
str = input()
N = int(input())
q = list([input().split()])
for i in range(N-1):
    q_next = input().split()
    if len(q[-1]) == 1 and len(q_next) == 1:
        continue
    else:
        q.append(q_next)
for i in q:
    if (i[0] == '1'):
        str = str[::-1]
    else:
        if (i[1] == '1'):
            str = i[2]+str
        else:
            str = str + i[2]
print(str)

Submission status: Wrong Answer

Solution code:
s = list(input())
Q = int(input())
for i in range(Q):
    print(s)
    q = input().split()
    if int(q[0]) == 1:
        s = s[::-1]
    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()
N = int(input())
Q = [input().split() for _ in range(N)]

head = ''
tail = ''
reverse = 0

for q in Q:
    if int(q[0]) == 1:
        reverse = 1 - reverse
    elif int(q[1]) == 1:
        if reverse:
            tail += q[2]
        else:
            head += q[2]
    else:
        if reverse:
            head += q[2]
        else:
            tail += q[2]

if reverse:
    print(tail[::-1] + s + head)
else:
    print(head[::-1] + s + tail)

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:
            _, f, c = query.split()
            if reverse:
                if f == '1':
                    footer = [c] + footer
                else:
                    header.append(c)
            else:
                if f == '1':
                    header.append(c)
                else:
                    footer = [c] + footer
    if reverse:
        print("".join(footer[::-1] + list(S)[::-1] + header))
    else:
        print("".join(header[::-1]) + S + "".join(footer))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
S = input()
Q = int(input())

count = 0
ans = S
ans2 = S

for i in range(Q):
    T = input().split()
    if len(T)==1:
        count+=1
        continue

    if count%2==0:
        if T[1] == '1':
            ans = T[2] + ans
            ans2 += T[2]
        else:
            ans += T[2]
            ans2 = ans2 + T[2]
    else:
        if T[1] == '1':
            ans += T[2]
            ans2 = ans2 + T[2]
        else:
            ans = T[2] + ans
            ans2 += T[2]

if count%2==0:
    print(ans)
else:
    print(ans2)

Submission status: Wrong Answer

Solution code:
from sys import stdin
S = stdin.readline()[:-1]
Q = int(stdin.readline())
for _ in range(Q):
    command = stdin.readline()[::-1]
    if command[0] == '2':
        a, b, char = command.split()
        if b == '1': S = char + S
        else: S = S + char
    else: S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
reverse = False
front = ""
end = ""

for i in range(Q):
    inp = input()
    if inp[0] == "1":
        reverse = ~reverse
    else:
        _, F, C = inp.split()
        if (F == "1") ^ reverse:
            front = front+C
        else:
            end = end+C

if reverse:
    print(end[::-1]+S[::-1]+front)
else:
    print(front[::-1]+S+end)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input=sys.stdin.readline
moji=deque([])
s=input().rstrip()
for i in range(len(s)):
	moji.append(ord(s[i]))
q=int(input())
rev=0
for _ in range(q):
	qw=input().split()
	if qw[0]=="1":
		rev+=1
	else:
		f=qw[1]
		c=qw[2]
		if f=="1":
			if rev%2==0:
				moji.appendleft(ord(c))
			else:
				moji.append(ord(c))
		else:
			if rev%2==0:
				moji.append(ord(c))
			else:
				moji.appendleft(ord(c))
ans=""
alph="abcdefghijklmnopqrstuvwxyz"
# for i in range(len(moji)):
# 	ans+=alph[moji[i]-97]
if rev%2==0:
	print(ans)
else:
	print(ans[::-1])

Submission status: Wrong Answer

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

S = input()
Q = int(input())
A = [(input().split()) for _ in range(Q)]

for i in range(Q):
  if A[i] == ["1"]:
    S = ''.join(list(reversed(S)))
  else:
    if A[i][1] == "1":
      S = S + A[i][2]
    else:
      S = A[i][2] + S
      
print(S)

Submission status: Wrong Answer

Solution code:
def main():
	s = list(str(input()))
	q = int(input())
	qu = [list(map(str,input().split())) for x in range(q)]
	shiji = 0
	rcount = 0
	for i in range(q):
		if qu[i][0] != '1':
			if shiji == 0:
				if qu[i][1] == '1':
					s.insert(0,qu[i][2])
					shiji = 0
				else:
					s.append(qu[i][2])
			else:
				if qu[i][1] == '2':
					s.insert(0,qu[i][2])
					shiji = 0
				else:
					s.append(qu[i][2])
		else:
			shiji = 1
			rcount += 1
	if rcount % 2 == 0:
		print(''.join(map(str,s)))
	else:
		s.reverse()
		print(''.join(map(str,s)))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

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

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

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

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

        if dada == 2:
            usi += tmp[2]
            # S = S + tmp[2]
        else:
            mae = tmp[2] + mae
            # S = tmp[2] + S


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

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
i = 0
while i < q:
    arr = list(input().split(' '))
    if int(arr[0]) == 1:
        tmp = s[0]
        s[0] = s[-1]
        s[-1] = tmp
    else:
        f, c = int(arr[1]), arr[2]
        if f == 1:
            tmp = s
            s = [c]
            s+= tmp
        else:
            s.append(c)
    i += 1
text = "".join(s)
print(text)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
rev_flag = False
for _ in range(q):
    i = input()
    if i == '1':
        rev_flag = not rev_flag
    else:
        a,b,c = i.split()
        if rev_flag:
            if b == '1':
                s = c + s
            else:
                s = s + c
        else:
            if b == '1':
                s = s + c
            else:
                s = c + s
if rev_flag:
    print(s)
else:
    print(''.join(list(reversed(s))))

Submission status: Wrong Answer

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    s = input()
    q = int(input())
    ans = deque()
    swap = False
    for i in range(len(s)):
        ans.append(s[i])

    for i in range(q):
        #print(ans, swap)
        inp = input()
        if len(inp) == 1:
            hoge = int(inp)
            swap = True
        else:
            # print(tuple(inp))
            fi, ci = tuple(inp)[2], tuple(inp)[4]
                       
            if int(fi) == 1:
                if not swap:
                    ans.appendleft(ci)
                else:
                    ans.append(ci)
            if int(fi) == 2:
                if not swap:
                    ans.append(ci)
                else:
                    ans.appendleft(ci)
    if swap:
        ans = list(ans)[::-1]
    else:
        ans = list(ans)
    s_ans = str()
    for i in range(len(ans)):
        s_ans += ans[i]
    print(s_ans)
                
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
top = ''
end = ''
reverse_flag = 1
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverse_flag *= -1
    else:
        _, F, C = query.split()
        if (int(F)-1.5) * reverse_flag == 1:
            end += C
        else:
            top += C
ans = top[::-1] + S + end
if reverse_flag == 1:
    print(ans)
else:
    print(ans[::-1])

Submission status: Wrong Answer

Solution code:
S=input()
b=0
for i in range(int(input())):
    q=input()
    if q=='1':b+=1
    else:
        a,f,c=q.split()
        S=c+S if (int(f)-1)^(b%2) else S+c 
print(S if b%2==1 else S[::-1])

Submission status: Wrong Answer

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

A=""
Z=""
AZFlag = 0
for i in range(Q):
    Que = input().split()

    if Que[0] == '1':
        if AZFlag == 0:
            AZFlag = 1
            continue

        if AZFlag == 1:
            AZFlag = 0
            continue

    if Que[0] == '2':
        if Que[1] == '1' and AZFlag == 0:
            A = Que[2]+A
            continue

        if Que[1] == '1' and AZFlag == 1:
            Z = Z+Que[2]
            continue
        
        if Que[1] == '2' and AZFlag == 0:
            Z = Z+Que[2]
            continue

        if Que[1] == '2' and AZFlag == 1:
            A = Que[2]+A
            continue


if AZFlag == 0:
    print(A+S+Z)
else:
    print(Z+S+A[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque(input().rstrip())

nq = int(input().rstrip())
for _ in range(nq):
    query = input().rstrip()
    if query[0] == '1':
        a, b = s.popleft(), s.pop()
        s.appendleft(b)
        s.append(a)
    else:
        _, f, c = query.split()
        if f == '1':
            s.appendleft(c)
        else:
            s.append(c)
print("".join(s))

Submission status: Wrong Answer

Solution code:
message = [input().strip()]
N = int(input().strip())

for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        message.reverse()
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        message.insert(0, c)
    else:
        message.append(c)
print(''.join(message))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

inv = 0
d = deque(s)

for _ in range(q):
  Q = list(input().split())
  
  if Q[0]=='1':
    inv += 1
  elif Q[0]=='2':
    if Q[1]=='1':
      if inv%2==0:
        d.append(Q[2])
      else:
        d.appendleft(Q[2])
    elif Q[1]=='2':
      if inv%2==0:
        d.appendleft(Q[2])
      else:
        d.append(Q[2])
        
if inv%2==1:
  print(*d, sep='')
else:
  print(*deque(reversed(d)), sep='')

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import numpy as np

def main():
    
    s = input()
    q = int(input())
    
    arr = np.array([s])
    for _ in range(q):
        a = input()
        if a == '1':
            arr = arr[::-1]
        else:
            tmp, f, c = a.split()
            if f == '1':
                arr = np.insert(arr, 0, c)
            else:
                arr = np.append(arr, c)
    print(''.join(arr))
        

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def main():
    from collections import deque

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

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

        if qi[0] == '1':
            d.reverse()
        else:
            if qi[1] == '1':
                d.appendleft(qi[2])
            else:
                d.append(qi[2])

    print(''.join(map(str, d)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in query:
    if i[0]=="1":
        S=S[::-1]
    else:
        if i[1]==1:
            S=i[2]+S
        else:
            S=S+i[2]
print(S)

Submission status: Wrong Answer

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
s = input()
n = int(input())
cnt = 0
cnt2 = 0
for i in range(n):
    a = input().split()
    # print(a)
    if len(a) == 1:
        cnt += 1
        cnt2 += 1
    elif cnt % 2 == 0:
        if a[1] == "1":
            s = a[2] + s
            # print(a[2])
        else:
            s = s + a[2]
        cnt = 0
    else:
        if a[1] == "1":
            s = s + a[2]
            # print(a[2])
        else:
            s = a[2] + s
        cnt = 0
if cnt2 % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def T2F1(org_S,rev_S, C):
    org_S.appendleft(C)
    rev_S.append(C)
    return org_S, rev_S
def T2F2(org_S, rev_S, C):
    rev_S.append(C)
    org_S.appendleft(C)
    return org_S, rev_S

org_S = deque(list(input()))
rev_S = deque(list(reversed(org_S)))
Q = int(input())
jdg = 0

for i in range(Q):
    A = list(input().split())
    if A[0] == "1":
        jdg += 1
    elif A[1] == "1":
        T2F1(org_S, rev_S, A[2])
    else:
        T2F2(org_S, rev_S, A[2])

if jdg%2 ==0:
    org_S = "".join(org_S)
    print(org_S)
else:
    rev_S = "".join(rev_S)
    print(rev_S)

Submission status: Wrong Answer

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

roll = False
ans = [s]

for i in range(q) :
    inp = input()
    if inp[:1] == '1' :
        roll = not roll
        #print(roll)
    else :
        t, f, c = inp.split()
        #print(t, f, c)
        if (f == '1' and (not roll)) or (f == '2' and roll) :
            ans.insert(0, c)
        else :
            ans.append(c)

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

#print(ans)

print("".join(ans))

Submission status: Wrong Answer

Solution code:
import sys

S = input()

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

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

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

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
ans = ""

for i in range(q):
    l = list(map(str,input().split()))
    print(l)
    t = int(l[0])
    if t == 1:
        s.reverse()
    elif t == 2:
        f = int(l[1])
        c = str(l[2])
        if f == 1:
            s.insert(0,c)
        else:
            s.append(c)

print(''.join(s))

Submission status: Wrong Answer

Solution code:
S = list(input().split())
q = int(input())
for ind in range(q):
  cur_list = list(input().split())
  if len(cur_list) == 1:
    S.reverse()
  else:
    f = int(cur_list[1])
    c = cur_list[2]
    if f == 1:
      S.insert(0,c)
    elif f == 2:
      S.append(c)
print("".join(S))