Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

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

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

If T_i = 1: reverse the string S.

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

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

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

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

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

Operation 2: Reverse S. S becomes ap.

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

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

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

Sample Output 2

aabc

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

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



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


Sample output:
cpa

Submission status: Wrong Answer

Solution code:
from sys import stdin
input = stdin.readline
S = input()
Q = int(input())
f = 0
for i in range(Q):
  Query = input().split()
  if Query[0] == '1':
    f = 1 - f
  else:
    if int(Query[1]) - f == 1:
      S = Query[2] + S
    else:
      S = S + Query[2]
if f == 0:
  print(S)
else:
  print(S[::-1])

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
bool = True
import sys
def input1():
	return sys.stdin.readline()[:-1]
for i in range(0, Q-1):
	s = input1()
	print(s)
	if len(s) == 1:
		bool = not bool
	else:
		if s[2] == "1":
			if bool:
				S = s[-1] + S
			else:
				S += s[-1]
		else:
			if bool:
				S += s[-1]
			else:
				S = s[-1] + S

s = input()
print(s)
if len(s) == 1:
	bool = not bool
else:
	if s[2] == "1":
		if bool:
			S = s[-1] + S
		else:
			S += s[-1]
	else:
		if bool:
			S += s[-1]
		else:
			S = s[-1] + S
if bool:
	print(S)
else:
	print(S[::-1])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
from collections import deque
s=deque([input()])
q=int(input())

for _ in range(q):
	t=input()
	if t[0]=="1":
		s.reverse()
	else:
		t,f,c=t.split()
		if f=="1":
			s.appendleft(c)
		else:
			s.append(c)
print(*s,sep="")

Submission status: Wrong Answer

Solution code:
import sys
s = sys.stdin.readline().rstrip()
q = int(sys.stdin.readline())

pre = ''
suf = ''
count = 0
for i in range(q):
    ts = sys.stdin.readline().rstrip().split()
    if ts[0] == '1':
        count += 1
    else:
        t = int(ts[0])
        f = int(ts[1])
        c = ts[2]
        if f == 1:
            strs = []
            if count % 2 == 1:                
                strs = [suf, c]
                suf = ''.join(strs)
            else:
                strs = [pre, c]
                pre = ''.join(strs)
        else:
            if count % 2 == 1:
                strs = [pre, c]
                pre = ''.join(strs)
            else:
                strs = [suf, c]
                suf = ''.join(strs)
        

strs  = []
if count % 2 == 0:
    strs = [pre[::-1], s, suf[::-1]]
else:
    strs = [suf, s[::-1], pre]

print (''.join(strs))

Submission status: Wrong Answer

Solution code:
from collections import deque
s = deque()
s.append(input())
q = int(input())
lines = [list(input().split()) for i in range(q)]
def q1(s):
    for i in range(len(s) // 2):
        s[i],s[-1-i] = s[-1-i],s[i]
    return s

def q2(s, l):
    if int(l[1]) == 1:
        s.appendleft(l[2])
    else:
        s.append(l[2])
    return s

for l in lines:
    if int(len(l)) == 1:
        q1(s)
    else:
        q2(s, l)

print(*s, sep='')

Submission status: Wrong Answer

Solution code:
s = input() # start string
str_list = [s]
q = input() # count
q = int(q)
for i in range(q):
  qq = input().split()
  if qq[0] == "1":
    str_list.reverse()
  else:
    if qq[1] == "1":
      str_list = qq[2:] + str_list
    else:
      str_list.append(qq[2])
print("".join(str_list))

Submission status: Wrong Answer

Solution code:
#D
#入力
S=input()
Q=int(input())
#Query=[]
#for i in range(Q):
#    Query.append(list(input().split()))
    
    
#処理
t=0
b=""
a=""
for j in range(Q):
    i=list(input().split())
    if i[0]=="1":
        t+=1
    elif i[0]=="2":
        if t%2==0:
            if i[1]=="1":
                b=i[2]+b
            elif i[1]=="2":
                a=a+i[2]
        elif t%2==1:
            if i[1]=="1":
                a=i[2]+a
            elif i[1]=="2":
                b=b+i[2]
if t%2==0:
    print(b+S+a)
    
else:
    print(a+S[::-1]+b)

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    S = input()
    S = deque([c for c in S])
    Q = int(input())
    rvCt = 0

    for _ in range(Q):
        query = input().split()
        if query[0] == '1':
            rvCt += 1
        else:
            doReverse = False
            if rvCt % 2 == 1:
                doReverse = True
                # rvCt = 0

            if query[1] == '1':
                if doReverse:
                    S.appendleft(query[2])
                else:
                    S.appendleft(query[2])
            else:
                if doReverse:
                    S.appendleft(query[2])
                else:
                    S.append(query[2])

    if rvCt % 2 == 1:
        S.reverse()
    
    print(''.join(S))

main()

Submission status: Wrong Answer

Solution code:
def main():
    S = list(input())

    Q = int(input())

    for _ in range(Q):
        a = tuple(input().split())
        if a[0] == "2":
            if a[1] == "1":
                S.insert(0, a[2])
            else:
                S.append(a[2])
        else:
            if len(S) > 1:
                S[0], S[-1] = S[-1], S[0]

    print("".join(S))

    return 0


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

for i in range(Q):                
  if int(Q_list[i][0]) == 1:
    if len(S) >= 2:
      S = S[-1] + S[1:-2] + S[0]
  
  else:
    F = Q_list[i][1]
    C = Q_list[i][2]
    
    if int(F) == 1:
      S = C + S
    else:
      S = S + C
      
print(S)

Submission status: Wrong Answer

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

for x in q:
    if x[0]=='1':
        l = len(s)-1
        if l == 1:
            a = s[0]
            b = s[1]
            s = b + a
        elif l > 1:
            a = s[0]
            b = s[l]
            s = b + s[1:l] + a
    elif x[0]=='2':
        if x[1]=='1':
            s = x[2] + s
        elif x[1]=='2':
            s += x[2]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
sousa = []
for i in range(Q):
  sousa.append(input().split())

rev = 1
for j in range(Q):
  if sousa[j][0] == '1':
    rev += 1
  else:
    if rev%2 == 1:
      S.reverse()
    rev = 0
    
    if sousa[j][1] == '1':
      S.appendleft(sousa[j][2])
    else:
      S.append(sousa[j][2])
if rev%2 == 1:
  S.reverse()
  
Ans = ''.join(list(S))
print(Ans)

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 = list(input())
Q = int(input())
l_list = []
r_list = []
flag = True
for i in range(Q):
    A = list(map(str, input().split()))
    if len(A)==1:
        flag = not flag
    else:
        if flag:
            if A[1]=="1":
                l_list.insert(0, A[2])
            else:
                r_list.append(A[2])
        else:
            if A[1]=="1":
                r_list.insert(0, A[2])
            else:
                l_list.append(A[2])
if flag:
    print("".join(l_list[::-1])+"".join(S)+"".join(r_list))
else:
    print("".join(r_list[::-1])+"".join(S[::-1])+"".join(l_list))

Submission status: Wrong Answer

Solution code:
def main():
    s = str(input())
    q = int(input())
    s = s.strip()
    a = [''] * (10**5*2)
    a.extend(list(s))
    a.extend([''] * (10**5*2))
    start = 10**5*2
    end = start + len(s) - 1
    for i in range(q):
        query = input().split()
        if query[0] == '1':
            tmp = a[start]
            a[start] = a[end]
            a[end] = tmp
        elif query[0] == '2':
            if query[1] == '1':
                a[start-1] = query[2]
                start -= 1
            elif query[1] == '2':
                a[end+1] = query[2]
                end += 1
    print(''.join(a[start:end+1]))


main()

Submission status: Wrong Answer

Solution code:
word = input()
words = [word]
number = int(input())
for i in range(number):
    a = input().split(" ")
    if a[0] == "1":
        words[0], words[-1] = words[-1], words[0]
    else:
        if a[1] == "1":
            words = [a[2]] + words
        else:
            words = words + [a[2]]
for j in words:
    print(j,end="")

Submission status: Wrong Answer

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

sum = 0
cnt = 0
for i in range(Q):
    q = input()
    if q == "1":
        cnt += 1
        sum += 1
    elif cnt%2 == 0:
        cnt = 0
        if q[2] == "1":
            S = q[-1] + S
        if q[2] == "2":
            S = S + q[-1]
    elif cnt%2 == 1:
        cnt = 0
        if q[2] == "1":
            S = S + q[-1]
        if q[2] == "2":
            S = q[-1] + S

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

Submission status: Wrong Answer

Solution code:
s = input()
S = []
for i in range(len(s)):
  S.append(s[i])
Q = int(input())
flag = 0
for _ in range(Q):
    q = list(input().split())
    if q[0] == "1":
        flag += 1
    else:
        if flag % 2 == 0:         
            if q[1] == "1":
                S.insert(0,q[2])
            else:
                S.insert(-1,q[2])
        else:
            if q[1] == "1":
                S.insert(-1,q[2])
            else:
                S.insert(0,q[2])
if flag%2 == 0:
    S = "".join(S)
    print(S)
else:
    S = S[::-1]
    S = "".join(S)
    print(S)

Submission status: Wrong Answer

Solution code:
S = list(input())
Q = int(input())
x = 1
for i in range(Q):
    a, *b = input().split()
    if a == "1":
        x *= -1
    elif a == "2":
        c, d = b
        if c == "2":
            x *= -1 
        if x == 1:
            S.insert(0, d)
        elif x == -1:
            S.append(d)
print(*S, sep="")

Submission status: Wrong Answer

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

flag = True
for i in range(q):
    now = query[i]
    if len(s) >= 3:
        if flag:
            l = s[0]
            r = s[-1]
            flag = False

        if now == ['1']:
            l, r = r[-1] + l[1:], r[:-1] + l[0]
        else:
            f = now[1]
            c = now[2]
            if f == '1':
                l = c + l
            elif f == '2':
                r = r + c
    else:
        if now == ['1']:
            if len(s) != 1:
                s = s[-1] + s[1:-1] + s[0]
            else:
                continue
        else:
            f = now[1]
            c = now[2]
            if f == '1':
                s = c + s
            elif f == '2':
                s = s + c

if len(s) <= 2:
    print(s)
else:
    print(l + s[1:-1] + r)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
P = []
for n in range(Q):
    P.append(input().split())
ans = [S]
for i in P:
    if len(i) == 1:
        ans.reverse()
    else:
        if i[1] == "1":
            ans = [i[2]] + ans
        else:
            ans = ans + [i[2]]
print("".join(ans))

Submission status: Wrong Answer

Solution code:
S=input()
Q=int(input())
cnt=0
for i in range(Q):
    query_list=input().split()
    if query_list[0] == "1":
        cnt+=1
    else:
        if cnt%2==0:
            if query_list[1] == "1":
                S=query_list[2]+S
            else:
                S+=query_list[2]
        else:
            if query_list[1] == "1":
                S+=query_list[2]
            else:
                S=query_list[2]+S
print(S)

Submission status: Wrong Answer

Solution code:
import sys
S = str(input())
Q = int(input())
Query = [list(map(str, input().split())) for _ in range(Q)]

sArry = S.split()
for i in range(Q):
    if Query[i][0] == '1':
        tmp_first = sArry[0]
        sArry[0] = sArry[-1]
        sArry[-1] = tmp_first
    else:
        if Query[i][1] == '1':
            sArry.insert(0, Query[i][2])
        else:
            sArry.append(Query[i][2])
#    print("  ", i, Query[i], "".join(sArry))

            
print("".join(sArry))

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]
      
print(inital_string)

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 q[0] == '1' or q[0] == 1:
            S.reverse()
        elif q[0] == '2':
            if q[1] == '1':
                tmp = [q[2]]
                tmp.extend(S)
                S = tmp
            elif q[1] == '2':
                S.append(q[2])
    print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()
q=int(input())
q_list=[input().split() for _ in range(q)]
for i in range(q):
  temp=q_list[i]
  print(temp)
  if int(temp[0])==1:
    temps=s[::-1]
    s=temps
  elif int(temp[0])==2:
    if int(temp[1])==1:
      s=temp[2]+s
    elif int(temp[1])==2:
      s=s+temp[2]
print(s)

Submission status: Wrong Answer

Solution code:
s = input()
qn = int(input())
q = []
rev = False

for i in range(qn):
    qi = input().split()
    if qi[0] == "1":
        rev = not rev
    else:
        if rev:
            if qi[1] == "1":
                qi[1] = "2"
            else:
                qi[1] = "1"
        q.append((2, int(qi[1]), qi[2]))
for i in range(len(q)):
    if q[i][0] == 2:
        if q[i][1] == 1:
            s = q[i][2] + s
        else:
            s = s + q[i][2]
print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
ans = [S]
for _ in range(0,Q):
    tmp = input().split(" ")
    if tmp[0] == "1":
        ans.reverse()
    elif tmp[0] == "2":
        if tmp[1] == "1":
            ans.insert(0,tmp[2])
        else:
            ans.append(tmp[2])

print("".join(ans))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input().split()
s=deque(s)
a=int(input())
for i in range(a):
  n=input().split()
  if(n[0]=="1"):
    s.reverse()
  else:
    if(n[1]=="1"):
      s.appendleft(n[2])
    else:
      s.append(n[2])
s=''.join(s)
print(s)

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  query = input().split()
  if len(query) == 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:
s = list(input())
q = int(input())
query = []
for i in range(q):
    a = input().split()
    query.append(list(a))
top = []
bot = []
ifrev = 0
for i in query:
    q1 = int(i[0])
    if q1 == 1:
        if ifrev == 0:
            ifrev = 1
        else :
            ifrev = 0
    else:
        q2 = int(i[1])
        q3 = i[2]
        if q2 == 1:
            if ifrev == 0:
                top.append(q3)
            else:
                bot.append(q3)
        else:
            if ifrev == 0:
                bot.append(q3)
            else:
                top.append(q3)
top.reverse()
a1 = top
if ifrev == 0:
    a2 = s
else:
    s.reverse()
    a2 = s
a3 = bot
ans = a1+a2+a3
if ifrev == 1:
    ans.reverse()
print("".join(ans))

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]+S2)
else:
    print((S2[::-1])[:-1]+S1)

Submission status: Wrong Answer

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

top=[]
bot=[]
cnt=0
for i in range(Q):
    if q[i][0]=='1':
        cnt+=1
    if q[i][0]=='2':
        if cnt%2==0:
            if q[i][1]=='1':
                top+=q[i][2]
            if q[i][1]=='2':
                bot+=q[i][2]
        else:
            if q[i][1]=='1':
                bot+=q[i][2]
            if q[i][1]=='2':
                top+=q[i][2]

ans=[S]
if cnt%2==1:
    ans=ans[::-1]
    bot=bot[::-1]
    print(''.join(bot+ans+top))
else:
    top=top[::-1]
    print(''.join(top+ans+bot))

Submission status: Wrong Answer

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

isRev = False

for i in range(q):
    query = list(input().split())
    if len(query) == 1:
        isRev = not isRev
    else:
        if query[1] == '1':
            s = query[2] + s
            isRev = False
        elif isRev and query[1] == '2':
            s = query[2] + s
            isRev = True
        elif query[1] == '2':
            s = s + query[2]
            isRev = False
        elif isRev and query[1] == '1':
            s = s + query[2]
            isRev = True
            
if isRev:
    s = s[::-1]
        
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque

S  = deque()
S.append(input().strip())

N = int(input().strip())

for _ in range(N):
    ope = input().strip()
    if ope[0] == "1":
        S.reverse()
        continue
    i, s, c = ope.split(" ")
    if s == "1":
        S.appendleft(c)
    else:
        S.append(c)

print(''.join(list(S)))

Submission status: Wrong Answer

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

for i in range(q):
    query = str(input())
    #前後を反転
    if query == '1' and len(s) > 1:
        s = s[len(s) - 1:] + s[1:len(s) - 1] + s[:1]
        continue

    querys = list(map(str, query.split()))
    #文字を付加
    if querys[1] == '1':
        s = querys[2] + s
    else:
        s = s + querys[2]
print(s)

Submission status: Wrong Answer

Solution code:
from collections import deque
S = deque(input())
Q = int(input())
sousa = []
for i in range(Q):
  sousa.append(input().split())

rev = 1
for j in range(Q):
  if sousa[j][0] == '1':
    rev += 1
  else:
    if rev%2 == 1:
      S.reverse()
    rev = 0
    
    if sousa[j][1] == '1':
      S.appendleft(sousa[j][2])
    else:
      S.append(sousa[j][2])
Ans = ''.join(list(S))
print(Ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

s = input()
t = s[::-1]
ss = deque(s)
tt = deque(s)
isS = True
n = int(input())
query = []

# print(q1, q2)

for i in range(n):
    query.append(list(input().split()))

# print(query)

for i in range(n):
    # Reverse
    if query[i][0] == '1':
        # s = s[::-1]
        isS = not isS
    # Add
    else:
        # To Head
        if query[i][1] == '1':
            if isS:
                # s = query[i][2] + s
                # t += query[i][2]
                ss.appendleft(query[i][2])
                tt.append(query[i][2])
            else:
                # s += query[i][2]
                # t = query[i][2] + t
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
        # To Tail
        else:
            if isS:
                # s += query[i][2]
                # t = query[i][2] + t
                ss.append(query[i][2])
                tt.appendleft(query[i][2])
            else:
                # s = query[i][2] + s
                # t += query[i][2]
                ss.appendleft(query[i][2])
                tt.append(query[i][2])

if isS:
    # print(s)
    # print(str(s))
    ans = ''
    for i in range(len(ss)):
        ans += ss.popleft()
    print(ans)
else:
    # print(t)
    # print(str(tt))
    ans = ''
    for i in range(len(tt)):
        ans += tt.popleft()
    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
sta = 1
for _ in range(q):
    t = list(map(str, input().split()))
    if t[0] == "1":
        sta *= -1
    elif t[1] == "1":
        sta *= -1
        if sta == -1:
            s = t[2]+s
        else:
            s = s + t[2]
    else:
        if sta == 1:
            s = t[2]+s
        else:
            s = s + t[2]
print(s)

Submission status: Wrong Answer

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

rev = [0] #反転する回数
list = [s] #単語のリスト

for i in range(q):
    t = input().split()

    if int(t[0]) == 1:
        list = list[::-1]
    else:
        if int(t[1]) == 1:
            list.insert(0,t[2])
        else:
            list.append(t[2])

for i in range(len(list)):
    print(list[i],end='')

print()

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 q[0] == '1':
            S.reverse()
        elif q[0] == '2':
            if q[1] == '1':
                tmp = [q[2]]
                tmp.extend(S)
                S = tmp
            elif q[1] == '2':
                S.append(q[2])
    print("".join(S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
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:
#!/usr/bin/env python3
import sys


def solve(S, Q, Qvery):
    pool = []
    fw = True
    for qvery in reversed(Qvery):
        if qvery[0] == 1:
            if fw:
                fw = False
            else:
                fw = True
        else:
            if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  # 先頭に描く
                print(qvery[2], end='')
            else:
                pool.append(qvery[2])
    rev_S = S[::-1]
    print(rev_S, end='')
    for p in reversed(pool):
        print(p, end='')
    print()

    # fw = True
    # ret = list(S)
    # # ret.append(S)
    # for qvery in Qvery:
    #     if qvery[0] == 1:
    #         # fw = not fw
    #         if fw:
    #             fw = False
    #         else:
    #             fw = True
    #     else:
    #         if qvery[1] == 1 and fw or qvery[1] == 2 and not fw:  # 先頭に追加
    #             ret.insert(0, qvery[2])
    #             # S = qvery[2] + S
    #         else:  # 末尾に追加
    #             ret.append(qvery[2])
    #             # S = S + qvery[2]

    # if fw:
    #     print(''.join(ret))
    # else:
    #     rev_ret = ret[::-1]
    #     print(''.join(rev_ret))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = str(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    Qvery = []
    for i in range(Q):
        T = int(next(tokens))
        if T == 1:
            Qvery.append([1])
        elif T == 2:
            Qvery.append([2, int(next(tokens)), str(next(tokens))])
    solve(S, Q, Qvery)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
Q = int(input())
m = True
for i in range(Q):
  q = input()
  if q == '1':
    m = not m
  else:
    _, f, c = q.split(' ')
    f = int(f)
    if (m and f == '2') or (not m and f == '1'): #元順で末尾または逆順で先頭
      S = S + c
    else:
      S = c + S
if not m:
  S = S[::-1]
print(S)

Submission status: Wrong Answer

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

q_list = []

rev_c = 0
count = 0

for _ in range(q):
  v = input()
  q_list.append(v)
  if v == '1':
    rev_c += 1

if rev_c % 2 == 0:
  s = s[::-1]
  
for v in q_list:
  if v == '1':
    count += 1
  else:
    num = v[2]
    char = v[4]
    if ((rev_c-count)%2==0 and num=='1') or  ((rev_c-count)%2==1 and num == '2'):
      s = char + s
    else:
      s = s + char
print(s)

Submission status: Wrong Answer

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

right=[]
left=[]
flag=0

for i in range(q):
    a = input()
    if int(a[0]) == 1:
        if flag == 0:
            flag=1
        else:
            flag=0
    else:
        if int(a[2]) == 1:
            if flag == 0:
                right.append(a[4])
            else:
                left.insert(0,a[4])
        elif int(a[2]) ==2:
            if flag ==0:
                left.insert(0,a[4])
            else:
                right.append(a[4])

right_result="".join(right)
left_result="".join(left)

if flag==0:
    print(str(right_result[::-1]+s[::-1]+left_result))
else:
    print(str(left_result[::-1]+s+right_result))

Submission status: Wrong Answer

Solution code:
from collections import deque
S=list(input())
Q=int(input())
Qlst=[list(input().split()) for _ in range(Q)]

len_S=len(S)
s0=deque(sorted(S[:len_S//2]))
s1=deque(S[len_S//2:])

if len_S%2!=0:
    mid=s1.popleft()
else:
    mid=''

fflag=0
for i in range(Q):
    q=Qlst[i]
    if q[0]=='1':
        fflag=abs(fflag-1)
        continue
    if q[1]=='1':
        if fflag==0:
            s0.append(q[2])
            if mid=='':
                mid=s0.popleft()
            else:
                s1.appendleft(mid)
                mid=''
        else:
            s1.append(q[2])
            if mid=='':
                mid=s1.popleft()
            else:
                s0.append(mid)
                mid=''
    elif q[1]=='2':
        if fflag==0:
            s1.append(q[2])
            if mid=='':
                mid=s1.popleft()
            else:
                s0.append(mid)
                mid=''
        else:
            s0.append(q[2])
            if mid=='':
                mid=s0.popleft()
            else:
                s1.append(mid)
                mid=''

if fflag==0:
    s0.reverse()
    print(''.join(s0)+mid+''.join(s1))
else:
    s1.reverse()
    print(''.join(s1)+mid+''.join(s0))

Submission status: Wrong Answer

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

for x in q:
    if x[0]=='1':
        l = len(s)-1
        if l > 0:
            a = s[0]
            b = s[l]
            s = b + s[1:l] + a
    elif x[0]=='2':
        if x[1]=='1':
            s = x[2] + s
        elif x[1]=='2':
            s += x[2]
print(s)

Submission status: Wrong Answer

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

flg = "moto"

atama = list()
shippo = list()

for _ in range(Q):
    tmp = list(map(str, input().split()))
    if flg == "moto":
        if int(tmp[0]) == 1:
            flg = "hanten"
        else:
            if int(tmp[1]) == 1:
                atama.append(tmp[2])
            else:
                shippo.append(tmp[2])
    
    else:
        if int(tmp[0]) == 1:
            flg = "moto"
        else:
            if int(tmp[1]) == 1:
                shippo.append(tmp[2])
            else:
                atama.append(tmp[2])

if flg == "moto":
    atama = atama[::-1]

    print("".join(atama) + S + "".join(shippo))

else:
    shippo = shippo[::-1]

    print("".join(shippo) + S + "".join(atama))

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
hanten = 0
check = 0
for i in range(q):
    l = list(map(str, input().split()))
    if l[0] == "1":
        hanten += 1
        check = 1
    else:
        if l[1] == "1":
            if check == 0:
                s = l[2] + s
            else:
                s = s + l[2]
        else:
            if check == 0:
                s = s + l[2]
            else:
                s = l[2] + s
        check = 0

if hanten%2 == 1:
    s = s[::-1]

print(s)

Submission status: Wrong Answer

Solution code:
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
l = r = ""
for op in qs:
    if op[0] == '1':
        n = not n
    if op[0] == '2':
        t , f, c = op
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f != 1:
            l=c+l
        elif f == 2:
            r+=c
s = l+s+r
print(s[::-1] if n else s)

Submission status: Wrong Answer

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

hanten = 0
t = 0
for i in range(Q):
    quary = input().split()
    if quary[0]=='1':
        #S = S[::-1]
        hanten = hanten ^ 1
    else:
        t = int(quary[1]) ^ hanten
        if t:
            S = quary[2] + S
        else:
            S = S + quary[2]
    #print(S)
if hanten:
    S = S[::-1]
print(S)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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