Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Wrong Answer

Solution code:
s = list(input())
q = int(input())
s0 = []
s1 = []
for _ in range(q):
    ql = list(input().split())
    if ql[0] == '1':
        s0, s1 = s1[::-1], s0[::-1]
    else:
        if ql[1] == '1':
            s0 += ql[2]
        else:
            s1 += ql[2]

ans = s0 + s + s1
print(*ans, sep='')

Submission status: Wrong Answer

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random

# List of ints
def LI(): return list(map(int, sys.stdin.readline().split()))

# Single int
def I(): return int(sys.stdin.readline())

# List, which contains Lists of chars (not strings)
def LS():return list(map(list, sys.stdin.readline().split()))

# Single List of chars (not strings)
def S(): return list(sys.stdin.readline())[:-1]

# List of "n" integers from "n" lines.
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
# List of "n" Lists of integers
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l


# Runtime Complexity: O(?)
# Space Complexity: O(?)
# Solution
s = sys.stdin.readline().split()
lines_count = I()

chars = deque(list(s))

def prepend(chars, ch):
    chars.appendleft(ch)

def append(chars, ch):
    chars.append(ch)

actions = {
    1: prepend,
    2: append
}

for i in range(lines_count):
    line = LS()

    if len(line) == 1:
        actions[1], actions[2] = actions[2], actions[1]
    else:
        if line[1][0] == '1':
            actions[1](chars, line[2][0])
        else:
            actions[2](chars, line[2][0])

chars = list(chars)
if len(chars[0]) > 1:
    chars = list(chars[0])

if actions[1] == append:
    chars.reverse()

print(''.join(list(chars)))

Submission status: Wrong Answer

Solution code:
def main():
    s = list(input().split())
    q = int(input())
    for _ in range(q):
        qu = input()
        if qu == '1':
            s.reverse()
        else:
            if qu[2] == '1':
                s.insert(0, qu[4])
            else:
                s.append(qu[4])

    print("".join(s))
main()

Submission status: Wrong Answer

Solution code:
from itertools import chain

def swap(head, tail):
    return (tail, head)

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

head = []
tail = []

for _i in range(q):
    query = input()
    if query == "1":
        head, tail = swap(head, tail)
    else:
        _n, f, c = tuple(query.split())
        if f == "1":
            head.append(c)
        else:
            tail.append(c)

print("".join(tuple(chain(reversed(head), s, tail))))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
arr = [list(input().split()) for i in range(q)]
count1 = 0
for i in range(q):
    if arr[i][0] == '1':
        count1 += 1
        if i != q-1 and arr[i+1][0] == '2':
            if arr[i+1][1] == '1':
                arr[i+1][1] = '2'
            else:
                arr[i+1][1] = '1'
    else:
        if arr[i][1] == '1':
            s = arr[i][2] + s
        else:
            s = s + arr[i][2]
if count1%2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

S=raw_input()
Q=input()
Query=[ sys.stdin.readline().split() for _ in range(Q) ]

s1=deque()
s2=deque()
judge={}
judge["front"]="s1"
judge["end"]="s2"

cnt=0
for x in Query:
    if x[0]=="1":
        n=x[0]
        cnt+=1
    elif x[0]=="2":
        n,f,c=x[0],x[1],x[2]
    if n=="1":
        judge["front"],judge["end"]=judge["end"],judge["front"]
    elif n=="2":
        if f=="1":
            if judge["front"]=="s1":
                s1.appendleft(c)
            elif judge["front"]=="s2":
                s2.appendleft(c)
        elif f=="2":
            if judge["end"]=="s1":
                s1.append(c)
            if judge["end"]=="s2":
                s2.append(c)
str1=""
for x in s1:
    str1+=x
str2=""
for x in s2:
    str2+=x

if cnt%2==0:
    print str1[::-1]+S+str2
elif cnt%2==1:
    print str2[::-1]+S[::-1]+str1

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
def asd(s,m):
    if len(m)==1:
        return s[::-1]
    else:
        if m[1]=="2":
            return s+m[2]
        else:
            return m[2]+s
for i in range(n):
    k= input().split()
    asd(s,k)
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

s = input()
n = int(input())

for i in range(n):
    Q = input()
    if int(Q[0]) == 2:
        F = int(Q[2])
        C = Q[4]
        print(F, C)
        if F == 1:
            s = C + s
        else:
            s = s + C
    else:
        s = s[::-1]
    
print(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
    

if(r%2==1):
    s = s[::-1]

print(head +  s + tail)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
qlst = []
for i in range(Q):
    qlst.append(input().split())
ans = [S]
rtmp = 0
for i in range(Q):
    if qlst[i][0] == "1":
        rtmp += 1
    else:
        if rtmp % 2 == 0:
            if qlst[i][1] == '1':
                ans.insert(0, qlst[i][2])
            else:
                ans.append(qlst[i][2])
        else:
            if qlst[i][1] == '2':
                ans.insert(0, qlst[i][2])
            else:
                ans.append(qlst[i][2])

if rtmp % 2 != 0:
    for i in reversed(ans):
        print(i, end='')
else:
    for i in ans:
        print(i, end='')

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
length = len(s)
que = [[''] * 3 for i in range(q)]
for i in range(q):
    c = input()
    if c[0] == '1':
        que[i][0] = 1
    else:
        n, f, c = map(str, c.split())
        que[i][0], que[i][1], que[i][2] = int(n), int(f), c

rev_cnt = 0
left = [''] * 200000
right = [''] * 200000
left_cnt = 0
right_cnt = 0
for com in que:
    if com[0] == 1:
        rev_cnt += 1
        left, right = right, left
        left_cnt, right_cnt = right_cnt, left_cnt
    else:
        if com[1] == 1:
            left[left_cnt] = com[2]
            left_cnt += 1
        else:
            right[right_cnt] = com[2]
            right_cnt += 1

l = list(reversed(left[0:left_cnt]))
ans = l + list(s) + right[0:right_cnt]
for c in ans:
    print(c, end='')
print()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
S = list(input())
n = int(input())
s = []
for i in range(n):
    array = list(map(str,input().split()))
    s.append(array)
for i in range(n):
    if s[i][0] == "1":
        S.reverse()
    else:
        if s[i][1] == "1":
            S.insert(0,s[i][2])
        else:
            S.append(s[i][2])

print("".join(S))

Submission status: Wrong Answer

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

def sol():
    s=input().rstrip()
    q=int(input())
    x=0
    for i in range(q):
        l=[i for i in input().split()]
        print(l)
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: s=l[2]+s
            else: s=s+l[2]
        elif l[1]=="2":
            if x==0: s=s+l[2]
            else: s=l[2]+s
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque(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:
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):
    org_S.append(C)
    rev_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:
import sys
from collections import deque

readline = sys.stdin.readline
s = readline().strip()
n = int(readline())
q = [sys.stdin.readline().split() for _ in range(n)]
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[2]
        if qi[1] == '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:
s=[input()]
q=int(input())
for i in range(q):
  aa=list(map(str,input().split()))
  if len(aa)==1:
    s.reverse()
  else:
    if aa[1]=="1":
      s.insert(0,aa[2])
    else:
      s.append(aa[2])
print(*s,sep='')

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]
        elif si[1] == "2" and toggle == "ok":
            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()]
q = int(input())
query = []
for _ in range(q):
  query.append(input().split())

for i in query:
  if len(i) == 1:
    s.reverse()
  else:
    _, f, c = i
    if f == "1":
      s.reverse()
      s.append(c)
      s.reverse()
    else:
      s.append(c)
print("".join(s))

Submission status: Wrong Answer

Solution code:
#!/bin/bash
''':'
docker run --rm -it -v $(cd $(dirname $0) && pwd):/vo:ro pypy:3-2.4.0 \
   /bin/bash -c "pypy3 /vo/$(basename $0) < /vo/g.inp"
exit $?
'''
import sys


def _s(): return sys.stdin.readline().strip()


def _sa(): return sys.stdin.readline().strip().split()


def _i(): return int(sys.stdin.readline().strip())


def _ia(): return [int(x) for x in sys.stdin.readline().strip().split()]


def main():
    s = [_s()]
    q = _i()
    f = [0] * q
    flag = True
    for _ in range(q):
        tmp = _sa()
        if tmp[0] == "1":
            if len(s[-1]) > 0:
                s.append("")
            f[len(s) - 2] += 1
        else:
            if tmp[1] == "1":
                s[-1] = tmp[2] + s[-1]
            else:
                s[-1] += tmp[2]
    ns = len(s)
    o = [i for i in range(ns)]
    for i in range(1, ns):
        m = f[i] % 2
        if m == 1:
            for j in range(i + 1):
                o[j] = i - o[j]
        for j in range(i):
            f[j] += f[i]
    #
    ans = ""
    for i, _ in sorted(enumerate(o), key=lambda x: x[1]):
        if f[i] % 2 == 0:
            ans += s[i]
        else:
            ans += s[i][::-1]
    return ans

if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
S = str(input())
Q = int(input())
ans = S
mode = 1
for _ in range(Q):
  q_in = str(input())
  if q_in == '1':
    mode *= -1
    continue
  else:
    t, f_in, c = map(str, q_in.split())
    f = int(f_in)
    if mode == -1:
      if f_in == 1:
        f = 2
      else:
        f = 1
    if f == 1:
      ans = c + ans
    else:
      ans = ans + c

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

print(ans)

Submission status: Wrong Answer

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

direction = 1  # 操作を始める前の向きを1、逆を-1とする
output_string = s
string1 = ''
string2 = ''

change_direction_count = 0
for query in q_datas :
    if query[0] == '1':
        direction *= -1
        change_direction_count += 1
    else:
        if query[1] == '1' and direction == 1:
            string1 = query[2] + string1
        elif  query[1] == '1' and direction == -1:
            string2 = string2 + query[2]
        elif  query[1] == '2' and direction == 1:
            string2 = string2 + query[2]
        else: # query[1] == '2' and direction == -1
            string1 = query[2] + string1

if change_direction_count % 2 == 0:
    print(string1 + s + string2)
else:
    print(string2[::-1] + s + string1[::-1])

Submission status: Wrong Answer

Solution code:
from collections import deque
s=deque(list(input().split()))
ans=""
q=int(input())
switch=0
for i in range(q):
  ques=list(input().split())
  if ques[0]=="1":
    switch+=1
    #print("1",s)
  else:
    if ques[1]=="1":
      if switch%2==0:
        s.appendleft(ques[2])
        #print("2",s)
      else:
        s.append(ques[2])
    else:
      if switch%2==0:
        s.append(ques[2])
      else:
        s.appendleft(ques[2])
      #print("22",s)
t=list(s)
if switch%2==1:
  for i in t[::-1]:
    ans+=i
else:
  for i in s:
    ans+=i
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
s=input()
q=int(input())
qv=[list(input().split()) for _ in range(q)]
cnt=0
ans=collections.deque()
ans.append(s)
for i in range(q):
  if qv[i][0]=="1":
    cnt+=1
  else:
    t,f,c = qv[i]
    if f=="1":
      if cnt%2==0:
        ans.appendleft(c)
      else:
        ans.append(c)
    elif f=="2":
      if cnt%2==0:
        ans.append(c)
      else:
        ans.appendleft(c)
if cnt%2!=0:
  ans.reverse()
print("".join(ans))

Submission status: Wrong Answer

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

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

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

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

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

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

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

        first = False

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



main()

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    S = S[::-1]
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      S = [C] + S
    else:  # F == '2'
      S += [C]
print(S)
print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    q = input()
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[2] == "1":
            S = S + q[4]
        else:
            S = q[4] + S
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

S = input()
Q = int(input())
Q_list = [input().split() for i in range(Q)]

Ans_list1 = deque([S])
Ans_list2 = deque([S])

sn = 0

for i in range(Q):
    if Q_list[i][0] == "1":
        sn += 1
    else:
        if (sn % 2 == 0 and Q_list[i][1] == "1") or (sn % 2 == 1 and Q_list[i][1] == "2"):
            Ans_list1.appendleft(Q_list[i][2])
            Ans_list2.append(Q_list[i][2])
        else:
            Ans_list1.append(Q_list[i][2])
            Ans_list2.appendleft(Q_list[i][2])


if sn % 2 == 0:
    print(''.join(Ans_list1))
else:
    print(''.join(Ans_list2))

Submission status: Wrong Answer

Solution code:
from collections import deque


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

for i in range(q):
    query = input()
    if query == '1':
        if len(d) > 1:
            tmpHead = d[0]
            d[0] = d[-1]
            d[-1] = tmpHead
    else:
        s_lis = query.split()
        n = int(s_lis[0])
        f = int(s_lis[1])
        c = s_lis[2]
        if f == 1:
            d.appendleft(c)
        else:
            d.append(c)

print("".join(d))

Submission status: Wrong Answer

Solution code:
# ABC158 D
s=input()
q=int(input())
l=[]

for i in range(q):
    l.append(list(map(str,input().split())))
# print(l)
t_str=s
gyaku_fig=False
for ll in l:
    if ll[0]=='1':
        if gyaku_fig==False:
            gyaku_fig=True
        elif gyaku_fig==True:
            gyaku_fig=False
    elif ll[0]=='2':
        if gyaku_fig==True:
            if ll[1]=='1':
                t_str=ll[2]+t_str
            elif ll[1]=='2':
                t_str+=ll[2]
        elif gyaku_fig==False:
            if ll[1]=='2':
                t_str=ll[2]+t_str
            elif ll[1]=='1':
                t_str+=ll[2]        
if gyaku_fig==True:
    print(t_str)
else:
    print(t_str[::-1])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
s = list(map(str, input().split()))
n = int(input())
mae_list = []
usiro_list = []
flag = 1
for i in range(0, n):
    q_list = list(map(str, input().split()))
    if len(q_list) == 1:
        flag *= -1
    else:
        if (q_list[1] == "1" and flag == 1) or (q_list[1] == "2" and flag == -1):
            mae_list.append(q_list[2])
        else:
            usiro_list.append(q_list[2])
if flag == 1:
    if len(mae_list) > 0:
        mae_list.reverse()
        print(''.join(mae_list), end="")
    print(''.join(s), end="")
    if len(usiro_list) > 0:
        print(''.join(usiro_list), end="")
else:
    s.reverse()
    if len(usiro_list) > 0:
        usiro_list.reverse()
        print(''.join(usiro_list), end="")
    print(''.join(s), end="")
    if len(mae_list) > 0:
        print(''.join(mae_list), end="")
print()

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

if reverse == 0 :
    S = r_f + S + r_b
    print("".join(S))
else:
    S.reverse()
    r_b.reverse()
    r_f.reverse()
    S = r_b + S + r_f
    print("".join(S))

Submission status: Wrong Answer

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

s = input().split()[0]
q = int(input())

left, right = [], []
state = 1
for _ in range(q):
    q_i = input().split()
    if len(q_i) == 1:
        state = (state)%2 + 1
    else:
        _, f, c = q_i
        f = int(f)
        if (f+state)%2 == 0:
            left += [c]
        else:
            right += [c]
if state == 1:
    print(''.join(left[::-1]) + s + ''.join(right))
else:
    print(''.join(right[::-1]) + s + ''.join(left))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

S = read_str()
Q = read_int()[0]
Query = [list(read_str().split()) for _ in range(Q)]

d = deque([S])
rd = deque([S])
def solve():
    reverse = False
    for q in Query:
        if q[0] == '1':
            reverse = not reverse
        else:
            if reverse:
                if q[1] == '1':
                    rd.appendleft(q[2])
                    d.append(q[2])
                else:
                    d.appendleft(q[2])
                    rd.append(q[2])
            else:
                if q[1] == '1':
                    d.appendleft(q[2])
                    rd.append(q[2])
                else:
                    rd.appendleft(q[2])
                    d.append(q[2])
        # print(d, rd, reverse)
    ans = ''
    if reverse:
        ans = ''.join(list(rd))
    else:
        ans = ''.join(list(d))
    return ans

if __name__ == "__main__":
    print(solve())


def solve_old():
    res = S
    res_r = S[::-1]
    reverse = False
    for q in Query:
        if q[0] == '1':
            reverse = not reverse
        else:
            if reverse:
                if q[1] == '1':
                    res += q[2]
                else:
                    res_r += q[2]
            else:
                if q[1] == '1':
                    res_r += q[2]
                else:
                    res += q[2]
        # print(res, res_r, reverse)
    ans = ''
    if reverse:
        ans = res + res_r[1:]
    else:
        ans = res_r[::-1] + res[1:]
    return ans

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
is_r = False
f_list = []
b_list = []

for i in range(q):
    q_i = input().split(' ')
    t_i = q_i[0]
    if t_i == '1':
        is_r = not is_r
    else:
        f_i = q_i[1]
        if (f_i == '1' and not is_r) or (f_i == '2' and is_r):
            f_list.append(q_i[2])
        else:
            b_list.append(q_i[2])

f_list = f_list[::-1]
f_list.append(s)
f_list.extend(b_list)
s = ''.join(f_list) if not is_r else ''.join(f_list[::-1])
print(s)

Submission status: Wrong Answer

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

f, l = S[0],S[-1]

for _ in range(Q):
    Query = input()
    if Query[0] == "1":
        f, l = l, f #swap
        S = f + S[1:-1] + l
    else:
        if Query[2] == "1":
            f = Query[4]
            S = f + S
        else:
            l = Query[4]
            S = S + l
    #print("Qans",S)

print(S)

Submission status: Wrong Answer

Solution code:
s = input()
totalre = 0
gm = []
gu = []

q = int(input())
for _ in range(q):
    query = list(input().split())
    if int(query[0])==1:
        totalre += 1
    else:
        if (totalre + int(query[1]))%2 != 0:
            gm.append(query[2])
        else:
            gu.append(query[2])
if totalre%2==0:
    ans = ''.join(gm)[::-1] + s + ''.join(gu)
else:
    ans = ''.join(gu)[::-1] + s + ''.join(gm)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
R = 1
Top = []
Bottom = []
for _ in range(Q):
  inp = input()
  if inp[0]=='1':
    R *= -1
  else:
    o, f, c = inp.split()
    if f=='1' and R==-1 or f=='2' and R==1:
      Bottom.append(c)
    else:
      Top.insert(0, c)
S = Top + [S] + Bottom
S = S if R==1 else reversed(S)
S = ''.join(S)
print(S)

Submission status: Wrong Answer

Solution code:
s=list(input())
q=int(input())
start=[]
owari=[]
r=1
for i in range(q):
    data=input().split()
    if(len(data)==1):
        r*=-1
    elif(data[1]=="1"):
        if(r==1):
            start.append(data[2])
        else:
            owari.append(data[2])
    else:
        if(r==1):
            owari.append(data[2])
        else:
            start.append(data[2])

if(r==1):
    start.reverse()
    print(''.join(start),end='')
    print(''.join(s),end='')
    print(''.join(owari),end='')
else:
    owari.reverse()
    print(''.join(owari),end='')
    print(''.join(s),end='')
    print(''.join(start),end='')

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
x=0
A=[]
B=[]
for i in range(0,Q):
    query=list(input().split())
    if query[0]=='1':
        x+=1
    else:
        if query[1]=='1':
            if x%2==1:
                B.append(query[2])
            else:
                A.append(query[2])
        else:
            if x%2==1:
                A.append(query[2])
            else:
                B.append(query[2])

if x%2==1:
    B=[B[len(B)-1-i] for i in range(0,len(B))]
    a=''.join(A)
    b=''.join(B)
    print(b+S+a)
else:
    A=[A[len(A)-1-i] for i in range(0,len(A))]
    a=''.join(A)
    b=''.join(B)
    print(a+S+b)

Submission status: Wrong Answer

Solution code:
import sys; input = sys.stdin.readline
def main():
  S = input()
  Q = int(input())
  for i in range(Q):
    TFC = input().split()
    S = S[::-1] if TFC[0]=="1" else (TFC[2]+S if TFC[1]=="1" else S+TFC[2])
  print(S)

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]
        elif si[1] == "2" and toggle == "ok":
            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()
n = int(input())

t = 0
for i in range(n):
  a = input().split()
  if int(a[0]) == 1:
    t += 1
    #s = s[::-1]
  else:
    if t % 2 == 1:
      s = s[::-1]
    t = 0
    if int(a[1]) == 1:
      s = a[2] + s
    if int(a[1]) == 2:
      s = s + a[2]
    
print(s)

Submission status: Wrong Answer

Solution code:
import sys
def main():
  input=sys.stdin.readline
  s=input()
  q=int(input())
  ok=1
  for i in range(q):
    query=list(input().split())
    if query[0]=='1':
      ok=(ok+1)%2
    else:
      f,c=query[1],query[2]
      if ok:
        if f=='1':
          s=c+s
        else:
          s+=c
      else:
        if f=='1':
          s=s+c
        else:
          s=c+s
  if ok:
    print(s)
  else:
    print(s[::-1])
if __name__=='__main__':
  main()

Submission status: Wrong Answer

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

def sol():
    s=input()
    q=int(input())
    x=0
    for i in range(q):
        l=[i for i in input().split()]
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: s=l[2]+s
            else: s=s+l[2]
        elif l[1]=="2":
            if x==0: s=s+l[2]
            else: s=l[2]+s
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

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

A = []
B = []
count = 0

for i in range(Q):
    k = input()
    if k == "1":
        count += 1
    else:
        if k[2] == "1":
            if count % 2 == 0:
                A = A + [k[4]]
            else:
                B = B + [k[4]]
        else:
            if count % 2 == 1:
                A = A + [k[4]]
            else:
                B = B + [k[4]]

if count % 2 == 0:
    A.reverse()
    S = A + S + B
else:
    B.reverse()
    S = B + S + A

print(*S, sep="")