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()
S_=list(S)
Q=int(input())
F1=[]
Q_=[]
for i in range(Q):
  Q_.append(list(input().split()))
for i in range(len(Q_)):
  if Q_[i]==['1'] and len(F1)!=0:
    a=F1[-1]
    b=S_[-1]
    F1[-1]=b
    S_[-1]=a
  elif F1==[] and Q_[i]==['1']:
    a=S_[0]
    b=S_[-1]
    S_[0]=b
    S_[-1]=a
  elif Q_[i][0:2]==['2','1']:
    F1.append(Q_[i][2])
  else:
    S_.append(Q_[i][2])
print(''.join(F1[::-1]+S_))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

ans = deque(s)

reverse_num = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])
    else:
      if reverse_num % 2 != 0:
        ans.appendleft(Q[i][2])
      else:
        ans.append(Q[i][2])

res = ''

#for i in range(len(ans)):
 # res += ans[i]
if reverse_num % 2 == 0:   
  print(res)
else:
  print(res[::-1])

Submission status: Wrong Answer

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

n = S
mode = 0
operations = []
string = [n]
# deq = deque()
# deq.append(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        # deq.append(op[2])
        string.append(op[2])
    else:
        # deq.appendleft(op[2])
        string.insert(0, op[2])
if mode == 0:
    # print("".join(deq))
    print("".join(string))
else:
    # print("".join(reversed(deq)))
    print("".join(reversed(string)))

Submission status: Wrong Answer

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

n = S
mode = 0
operations = []
deq = deque()
deq.append(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))

Submission status: Wrong Answer

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

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
operations = []
deq = deque()
deq.append(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
result = list(deq)
if mode == 0:
    print("".join(result))
else:
    result.reverse()
    print("".join(reversed(result)))

Submission status: Wrong Answer

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

n = S
# r = S
# for i in range(Q):
#     op = input().split()
#     if len(op) == 1:
#         n, r = r, n
#     elif op[1] == "1":
#         n = op[2] + n
#         r = r + op[2]
#     else:
#         n = n + op[2]
#         r = op[2] + r
# print(n)

mode = 0
operations = []
deq = deque()
deq.append(n)
for i in range(Q):
    operations.append(input().split())
for op in operations:
    if op[0] == "1":
        mode += 1
        mode %= 2
    elif int(op[1]) + mode == 2:
        deq.append(op[2])
    else:
        deq.appendleft(op[2])
if mode == 0:
    print("".join(deq))
else:
    print("".join(reversed(deq)))

Submission status: Wrong Answer

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

n = S
r = S
for i in range(Q):
    op = input().split()
    if len(op) == 1:
        n, r = r, n
    elif op[1] == "1":
        n = op[2] + n
        r = r + op[2]
    else:
        n = n + op[2]
        r = op[2] + r
print(n)

Submission status: Wrong Answer

Solution code:
from collections import deque
x=input()
Q=int(input())
D=deque()
D.append(x)
count=0
for _ in range(Q):
	Y=list(input().split())
	if Y[0]=="1":
		count=(count+1)%2
	else:
		if Y[1]=="1":
			if count==0:
				D.appendleft(Y[2])
			else:
				D.append(Y[2])
		else:
			if count==0:
				D.append(Y[2])
			else:
				D.appendleft(Y[2])
if count==0:
	print("".join(D))
else:
	D.reverse()
	print("".join(D))

Submission status: Wrong Answer

Solution code:
from collections import deque
s=deque([input()])
q=int(input())
rev=0
for i in range(q):
  t=list(input().split(" "))
  if t[0]=="1":
    rev=rev+1
  else:
    if t[1]=="1":
      if rev%2==0:
        s.appendleft(t[2])
      else:
        s.append(t[2])
    else:
      if rev%2==0:
        s.append(t[2])
      else:
        s.appendleft(t[2])
if rev%2==0:
  print("".join(s))
else:
  s.reverse()
  print("".join(s))

Submission status: Wrong Answer

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

flag = 1
s1 = ''
s2 = ''

for i in range(Q):
	q = input()
	if len(q) == 1:
		s1,s2 = s2,s1
	else:
		T,F,C = q.split(' ')
		if int(F) == 1:
			s1 = s1 + C
		else:
			s2 = s2 + C

print(s1[::-1] + S + s2)

Submission status: Wrong Answer

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

flag = 1
s1 = ''
s2 = ''

for i in range(Q):
	q = input()
	if len(q) == 1:
		flag = flag * (-1)
	else:
		T,F,C = q.split(' ')
		if flag == 1:
			if int(F) == 1:
				s1 = s1 + C
			else:
				s2 = C + s2
		else:
			if int(F) == 1:
				s2 = C + s2
			else:
				s1 = s1 + C
if flag == 1:
	print(s1 + S + s2)
else:
	print(s2 + S + s1)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = ['' for _ in range(10 ** 5 * 4 + 2)]

for i in range(len(s)):
  ans[10 ** 5 + i + 2] = s[i]

reverse_num = 0
tmp1 = 0
tmp2 = 1
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans[10 ** 5 - tmp1 + 1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2 + 100] = Q[i][2]
        tmp2 += 1
    else:
      if reverse_num % 2 != 0:
        ans[10 ** 5 - tmp1 + 1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2 + 100] = Q[i][2]
        tmp2 += 1
      
res = ''
for i in range(len(ans)):
  if ans[i] != '':
    res += ans[i]

if reverse_num % 2 == 0:   
  print(res)
else:
  print(res[::-1])

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = ['' for _ in range(10 ** 5 * 3 + 2)]

for i in range(len(s)):
  ans[10 ** 5 + i + 2] = s[i]

reverse_num = 0
tmp1 = 0
tmp2 = 1
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans[10 ** 5 - tmp1 + 1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2 + 1] = Q[i][2]
        tmp2 += 1
    else:
      if reverse_num % 2 != 0:
        ans[10 ** 5 - tmp1 + 1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2 + 1] = Q[i][2]
        tmp2 += 1
      
res = ''
for i in range(len(ans)):
  if ans[i] != '':
    res += ans[i]

if reverse_num % 2 == 0:   
  print(res)
else:
  print(res[::-1])

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = ['' for _ in range(10 ** 5 * 3 + 2)]

for i in range(len(s)):
  ans[10 ** 5 + i + 1] = s[i]

reverse_num = 0
tmp1 = 0
tmp2 = 1
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
    else:
      if reverse_num % 2 != 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
      
res = ''
for i in range(len(ans)):
  if ans[i] != '':
    res += ans[i]

if reverse_num % 2 == 0:   
  print(res)
else:
  print(res[::-1])

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = ['' for _ in range(10 ** 5 * 3 + 2)]

for i in range(len(s)):
  ans[10 ** 5 + i + 1] = s[i]

reverse_num = 0
tmp1 = 0
tmp2 = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
    else:
      if reverse_num % 2 != 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
      
res = ''
for i in range(len(ans)):
  if ans[i] != '':
    res += ans[i]
    
print(res)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
Q = [list(map(str, input().split())) for i in range(q)]

ans = ['' for i in range(10 ** 5 * 3 + 2)]

for i in range(len(s)):
  ans[10 ** 5 + i + 1] = s[i]

reverse_num = 0
tmp1 = 0
tmp2 = 0
for i in range(q):
  if int(Q[i][0]) == 1:
    reverse_num += 1
  else:
    if int(Q[i][1]) == 1:
      if reverse_num % 2 == 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
    else:
      if reverse_num % 2 != 0:
        ans[10 ** 5 - tmp1] = Q[i][2]
        tmp1 += 1
      else:
        ans[10 ** 5 + len(s) + tmp2] = Q[i][2]
        tmp2 += 1
      
res = ''
for i in range(len(ans)):
  if ans[i] != '':
    res += ans[i]
    
print(ans)

Submission status: Wrong Answer

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

def main():
    s = input()
    n = int(input())
    li = [input().split() for i in range(n)]
    a = 0 #先頭がどっちかという情報

    for x in li:
        if x[0] == "1":
            if a == 0:
                a = 1
            else:
                a = 0
        else:
            if (x[1] == "1" and a == 0) or (x[1] == "2" and a == 1):
                s = x[2] + s
            else:
                s = s + x[2]

    if a == 0: #先頭が前なら
        print(s)
    else: #先頭が後なら
        print(s[::-1])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

t = ''
for i in range(q):
    query = input().split()
    if query[0] == '1':
        s,t = t,s
    else:
        if query[1] == '1':
            t = t + query[2]
        else:
            s += query[2]

print(t + s)

Submission status: Wrong Answer

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

flag = False
cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '1':
        cnt += 1
        flag = (cnt %2 != 0)
    else:
        if flag:
            if query[1] == '1':
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if query[1] == '1':
                s = s + query[2]
            else:
                s = query[2] + s

if flag:
    print(s)
else:
    print(s[::-1])

Submission status: Wrong Answer

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

flag = False
cnt = 0
for i in range(q):
    query = input().split()
    if query[0] == '1':
        cnt += 1
        flag = (cnt %2 != 0)
    else:
        if flag:
            if query[1] == '1':
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if query[1] == '1':
                s = s + query[2]
            else:
                s = query[2] + s

print(s if flag else s[::-1])

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

S = str(input())
Q = int(input())
q = [0 for i in range(Q)]
for i in range(Q):
    q[i] = list(input().split(" "))
# print(q)

status = True # True: normal, False: reversed
ans = collections.deque()
ans.append(S)
for i in range(Q):
    query = q[i]
    if query[0] == "1":
        status = not(status)
    else:
        if query[1] == "1":
            if status:
                ans.appendleft(query[2])
            else:
                ans.append(query[2])
        else:
            if status:
                ans.append(query[2])
            else:
                ans.appendleft(query[2])
if status:
    print("".join(list(ans)))
else:
    ans_s = list(ans)
    ans_s.reverse()
    print("".join(ans_s))

Submission status: Wrong Answer

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


def main():
    S = input().strip()
    Q = int(input())
    Query = [list(input().split()) for _ in range(Q)]
    for T in Query:
        if T[0] == "1":
            s = list(S)
            s[0], s[len(s)-1] = s[len(s)-1], s[0]
            S = "".join(s)
        elif T[0] == "2":
            if T[1] == "1":
                S = T[2] + S
            else:
                S = S + T[2]
    print(S)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
judge = 1
n = 0
for i in range(q):
    q_i = input()
    if q_i == '1':
        judge *= -1
        n += 1
    else:
        a, f, c = q_i.split()
        if f == '2':
            judge *= -1
        s = c + s
if n % 2:
    print(''.join(list(s)[::-1]))
else:
    print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque()
S.append(input())
Q = int(input())
flag = True
for i in range(Q):
  string = input().split()
  if string[0] == '1':
    flag = not flag
  elif flag ^ (string[1] == '1'):
      S.append(string[2])
  else:
      S.appendleft(string[2])
if not flag:
  S.reverse()
for s in S:
  print(s, end='')

Submission status: Wrong Answer

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

s = readChar()
q = readInt()
qa = []
booh = 0
for i in range(q):
	x = input()
	if x=="1":
		qa.append([1,0,0])
	else:
		x = x.split()
		qa.append([int(x[0]),int(x[1]),x[2]])

def t1(s):
	return s[::-1]

def t2(f,c,s):
	if f==1:
		return c+s
	else:
		return s+c

fi=""
la=""
for a in qa:
	if a[0]==1:
		booh = (booh+1)%2
	else:
		if (a[1]+booh)%2==1:
			fi+=a[2]
		else:
			la+=a[2]

s = fi+s+la
if booh==1:
	s = t1(s)

print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
flag = True
for i in range(Q):
  string = input().split()
  if string[0] == '1':
    flag = not flag
  elif flag ^ (string[1] == '1'):
      S = string[2] + S
  else:
      S = S + string[2]
if flag:
  S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# TLE
s = input()
q = int(input())
reverse_flag = False
prefix = ''
suffix = ''
for _ in range(q):
  query = input()
  if query == '1':
    if reverse_flag:
      reverse_flag = False
    else:
      reverse_flag = True
  else:
    if reverse_flag and query[2] == '1':
      suffix += query[4]
    elif not reverse_flag and query[2] == '2':
      suffix += query[4]
    else:
      prefix += query[4]

if reverse_flag:
  print(suffix[::-1] + s + prefix)
else:
  print(prefix[::-1] + s + suffix)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# TLE
s = input()
q = int(input())
reverse_flag = False
prefix = ''
suffix = ''
for _ in range(q):
  query = input()
  if query == '1':
    if reverse_flag:
      reverse_flag = False
    else:
      reverse_flag = True
  else:
    if reverse_flag and query[2] == '1'\
      or not reverse_flag and query[2] != '1':
      suffix += query[4]
    else:
      prefix += query[4]

if reverse_flag:
  print(suffix[::-1] + s + prefix)
else:
  print(prefix[::-1] + s + suffix)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]
toplist=[]
backlist=[]
Flag=1

i=0
while i<Q:
    if Qlist[i][0]=="1":
        Flag*=-1
    else:
        #check=Qlist[i][2]
        if Qlist[i][1]=="1" and Flag==1:
            backlist.append(Qlist[i][2])
        elif Qlist[i][1]=="1" and Flag==-1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==-1:
            backlist.append(Qlist[i][2])
    i+=1
if Flag==-1:
    #print(toplist)
    #print(S)
    #print(backlist)
    print("".join(toplist[::-1])+S+"".join(backlist))
else:
    #print(backlist[::-1])
    #print(S)
    #print(toplist[::-1])
    print("".join(backlist[::-1])+S[::-1]+"".join(toplist))

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]
toplist=[]
backlist=[]
Flag=1

i=0
while i<Q:
    if Qlist[i][0]=="1":
        Flag*=-1
    else:
        #check=Qlist[i][2]
        if Qlist[i][1]=="1" and Flag==1:
            backlist.append(Qlist[i][2])
        elif Qlist[i][1]=="1" and Flag==-1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==1:
            toplist.append(Qlist[i][2])
        elif Qlist[i][1]=="2" and Flag==-1:
            backlist.append(Qlist[i][2])
    i+=1
if Flag==-1:
    #print(toplist)
    #print(S)
    #print(backlist)
    print("".join(toplist)+S+"".join(backlist))
else:
    #print(backlist[::-1])
    #print(S)
    #print(toplist[::-1])
    print("".join(backlist[::-1])+S+"".join(toplist[::-1]))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]

ReversCount=0
Rcount="NO"
i=0
while i<Q:
    if Qlist[i][0]=="1":
        ReversCount+=1
        Rcount="YES"
    else:
        check=Qlist[i][2]
        if Qlist[i][1]=="1" and Rcount=="NO":
            S=check+S
        elif Qlist[i][1]=="1" and Rcount=="YES":
            S=S+check
        elif Qlist[i][1]=="2" and Rcount=="NO":
            S=S+check
        elif Qlist[i][1]=="2" and Rcount=="YES":
            S=check+S
    i+=1

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
S = input()
Q = int(input())
Qlist = [list(input().split()) for _ in range(Q)]

#print(S)
Rcount="NO"
i=0
while i<Q:
    if Qlist[i][0]=="1":
        #S=S[::-1]
        Rcount="YES"
    else:
        check=Qlist[i][2]
        if Qlist[i][1]=="1" and Rcount=="NO":
            S=check+S
        elif Qlist[i][1]=="1" and Rcount=="YES":
            S=S+check
        elif Qlist[i][1]=="2" and Rcount=="NO":
            S=S+check
        elif Qlist[i][1]=="2" and Rcount=="YES":
            S=check+S
    i+=1

if Rcount=="YES":
    print(S[::-1])
else:
    print(S)

Submission status: Wrong Answer

Solution code:
B=input().split()#usiro
Q=int(input())
A=[]#mae



for i in range(Q):
  T=input().split()
  if T[0]=="1":
    A,B=B,A
    
  else:
    F=T[1]
    C=T[2]
    if F=="1":
      A.append(C)
    else:
      B.append(C)

A.reverse
ans=A+B
print("".join(ans))

Submission status: Wrong Answer

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

rev = 0
head = []
tail = []

for i in range(q):
    t = input().split()
    if t[0] == "1":
        rev ^= 1
    else:
        if (int(t[1])-1)^rev == 0:
            head.append(t[2])
        else:
            tail.append(t[2])

h = "".join(head)
t = "".join(tail)

if rev == 0:
    s = s[::-1]
    print(h[::-1] + s + t)
else:
    print(t[::-1] + s + h)

Submission status: Wrong Answer

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

rev = 0
head = []
tail = []

for i in range(q):
    t = input().split()
    if t[0] == "1":
        rev ^= 1
    else:
        if (int(t[1])-1)^rev == 0:
            head.append(t[2])
        else:
            tail.append(t[2])

h = "".join(head)
t = "".join(tail)

if rev == 0:
    print(h[::-1] + s + t)
else:
    print(t[::-1] + s + h)

Submission status: Wrong Answer

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

for i in range(Q):
  T=input().split()
  if T[0]=="1":
    b=S[0]
    S[0]=S[-1]
    S[-1]=b
    
  else:
    F=T[1]
    C=T[2]
    if F=="1":
      S.insert(0,C)
    else:
      S.append(C)
print("".join(S))

Submission status: Wrong Answer

Solution code:
import collections
s = collections.deque(input())
q = int(input())

for i in range(q):
    t = tuple(input().split())
    if t[0] == "1":
        s.reverse()
    elif t[1] == "1":
        s.appendleft(t[2])
    else:
        s.append(t[2])

ans = ""

print(ans)

Submission status: Wrong Answer

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

S = input()
Q = int(input())
query = []
flag = 1
before = ""
after  = ""

for i in range(Q):
    query = input().split()
    if query[0] == "1":
        flag *= -1
    else:
        if (query[1] == "1" and flag == 1) or  (query[1] == "2" and flag ==-1):
            before += query[2]
        else:
            after += query[2]
        #print(before,after)
if flag == 1:
    print(''.join(list(reversed(before))) + S + after)
else:
    print(''.join(list(reversed(after))) + S + before)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
query = []
for _ in range(Q):
    query.append(input().split(" "))

def calc(q, S):
    global hanten
    if q[0] == "1":
        hanten = not hanten
    else:
        if (q[1] == "1" and (not hanten)) or (q[1] == "2" and (hanten)):
            S = q[2] + S
        else:
            S = S + q[2]
    return S

hanten = False
for q in query:
    S = calc(q, S)
if hanten:
    print(S)
else:
    print(S)

Submission status: Wrong Answer

Solution code:
import collections

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

s2 = collections.deque(s)

inn = []
jun = 0

for i in range(q):
    inn = list(input().split())
    if inn[0] == "1":
        if jun == 0:
            jun = 1
        else:
            jun = 0#jun = 0はそのまま　jun = 1は逆をする
    elif inn[0] == "2":
        if inn[1] == "1" and jun == 0:
            s2.appendleft(inn[2])
        elif inn[1] == "2" and jun == 0:
            s2.append(inn[2])
        if inn[1] == "1" and jun == 1:
            s2.append(inn[2])
        elif inn[1] == "2" and jun == 1:
            s2.appendleft(inn[2])
    
if jun == 0:
    news = ''.join(s2)
else:
    s = s[::-1]
    news = ''.join(s2)

print(news)

Submission status: Wrong Answer

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

Queries = []
for i in range(Q):
    Queries.append(list(input().split()))

rev_count = 0
head = ""
tail = ""

for query in Queries:
    if query[0] == '1':
        tmp = head[::-1]
        head = tail[::-1]
        tail = tmp
        rev_count += 1

    if query[0] == '2':
        if query[1] == '1':
            head += query[2]
        if query[1] == '2':
            tail += query[2]


if rev_count % 2 == 1:
    S = S[::-1]
S = head[::-1] + S + tail
print(S)

Submission status: Wrong Answer

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

Queries = []
for i in range(Q):
    Queries.append(list(input().split()))

rev_count = 0
head = ""
tail = ""

for query in Queries:
    if query[0] == '1':
        tmp = head[::-1]
        head = tail[::-1]
        tail = tmp
        rev_count += 1

    if query[0] == '2':
        if query[1] == '1':
            head += query[2]
        if query[1] == '2':
            tail += query[2]


if rev_count % 2 == 1:
    S = S[::-1]
S = head + S + tail
print(S)

Submission status: Wrong Answer

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# a/b の切り上げ


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def main():
    s = input()
    q = int(input())
    Q = []
    for _ in range(q):
        Q.append(list(map(str, input().split())))
    # print(Q)
    for i in range(len(Q)):
        if Q[i][0] == '1':
            s = s[::-1]
        elif Q[i][0] == '2':
            if Q[i][1] == '1':
                s = s + Q[i][2]
            else:
                s = Q[i][2] + s
    print(s[::-1])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
#import queue
from copy import copy, deepcopy
from operator import itemgetter

import bisect#2分探索
#bisect_left(l,x), bisect(l,x)
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
#dequeを使うときはpython3を使う、pypyはダメ
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#q=heapq.heapify(l),heappush(q,a),heappop(q)

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s): return sorted(range(len(s)), key=lambda k: s[k])

#mod = 10**9+7

S = input()
Q=int(input())
#N, K = map(int, input().split())
#L = [int(input()) for i in range(N)]
#A = list(map(int, input().split()))
#S = [inputl() for i in range(H)]
#q = queue.Queue() #q.put(i) #q.get() 
#q = queue.LifoQueue() #q.put(i) #q.get() 

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b=copy.deepcopy(a)  #2次元配列はこうコピーする
#w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

#素数リスト
# n = 100
# primes = set(range(2, n+1))
# for i in range(2, int(n**0.5+1)):
#     primes.difference_update(range(i*2, n+1, i))
# primes=list(primes)

#bisect.bisect_left(a, 4)#aはソート済みである必要あり。aの中から4未満の位置を返す。rightだと以下
#bisect.insort_left(a, 4)#挿入
qs=[]
s=deque(list(S))
for i in range(Q):
    qs.append(list(input().split()))

rev=0
for q in qs:
    if int(q[0])==1: rev=(rev+1)%2
    else:
        if (int(q[1])+rev)%2==1:
            s.appendleft(q[2])
        else:
            s.append(q[2])
    print(s,rev)

if rev==0:
    for i in range(len(s)): print(s.popleft(), end="")
else:
    for i in range(len(s)): print(s.pop(), end="")
print()

Submission status: Wrong Answer

Solution code:
word = input().rstrip()
n = int(input())
hanten = 0
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        if len(word) == 1:
            pass
        else:
            hanten = (hanten + 1) % 2
            # word = word[::-1]
            # print(word)
    else:
        if (a[1] == "2" and hanten == 0) or (a[2] == "1" and hanten == 1):
            word = word + a[2]
            # print("rtuika")
        else:
            word = a[2] + word
            # print("ltuika")

if hanten == 0:
    print(word)
else:
    print(word[::-1])

Submission status: Wrong Answer

Solution code:
word = input().rstrip()
n = int(input())
for i in range(n):
    a = input().rstrip().split(" ")
    if a[0] == "1":
        if len(word) == 1:
            pass
        else:
            tmp = [word[0], word[1:-1], word[-1]]
            # print(tmp)
            word = tmp[2] + tmp[1] + tmp[0]
            # print(word)
    else:
        if a[1] == "2":
            word = word + a[2]
            # print("rtuika")
        else:
            word = a[2] + word
            # print("ltuika")
print(word)