Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Wrong Answer

Solution code:
s = input()
q= int(input())
f = False
count = 0

for i in range(q):
    t = input().split()
    if t[0] == '1':
        count += 1
#        if i == q - 1:
#            if not f:
#                s = s[::-1]
#        else:
        f = not f
#        print (f)
    else:
        if t[1] == '1':
            if f:
                s += t[2]
            else:
                s = t[2] + s
        else:
            if f:
                s += t[2]

            else:
                s = t[2] + s

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

Submission status: Wrong Answer

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

S = input()
Q = int(input())
L = [list(map(str,input().split())) for i in range(Q)]

cnt = 0

for i in range(Q):
    if len(L[i]) == 1:
        cnt += 1
    else:
        if (int(L[i][1])+cnt)%2 == 0:
            S = S+L[i][2]
        else:
            S = L[i][2]+S

if cnt%2 == 0:
    print(S)
else:
    new_S = ''.join(list(reversed(S)))
    print(new_S)

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)
cnt = 0
for i in range(Qlen):
    if int(Query[i][0]) == 1:
        cnt +=1
    else:
        if int(Query[i][1]) == 1:
            if cnt % 2 != 0:   
                Slist = Slist + [Query[i][2]]
            else:
                Slist = [Query[i][2]] + Slist
        else:
            if cnt % 2 != 0:
                Slist = [Query[i][2]] + Slist
            else:
                Slist = Slist + [Query[i][2]]
if cnt % 2 == 0:
    print("".join(Slist))
else:
    Slist = Slist[::-1]
    print(".join(Slist)")

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
last = 0
for i in range(Q):
  q = input().split()
  if (last == 1 and q[0] == '1'):
    last = 0
  elif (last == 0 and q[0] == '1'):
    last = 1
  elif (last == 1 and q[1] == '1'):
    last = 0
    S = S[::-1]
    S = q[2] + S
  elif (last == 0 and q[1] == '1'):
    S = q[2] + S
  elif (last == 1 and q[1] == '2'):
    last = 0
    S = S[::-1]
    S += q[2]
  else:
    S += q[2]
print(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
from collections import deque


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()
    s0 = s2 = ""
    q = _i()
    last = 0
    flip = False
    for _ in range(q):
        tmp = _sa()
        op = int(tmp[0])
        if op == 1:
            if last == 2:
                s = s0[::-1] + s + s2
                s0 = s2 = ""
            flip ^= 1
        else:
            if last == 1 and flip:
                flip = False
                s = s[::-1]

            if tmp[1] == "1":
                s0 += tmp[2]
            else:
                s2 += tmp[2]
        last = op

    if last == 1 and flip:
        return s[::-1]
    elif last == 2:
        return s0[::-1] + s + s2


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

Submission status: Wrong Answer

Solution code:
##D
S = str(input())
Q = int(input())
Query = [str(input()) for i in range(Q)]
print(Query)
ans = S
for j in range(len(Query)):
    if Query[j][0] == "1":
        ans = ans[::-1]
    elif Query[j][0] == "2":
        if Query[j][2] == "1":
            ans = str(Query[j][4])+ans
        elif Query[j][2] == "2":
            ans = ans+ str(Query[j][4])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    S = readline().strip().decode('utf-8')
    Q = int(readline())
    Query = [tuple(readline().decode('utf-8').split()) for _ in range(Q)]
    rev_flg = 0
    for q in Query:
        if q[0] == '1':
            rev_flg += 1
        else:
            if int(q[1])+(rev_flg%2) == '1':
                S = q[2] + S
            else:
                S += q[2]
    if rev_flg%2 == 0:
        S = S[::-1]
    print(S)

    return
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
syoki_str = input()
syori_cnt = int(input())

result = ""
for i in range(syori_cnt):
    line = input().split(" ")
    if line[0] == "1":
        result = ''.join(list(reversed(syoki_str)))
    else:
        output = []
        if line[1] == "1":
            output.append(line[2])
            output.append(syoki_str)
        else:
            output.append(syoki_str)
            output.append(line[2])
        result = ''.join(output)

print(result)

Submission status: Wrong Answer

Solution code:
S_list = [str(i) for i in input().split()]
Q = [int(i) for i in input().split()]
Query = []
for i in range(Q[0]):
    Query.append([str(i) for i in input().split()])
S = S_list[0]
for i in range(Q[0]):
    look = Query[i]
    if int(look[0]) == 1:

        
        first = S[0]
        end = S[len(S)-1]
        #listに置換してから書き換える
        s_lis = list(S)
        s_lis[0] = end
        s_lis[len(S)-1] = first
        S = "".join(s_lis)

    elif int(look[0]) == 2:
        if int(look[1]) == 1:
            S = look[2] + S
        elif int(look[1]) == 2:
            S = S + look[2]
print(S)

Submission status: Wrong Answer

Solution code:
def main():

    S = input().split()
    Q = int(input())

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

    for q in q_list:
        if len(q) == 1:
            t = q[0]
        else:
            t, f, c = q

        if t == '1' or t == 1:
            S.reverse()
        elif t == '2':
            if f == '1':
                tmp = [c]
                tmp.extend(S)
                S = tmp
            elif f == '2':
                S.append(c)
    print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

a = input().split()
q = int(input())

for i in range(q):
    q_list = input().split()
    if q_list[0] == '1':
        a = a[::-1]
    else:
        if q_list[1] == '1':
            a.insert(0, q_list[2])
        else:
            a.append(q_list[2])
result = ''.join(a)
print(result)

Submission status: Wrong Answer

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))

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

bef = list()
aft = list()
rev = 1
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = -1 * rev
    elif query[1] == '1':
        if rev > 0:
            bef.append(query[2])
        else:
            aft.append(query[2])
    else:
        if rev > 0:
            aft.append(query[2])
        else:
            bef.append(query[2])

if rev > 0:
    if len(bef) > 0:
        print(''.join(reversed(bef)), end="")

    print(S, end="")

    if len(aft) > 0:
        print(''.join(aft))
else:
    if len(aft) > 0:
        print(''.join(reversed(aft)), end="")

    print(S, end="")
    
    if len(bef) > 0:
        print(''.join(bef))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ARR = []
TOTAL1 = 0

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

    if int(arr[0]) == 1:
        TOTAL1 = TOTAL1 + 1
    ARR.append(arr)

def calculate(s,q,arr,total1):

    for ar in arr:
        if int(ar[0]) == 1:
            total1 = total1 - 1

        if int(ar[0]) == 2:

            if int(ar[1]) == 1:
                if total1 % 2 == 0:
                    s = ar[2] + s
                else:
                    s = s + ar[2]
            else:
                if total1 % 2 == 0:
                    s = s + ar[2]
                else:
                    s = ar[2] + s

    print(s)



calculate(S,Q,ARR,TOTAL1)

Submission status: Wrong Answer

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

for _ in range(Q):
    q = list(input().split())
    if q[0] == '1':
        S[0], S[-1] = S[-1], S[0]
    else:
        if q[1] == '1':
            S.appendleft(q[2])
        else:
            S.append(q[2])
print(''.join(S))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
re = False
front = []
back = []

for i in range(Q):
    t = input()
    
    if len(t) == 1:
        re = not re
        
    else:
        q, f, c = t.split()
        if (re == True and int(f) == 2) or (re == False and int(f) == 1):
            front.append(c)
        elif (re == True and int(f) == 1) or (re == False and int(f) == 2):
            back.append(c)

if re == False:
    front.reverse()
    F = "".join(front)
    B = "".join(back)
    print(F + S + B)
else:
    back.reverse()
    F = "".join(front)
    B = "".join(back)
    print(B + S + F)

Submission status: Wrong Answer

Solution code:
S = input()
Q=int(input())
Query=[S]
flag = True
for i in range(Q):
  S2=input()
  if S2=="1":
    flag = not flag
  else:
    x=S2.split()
    if flag:
      if x[1]=="1":
        Query=[x[2]]+Query
      else:
        Query.append(x[2])
    else:
      if x[1]=="1":
        Query.append(x[2])
      else:
        Query=[x[2]]+Query
if flag==False:
  Query.reverse()

print("".join(Query))

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    S = deque(input().split())
    Q = int(input())

    for i in range(Q):
        QList = deque(input().split())
    
        if QList[0] == '1':
            S.reverse()
        else:
            if QList[1] == '1':
                S.appendleft(QList[2])
            else:
                S.append(QList[2])
    print("".join(list(S)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
init = input()
num = int(input())

_strings = [''] * num
_strings[0] = init

isReverse = False

queue = []

i = 0
while i < num:
    query = input().split()
    type = int(query[0])

    if type == 1:
        # _strings.reverse()
        isReverse = not isReverse
    else:
        mode = int(query[1])

        if mode == 1 or (mode == 2 and isReverse):
            _strings.insert(0, str(query[2]))
        else:
            _strings.append(str(query[2]))

    i += 1

if isReverse:
    _strings.reverse()

print(''.join(_strings))

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
total_t=1
result=[s]
for i in range(q):
  t=input()
  if t=="1":
    total_t=total_t*(-1)
  else:
    f=(t.split())[1]
    c=(t.split())[2]
    if (f=='1' and total_t==1) or (f=='2' and total_t==-1):
      result.insert(0,c)
    else:
      result.append(c)
if total_t==-1:
  result.reverse()
r=''.join(result)
print(r)

Submission status: Wrong Answer

Solution code:
def input_int_list():
    return [int(thing) for thing in input().split(" ")]


S=input()
Q=int(input())
Q_list=[input().split(" ") for i in range(Q)]
hantenness=1
left=[]
right=[]
for Query in Q_list:
    if Query[0]==str(1):
        hantenness*=-1
    else:
        C=Query[2]
        F=int(Query[1])
        if hantenness==1:
            if F==1:
                left=left+[C]
            else:
                right=right+[C]
        else:
            if F==1:
                right=right+[C]
            else:
                left=left+[C]

if hantenness==1:
    print("".join(left[::-1])+S+"".join(right))
else:
    S="".join(list(S)[::1])
    print("".join(right[::-1])+S+"".join(left))

Submission status: Wrong Answer

Solution code:
S = input()
Q = list(map(int,input().split()))[0]

S1=S
S2=S
flg=1
count=0
for i in range(Q):
    Query = input().split()
    if Query[0]=='1':
        flg*=-1
        count+=1
        continue
    if flg==1:
        if Query[1]=='1':
            S2+=Query[2]
        else:
            S1+=Query[2]
    else:
        if Query[1]=='1':
            S1+=Query[2]
        else:
            S2+=Query[2]
print(S1, S2)
if count%2==1:
    print((S1[::-1])[:-1]+S2)
else:
    print((S2[::-1])[:-1]+S1)

Submission status: Wrong Answer

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

reverse_num = 0
 
reverse = 0
for _ in range(q):
    Q = list(input().split())
    if int(Q[0]) == 1:
        if reverse == 0:
            reverse = 1
        else:
            reverse = 1
        reverse_num += 1
    else:
        if reverse == 0:
            if int(Q[1]) == 2:
                s.append(Q[2])
            else:
                s = [Q[2]] + s
        else:
            if int(Q[1]) == 2:
                s = [Q[2]] + s
            else:
                s.append(Q[2])
                
                
if reverse_num%2 == 0:
    print(''.join(s))
else:
    print(''.join(s[::-1]))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
def rev(s):
    l = len(s)
    return s[-1]+s[1:l-1]+s[0]
    
s=S[0]
for i in range(Q):
    T = input().split()
    if T[0]=='1':
        s=rev(s)
    elif T[0]=='2':
        F,C=T[1],T[2]
        if F=='1':
            s=str(C)+s
        else:
            s=s+str(C)
print(s)

Submission status: Wrong Answer

Solution code:
a=input()
q=int(input())
val=0
for i in range(q):
    b=input().split()
    if b[0]=="1":
        val+=1
    else:
        if b[1]=="1":
            if val%2==0:
                a=b[2]+a
            else:
                a+=b[2]
        else:
            if val%2==0:
                a+=b[2]
            else:
                a=b[2]+a
print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = list(input())
N = int(input())
sc = [list(input().split()) for i in range(N)]
count = 0
a = ''
b = ''
flg = 0
for i in sc:
    if int(i[0]) == 1:
        count += 1
        flg = 1
        continue
    elif int(i[0]) == 2 and int(i[1]) == 1:
        if flg == 0:
            a += i[2]
        else:
            b += i[2]
    elif int(i[0]) == 2 and int(i[1]) == 2:
        if flg == 0:
            b += i[2]
        else:
            a += i[2]
    flg = 0
if count % 2 == 0:
    a = a[::-1]
    S.insert(0,a)
    S.append(b)
elif count % 2 == 1 and flg == 1:
    S.append(b)
    S.append(a)
elif count % 2 == 1 and flg == 0:
    S.append(a)
    S.insert(0,b)
print(''.join(S))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque

d=deque()

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

d.append(S[0])
#print(d)
for i in range(Q[0]):
  s=input().split()

  if len(s)==1:

      #S=S[::-1]
      #print(d)
      d.reverse()
      #print(d)

  else:
      if int(s[1])==1:
          d.appendleft(s[2])
          #print(d)
          #S.insert(0,s[2])


      else:
          d.append(s[2])
          #print(d)
          #S.append(s[2])
print("".join(d))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  query = input().split()
  if len(query) == 1:
    if len(S) > 1:
      S = S[-1] + S[1:-1] + S[0]
  else:
    if query[1] == "1":
      S = query[2] + S
    else:
      S += query[2]
print(S)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().strip()
S = input()[0:1]
Q = int(input())

mode = 1

for i in range(Q):
  TFC = list(map(str, input().split()))
  if "1" in TFC[0]:
    mode *= -1
    continue
  if mode == 1:
    if "1" in TFC[1]:
      S = ''.join([TFC[2][0],S])
    else:
      S = ''.join([S,TFC[2][0]])
  else:
    if "1" in TFC[1]:
      S = ''.join([S,TFC[2][0]])
    else:
      S = ''.join([TFC[2][0],S])

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

reverse_status = False

for i in range(q):
  query = list(map(format, input().split()))
  if int(query[0]) == 1:
    if reverse_status:
      reverse_status = False
    else:
      reverse_status = True
  else:
    if int(query[1]) == 1:
      if reverse_status:
        s.append(query[2])
      else:
        s.appendleft(query[2])
    else:
      if reverse_status:
        s.appendleft(query[2])
      else:
        s.append(query[2])

if reverse_status:
  s.reverse()
print(''.join(s))

Submission status: Wrong Answer

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

if len(s) >= 2:
  s1 = list(s[0])
  s2 = list(s[len(s)-1])
  s = s[1:len(s)-1]
else:
  s1 = list(s)
  s2 = []
  s = ''
  
for q in range(Q):
  query = input().split()
  if len(query) == 1:
    if s1 and s2:
      s1[-1], s2[-1] = s2[-1], s1[-1]
    else:
      s1[0],s1[-1] = s1[-1],s1[0]
  else:
    T,F,c = list(query)
    if F == '1':
      s1.append(c)
    else:
      s2.append(c)
      
print(''.join(s1[::-1]) + s + ''.join(s2))

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)

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []

    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])

    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S + head)
    
    print(ans)
    """
    S = stdin.readline().rstrip() 
    Q = int(input())
    rev = 0
    for _ in range(Q):
        q = stdin.readline()

        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    S = "{}{}".format(q[4], S)
                else:
                    S = "{}{}".format(S, q[4])
            else:
                if rev ==0:
                    S = "{}{}".format(S, q[4])
                else:
                    S = "{}{}".format(q[4], S)
    if rev == 1:
        print(S[::-1])
    else:
        print(S)
    """
main()

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):
    #print(i,len(d))
    if (len(query[i])==1):
        if len(d)>1:
            flag=(flag+1)%2 #反転フラグをひっくり返す
    else:
        if flag==0:
            if query[i][1]=="1":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
        else:
            if query[i][1]=="2":
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    #print(d)
if flag==0:
    print("".join(list(d)))
else:
    d.reverse()
    print("".join(list(d)))
"""
for i in range(Q):
    #print(i,len(d))
    if (len(query[i])==1):
        d.reverse()
    else:
        if query[i][1]=="1":
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    #print(d)
print("".join(list(d)))

Submission status: Wrong Answer

Solution code:
inital_string = input()
num_of_q = int(input())
flag = True

for i in range(num_of_q):
  q = input()
  if q[0] == '1':
    flag = not flag
  else:
    if q[2] == "1":
      if flag:
        inital_string = inital_string + q[4]
      else:
        inital_string = q[4] + inital_string
    else:
      if flag:
        inital_string = q[4] + inital_string
      else:
        inital_string = inital_string + q[4]
if flag:
    inital_string = inital_string[::-1]

print(inital_string)

Submission status: Wrong Answer

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

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
chs = ""
flip = True
ins = 0
while idx < q:
    if t[idx] == 1:
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            chs = chs[::-1]
            ins = len(chs) - ins
    else:
        if f[idx] == 1:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 1:
                idx += 1
                ch = c[idx] + ch
            chs = ch + chs
            ins += len(ch)
        else:
            ch = c[idx]
            while idx + 1 < q and t[idx + 1] == 2 and f[idx + 1] == 2:
                idx += 1
                ch = ch + c[idx]
            chs = chs + ch
    idx += 1

ans = chs[:ins] + s + chs[ins:]
print(ans)

Submission status: Wrong Answer

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

sentence = [S]
head = sentence[0]
tail = sentence[-1]
ans = []

for i in range(Q):
    ans2 =[]
    ans.append(input())
    if ans[i] == "1":
        tmp = sentence[0]
        sentence[0]  = sentence[-1]
        sentence[-1] = tmp
    else:
        ans2 = list(ans[i].split())
        if ans2[1] == "1":
            sentence.insert(0, ans[i][-1])
        else:
            sentence.append(ans[i][-1])
f = ""
for j in range(len(sentence)):
    f = f + sentence[j]
print(f)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
query = [input().split() for i in range(Q)]
print(query)
for i in query:
    if i[0] == '1':
        S = list(reversed(S))
    elif i[0]=='2' and i[1]=='1':
        S.insert(0, i[2])
    elif i[0]=='2' and i[1]=='2':
        S.append(i[2])
print(''.join(S))

Submission status: Wrong Answer

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))

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

bef = list()
aft = list()
rev = 1
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = -1 * rev
    elif query[1] == '1':
        if rev > 0:
            bef.append(query[2])
        else:
            aft.append(query[2])
    else:
        if rev > 0:
            aft.append(query[2])
        else:
            bef.append(query[2])

if rev > 0:
    if len(bef) > 0:
        print(''.join(reversed(bef)), end="")

    print(S, end="")

    if len(aft) > 0:
        print(''.join(aft))
else:
    if len(bef) > 0:
        print(''.join(reversed(aft)), end="")

    print(S, end="")
    
    if len(aft) > 0:
        print(''.join(bef))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import collections
#input = sys.stdin.buffer.readline

def inn(func=int):
    return list(map(func,input().split()))

def ins():
    return input().split()

s = ins()
q = inn()[0]
d = collections.deque(s)
r = False
for i in range(q):
    que = ins()
    if que[0] == "1":
        if r:
            r = False
        else:
            r = True
    else:
        if que[1] == "1":
            if not r:
                d.appendleft(que[2])
            else:
                d.append(que[2])
        else:
            if not r:
                d.append(que[2])
            else:
                d.appendleft(que[2])

if r:
    d.reverse()
print("".join(list(d)))

Submission status: Wrong Answer

Solution code:
from collections import deque

S = deque(input().split())
Q = int(input())

for i in range(Q):
    QList = deque(input().split())
    
    if QList[0] == '1':
        S.reverse()
    else:
        if QList[1] == '1':
            S.appendleft(QList[2])
        else:
            S.append(QList[2])
print("".join(list(S)))

Submission status: Wrong Answer

Solution code:
ans = list(input())
q = int(input())
f = []
c = 0
cnt = 0
for i in range(q):
    ff = [i for i in input().split()]
    f.append(ff)
    if ff[0] == '1':
        c += 1
if c % 2 == 0:
    for i in range(q):
        if f[i][0] == '1':
            cnt += 1
        else:
            if f[i][1] == '1':
                if cnt % 2 == 0:
                    ans.insert(0,f[i][2])
                else:
                    ans.append(f[i][2])
            else:
                if cnt % 2 == 0:
                    ans.append(f[i][2])
                else:
                    ans.insert(0,f[i][2])
else:
    for i in range(q):
        if f[i][0] == '1':
            cnt += 1
        else:
            if f[i][1] == '1':
                if cnt % 2 == 0:
                    ans.append(f[i][2])
                else:
                    ans.insert(0, f[i][2])
            else:
                if cnt % 2 == 0:
                    ans.insert(0, f[i][2])
                else:
                    ans.append(f[i][2])

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

Submission status: Wrong Answer

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

sentence = [S]
head = sentence[0]
tail = sentence[-1]
ans = []

for i in range(Q):
    ans2 =[]
    ans.append(input())
    if ans[i] == "1":
        tmp = sentence[0]
        sentence[0]  = sentence[-1]
        sentence[-1] = tmp
    else:
        ans2 = list(ans[i].split())
        print(ans2)
        if ans2[1] == "1":
            sentence.insert(0, ans[i][-1])
        else:
            sentence.append(ans[i][-1])
f = ""
for j in range(len(sentence)):
    f = f + sentence[j]
print(f)

Submission status: Wrong Answer

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

result_list = list(S)
for i, que in enumerate(Query):
    if len(que) == 1:
        if i + 1 < Q:
            if len(Query[i + 1]) == 1:
                continue
            else:
                result_list.reverse()
        else:
            result_list.reverse()
        
    else:
        que_list = que.split()
        if que_list[1] == "1":
            result_list.insert(0, que_list[2])
        else:
            result_list.append(que_list[2])

print(''.join(result_list))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
S = input()
Q = int(input())
 
alpha_map = {chr(i):i for i in range(97,123)}
alpha_map_inv = {i:chr(i) for i in range(97,123)}
a = deque()
for s in S:
  a.append(s)
print(a)
reversed = False
 
for _ in range(Q):
  query = list(map(str, input().split()))
  if query[0] == '1':
    if reversed:
      reversed = False
    else:
      reversed = True
  else:
    if (query[1] == '1')&(not reversed):
      a.appendleft(query[2])
    elif (query[1] == '2')&(reversed):
      a.appendleft(query[2])
    else:
      a.append(query[2])
if reversed:
  a.reverse()
print(''.join(list(a)))

Submission status: Wrong Answer

Solution code:
S = input()

Q = int(input())

for i in range(Q):
  instraction = list(map(str, input().split()))
  if instraction[0] == '1':
    S = S[::-1]
  else:
    if instraction[1] == '1':
      S = S.join(instraction[2])
    else:
      S = instraction[2].join(S)

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque
ss=deque()
s=list(input())
l=len(s)
for i in range(l):
    ss.append(s[i])
q=int(input())
info=[]
for i in range(q):
    info.append(list(input().split()))
moji=l
for i in range(q):
    if info[i][0]=="1":
        ss.rotate(moji-1)
    else:
        if info[i][1]=="2":
            ss.append(info[i][2])
        else:
            ss.appendleft(info[i][2])
        moji+=1
    #print(s)
print("".join(ss))

Submission status: Wrong Answer

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

for i in s:
    d.append(i)

q = int(input())

for i in range(q):
    query = input()
    try:
        int(query)
        t = query
    except:
        t, f, c = query.split()

    if t == "1":
        _t = d.pop()
        d.append(d.popleft())
        d.appendleft(_t)
    else:
        if f == "1":
            d.appendleft(c)
        else:
            d.append(c)

print("".join(d))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    S = readline().strip().decode('utf-8')
    Q = int(readline())
    Query = [tuple(readline().decode('utf-8').split()) for _ in range(Q)]
    rev_flg = False
    for q in Query:
        if q[0] == '1':
            rev_flg += 1
        else:
            if int(q[1])+rev_flg == '1':
                S = q[2] + S
            else:
                S += q[2]
    if rev_flg == True:
        S = S[::-1]
    print(S)

    return
if __name__ == '__main__':
    main()