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
import sys
input = sys.stdin.readline
s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        f = not f
    else:
        f_i = int(q[1])
        if f_i ^ f:
            ans.appendleft(q[2])
        else:
            ans.appendleft(q[2])
ans = "".join(list(ans))
if not f:
    ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
# D - String Formation
s = input()
q = int(input())
f = ''
b = ''
c = 0
for i in range(q):
    query = input().split()
    if query[0]=='1': c += 1 #t1
    else: #t2
        if c%2: #odd:reverse
            if query[1]=='1': #f1
                b += query[2]
            else: #f2
                f += query[2]
        else: #even
            if query[1]=='1': #f1
                f += query[2]
            else: #f2
                b += query[2]

if c%2: print(b + s[::-1] + f) #odd
else: print(f[::-1] + s + b) #even

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline
s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        f = not f
    else:
        f_i = int(q[1])
        if f_i ^ f:
            ans.appendleft(q[2])
        else:
            ans.appendleft(q[2])
ans = "".join(list(ans))
if not f:
  ans = ans[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

s = input()
q = int(input())
rev = 0
ans = collections.deque(['S'])

for _ in range(q):
  query = list(input().split())
  if int(query[0]) == 1:
    rev += 1
  else:
    if int(query[1]) == 1:      # 先頭
      if rev%2 == 0:
        ans.appendleft(query[2])
      else:
        ans.append(query[2])
    else:                       # 末尾
      if rev%2 == 0:
        ans.appendleft(query[2])
      else:
        ans.appendleft(query[2])

ans = ''.join(ans)
mes = ans.replace("S", s)
if rev%2 == 0:
  print(mes)
else:
  print(mes[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque

ans = deque()
ans.append(input())
q = int(input())

list_data = [ list(map(str,input().split(" "))) for i in range(q)]

#print(list_data)

for i in range(q):
    list_a = list_data[i]
    #print(list_a)
    if list_a[0] == "1":
        ans.reverse()
    elif list_a[0] == "2":
        if list_a[1] == "1":
            ans.appendleft(list_a[2])
        elif list_a[1] == "2":
            ans.append(list_a[2])

print("".join(ans))

Submission status: Wrong Answer

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

from collections import deque
d = deque(s)

vec = True
for _ in range(q):
    X = input().split()
    if X[0] == '1':
        vec = not vec
    else:
        if X[1] == '1' and not vec:
            d.appendleft(X[2])
        elif X[1] == '2' and not vec:
            d.append(X[2])
        elif X[1] == '1' and vec:
            d.append(X[2])
        elif X[1] == '2' and vec:
            d.appendleft(X[2])

if vec:
    d.reverse()

# print(d)
print(''.join(map(str, d)))
# print(*d, sep='')

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
c=0
l=[]
for i in range(Q):
  q=input()
  if q[0]==1:
    c+=1
  elif q[0]==2:
    if q[1]==1:
      if c%2==0:
        l.insert(0,q[2])
      else:
        l.append(q[2])
    elif q[1]==2:
      if c%2==0:
        l.append(q[2])
      else:
        l.insert(0,q[2])
if c%2==0:
  print(l)
else:
  print(l.reverse())

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
n = int(input())
s = deque(s)
cnt = 0
def ap(s,a,b):
    global cnt
    if flag ==True and (not cnt):
        if a == "1":
            s.appendleft(b)
        elif a == "2":
            s.append(b)
    if flag == False and cnt:
        if a == "1": 
            s.append(b)
        elif a == "2":
            s.appendleft(b)
flag = True
for i in range(n):
    L = list(input())
    if L[0] == "1":
        flag = False
        cnt ^=1
    elif L[0] == "2":
        ap(s,L[2],L[4])
        flag = True
#print(flag,cnt,s)
if cnt:
    s = reversed(s)
print("".join(s))

Submission status: Wrong Answer

Solution code:
s = input().split()
ans = s
ans_top=[]
s = input().split()
q = int(s[0])
count = 0
for i in range(q):
    s = input().split()
    if s[0] == '1':
        count += 1
    else:
        if s[1] == '1':
            if count%2==0:
                ans_top = list(s[2])+ans_top
            else:
                ans.append(s[2])
        else:
            if count % 2 == 0:
                ans.append(s[2])
            else:
                ans_top = list(s[2])+ans_top
ans=ans_top+ans
if count % 2 == 1:
    ans = ans[::-1]
print(''.join(ans))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
r = False#ひっくり返されたか
for i in range(q):
  t=list(input().split())
  if len(t) == 1:#一つしか入力されていない
    r = not(r)
  else:
    if (t[1] == '1' and r == False):
      s = t[2] + s
    elif (t[1] == '2' and r == True):
      s = t[2] + s
    else:
      s = s + t[2]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
queue = deque([s])
q = int(input())
pre = 0
reverse = 0
for _ in range(q):
    query = input()
    if query == '1':
        if pre:
            pre = 0
        else:
            pre = 1
        reverse += 1
    else:
        _, f, c = query.split()
        if f == '1':
            if pre == 0:
                # s = c + s
                queue.appendleft(c)
            else:
                # s = s + c
                queue.append(c)
        else:
            if pre == 1:
                # s = c + s
                queue.appendleft(c)
            else:
                # s = s + c
                queue.append(c)
if reverse % 2 == 1:
    queue = reversed(queue)
print(''.join(queue))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
queue = deque([s])
q = int(input())
pre = 0
reverse = 0
for _ in range(q):
    query = input()
    if query == '1':
        if pre:
            pre = 0
        else:
            pre = 1
        reverse += 1
    else:
        _, f, c = query.split()
        if f == '1':
            if pre == 0:
                queue.appendleft(c)
            else:
                queue.append(c)
        else:
            if pre == 1:
                queue.appendleft(c)
            else:
                queue.append(c)
if reverse % 2 == 1:
    queue = reversed(queue)
print(''.join(queue))

Submission status: Wrong Answer

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

from collections import deque
d = deque(s)

vec = True
for _ in range(q):
    X = input().split()
    if X[0] == '1':
        vec = not vec
    else:
        if X[1] == '1' and vec == 0:
            d.appendleft(X[2])
        elif X[1] == '2' and vec == 0:
            d.append(X[2])
        elif X[1] == '1' and vec == 1:
            d.append(X[2])
        elif X[1] == '2' and vec == 1:
            d.appendleft(X[2])

if vec == 1:
    d.reverse()

# print(''.join(map(str, s)))
print(*d, sep='')

Submission status: Wrong Answer

Solution code:
s = input().split()
ans = s
s = input().split()
q = int(s[0])
count = 0
for i in range(q):
    s = input().split()
    if s[0] == '1':
        count += 1
    else:
        if s[1] == '1':
            if count%2==0:
                ans = list(s[2])+ans
            else:
                ans.append(s[2])
        else:
            if count % 2 == 0:
                ans.append(s[2])
            else:
                ans = list(s[2])+ans
if count % 2 == 1:
    ans = ans[::-1]
print(''.join(ans))

Submission status: Wrong Answer

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


flip = 0
for _ in range(Q):
    q = input()
    if q == '1':
        flip += 1
    else:
        _, f, c = q.split(' ')
        if f == '1' and (flip % 2) == 0:
            S = [c] + S
        elif f == '1' and (flip % 2) != 0:
            S = S + [c]
        elif f != '1' and (flip % 2) == 0:
            S = S + [c]
        else:
            S = [c] + S

if (flip % 2) == 0:
    print(''.join(S))
else:
    print(''.join(S[::-1]))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = input()
n = int(input())
s = deque(s)
cnt = 0
def ap(s,a,b):
    if flag ==True:
        if a == "1":
            s.appendleft(b)
        elif a == "2":
            s.append(b)
    if flag == False:
        if a == "1": 
            s.append(b)
        elif a == "2":
            s.appendleft(b)
flag = True
for i in range(n):
    L = list(input())
    if L[0] == "1":
        flag = False
        cnt +=1
    elif L[0] == "2":
        ap(s,L[2],L[4])
        flag = True
#print(flag,cnt,s)
if cnt %2 == 1:
    s = reversed(s)
print("".join(s))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque(list(input()))

Q = int(input())

cnt = 0
for i in range(Q):
    l = list(map(str,input().split()))
    if int(l[0]) == 1:
        cnt += 1
    else:
        if int(l[1]) == 1:
            if cnt % 2 == 1:
                S.append(l[2])
            else:
                S.appendleft(l[2])
        else:
            if cnt % 2 == 1:
                S.appendleft(l[2])
            else:
                S.append(l[2])

print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = list(input())
N = int(input())
Q = [input().split() for i in range(N)]
r = 1
a1, a2 = [], []

for i in range(N):
  if int(Q[i][0]) == 1:
    r = r * -1
  else:
    if r == 1:
      if int(Q[i][1]) == 1:
        a1.insert(0, Q[i][2])
      else:
        a2.append(Q[i][2])
    else:
      if int(Q[i][1]) == 1:
        a2.insert(0, Q[i][2])
      else:
        a1.append(Q[i][2])

if r == 1:
  a1.extend(S)
  a1.extend(S)
  ans = "".join(a1)
else:
  S.reverse()
  a2.extend(S)
  a2.extend(a1)
  ans = "".join(a2)
  
print(ans)

Submission status: Wrong Answer

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

reverse = False
for i in range(Q):
    q = input()
    if q == "1":
        reverse = False if reverse else True
    else:
        t, f, c = q.split(" ")
        if f == "1":
            if reverse:
                s.append(c)
            else:
                s.appendleft(c)
        else:
            if reverse:
                s.appendleft(c)
            else:
                s.append(c)
ans = ''.join(s)
if reverse:
    print(ans[::-1])
else:
    print(ans)

Submission status: Wrong Answer

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


q = []
for n in range(Q):
  q.append(list(input().rstrip().split()))


d = deque()
d.append(S)
  
  
#print(q)
def rev(olist):
  nlist  = olist[::-1]
  return nlist

def addhead(olist,ele):
  nlist = olist.insert(0,ele)
  return nlist
  
def addtail(olist,ele):
  nlist = olist.append(ele)
  return nlist

def rev2(flg):
  if flg == 0:
    flg = 1
  else:
    flg = 0
  return flg

def add(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head.insert(0,ele)
  elif j == '1' and flg == 1:
    tail.append(ele)
  elif j == '2' and flg == 0:
    tail.append(ele)
  elif j == '2' and flg == 1:
    head.insert(0,ele)
  
def add2(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head = ele + head
  elif j == '1' and flg == 1:
    tail = tail + ele
  elif j == '2' and flg == 0:
    tail = tail + ele
  elif j == '2' and flg == 1:
    head = ele + head
  return head,tail

def add3(d,ele,j,flg):
  if j == '1' and flg == 0:
    d.appendleft(ele)
  elif j == '1' and flg == 1:
    d.append(ele)
  elif j == '2' and flg == 0:
    d.append(ele)
  elif j == '2' and flg == 1:
    d.appendleft(ele)
  #return d

flg = 0
#head = []
#tail = []
head=''
tail = ''
for n in range(Q):
  if q[n][0] == '1':
    flg = rev2(flg)

  else:
    #head,tail = add2(S,q[n][2],q[n][1],flg,head,tail)

    add3(d,q[n][2],q[n][1],flg)
ans = ''

if flg == 1:
  while d:
    ans = ans + d.pop()
else:
  while d:
    ans = ans + d.popleft()

print(ans)

Submission status: Wrong Answer

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

reverse = True
head = [""] * 2 * 10 **5
back = [""] * 2 * 10 ** 5
for _ in range(Q):
  q = list(input())
  if q[0] == "1":
    if reverse: reverse = False
    else: reverse = True
  elif q[0] == "2":
    if q[2] == "1":
      if reverse: head.insert(0, q[4])
      else: back.append(q[4])
    else:
      if reverse: back.append(q[4])
      else: head.insert(0, q[4])

if reverse: ans = head + S + back
else: ans = back[::-1] + S + head[::-1]

print("".join(ans))

Submission status: Wrong Answer

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


q = []
for n in range(Q):
  q.append(list(input().rstrip().split()))


d = deque()
d.append(S)
  
  
#print(q)
def rev(olist):
  nlist  = olist[::-1]
  return nlist

def addhead(olist,ele):
  nlist = olist.insert(0,ele)
  return nlist
  
def addtail(olist,ele):
  nlist = olist.append(ele)
  return nlist

def rev2(flg):
  if flg == 0:
    flg = 1
  else:
    flg = 0
  return flg

def add(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head.insert(0,ele)
  elif j == '1' and flg == 1:
    tail.append(ele)
  elif j == '2' and flg == 0:
    tail.append(ele)
  elif j == '2' and flg == 1:
    head.insert(0,ele)
  
def add2(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head = ele + head
  elif j == '1' and flg == 1:
    tail = tail + ele
  elif j == '2' and flg == 0:
    tail = tail + ele
  elif j == '2' and flg == 1:
    head = ele + head
  return head,tail

def add3(d,ele,j,flg):
  if j == '1' and flg == 0:
    d.appendleft(ele)
  elif j == '1' and flg == 1:
    d.append(ele)
  elif j == '2' and flg == 0:
    d.append(ele)
  elif j == '2' and flg == 1:
    d.appendleft(ele)
  return d

flg = 0
#head = []
#tail = []
head=''
tail = ''
for n in range(Q):
  
  if q[n][0] == '1':
    flg = rev2(flg)
  
  else:
    #head,tail = add2(S,q[n][2],q[n][1],flg,head,tail)
    d = add3(d,q[n][2],q[n][1],flg)
    
ans = ''


if flg == 1:
  while d:
    ans = ans + d.pop()
else:
  while d:
    ans = ans + d.popleft()

print(ans)

Submission status: Wrong Answer

Solution code:
s = input().split()
 
q=int(input())
front=[]
back=[]
hantei = True
for _ in range(q):
    t = input().split()
    if t[0] == '1':
        hantei = not hantei
    else:
        if hantei:
            if t[1] == '1':
                front.append(t[2])
            else:
                back.append(t[2])
        else:
            if t[1] == '2':
                front.append(t[2])
            else:
                back.append(t[2])

if hantei:
    front.reverse()
    ans = front + s + back 
else:
    back.reverse()
    s.reverse()
    ans = back + s + front
print(''.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
s = input().split()
 
q=int(input())
front=[]
back=[]
hantei = True
for _ in range(q):
    t = input().split()
    if t[0] == '1':
        hantei = not hantei
    else:
        if hantei:
            if t[1] == '1':
                front.append(t[2])
            else:
                back.append(t[2])
        else:
            if t[1] == '2':
                front.append(t[2])
            else:
                back.append(t[2])

if hantei:
    front.reverse()
    ans = front + s + back 
else:
    back.reverse()
    ans = back + s + front
print(''.join(map(str,ans)))

Submission status: Wrong Answer

Solution code:
s,_,*q=open(0)
a='%s'
p=1
for q in q:
  try:
    q,f,c=q.split()
    if(f<'2')^p:a+=c
    else:a=c+a
  except:p^=1
print(a%s[:-1][::p or-1])

Submission status: Wrong Answer

Solution code:
#D - String Formation
S = list(str(input()))
Q = int(input())
q = [input().split() for i in range(Q)]

#print(S)
#print(q)
tmp = 0
for i in range(Q):
    tmp1 = ""
    if q[i][0] == "1" and i == Q - 1 and tmp % 2 == 1:
        S = S[::-1]
    elif q[i][0] == '1':
        tmp += 1
       # tmp1 = S[0]
       # S[0] = S[-1]
       # S[-1] = tmp1
    else:
        if q[i][1] == "1":
            if tmp % 2 ==0:
                S.insert(0,q[i][2])
            else:
                S = S[::-1]
                S.insert(0, q[i][2])
        else:
            if tmp % 2 ==0:
                S.append(q[i][2])
            else:
                S = S[::-1]
                S.append(q[i][2])

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

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:
S = list(input())
Q = int(input())

# 文字列格納リスト
ans = S

# クエリ格納リスト
q_s = []

# for i in range(Q):
#   _q = list(map(str, input().split()))
#   if _q[0] == "2":
#     q_s.append(_q)
#   elif _q[0] == "1":
#     if len(q_s) > 0:
#       if q_s[-1][0] == "1":
#         del q_s[-1]
#       else:
#         q_s.append(_q)
#     elif len(q_s) == 0:
#       q_s.append(_q)
# 状態確認用の変数
# 1 = 順列
# -1 = 逆列
pos = 1
for j in range(Q):
  i = list(map(str, input().split()))
# for i in q_s:
  if i[0] == "1":
    # 反転クエリ時はフラグを反転
    pos = pos * -1
  elif i[0] == "2":
    if pos == 1 and i[1] == "1":
      # 順列時は前に追加
      ans.insert(0,i[2])
    elif pos == -1 and i[1] == "1":
      # 逆列時は後ろに追加
      ans.append(i[2])
    elif pos == 1 and i[1] == "2":
      # 順列時は後ろに追加
      ans.append(i[2])
    elif pos == -1 and i[1] == "2":
      # 逆列時は前に追加
      ans.insert(0,i[2])
if pos == -1:
  # クエリ消化時点で逆順なら文字列を反転
  ans.reverse()
print(ans)
print("".join(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if (flag == 1 and f == 1) or (flag == -1 and f == 2):
            d.appendleft(c)
        else:
            d.append(c)
d = list(d)
if flag == -1:
    d = d[::-1]

print(''.join(d))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if flag == 1 and f == 1 or flag == -1 and f == 2:
            d.appendleft(c)
        else:
            d.append(c)
d = list(d)
if flag == 1:
    d = d[::-1]

print(''.join(d))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if flag == 1 and f == 1 or flag == -1 and f == 2:
            d.appendleft(c)
        else:
            d.append(c)
d = list(d)
if flag == -1:
    d = d[::-1]

print(''.join(d))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = list(input())
Q = int(input())
d = deque(S)
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if flag == 1 and f == 1 or flag == -1 and f == 2:
            d.appendleft(c)
        else:
            S.append(c)

if flag == -1:
    S = S[::-1]

print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
flag = -1
for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        flag *= -1
    else:
        f = int(q[1])
        c = q[2]
        if flag == 1 and f == 1 or flag == -1 and f == 2:
            S.insert(0, c)
        else:
            S.append(c)

if flag == -1:
    S = S[::-1]

print(''.join(S))

Submission status: Wrong Answer

Solution code:
s,_,*q=open(0)
a=['',s[:-1]]
p=1
for q in q:
  if q>'1':
    f=1if q<'2 2'else 0
    a[f^p]+=q[-2]
  else:p^=1
print(a[p^1][::-1]+a[p])

Submission status: Wrong Answer

Solution code:
f=input
s=f()
n=int(f())
t=0
l,r='',''
for i in range(n):
  q=f()
  if q[0]=='1': t=1-t
  elif int(q[2])+t==2: r+=q[4]
  else: l+=q[4]
print([l[::-1]+s+r,r[::-1]+s+l][t])

Submission status: Wrong Answer

Solution code:
f=input
s=f()
n=int(f())
t=0
l,r='',''
for i in range(n):
  q=f()
  if q[0]=='1': t=1-t
  elif int(q[2])+t==2: r+=q[4]
  else: l+=q[4]
print(''.join([l[::-1]+s+r,r[::-1]+s+l][t]))

Submission status: Wrong Answer

Solution code:
S = input()
Slist = [S[i] for i in range(len(S))]
Q = int(input())
Query = [list(input().split()) for i in range(Q)]
Qlen = len(Query)
reverse = False
back = []
head = []

for idx, value in enumerate(Query):
    if value[0] == "1":
        reverse ^= True
        head, back = back, head
    else:
        if value[1] == "1":
            back = back + [value[2]]
        else:
            head = head + [value[2]]

if reverse == False:
    head.reverse()
    print("".join(head + Slist + back))
else:
    head.reverse()
    print("".join(head + Slist[::-1] + back))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
is_reversed = False
for _ in range(Q):
    q = input().split()
    if q[0] == "1":
        is_reversed = not is_reversed
    else:
        f = int(q[1])
        if is_reversed:
            f ^= 1
        c = q[2]
        if f:
            S.appendleft(c)
        else:
            S.append(c)
if is_reversed:
    S = reversed(S)
print(*S, sep="")

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
S = deque(S)
Q = INT()

for _ in range(Q):
	q = input().split()
	if len(q) == 1:  # 前後入れ替え
		if len(S) != 1:
			f, b = S.popleft(), S.pop()
			S.append(f)
			S.appendleft(b)
	else:  # 先頭 or 末尾に追加
		num, F, C = q
		if F == "1":
			S.appendleft(C)
		else:
			S.append(C)

print("".join(S))

Submission status: Wrong Answer

Solution code:
import math
str=[]
inputt=[]
str.insert(0,input())
num=int(input())
for i in range(0,num):
      inputt=list((x) for x in input().split(' '))
      if(len(inputt)==1):
          str=str[::-1]
      elif((len(inputt)==3)):
         a=int(inputt[0])
         b=int(inputt[1])
         c=inputt[2]
         if(a==1):
             str=''.join(reversed(c))
         elif(a==2):
            if(b==1):
               str.insert(0,c)
            elif(b==2):
               str.append(c)
print(''.join(str))

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
query = [0]*n
rev = 0
for i in range(n):
    query[i] = list(map(str, input().split()))
    if query[i][0] == '1':
        rev = rev + 1

begin = []
end = []
pal = 0

for i in range(n):
    if int(query[i][0]) == 1:
        pal = (pal+1)%2
    else:
        if pal == 0:
            if int(query[i][1]) == 1:
                begin.append(query[i][2])
            else:
                end.append(query[i][2])
        else:
            if int(query[i][1]) == 1:
                end.append(query[i][2])
            else:
                begin.append(query[i][2])
#    print('--------'+str(i+1)+'th try-----')
#    print(begin)
#    print(end)
#    print('pal:'+str(pal))
#print(s)

if rev%2 == 1:
    print(''.join(end)+s[::-1]+''.join(begin[::-1]))
else:
    print(''.join(begin[::-1])+s+''.join(end))

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
n = int(input())
query = [0]*n
rev = 0
for i in range(n):
    query[i] = list(map(str, input().split()))
    if query[i][0] == '1':
        rev = rev + 1

begin = deque()
end = deque()
pal = 0

for i in range(n):
    if int(query[i][0]) == 1:
        pal = (pal+1)%2
    else:
        if pal == 0:
            if int(query[i][1]) == 1:
                begin.appendleft(query[i][2])
            else:
                end.append(query[i][2])
        else:
            if int(query[i][1]) == 1:
                end.append(query[i][2])
            else:
                begin.appendleft(query[i][2])
#    print('--------'+str(i+1)+'th try-----')
#    print(begin)
#    print(end)
#    print('pal:'+str(pal))
print(s)

if rev%2 == 1:
    print(''.join(deque(reversed(end)))+s[::-1]+''.join(deque(reversed(begin))))
else:
    print(''.join(begin)+s+''.join(end))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
from collections import deque

def solve():
    d = deque(list(input().split()))
    Q = int(input())

    is_reverse = False # 反転中か？

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            _, f, c = query.split()

            if (f=='2') ^ is_reverse:
                d.append(c)
            else:
                d.appendleft(c)
            # if f == "1":
            #     if is_reverse:
            #         # 末尾にcを追加
            #         d.append(c)
            #     else:
            #         # 先頭にcを追加
            #         d.appendleft(c)
            # else:
            #     if is_reverse:
            #         # 先頭に追加
            #         d.appendleft(c)
            #     else:
            #         # 末尾に追加
            #         d.append(c)

    # 出力
    # if is_reverse:
    #     d.reverse()
    #     for moji in d:
    #         print(moji, end="")
    # else:
    #     for moji in d:
    #         print(moji, end="")
    # print()

    if is_reverse:
        print("".join(reversed(list(d))))
    else:
        print("".join(d))


def solve2():
    S = list(input().split())
    Q = int(input())
    M = 2*10**5+10
    data = [None]*M
    data.extend(S)
    data.extend([None]*M)
    left, right = M-1, M+len(S)

    is_reverse = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    data[right] = c
                    right += 1
                else:
                    # 先頭にcを追加
                    data[left] = c
                    left -= 1
            else:
                if is_reverse:
                    # 先頭に追加
                    data[left] = c
                    left -= 1
                else:
                    # 末尾に追加
                    data[right] = c
                    right += 1

    # 出力
    if is_reverse:
        for i in range(right-1, left, -1):
            print(data[i], end="")
    else:
        for i in range(left+1, right):
            print(data[i], end="")
    print()


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
from collections import deque

def solve():
    d = deque(list(input().split()))
    Q = int(input())

    is_reverse = False # 反転中か？

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    d.append(c)
                else:
                    # 先頭にcを追加
                    d.appendleft(c)
            else:
                if is_reverse:
                    # 先頭に追加
                    d.appendleft(c)
                else:
                    # 末尾に追加
                    d.append(c)

    # 出力
    # if is_reverse:
    #     d.reverse()
    #     for moji in d:
    #         print(moji, end="")
    # else:
    #     for moji in d:
    #         print(moji, end="")
    # print()

    if is_reverse:
        print("".join(reversed(list(d))))
    else:
        print("".join(d))


def solve2():
    S = list(input().split())
    Q = int(input())
    M = 2*10**5+10
    data = [None]*M
    data.extend(S)
    data.extend([None]*M)
    left, right = M-1, M+len(S)

    is_reverse = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_reverse = not is_reverse
        else:
            t, f, c = query.split()
            if f == "1":
                if is_reverse:
                    # 末尾にcを追加
                    data[right] = c
                    right += 1
                else:
                    # 先頭にcを追加
                    data[left] = c
                    left -= 1
            else:
                if is_reverse:
                    # 先頭に追加
                    data[left] = c
                    left -= 1
                else:
                    # 末尾に追加
                    data[right] = c
                    right += 1

    # 出力
    if is_reverse:
        for i in range(right-1, left, -1):
            print(data[i], end="")
    else:
        for i in range(left+1, right):
            print(data[i], end="")
    print()


if __name__ == "__main__":
    solve()