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: Time Limit Exceeded

Solution code:
def main():

    s = input()
    q = int(input())
    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"]) % 2 == 1

    new_query = []
    flag = 0
    for q in query:
        if len(q) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
        else:
            new_query.append(q + [flag])

    for q in new_query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

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

main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
query = [input().split() for _ in range(int(input()))]
ctn = query.count(["1"]) % 2 == 1

new_query = []
flag = 0
for q in query:
    if len(q) == 1:
        if flag == 0:
            flag = 1
        else:
            flag = 0
    else:
        new_query.append(q + [flag])

for q in new_query:
    if q[-1] == 0:
        if q[1] == "1":
            s = q[2] + s
        else:
            s = s + q[2]
    else:
        if q[1] == "1":
            s = s + q[2]
        else:
            s = q[2] + s

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

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    query = []
    flag = 0
    ctn = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
            ctn += 1
        else:
            query.append(temp + [flag])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    query = []
    flag = 0
    ctn = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            if flag == 0:
                flag = 1
            else:
                flag = 0
            ctn += 1
        else:
            query.append(temp + [flag])

    for q in query:
        if q[-1] == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

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

query = []
flag = 0
ctn = 0
for _ in range(q):
    temp = input().split()
    if len(temp) == 1:
        if flag == 0:
            flag = 1
        else:
            flag = 0
        ctn += 1
    else:
        query.append(temp + [flag])

for q in query:
    if q[-1] == 0:
        if q[1] == "1":
            s = q[2] + s
        else:
            s = s + q[2]
    else:
        if q[1] == "1":
            s = s + q[2]
        else:
            s = q[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    query = [tuple(input().split()) for _ in range(q)]
    ctn = query.count(("1",))

    flag = 0
    for q in query:
        if q[0] == "1":
            if flag == 1:
                flag = 0
            else:
                flag = 1
        else:
            if flag == 0:
                if q[1] == "1":
                    s = q[2] + s
                else:
                    s = s + q[2]
            else:
                if q[1] == "1":
                    s = s + q[2]
                else:
                    s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    query = [input().split() for _ in range(q)]
    ctn = query.count(["1"])

    flag = 0
    for q in query:
        if q[0] == "1":
            if flag == 1:
                flag = 0
            else:
                flag = 1
        else:
            if flag == 0:
                if q[1] == "1":
                    s = q[2] + s
                else:
                    s = s + q[2]
            else:
                if q[1] == "1":
                    s = s + q[2]
                else:
                    s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    ctn = 0
    query = []
    for _ in range(q):
        temp = input().split()
        if len(temp) == 1:
            ctn += 1
        query.append(temp)

    flag = 0
    for q in query:
        if q[0] == "1":
            if flag == 1:
                flag = 0
            else:
                flag = 1
        else:
            if flag == 0:
                if q[1] == "1":
                    s = q[2] + s
                else:
                    s = s + q[2]
            else:
                if q[1] == "1":
                    s = s + q[2]
                else:
                    s = q[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

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

ctn = 0
query = []
for _ in range(q):
    temp = input().split()
    if len(temp) == 1:
        ctn += 1
    query.append(temp)

flag = 0
for q in query:
    if q[0] == "1":
        if flag == 1:
            flag = 0
        else:
            flag = 1
    else:
        if flag == 0:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]
        else:
            if q[1] == "1":
                s = s + q[2]
            else:
                s = q[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

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

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = [query[-1]] + s
                else:
                    s = s + [query[-1]]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + [query[-1]]
                else:
                    s = [query[-1]] + s

    if ctn % 2 == 1:
        print("".join(s[::-1]))
    else:
        print("".join(s))

main()

Submission status: Time Limit Exceeded

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

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[-1] + s
                else:
                    s = s + query[-1]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[-1]
                else:
                    s = query[-1] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

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

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                else:
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                else:
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

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

    flag = 1
    ctn = 0
    for _ in range(q):
        query = input().split()
        if flag:
            if query[0] == "1":
                flag = 0
                ctn += 1
            else:
                if query[1] == "1":
                    s = query[2] + s
                elif query[1] == "2":
                    s = s + query[2]
        else:
            if query[0] == "1":
                flag = 1
                ctn += 1
            else:
                if query[1] == "1":
                    s = s + query[2]
                elif query[1] == "2":
                    s = query[2] + s

    if ctn % 2 == 1:
        print(s[::-1])
    else:
        print(s)

main()

Submission status: Time Limit Exceeded

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

flag = 1
ctn = 0
for _ in range(q):
    query = input().split()
    if flag:
        if query[0] == "1":
            flag = 0
            ctn += 1
        else:
            if query[1] == "1":
                s = query[2] + s
            elif query[1] == "2":
                s = s + query[2]
    else:
        if query[0] == "1":
            flag = 1
            ctn += 1
        else:
            if query[1] == "1":
                s = s + query[2]
            elif query[1] == "2":
                s = query[2] + s

if ctn % 2 == 1:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    query = []
    ctn = 0
    flag = 0

    for _ in range(q):
        temp = input().split()
        if len(temp) == 3:
            if flag and ctn % 2 == 1:
                query.append(["1"])
                query.append(temp)
                ctn = 0
            else:
                query.append(temp)
        else:
            if flag == 1:
                ctn += 1
            else:
                flag = 1
                ctn += 1
                
    if ctn % 2 == 1:
        query.append(["1"])

    for q in query:
        if q[0] == "1":
            s = s[::-1]
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():

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

    query = []
    ctn = 0
    flag = 0
    for _ in range(q):
        temp = input().split()
        if len(temp) == 3 and flag:
            if ctn % 2 == 1:
                query.append(["1"])
            query.append(temp)
            ctn = 0
        elif len(temp) == 3:
            query.append(temp)
        else:
            if flag == 1:
                ctn += 1
            else:
                flag = 1
                ctn += 1
    if flag and ctn % 2 == 1:
        query.append(["1"])

    for q in query:
        if q[0] == "1":
            s = s[::-1]
        else:
            if q[1] == "1":
                s = q[2] + s
            else:
                s = s + q[2]

    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    for _ in range(int(input())):
        temp = input().split()
        if temp[0] == "1":
            s = s[::-1]
        else:
            if temp[1] == "1":
                s = temp[2] + s
            elif temp[1] == "2":
                s = s + temp[2]
    print(s)

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = list(input())
Q = int(input())
answer = deque(S)
reverse = False
for _ in range(Q):
  query = input()
  if query == '1':
    reverse ^= True
  else:
    ope, types, alphabet = query.split()
    if types == '1':
      if reverse:
        answer.append(alphabet)
      else:
        answer.appendleft(alphabet)
    else:
      if reverse:
        answer.appendleft(alphabet)
      else:
        answer.append(alphabet)
if reverse:
  result = ''
  for i in range(len(answer)):
    result += answer.pop()
  print(result)
else:
  result = ''
  for i in range(len(answer)):
    result += answer.popleft()
  print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
	S = list(input())
	reverse = False
	n_q = int(input())
	for i in range(n_q):
		q = input().split(" ")
		if q[0] == "1":
			reverse = not reverse
		elif q[0] == "2":
			f = q[1]
			c = q[2]
			if (f == "1" and not reverse) or (f == "2" and reverse):
				S.insert(0, c)
			elif (f == "1" and reverse) or (f == "2" and not reverse):
				S.append(c)
	if reverse:
		S.reverse()
	print("".join(S))

main()

Submission status: Time Limit Exceeded

Solution code:

s = input()

q = int(input())


for i in range(q):
    li = input().split()
    if li[0] == "2":
        if li[1] =="1":
            s = li[2] + s

        else:
            s = s + li[2]

    else:
        s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:

s = input()

q = int(input())


for i in range(q):
    li = input().split()
    if li[0] == "2":
        if li[1] =="1":
            s = li[2] + s

        elif li[1] == "2":
            s = s + li[2]

    elif li[0] == "1":
        s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

reverse = False

for i in range(Q):
    Query = tuple(input().split())
    if int(Query[0]) == 1:
        reverse = not reverse
    else:
        if reverse:
            if int(Query[1]) == 1:
                S += Query[2]
            else:
                S = Query[2] + S
        else:
            if int(Query[1]) == 1:
                S = Query[2] + S
            else:
                S += Query[2]

if reverse:
    S = "".join(list(reversed(S)))

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()

q = int(input())

for i in range(q):
    li = list(map(str, input().split()))
    if li[0] == "2":
        if li[1] =="1":
            s = li[2] + s

        elif li[1] == "2":
            s = s + li[2]

    elif li[0] == "1":
        s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

Solution code:
S,Q=input(),int(input())
reverse=0
for i in range(Q):
    T=input().split()
    if T[0]=='1':reverse=0 if reverse else 1
    else:
        if T[1]=='1':
            if reverse:S=S+T[2]
            else:S=T[2]+S
        else:
            if reverse:S=T[2]+S
            else:S=S+T[2]
s=list(S);s.reverse()
print(''.join(s) if reverse else S)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = input()
q = int(input())
S = [c for c in s]
deque = deque(S)
reversed = 0
for i in range(q):
    line = input()
    if line[0] == '1':
        reversed = reversed ^ 1
    else:
        op, f, c = line.split()
        f = int(f)
        if reversed == 0:
            if f == 1:
                deque.appendleft(c)
            else:
                deque.append(c)
        else:
            if f == 1:
                deque.append(c)
            else:
                deque.appendleft(c)
if reversed == 0:
    deque.reverse()
result = ""
for i in range(len(deque)):
    result += deque.pop()
print(result)

Submission status: Time Limit Exceeded

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

for _ in range(q):
    t = list(input().split())
    if t[0] == '1':
        s = s[::-1]
    else:
        if t[1] == '1':
            s = t[2] + s
        else:
            s = s + t[2]
print(s)

Submission status: Time Limit Exceeded

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

flg = True
for q in Q:
    if q[0] == "1":
        flg = not flg
    else:
        F = int(q[1])
        if not flg:
            F = 3 - F
        if F == 1:
            S = q[2] + S
        if F == 2:
            S = S + q[2]

if not flg:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

flg = True
for q in Q:
    if q[0] == "1":
        flg = not flg
    else:
        if (q[1] == "1" and flg) or (q[1] == "2" and not flg):
            S = q[2] + S
        else:
            S = S + q[2]
if not flg:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

flg = True
for q in Q:
    if q[0] == "1":
        flg = not flg
    else:
        if q[1] == "1" and flg:
            S = q[2] + S
        elif q[1] == "1" and not flg:
            S = S + q[2]
        elif q[1] == "2" and flg:
            S = S + q[2]
        else:
            S = q[2] + S

if not flg:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

for q in Q:
    if q[0] == "1":
        S = S[::-1]
    else:
        if q[1] == "1":
            S = q[2] + S
        else:
            S = S + q[2]
print(S)

Submission status: Time Limit Exceeded

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

S=pin()[:-1]
Q=int(pin())
now=1

for i in range(Q):
  D=pin().split()
  T=0
  F=0
  C=""
  if len(D)==1:
    T=int(D[0])
  else:
    T=int(D[0])
    F=int(D[1])
    C=D[2]
  if T==1:
    now*=-1
  else:
    if F==1:
      if now==1:
        S=C+S
      else:
        S=S+C
    else:
      if now==1:
        S=S+C
      else:
        S=C+S
if now==1:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

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

S=pin()[:-1]
Q=int(pin())
now=1

for i in range(Q):
  D=pin().split()
  T=0
  F=0
  C=""
  if len(D)==1:
    T=int(D[0])
  else:
    T=int(D[0])
    F=int(D[1])
    C=D[2]
  if T==1:
    now*=-1
  else:
    if F==1:
      if now==1:
        S=C+S
      else:
        S=S+C
    else:
      if now==1:
        S=S+C
      else:
        S=C+S
if now==1:
  print(S)
else:
  print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline
S = input().rstrip()
input()
rev = 0
for line in readlines():
    q, *query, = line.split()
    if q == '1':
        rev ^= 1
    else:
        f, c = query
        if f == '1':
            if not rev:
                S = c+S
            else:
                S = S+c
        else:
            if not rev:
                S = S+c
            else:
                S = c+S

print(S if not rev else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
for i in range(q):
    query=input()
    if query=="1":
        s.reverse()
    elif query[2]=="1":
        s.appendleft(query[-1])
    else:
        s.append(query[-1])
print(*s,sep="")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=deque(list(input()))
q=int(input())
for i in range(q):
    query=input()
    if query[0]=="1":
        s.reverse()
        continue
    if query[2]=="1":
        s.appendleft(query[4])
    else:
        s.append(query[4])
print(*s,sep="")

Submission status: Time Limit Exceeded

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

cnt=0

for i in range(q):
  x=input()
  if x=='1':
    cnt+=1
    
  elif x[0]=='2':
    if (x[2]=='1' and cnt%2==0) or (x[2]=='2' and cnt%2==1):
      s.insert(0,x[4])
      
    else:
      s+=x[4]
      
if cnt%2==1:
  s.reverse()
  

print(''.join(s))

Submission status: Time Limit Exceeded

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

cnt=0

for i in range(q):
  x=input()
  if x=='1':
    cnt+=1
    
  elif x[0]=='2':
    if (x[2]=='1' and cnt%2==0) or (x[2]=='2' and cnt%2==1):
      s=x[4]+s
      
    else:
      s=s+x[4]
      
if cnt%2==1:
  s=s[::-1]
  

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
f = 1
a = [0] * Q

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

for i in range(Q):
    if int(a[i][0]) == 1:
        f *= -1
    else:
        a[i][1] = int(a[i][1]) - 1.5
        if a[i][1] * f > 0:
            S = S + a[i][2]
        else:
            S = a[i][2] + S

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    a = list(input().split())
    if int(a[0]) == 1:
        f *= -1
    else:
        a[1] = int(a[1]) - 1.5
        if a[1] * f > 0:
            S = S + a[2]
        else:
            S = a[2] + S

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

Submission status: Time Limit Exceeded

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

for i in range(Q):
    a = list(input().split())
    if int(a[0]) == 1:
        S = S[::-1]
    elif int(a[0]) == 2 and int(a[1]) == 1:
        S = a[2] + S
    else:
        S = S + a[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque


def LS():
    return list(input().split())


S = deque(input())
Q = int(input())
rev = 0
for i in range(Q):
    A = LS()
    if A[0] == "1":
        rev += 1
        rev %= 2
    else:
        if A[1] == "1":
            if rev == 0:
                S.appendleft(A[2])
            else:
                S.append(A[2])
        else:
            if rev == 0:
                S.append(A[2])
            else:
                S.appendleft(A[2])
if rev == 0:
    for i in range(len(S)):
        print(S[i], end="")
else:
    for i in range(len(S)-1, -1, -1):
        print(S[i], end="")

Submission status: Time Limit Exceeded

Solution code:
import sys


def LS():
    return list(input().split())


S = input()
Q = int(input())
rev = 0
for i in range(Q):
    A = LS()
    if A[0] == "1":
        rev += 1
        rev %= 2
    else:
        if A[1] == "1":
            if rev == 0:
                S = A[2]+S
            else:
                S = S+A[2]
        else:
            if rev == 0:
                S = S+A[2]
            else:
                S = A[2]+S
if rev == 0:
    print(S)
    sys.exit()
else:
    S = list(S)
    S.reverse()
for i in range(len(S)):
    print(S[i], end="")

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for i in range(Q):
  tq = list(input().split())
  if (len(tq) == 1):
    query.append(1)
  if (len(tq) == 3):
    query.append([int(tq[1]),tq[2]])
eo = 1
for q in query:
  if q == 1:
    eo = eo*(-1)
  else:
    eoq = eo*q[0]
    if eoq == 1 or eoq == -2:
      S = q[1] + S
    else:
      S = S + q[1]
if eo == -1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = []
for i in range(Q):
  tq = list(input().split())
  if (len(tq) == 1):
    query.append(1)
  if (len(tq) == 3):
    query.append([int(tq[1]),tq[2]])
for q in query:
  if q == 1:
    S = S[::-1]
  else:
    if q[0] == 1:
      S = q[1] + S
    else:
      S = S + q[1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
def LS():
    return list(input().split())


S = input()
Q = int(input())
rev = 0
for i in range(Q):
    A = LS()
    if A[0] == "1":
        rev += 1
        rev %= 2
    else:
        if A[1] == "1":
            if rev == 0:
                S = A[2]+S
            else:
                S = S+A[2]
        else:
            if rev == 0:
                S = S+A[2]
            else:
                S = A[2]+S
if rev == 0:
    ans = S
else:
    ans = S[::-1]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

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

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

s = S()
q = I()
# Q = LS(q)

# print(Q)

flag = True
for i in range(q):
    d = input().split()
    if len(d)==1:
        flag = not flag
    elif d[1]=='1':
        if flag:
            s = d[2] + s
        else:
            s = s + d[2]
    elif d[1]=='2':
        if flag:
            s = s + d[2]
        else:
            s = d[2] + s
# print(s)
if flag:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

def I():
    return int(input())

def MI():
    return map(int,input().split())

def RI():
    return list(map(int,input().split()))

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

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

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

s = S()
q = I()
Q = LS(q)

# print(Q)

flag = True
for i in range(q):
    d = Q[i]
    if len(d)==1:
        flag = not flag
    elif d[2]=='1':
        if flag:
            s = d[4] + s
        else:
            s = s + d[4]
    elif d[2]=='2':
        if flag:
            s = s + d[4]
        else:
            s = d[4] + s
# print(s)
if flag:
    print(s)
else:
    print(s[::-1])