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()
q = int(input())
x, y = deque(), deque()
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        x, y = y, x
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            if len(x) > 1:
                if r % 2 == 0:
                    # x.appendleft(c)
                    x.append(c)
                else:
                    # x.append(c)
                    x.appendleft(c)
            else:
                if r % 2 == 0:
                    # x.append(c)
                    x.appendleft(c)
                else:
                    # x.appendleft(c)
                    x.append(c)

        elif f == "2":
            if len(y) > 1:
                if r % 2 == 0:
                    # y.append(c)
                    y.appendleft(c)
                else:
                    # y.appendleft(c)
                    y.append(c)
            else:
                if r % 2 == 0:
                    y.append(c)
                    # y.appendleft(c)
                else:
                    y.appendleft(c)
                    # y.append(c)

    # t = s
    # xx = x
    # yy = y
    # if r % 2 == 1:
    #     t = t[::-1]
    #     xx = reversed(x)
    #     yy = reversed(y)
    # print("".join(xx) + "+" + t + "+" + "".join(yy))


if r % 2 == 1:
    s = s[::-1]
    x = reversed(x)
    y = reversed(y)
print("".join(x) + s + "".join(y))

Submission status: Wrong Answer

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

if __name__ == '__main__':
	main()

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:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


s = input()

n = ii()
r = 0
head = ''
tail = ''
for i in range(n):
    q = input().split()

    if(q[0] == '1'):
        # reverse
        r += 1
    else:
        if(q[1] == '1'):
            if(r%2 ==0):
                # s = q[2] + s
                head = q[2] + head
            else:
                # s = s + q[2]
                tail = tail + q[2]
        else:
            if(r%2 ==0):
                # s = s + q[2]
                tail = tail + q[2]
            else:
                # s = q[2] + s
                head = q[2] + head

    # print('*********************',i,head,tail)
    

if(r%2==1):
    s = s[::-1]
    # print('rev')
    print( tail[::-1] + s[::-1]+ head[::-1])
else:

    print(head +  s + tail)

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
ans = ""
flag = False
cnt = 0

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

if cnt % 2 != 0:
    s.reverse()

print(''.join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque

s = deque((str(input())).split())
loop = int(input())

for i in range(loop):
    queries = input().split()
    if len(queries) == 1:
        s.reverse()
    else:
        type = int(queries[1])
        c = str(queries[2])
        if type == 1:
            s.appendleft(c)
        else:
            s.append(c)
print("".join(list(s)))

Submission status: Wrong Answer

Solution code:
S = input().split(" ")
Q = int(input())

for i in range(0, Q):
  inp = input().split(" ")
  
  if inp[0] == "2":
    if inp[1] == "1":
      S.insert(0, inp[2])
    elif inp[1] == "2":
      S.append(inp[2])
  
  elif inp[0] == "1":
    S = S[::-1]
  
  # print(S)
      
print("".join(S))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
flag = True
ans = [s]

for i in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    flag = not flag
  else:
    if int(query[1]) == 1:
      if flag:
        ans.insert(0, query[2])
      else:
        ans.append(query[2])
    else:
      if flag:
        ans.append(query[2])
      else:
        ans.insert(0, query[2])

if flag:
  print("".join(ans))
else:
  ans.reverse()
  print("".join(ans))

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:
        if len(list(d)) == 1:
            pass
        else:
            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:
    print(i, end="")

Submission status: Wrong Answer

Solution code:
# a, b = [int(x) for x in input().split()]
s = input()
nq = int(input())
qs = []
num_char = 1
for _ in range(nq):
    q = input()
    if q[0] == "2":
        num_char += 1
    qs.append(q)

reverse_num = 0

idx_prepend = 0
idx_append = num_char - 1

out = [s] * num_char
for q in reversed(qs):
    if q[0] == "1":
        reverse_num += 1
    else:
        if reverse_num % 2 == 0:
            # same as query
            if q[2] == "1":
                out[idx_prepend] = q[4]
                idx_prepend += 1
            else:
                out[idx_append] = q[4]
                idx_append -= 1
        else:
            # reverse position of query
            if q[2] == "1":
                out[idx_append] = q[4]
                idx_append -= 1
            else:
                out[idx_prepend] = q[4]
                idx_prepend += 1
print("".join(out))

Submission status: Wrong Answer

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  s=input()
  q=int(input())
  count=0
  for _ in [0]*q:
    a=input().split()
    if len(a)==1:
      count+=1
    else:
      if count%2==0:
          s=a[2]+s if a[1]=='1' else s+a[2]
      else:
          s=s+a[2] if a[1]=='1' else a[2]+s
  if count%2==1:
    s=s[::-1]
  print(s)
if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
from sys import stdin

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(Q[0]):
  s=input().split()

  if len(s)==1:
      
      S=S[::-1]
      
  else:
      if int(s[1])==1:
          S.insert(0,s[2])
          

      else:
          S.append(s[2])
for i in range(len(S)):
  print(S[i],end="")

Submission status: Wrong Answer

Solution code:
array = []
array.append(input())

Q = int(input())

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

    if query[0]=="1":
        array.reverse()
    else:
        if query[1]=="1":
            array.insert(0, query[2])
        else:
            array.append(query[2])

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

Submission status: Wrong Answer

Solution code:
from collections import deque

S  = deque()
S.append(input().strip())

N = int(input().strip())

order = 1
for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        order *= -1
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        if order == 1:
            S.appendleft(c)
        else:
            S.append(c)
    else:
        if order == -1:
            S.appendleft(c)
        else:
            S.append(c)
if order == 1:
    msg = ''.join(list(S))
    print(msg)
else:
    msg = ''.join(reversed(list(S)))
    print(msg)

Submission status: Wrong Answer

Solution code:
import bisect
s=input().split()
a=int(input())
for i in range(a):
  n=input().split()
  if(n[0]=="1"):
    s =s[::-1]
  else:
    if(n[1]=="1"):
      s.insert(0,n[2])
    else:
      s.append(n[2])
s=''.join(s)
print(s)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
Query=[list(input().split()) for i in range(Q)]
revcnt = 0
rFlg = False
for i in Query:
    if int(i[0]) == 1:
        revcnt += 1
        rFlg = not(rFlg)
    elif int(i[0]) == 2:
        if int(i[1]) == 1:
            if rFlg:
                tmp = [S,i[2]]
                S = ''.join(tmp)
                rFlg = False
            else:
                tmp = [i[2],S]
                S = ''.join(tmp)
        elif int(i[1]) == 2:
            if rFlg:
                tmp = [i[2],S]
                S = ''.join(tmp)
                rFlg = False
            else:
                tmp = [S,i[2]]
                S = ''.join(tmp)

if revcnt % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ans = S
## vector が1なら正方向、-1なら逆方向
vector = 1
before = ""
after = ""
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        vector *= -1
    elif vector == 1 and Query[0:3] == "2 1":
        before += Query[4:]
    elif vector == 1 and Query[0:3] == "2 2":
        after = Query[4:]+after
    elif vector == -1 and Query[0:3] == "2 1":
        after += Query[4:]
    elif vector == -1 and Query[0:3] == "2 2":
        before = Query[4:] + before
#     print(before)
#     print(after)
if vector==1:
    print(before + S + after)
else:
    print(after[::-1]+S+before[::-1])

Submission status: Wrong Answer

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

if count%2 == 0:
    print(''.join(list(S)))
else:
    list(S).reverse()
    print(''.join(S))

Submission status: Wrong Answer

Solution code:
# 先頭と末尾を保ちつつ
from collections import deque

s = input()
q = int(input())
x, y = deque(), deque()
r = 0
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        r += 1
        x, y = y, x
    elif query[0] == "2":
        f, c = query[1], query[2]
        if f == "1":
            if len(x) > 1:
                if r % 2 == 0:
                    x.appendleft(c)
                else:
                    x.append(c)
            else:
                if r % 2 == 0:
                    x.append(c)
                else:
                    x.appendleft(c)

        elif f == "2":
            if len(y) > 1:
                if r % 2 == 0:
                    # y.append(c)
                    y.appendleft(c)
                else:
                    # y.appendleft(c)
                    y.append(c)
            else:
                if r % 2 == 0:
                    y.append(c)
                    # y.appendleft(c)
                else:
                    y.appendleft(c)
                    # y.append(c)

    # t = s
    # xx = x
    # yy = y
    # if r % 2 == 1:
    #     t = t[::-1]
    #     xx = reversed(x)
    #     yy = reversed(y)
    # print("".join(xx) + "+" + t + "+" + "".join(yy))


if r % 2 == 1:
    s = s[::-1]
    x = reversed(x)
    y = reversed(y)
print("".join(x) + s + "".join(y))

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) > 3:
			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 = ""
			for i,val in enumerate(save):
				if i == 0:
					tmp = val
					continue
				tmp = val[0] + tmp + val[-1]
			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 = ""
			for i,val in enumerate(save):
				if i == 0:
					tmp = val
					continue
				tmp = val[0] + tmp + val[-1]
			#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:
s=input()
q = int(input())
toggle = "ok"
top = ""
last = ""

for i in range(q):
    si = input().split()
    if si[0] == "1":
        if toggle == "ok":
            toggle = "re"
        else:
            toggle = "ok"
    else:
        if si[1] == "1" and toggle == "ok":
            top = top + si[2] 
        elif si[1] == "2" and toggle == "re":
            top = top + si[2] 
        elif si[1] == "1" and toggle == "re":
            last = last + si[2]
        else:
            last = last + si[2]
if toggle == "ok":
    print(top[::-1]+s+last)
else:
    print(last[::-1]+s+top)

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
r_b = []
r_f = []
for i in range(N):
    if Q[i][0] == '1' :
        reverse = (reverse + 1) % 2
    else :
        if reverse == 0 :
            if Q[i][1] == '1' :
                r_f = [Q[i][2]] + r_f
            else :
                r_b.append(Q[i][2])
        else:
            if Q[i][1] == '1':
                r_b.append(Q[i][2])
            else:
                r_f = [Q[i][2]] + r_f

print(1)

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:
        s[0], s[-1] = s[-1], s[0]
    else:
        f, c = int(arr[1]), arr[2]
        if f == 1:
            s.insert(0, c)
        else:
            s.append(c)
    i += 1

text = "".join(s)
print(text)

Submission status: Wrong Answer

Solution code:
s = input()
sc = s[::-1]
n = int(input())
l = [list(input().split()) for i in range(n)]

last = 1

for i in range(n):
    if l[i][0] == '1':
        last = last * -1
        
if last > 0:
    print(s)
else:
    print(sc)

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:
import sys
if __name__ == '__main__':
    lines = sys.stdin.readlines()
    line = [a.strip('\n').split(' ') for a in lines]
    # print(line)
    right = []
    left = []
    count = 0

    for i,v in enumerate(line):
        if i == 0:
            st = v
        if i == 1:
            q = v
        if i >= 2:
            if len(v)==3:
                for j,k in enumerate(v):
                    if j == 1:
                        ichi = int(k)
                    if j==2:
                        alp = k
                if count%2 == 0:
                    if ichi == 1:
                        left.append(alp)
                        # print('left'+str(left))
                    if ichi == 2:
                        right.append(alp)
                        # print('right'+str(right))
                else:
                    if ichi == 1:
                        right.append(alp)
                        # print('right'+str(right))
                    if ichi == 2:
                        left.append(alp)
                        # print('left' + str(left))

            if len(v)==1:
                count += 1
    
    # print(left)
    # print(right)
    if count%2 == 0:
        d = list(reversed(left))+st+right
    if count%2 == 1:
        d = list(reversed(right))+st+left
    print(''.join(d))

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    s = ns()
    Q = ni()

    T1 = 0
    FrontS = ""
    BackS = ""
    for _ in range(Q):
        T = stdin.readline().split()
        if T[0] == "1":
            T1 += 1
        else:
            if T1 % 2 == 0:
                # これは元通り
                if T[1] == "1":
                    FrontS = T[2] + FrontS
                else:
                    BackS += T[2]
            else:
                # これは逆向き
                if T[1] == "1":
                    BackS = T[2] + BackS
                else:
                    FrontS = T[2] + FrontS
    s = FrontS + s + BackS
    if T1 % 2 == 0:
        print(s)
    else:
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=list(input())
n=int(input())

reverse_check=0
reverse_check_sum=0

for i in range(n):
    l=list(map(str,input().split()))
    if int(l[0])==1:
        reverse_check_sum+=1
        if reverse_check==1:
          reverse_check=0
        else:
          reverse_check=1
          
    elif int(l[0])==2:
        if reverse_check==1:
          if int(l[1])==1:
            s.insert(0, l[2])
          if int(l[1])==2:
            s.append(l[2])
        else:
          if int(l[1])==2:
            s.insert(0, l[2])
          if int(l[1])==1:
            s.append(l[2])
            
if reverse_check_sum%2==0:
	s=list(reversed(s))

print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque([str for str in input()])

dic = 1
Q = int(input())
for i in range(0,Q):
    Query = input()
    if Query == '1':
        dic = dic*(-1)
    else:
        T, F, C = Query.split()
        if (F*dic == 2) or (F*dic == -1):
            S.append(C)
        else:
            S.appendleft(C)
print(''.join(list(S)[::dic]))

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())
r = False
for i in range(n):
    a = input().split()
    # print(a)
    if len(a) == 1:
        if r:
           r = False
        else:
            r = True
    elif r:
        if a[1] == "1":
            s = a[2] + s
            # print(a[2])
        else:
            s = s + a[2]
    else:
        if a[1] == "1":
            s = s + a[2]
            # print(a[2])
        else:
            s = a[2] + s
# print(r)
if not r:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
for i in range(q):
    x = list(map(str, input().split()))
    if x[0] == '1':
        s = s[-1] + s[1:-1] + s[0]
    elif x[0] == '2':
        if x[1] == '1':
            s = x[2] + s
        else:
            s = s + x[2]

print(s)

Submission status: Wrong Answer

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

S = input()
Q = int(input())
f = False
for i in range(Q):
    QUERY = input()
    if QUERY[0] == "1":
        T = QUERY
    else:
        T, F, C = QUERY.split()

    if T == "1":
        if f == True:
            f = False
        else:
            f = True
    elif f == False:
        if F == "1":
            S = C + S
        elif int(F) > len(S):
            S = S + C
        else:
            S = S[:int(F)-1] + C + S[int(F)-1:]
    else:
        if F == "1":
            S = S + C
        elif int(F) > len(S):
            S = C + S
        else:
            S = S[:-int(F)-1] + C + S[-int(F)-1:]
if f:
    STR = ''.join(list(reversed(S)))
    print(STR)
else:
    print(S)

Submission status: Wrong Answer

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

f = True
l, r = [], []
for i in range(q):
    if query[i][0] == "1":
        f = not f
    else:
        if f:
            if query[i][1] == "1":
                l.append(query[i][2])
            else:
                r.append(query[i][2])
        else:
            if query[i][1] == "1":
                r.append(query[i][2])
            else:
                l.append(query[i][2])

if f:
    print("".join(l[::-1]) + s + "".join(r))
else:
    print("".join(r[::-1]) + s + "".join(l))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ans = S
## vector が1なら正方向、-1なら逆方向
vector = 1
before = ""
after = ""
for i in range(Q):
    Query = input()
    if Query[0] == "1":
        vector *= -1
    elif vector == 1 and Query[0:3] == "2 1":
        before += Query[4]
    elif vector == 1 and Query[0:3] == "2 2":
        after = Query[4]+after
    elif vector == -1 and Query[0:3] == "2 1":
        after += Query[4]
    elif vector == -1 and Query[0:3] == "2 2":
        before = Query[4] + before
#     print(before)
#     print(after)
if vector==1:
    print(before + S + after)
else:
    print(after[::-1]+S+before[::-1])

Submission status: Wrong Answer

Solution code:
s = list(str(input()))
q = int(input())
u = [list(map(str, input().split())) for _ in range(q)]
for i in range(q):
    if u[i][0] == '1':
        s[0], s[len(s) - 1] = s[len(s) - 1], s[0]
    if u[i][0] == '2' and u[i][1] == '1':
        s.insert(0, u[i][2])
    if u[i][0] == '2' and u[i][1] == '2':
        s.append(u[i][2])
print(''.join(s))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
s = input()
q = int(input())
ans = ""
for _ in range(q):
  t = input().split()
  if t[0] == '1':
    s = tuple(s)
    s=str(s[::-1])
  else:
    if t[1] == '1':
      s = (str(t[2]) +str(s))
    else:
      s = (str(s) + str(t[2]))
print(str(s))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Query = []
flag = 1
sum_re = 0
for i in range(Q):
    q = input().split(" ")
    q = [int(a) if i == 0 or i == 1 else a for i, a in enumerate(q)]
    if len(q) == 1:
        flag *= -1
        sum_re += 1
    else:
        if q[1] == 1 and flag == 1:
            S = q[2] + S
        elif q[1] == 1 and flag == -1:
            S = S + q[2]
        elif q[1] == 2 and flag == 1:
            S = S + q[2]
        elif q[1] == 2 and flag == -1:
            S = q[2] + S
        flag = 1

if sum_re % 2 == 1:
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

Solution code:
def string_formation(s, q_list):
    s = [x for x in s]
    for query in q_list:
        if query[0] == 1:
            a = s[0]
            s[0] = s[-1]
            s[-1] = a
        else:
            if query[1] == 1:
                s = [query[2]] + s
            else:
                s = s + [query[2]]
    print(''.join(s))
    
s = input()
q = int(input())
q_list = []
for _ in range(q):
    tmp = input().split()
    if len(tmp)==1:
        tmp[0] = int(tmp[0])
        q_list.append(tmp)
    else:
        tmp[0] = int(tmp[0])
        tmp[1] = int(tmp[1])
        q_list.append(tmp)

string_formation(s, q_list)

Submission status: Wrong Answer

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

for i in range(Q):
    q =  [ i for i in input().split(' ')]
    if q[0] == '1':
        if len(S) != 1:
            head = S[0]
            tail = S[-1]
            S = tail + S[1:-1] + head
    elif q[1] == '1':
        S = q[2] + S
    else:
        S = S + q[2]
print(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):
    #print(i,len(d))
    if (len(query[i])==1):
        if len(d)>1:
            flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if (flag==0)or(len(d)==1):
            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())

ans = [" " for n in range(2*10**5 + 1)]
ans[10**5] = s
frontIndex = 10**5 - 1
endIndex = 10**5 + 1
isReverse = -1

for i in range(q):
    query = input().split()
    if(query[0] == "1"):
        isReverse *= -1
    else:
        if(query[1] == "1"):
            if(isReverse == 1):
                ans[endIndex] = query[2]  
                endIndex += 1
            else:
                ans[frontIndex] = query[2]
                frontIndex -= 1
        else:
            if(isReverse == 1):
                ans[frontIndex] = query[2] 
                frontIndex -= 1
            else:
                ans[endIndex] = query[2]
                endIndex += 1
ans = "".join(ans).strip()
if(isReverse == -1):
    print(ans)
else:
    print(ans[::-1])