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:
import math
import heapq
import bisect
from collections import defaultdict, Counter, deque
import sys
# input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))


def main():
    s = input().strip()
    q = getN()
    parity = True
    left = ""
    right = ""
    for _ in range(q):
        query = input().strip()
        if query == "1":
            parity = not parity
        else:
            two, f, c = query.split()
            if parity and (f == "2"):
                right += c
            else:
                left += c

    if parity:
        # left.reverse()
        print(left[::-1] + s + right)
    else:
        # right.reverse()
        # s.reverse()
        print(right[::-1] + s[::-1] + left)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
hanten = 1
front = []
back = []
for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0]=='1':
        hanten *= -1
    else:
        if hanten==1:
            if q[1]=='1':
                front.append(q[2])
            else:
                back.append(q[2])
        else:
            if q[1]=='1':
                back.append(q[2])
            else:
                front.append(q[2])


if hanten==1:
    print(''.join(list(reversed(front)) + list(S) + back))
else:
    print(''.join(list(reversed(back+list(S)))+front))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    q = list(map(str, input().split()))
    if q[0]=='1':
        list(S).reverse()
        S = ''.join(S)
    else:
        if q[1]=='1':
            S = q[2] + S
        else:
            S = S + q[2]

print(S)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    query = list(map(str, input().split()))
    if query[0]=='1':
        ''.join(reversed(list(S)))
    else:
        if query[1]=='1':
            S = query[2] + S
        else:
            S = S + query[2]

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
q = int(input())
query = [list(input().split()) for i in range(q)]
now = 0
a = deque([s])
for i in query:
  if i[0] == "1":
    now ^= 1
  elif i[0] == "2":
    f,c = int(i[1]),i[2]
    if (f+now) % 2:
      a.appendleft(c)
    else:
      a.append(c)
if now:
  a.reverse()
ans = "".join(a)
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]
hanten = -1
atama = ""
shiri = ""
for row in range(quantity):
    each = L[row]
    if each[0] == "1":
        hanten = hanten * -1
    else:
        if each[1] == "1":
            if hanten == -1:
                atama = atama + each[2]
            else:
                shiri = shiri + each[2]
        else:
            if hanten == -1:
                shiri = shiri + each[2]
            else:
                atama = atama + each[2]

print(atama)
#print(shiri)
atama = atama[::-1]
ans = atama + S + shiri
if hanten == 1:
    ans = ans[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
quantity = int(input())
L = [input().split() for i in range(quantity)]
hanten = -1
atama = ""
shiri = ""
for row in range(quantity):
    each = L[row]
    if each[0] == "1":
        hanten = hanten * -1
    else:
        if each[1] == "1":
            if hanten == -1:
                atama = atama + each[2]
            else:
                shiri = shiri + each[2]
        else:
            if hanten == -1:
                shiri = shiri + each[2]
            else:
                atama = atama + each[2]

atama = atama[::-1]
ans = atama + S + shiri
if hanten == 1:
    ans = ans[::-1]

print(S)

Submission status: Wrong Answer

Solution code:
answer = input()
q = int(input())
# querrys = []
rev_cnt = 0
for i in range(0, q):
  querry = input().split()
  if querry[0] == '1':
    rev_cnt += 1
  else:
    if rev_cnt % 2 == 1:
      answer = ''.join(list(reversed(answer)))
    rev_cnt = 0  
    if querry[1] == '1':
      answer = querry[2] + answer
    else:
      answer = answer + querry[2]
print(answer)

Submission status: Wrong Answer

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

for _ in range(Q):
    query = list(input().split())
    if query[0] == '1':
        S = S[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            S = query[2] + S
        elif query[1] == '2':
            S = S + query[2]

Submission status: Wrong Answer

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]
key = 0  #0 start

for i in range(kai):
    if(data[i][0] == '1'):
        key = 1
    elif(data[i][1] == '1'):
        string = data[i][2] + string
    else:
        string = string + data[i][2]
if(key == 0):
    print(string)
else:
    print(string[::-1])

Submission status: Wrong Answer

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(kai):
    if(data[i][0] == '1'):
        string = string[-1] + string[1:-1] + string[0]
    elif(data[i][1] == '1'):
        string = data[i][2] + string
    else:
        string = string + data[i][2]

print(string)

Submission status: Wrong Answer

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(kai):
    if(data[i][0] == '1'):
        string = string[-1] + string[1:-1] + string[0]
    else:
        if(data[i][1] == '1'):
            string = data[i][2] + string
        else:
            string = string + data[i][2]

print(string)

Submission status: Wrong Answer

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(kai):
    if(data[i][0] == '1'):
        if(len(string) >= 2):
            string = string[-1] + string[1:-1] + string[0]
    else:
        if(data[i][1] == '1'):
            string = data[i][2] + string
        else:
            string = string + data[i][2]

print(string)

Submission status: Wrong Answer

Solution code:
string = input()
kai = int(input())
data = [input().split(' ') for i in range(kai)]

for i in range(kai):
    if(data[i][0] == '1'):
        if(len(string) == 2):
            string = string[1] + string[0]
        elif(len(string) >= 3):
            string = string[-1] + string[1:-1] + string[0]
        
    else:
        if(data[i][1] == '1'):
            string = data[i][2] + string
        else:
            string = string + data[i][2]

print(string)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for q in range(q_):
  t = input()
  if t[0]==1: s=s[::-1]
  elif t[0]=='2' and t[2]=='1': s=t[4]+s
  elif t[0]=='2' and t[2]=='2': s+=t[4]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
n=int(input())
l = [input().split(" ") for _ in range(n)]

x = 0
for i in range(n):
    if l[i][0]=='1':
        if x == 0:
            x = 1
        else:
            x = 0
    elif l[i][0]=='2':
        if x == 0:
            if l[i][1]=='1':
                s = l[i][2] + s
            else:
                s = s + l[i][2]
        else:
            if l[i][1]=='1':
                s = s + l[i][2]
            else:
                s = l[i][2] + s
if x == 0:
    print(s)
else:
    print(reversed(s))

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self, char: str):
        self.before = None
        self.char = char
        self.next = None

class Linked_List:
    def __init__(self):
        self.head = None
        self.tail = None
    
    def add_head(self, node):
        if self.head is None:
            self.head = node
            self.tail = node
        else:
            node.next = self.head
            self.head.before = node
            self.head = node

    def add_tail(self, node):
        if self.tail is None:
            self.head = node
            self.tail = node
        else:
            node.before = self.tail
            self.tail.next = node
            self.tail = node

    def show(self, is_reversed: bool):
        ret = []
        node = self.tail if is_reversed else self.head
        while not node is None:
            ret.append(node.char)
            node = node.before if is_reversed else node.next
        print(''.join(ret))

S = input()
Q = int(input())
ll = Linked_List()
ll.add_head(Node(S))
is_reversed = False

for _ in range(Q):
    query = input()
    if query[0] == '1':
        is_reversed = False if is_reversed else True
    else:
        t, f, c = query.split()
        if f == '1':
            if is_reversed:
                ll.add_tail(Node(c))
            else:
                ll.add_head(Node(c))
        else:
            if is_reversed:
                ll.add_head(Node(c))
            else:
                ll.add_tail(Node(c))

ll.show(is_reversed)

Submission status: Wrong Answer

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

for i in range(Q):
  Query=input()
  if len(Query)==1:
    S=S[::-1]
  else:
    if Query[2]=='1':
      S=S[::-1]
      S=Query[4]+S
    else:
      S+=Query[4]

print(S)

Submission status: Wrong Answer

Solution code:
# problem D
s = input()
Q = int(input())

query = []
for i in range(0,Q):
    query.append(input().split())
    
summ = 0
hanten = 0
    
for i in query:
    if int(i[0]) == 1:
        summ += 1
        hanten += 1
    elif (summ % 2 == 0 and int(i[1]) == 1) or (summ % 2 == 1 and int(i[1]) == 2):
        s = ''.join([str(i[2]),s])
        summ = 0
    else:
        s = ''.join([s,str(i[2])])
        summ = 0
    
if hanten % 2 ==1:
    s = s[::-1]
        
print(s)

Submission status: Wrong Answer

Solution code:
# problem D

s = input()
Q = int(input())

query = []
for i in range(0,Q):
    query.append(input().split())
    
summ = 0
hanten = 0
    
for i in query:
    if int(i[0]) == 1:
        summ += 1
        hanten += 1
    elif (summ % 2 == 0 and int(i[1]) == 1) or (summ % 2 == 1 and int(i[1]) == 2):
        s = str(i[2]) + s
        summ = 0
    else:
        s = s + str(i[2])
        summ = 0
    
if hanten % 2 ==1:
    s = s[::-1]
        
print(s)

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
cnt=0
a1,a2=[],[]
for _ in range(q):
  t=list(input().split())
  if t[0]=='1':
    cnt+=1
    a1=a1[::-1]
    a2=a2[::-1]
  else:
    if t[1]=='1' and cnt%2==0:
      a1.append(t[2])
    elif t[1]=='1' and cnt%2==1:
      a2.append(t[2])
    elif t[1]=='2' and cnt%2==0:
      a2.append(t[2])
    else:
      a1.append(t[2])
if cnt%2==1:
  s=s[::-1]
if cnt%2==0:
  print(''.join(a1)+s+''.join(a2))
else:
  print(''.join(a2)+s+''.join(a1))

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque(str(input()))
Q = int(input())
count = 0
for i in range(Q):
    tmp = list(input().split())
    if tmp[0]=="1":
        count += 1
    else:
        if tmp[1]=="1" and count%2==0 or tmp[1]=="2" and count%2==1:
            S.append(tmp[2])
        elif tmp[1]=="2" and count%2==0 or tmp[1]=="1" and count%2==1:
            S.appendleft(tmp[2])
if count%2==0:
    S.reverse()
    ans = "".join(S)
    print(ans)
else: 
    ans = "".join(S)
    print(ans)

Submission status: Wrong Answer

Solution code:
import copy

S=input()
Q=int(input())
Query=[]
temp=[]

revflag=0
sbef=[]
saft=[]

for i in range(Q):
    temp=input().split()
    if len(temp) ==1:
        revflag += 1
    else:
        if (revflag + int(temp[1])-1 )%2 == 0:
            sbef.append(temp[2])
        else:
            saft.append(temp[2])

ans=""

for i in range(len(sbef)):
    ans+=sbef[len(sbef)-i-1]

ans += S
for i in range(len(saft)):
    ans+=saft[i]
print(ans)

tmp=""
tmp2 = copy.copy(ans)


if revflag%2 ==1:
    for i in range(len(ans)):
        tmp+=tmp2[len(ans)-i-1]
    ans=tmp


print(ans)

Submission status: Wrong Answer

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

for i in range(Q):
    a=input()
    if len(a)==1:
        Reverse+=1
    else:
        if int(a[2])+Reverse%2==1 or 3:
            S.appendleft(a[4])
        else:
            S.append(a[4])
if Reverse%2==0:
    ans="".join(S)
else:
    ans="".join(S)[::-1]
print(ans)

Submission status: Wrong Answer

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

for i in range(Q):
    a=input()
    if len(a)==1:
        Reverse+=1
    else:
        if int(a[2])+Reverse%2==1 or 3:
            S.appendleft(a[4])
        else:
            S.append(a[4])
if Reverse%2==0:
    ans="".join(S)
else:
    ans="".join(S)[::-1]
print(ans)

Submission status: Wrong Answer

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

for i in range(Q):
    a=input()
    if len(a)==1:
        Reverse+=1
    else:
        if int(a[2])+Reverse%2==1:
            S.appendleft(a[4])
        else:
            S.append(a[4])
if Reverse%2==0:
    ans="".join(S)
else:
    ans="".join(S)[::-1]
print(ans)

Submission status: Wrong Answer

Solution code:
def change_fb(str_list):
    tmp = str_list[0]
    str_list[0] = str_list[-1]
    str_list[-1] = tmp

s = str(input())
q = int(input())
w = [input() for i in range(q)]

change = False
str_list = list(s)
for data in w:
    if int(data[0]) == 1:
#         change = not change
        change_fb(str_list)
    else:
#         if change:
#             change = False
#             change_fb(str_list)
        t,f,c = data.split()
        if int(f) == 1:
            str_list.insert(0, c)
        else:
            str_list.append(c)

if change:
    change_fb(str_list)

print("".join(str_list))

Submission status: Wrong Answer

Solution code:
def change_fb(str_list):
    tmp = str_list[0]
    str_list[0] = str_list[-1]
    str_list[-1] = tmp

s = str(input())
q = int(input())
w = [input() for i in range(q)]

change = False
str_list = list(s)
for data in w:
    if int(data[0]) == 1:
        change = not change
    else:
        if change:
            change = False
            change_fb(str_list)
        t,f,c = data.split()
        if int(f) == 1:
            str_list.insert(0, c)
        else:
            str_list.append(c)

if change:
    change_fb(str_list)

print("".join(str_list))

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
que=deque([s])
q=int(input())
rev=False
for i in range(q):
    t=input()
    if t=='1':
        rev=not rev
    else:
        tt,f,c=t.split()
        if (rev==False and f=='1')or(rev and f=='2'):
            que.appendleft(c)
        else:
            que.append(c)
if rev:
    que.reverse()
print(''.join(que))

Submission status: Wrong Answer

Solution code:
from collections import deque

s=input()
s=deque(s)
q=int(input())
ans=''
flg=False
for _ in range(q):
	query=list(map(str,input().split()))
	if query[0]=='1':
		flg=True
	else:
		if flg:
			s.reverse()
			#print(s)
			flg=False
		if query[1]=='1':
			s.appendleft(query[2])
		else:
			s.append(query[2])
ans=''.join(s)
if flg:
	ans=ans[::-1]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    cn = 0
    s = deque(s)

    for q in qs:
        if len(q) == 1:
            cn+=1
        else:
            if q[1] == '1':
                s.appendleft(q[2])
            else:
                s.append(q[2])

        if cn%2 == 1:
            s.reverse()
            print('%s' % ''.join(s))

if __name__ == "__main__":

    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    cn = 0
    s = deque(s)

    for q in qs:
        if len(q) == 1:
            cn+=1
        else:
            if q[1] == '1':
                s.appendleft(q[2])
            else:
                s.append(q[2])
        if cn%2 == 1:
            s.reverse()
    print('%s' % ''.join(s))

if __name__ == "__main__":

    main()

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
n = len(s)
flag = 0
mae = 0
ushiro = 0
b1 = ["a" for i in range(q)]
b2 = ["a" for i in range(q)]
for i in range(q):
    a = list(input().split())
    if a[0] == "1":
        flag += 1
    elif (flag+int(a[1])) % 2 == 1:
        b1[mae] = a[2]
        mae += 1
    else:
        b2[ushiro] = a[2]
        ushiro += 1

for i in range(mae):
    print(b1[mae-1-i], end="")

if flag % 2 == 0:
    print(s, end="")
else:
    print(reversed(s), end="")

for i in range(ushiro):
    print(b2[i], end="")

Submission status: Wrong Answer

Solution code:
def main():
  s = [input()]
  n = int(input()) 
  str_list = [list(input().split()) for _ in range(n)]

  for item in str_list:
    if int(item[0]) == 1:
      s.reverse()
    elif int(item[0]) == 2:
      if int(item[1]) == 1:
        s.insert(0, item[2])
      elif int(item[1]) == 2:
        s.append(item[2])
  print(''.join(s))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

s = input()
q = int(input())
ht = 1
count = 0
for i in range(2, q):
    txt3 = input().split()
    if txt3[0] == '1':
        ht = 3 - ht
        count += 1
    else:
        if int(txt3[1]) == ht:
            s = txt3[2] + s
        else:
            s = s + txt3[2]
if count % 2 == 1:
    s = s[::-1]
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
print(1)

Submission status: Wrong Answer

Solution code:
a = input()
flag = 0
first = []
last = []
for i in range(int(input())):
    query = [j for j in input().split()]
    t = int(query[0])
    if t == 1:
        flag = 1 - flag
    else:
        x = int(query[1])
        if x == 1:
            if flag == 0:
                first.append(query[2])
            else:
                last.append(query[2])
        else:
            if flag == 0:
                last.append(query[2])
            else:
                first.append(query[2])

if flag == 0:
    print("".join(first)[::-1] + a + "".join(last))
else:
    print("".join(last)[::-1] + a + ''.join(first))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
queries = []
for i in range(Q):
    query = [x for x in input().split()]
    query[0] = int(query[0])
    if query[0] == 2:
        query[1] = int(query[1])
    queries.append(query)

former = []
latter = []
is_normal = True
for query in queries:
    if query[0] == 1:
        is_normal = not is_normal
    
    else:
        if is_normal and query[1] == 1:
            former.append(query[2])
        elif not is_normal and query[1] == 1:
            latter.append(query[2])
        elif is_normal and query[1] == 2:
            latter.append(query[2])
        elif not is_normal and query[1] == 2:
            former.append(query[2])

if is_normal:
    ans = "".join(former[::-1]) + S + "".join(latter)
else:
    ans = "".join(latter[::-1]) + S + "".join(former)

print(ans)

Submission status: Wrong Answer

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

guuki=0
ANS=S
ANS_front=""
ANS_back=""
for i in range(Q):
    A=list(input().split())
    if A[0]=="1":
        guuki+=1
    elif int(A[1])*guuki%2==1 or (A[1]=="2" and guuki%2==0):
        ANS_back+=A[2]
    else:
        ANS_front=A[2]+ANS_front
if guuki%2==1:
    ANS=ANS[::-1]
    ANS_back,ANS_front=ANS_front,ANS_back
print(ANS_front[::-1]+ANS+ANS_back)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
before_chr = []
after_chr = []
reverse_status = 1
for i in range(Q):
    query = list(input().split())
    if query[0] == "1":
        reverse_status *=  -1
    elif reverse_status * (1.5-int(query[1])) == 0.5:
        before_chr += query[2]
    else:
        after_chr += query[2]
if reverse_status == 1:
    print("".join(before_chr[::-1])+S+"".join(after_chr))
else:
    print("".join(after_chr[::-1])+S+"".join(before_chr))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Query = [[]]*Q
for i in range(Q):
    Query[i] = list(input().split())
def connect_chr(word, querylist):
    before_chrs = "".join([query[2] for query in querylist if query[1]=="1"])
    after_chrs = "".join([query[2] for query in querylist[::-1] if query[1]=="2"])
    return before_chrs + word + after_chrs
from_idx = 0
for i in range(Q):
    if len(Query[i]) == 1:
        S = connect_chr(S, Query[from_idx:i])
        S = S[::-1]
        from_idx = i + 1
    elif i == Q-1:
        S = connect_chr(S, Query[from_idx:i+1])
print(S)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
A=[input().split() for i in range(Q)]

guuki=0
ANS=S
for i in range(Q):
  if A[i][0]=="1":
    guuki+=1
  elif int(A[i][1])*guuki%2==1 or (A[i][1]=="2" and guuki%2==0):
    ANS=ANS+A[i][2]
  else:
    ANS=A[i][2]+ANS

print(ANS)

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
ref=0
for i in range(Q):
    T=input().split()
    if T[0] == '2':
        if (T[1]=='1' and ref%2==0):
            S=T[2]+S
        elif (T[1]=='2' and ref%2==1):
            S=T[2]+S
        elif (T[1]=='1' and ref%2==0):
            S=S+T[2]
        elif (T[1]=='1' and ref%2==1):
            S=S+T[2]
    else:
        ref+=1
print(S)

Submission status: Wrong Answer

Solution code:
s=list(map(str,input().split()))

q=int(input())

b=1 #1は順方向、-1は逆方向

top=[]

for i in range(q):

  a=[]
  a=list(map(str,input().split()))

  if len(a)==1:
    b=b*(-1)

  else:

    if a[1]=="1":
      if b==1:
        top.append(a[2])
      elif b==-1:
        s.append(a[2])

    elif a[1]=="2":
      if b==1:
        s.append(a[2])
      elif b==-1:
        top.append(a[2])


if b==1:
  if len(top)>0:
    for j in range(len(top)):
      print(top[-1-j],end="")

  for i in range(len(s)):
    print(s[i],end="")

elif b==-1:
  for i in range(len(s)):
    print(s[-1-i],end="")

  if len(top)>0:
    for j in range(len(top)):
      print(top[j],end="")

print()

Submission status: Wrong Answer

Solution code:
s=list(map(str,input().split()))

q=int(input())

b=1 #1は順方向、-1は逆方向

for i in range(q):

  a=[]
  a=list(map(str,input().split()))

  if len(a)==1:
    b=b*(-1)

  else:

    if a[1]=="1":
      if b==1:
        s.insert(0,a[2])
      elif b==-1:
        s.append(a[2])

    elif a[1]=="2":
      if b==1:
        s.append(a[2])
      elif b==-1:
        s.insert(0,a[2])


if b==1:
  for i in range(len(s)):
    print(s[i],end="")

elif b==-1:
  for i in range(len(s)):
    print(s[-1-i],end="")
print()

Submission status: Wrong Answer

Solution code:
from collections import deque

s=input()
s=deque(s)
q=int(input())
ans=''
flg=False
for _ in range(q):
	flg=False
	query=list(map(str,input().split()))
	if query[0]=='1':
		flg=True
	else:
		if flg:
			s.reverse()
		if query[1]=='1':
			s.appendleft(query[2])
		else:
			s.append(query[2])
ans=''.join(s)
if flg:
	ans=ans[::-1]

print(ans)

Submission status: Wrong Answer

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

S_normal = S
S_inverse = S[::-1]
for q in range(Q):
    tmp1 = []
    tmp2 = []
    query = list(map(str, input().split()))
    if int(query[0]) == 1:
        S = S_inverse
        S_inverse = S_normal
        S_normal = S
    else:
        if int(query[1]) == 1:
            tmp1.append(query[2])
            tmp1.append(S)
            S = ''.join(tmp1)
            S_normal = S
            tmp2.append(S_inverse)
            tmp2.append(query[2])
            S_inverse = ''.join(tmp2)
        else:
            tmp2.append(S)
            tmp2.append(query[2])
            S = ''.join(S)
            S_normal = S
            tmp1.append(query[2])
            tmp1.append(S_inverse)
            S_inverse = ''.join(tmp1)

print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    st=deque([input()])
    N = int(input())
    sw=True
    for i in range(N):
        t= input().split()
        if t[0]=='1':
            sw=not sw
        else:
            if (sw and t[1]=='2') or (not sw and t[1] == '1'):
                st.append(t[2])
            else:
                st.appendleft(t[2])
    if not sw:
        st.reverse()
        print(''.join(st))
    else:
        print(''.join(st))
main()

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    st=deque([input()])
    N = int(input())
    sw=True
    for i in range(N):
        t= input().split()
        if t[0]=='1':
            sw=not sw
        else:
            if (sw and t[1]=='2') or (not sw and t[1] == '1'):
                st.append(t[2])
            else:
                st.appendleft(t[2])
    if not sw:
        print(''.join(reversed(st)))
    else:
        print(''.join(st))
main()

Submission status: Wrong Answer

Solution code:
s = [i for i in input()]
n = int(input())
l = [[i for i in input().split()] for _ in range(n)]

front = True

for i in l:
    if i[0] == '1':
        front = not front
    elif i[1] == '1':
        s.append(i[2])
        front = not front
    else:
        s.append(i[2])
            

if front:
    print(*s,sep = '')
else:
    s = s[::-1]
    print(*s,sep = '')

Submission status: Wrong Answer

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

data = [list(map(str,input().split())) for _ in range(Q)]

A = []
B = []
a = 0

for i in range(Q-1,-1,-1):
  if data[i][0] == '1':
    a += 1
  else:
    if (int(data[i][1]) + a) % 2 == 1:
      A.append(data[i][2])
    else:
      B.insert(0,data[i][2])
      
if a % 2 == 1:
  for i in range(len(S)):
    S[i] = S[len(S)-1-i]

A.extend(S)
A.extend(B)

C = ''.join(A)

print(C)

Submission status: Wrong Answer

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

data = [list(map(str,input().split())) for _ in range(Q)]

A = []
B = []
a = 0

for i in range(Q-1,-1,-1):
  if data[i][0] == '1':
    a += 1
  else:
    if (int(data[i][1]) + a) % 2 == 1:
      A.append(data[i][2])
    else:
      B.insert(0,data[i][2])

A.extend(S)
A.extend(B)

C = ''.join(A)

print(C)

Submission status: Wrong Answer

Solution code:
import sys
import math

s = input()
Q = int(input())
query = []
[query.append(input().split()) for _ in range(Q)]

inversion = -1

if len(s) >= 3:
    head = s[0]
    tail = s[-1]
    body = s[1:-2]
    for i in range(Q):
        if query[i][0] == "1":
            inversion *= -1
            continue

        c = str(query[i][2])
        if query[i][1] == "1" and inversion == -1:
            head = c + head
        elif query[i][1] == "1" and inversion == 1:
            tail = tail + c
        elif query[i][1] == "2" and inversion == -1:
            tail = tail + c
        else:
            head = c + head
        
    if inversion == -1:
        print(head + body + tail)
    else:
        print(tail[::-1] + body[::-1] + tail[::-1])

else:
        for i in range(Q):
            if query[i][0] == "1":
                inversion *= -1
                continue

            c = str(query[i][2])
            if query[i][1] == "1" and inversion == -1:
                s = c + s
            elif query[i][1] == "1" and inversion == 1:
                s = s + c
            elif query[i][1] == "2" and inversion == -1:
                s = s + c
            else:
                s = c + s
        
        if inversion == -1 :
            print(s)
        else:
            print(s[::-1])

Submission status: Wrong Answer

Solution code:
import sys
import math

s = input()
Q = int(input())
query = []
[query.append(input().split()) for _ in range(Q)]

inversion = -1

if len(s) >= 3:
    head = s[0]
    tail = s[-1]
    body = s[1:-2]
    for i in range(Q):
        if query[i][0] == "1":
            inversion *= -1
            continue

        c = str(query[i][2])
        if query[i][1] == "1" and inversion == -1:
            head = c + head
        elif query[i][1] == "1" and inversion == 1:
            tail = tail + c
        elif query[i][1] == "2" and inversion == -1:
            tail = tail + c
        else:
            head = c + head
        
    if inversion == -1:
        print(head + body + tail)
    else:
        print(tail[::-1] + body[::-1] + tail[::-1])

elif len(s) == 1:
        for i in range(Q):
            if query[i][0] == "1":
                inversion *= -1
                continue

            c = str(query[i][2])
            if query[i][1] == "1" and inversion == -1:
                s = c + s
            elif query[i][1] == "1" and inversion == 1:
                s = s + c
            elif query[i][1] == "2" and inversion == -1:
                s = s + c
            else:
                s = c + s
        
        if inversion == -1 :
            print(s)
        else:
            print(s[::-1])