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:
from collections import deque
A =input()
S = deque(A)
H = 0
Q = int(input())
Qlist = []
for i in range(Q):
    op = list((map(str,input().split())))
    if len(op) ==1:
        H +=1
    else:
        if op[1] =='1':
            if H%2 == 0:
                S.appendleft(op[2])
            else:
                S.append(op[2]) 
        else:
            if H%2 == 0:
                S.append(op[2])
            else:
                S.appendleft(op[2])
if H%2 == 1:
    A = S[-1]
    for i in range(1,len(S)):
        A += S[-i-1]
else:
    A = S[0]
    for i in range(1,len(S)):
        A += S[i]
print(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
A =input()
S = deque(A)
H = 0
Q = int(input())
Qlist = []
for i in range(Q):
    Qlist.append(list((map(str,input().split()))))
for i in range(Q):
    op = Qlist[i]
    if len(op) ==1:
        H +=1
    else:
        if op[1] =='1':
            if H%2 == 0:
                S.appendleft(op[2])
            else:
                S.append(op[2]) 
        else:
            if H%2 == 0:
                S.append(op[2])
            else:
                S.appendleft(op[2])
if H%2 == 1:
    A = S[-1]
    for i in range(1,len(S)):
        A += S[-i-1]
else:
    A = S[0]
    for i in range(1,len(S)):
        A += S[i]
print(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
A =input()
S = deque(A)
H = 0
Q = int(input())
Qlist = []
for i in range(Q):
    Qlist.append(list((map(str,input().split()))))
for i in range(Q):
    op = Qlist[i]
    if len(op) ==1:
        H +=1
    else:
        if op[1] =='1':
            if H%2 == 0:
                S.appendleft(op[2])
            else:
                S.append(op[2]) 
        else:
            if H%2 == 0:
                S.append(op[2])
            else:
                S.appendleft(op[2])
if H%2 == 1:
    S.reverse()
A = S[0]
for i in range(1,len(S)):
    A += S[i]
print(A)

Submission status: Time Limit Exceeded

Solution code:
A =input()
S = list(A)
Q = int(input())
Qlist = []
for i in range(Q):
    Qlist.append(list((map(str,input().split()))))
for i in range(Q):
    op = Qlist[i]
    if len(op) ==1:
        S.reverse()
    else:
        if op[1] =='1':
            S.reverse()
            S.append(op[2])
            S.reverse()
        else:
            S.append(op[2])
A = S[0]
for i in range(1,len(S)):
    A += S[i]
print(A)

Submission status: Time Limit Exceeded

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

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

        if ((not reverse) and F=="1") or (reverse and F=="2"):
            A+=C
        else:
            B+=C

if reverse:
    print(B[::-1]+S[::-1]+A)
else:
    print(A[::-1]+S+B)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
reverse=False
for _ in range(Q):
    T=input()
    if T[0]=="1":
        reverse=not reverse
    else:
        _,F,C=T.split()

        if ((not reverse) and F=="1") or (reverse and F=="2"):
            S=C+S
        else:
            S=S+C

if reverse:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
Q = tuple([_ for _ in input().split()] for _ in range(N))
C = 0
def move(s, q, c):
    if q[0] == "1":
        return s, (c+1)%2
    else:
        if (int(q[1])+c)%2 == 0:
            return s + q[2], c
        else:
            return q[2] + s, c
for q in Q:
    S, C = move(S, q, C)
if C != 0:
    S = S[::-1]
else:
    pass
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = int(input())
Q = tuple([_ for _ in input().split()] for _ in range(N))

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
p = 0
A = '0'
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        A += Qr[2]
      else:
        A = Qr[2]+A
    else:
      if p:
        A = Qr[2]+A
      else:
        A += Qr[2]
S = A.replace('0',S)
if p:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
p = 0
Q = int(input())
for _ in range(Q):
  Qr = list(input().split())
  if int(Qr[0])==1:
    p = not p
  else:
    if int(Qr[1])==1:
      if p:
        S += Qr[2]
      else:
        S = Qr[2]+S
    else:
      if p:
        S = Qr[2]+S
      else:
        S += Qr[2]
if p:
  print(S[::-1])
else:
  print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
B = []
res = 0
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    C = []
    A = list(map(str, input().split()))
    if len(A) == 3:
        C.append(A[2])
    if int(A[0]) == 1:
        if res == 0:
            res = 1
        else:
            res = 0
    else:
        if int(A[1]) == 1:
            if res == 0:
                B = C + B
            else:
                B.append(A[2])
        else:
            if res == 0:
                B.append(A[2])
            else:
                B = C + B
if res == 0:
    print(''.join(B))
else:
    B = B[::-1]
    print(''.join(B))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

d = deque(s)
for i in range(q):
  qs=list(input().split())
  
  if qs[0]=='1':
    d.reverse()
  else:
    if qs[1]=='1':
      d.appendleft(qs[2])
    else:
      d.append(qs[2])

print("".join(d))

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
p=0 #front=0 back=1
for i in range(Q):
    A="".join(input().split())
    if A[0]=="1":
        if p==0:p=1
        else:p=0
    else:
        if A[1]=="1":
            if p==0:
                S=A[2]+S
            else:
                S+=A[2]
        else:
            if p==0:
                S+=A[2]
            else:
                S=A[2]+S
                
print(S if p==0 else S[::-1])

Submission status: Time Limit Exceeded

Solution code:
s=input()
Q=int(input())
count = 0
for i in range(Q):
    q=input()
    if q[0]=='1':
        count += 1
    elif (q[2]=='1' and count%2==0) or (q[2]=='2' and count%2==1):
        s=q[4]+s
    elif (q[2]=='2' and count%2==0) or (q[2]=='1' and count%2==1):
        s=s+q[4]

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
Q=int(input())
for i in range(Q):
    q=input()
    if q[0]=='1':
        s=s[::-1]
    elif q[2]=='1':
        s=q[4]+s
    elif q[2]=='2':
        s=s+q[4]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
Q=int(input())
for i in range(Q):
    q=input()
    if q[0]=='1':
        s=s[::-1]
    elif q[0]=='2':
        if q[2]=='1':
            s=q[4]+s
        elif q[2]=='2':
            s=s+q[4]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S = input().rstrip()
Q = int(input())
Que = (input().rstrip() for _ in range(Q))

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if len(elem) == 1:
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S = elem[4] + S
    elif s == 2 and flag == 0:
      S += elem[4]
    elif s == 1 and flag == 1:
      S += elem[4]
    else:
      S = elem[4] + S

if flag == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Que = (input() for _ in range(Q))

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if len(elem) == 1:
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S = elem[4] + S
    elif s == 2 and flag == 0:
      S += elem[4]
    elif s == 1 and flag == 1:
      S += elem[4]
    else:
      S = elem[4] + S

if flag == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

flag = 0 #0:そのまま 1:反転
for _ in range(Q):
  elem = input().rstrip()
  if len(elem) == 1:
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S = elem[4] + S
    elif s == 2 and flag == 0:
      S = S + elem[4]
    elif s == 1 and flag == 1:
      S = S + elem[4]
    else:
      S = elem[4] + S

if flag == 1:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

rev = False

for i in range(q):
    inp = input()
    if inp == '1':
        rev = True if rev == False else False
    else:
        n = inp.split()[1]
        t = inp.split()[2]
        if n == '1':
            s = s + t if rev == True else t + s
        else:
            s = t + s if rev == True else s + t

if rev == True:
    s_rev = s[::-1]
    print(s_rev)
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = input()
a = deque(s)

q = int(input())

for i in range(q):
    inp = input()
    if inp == '1':
        a.reverse()
    else:
        if inp.split()[1] == '1':
            a.appendleft(inp.split()[2])
        else:
            a.append(inp.split()[2])
            
print(''.join(a))

Submission status: Time Limit Exceeded

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

head = 1

for i in range(q):
    if(a[i][0] == '1'):
        head *= -1
    else:
        if(a[i][1] == '2' and head == 1):
            s = s + a[i][2]
        elif(a[i][1] == '1' and head == -1):
            s = s + a[i][2]    
        else:
            s = a[i][2] + s
if(head == 1):
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
B = []
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    C = []
    A = list(map(str, input().split()))
    if len(A) == 3:
        C.append(A[2])
    if int(A[0]) == 1:
        B = B[::-1]
    else:
        if int(A[1]) == 1:
            B = C + B
        else:
            B.append(A[2])
print(''.join(B))

Submission status: Time Limit Exceeded

Solution code:
S = input()
B = []
for s in range(len(S)):
    B.append(S[s])
Q = int(input())
for q in range(Q):
    C = []
    A = list(map(str, input().split()))
    if len(A) == 3:
        C.append(A[2])
    if int(A[0]) == 1:
        B.reverse()
    else:
        if int(A[1]) == 1:
            B = C + B
        else:
            B.append(A[2])
print(''.join(B))

Submission status: Time Limit Exceeded

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

rev = False
for i in range(q):
  inp = list(input().split())
  if inp[0] == '1':
    rev = not rev
  else:
    f = inp[1]
    c = inp[2]
    if rev:
      if f == '1':
        f = '2'
      else:
        f = '1'
    if f == '1':
      s = c + s
    else:
      s = s + c
if rev:
  s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
#D
s_list=list(input())
q=int(input())
r=0
for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        r+=1
    else:
        if (int(ope[1])+r)%2 == 1:
            s_list.insert(0,ope[2])
        else:
            s_list.append(ope[2])
if r%2 !=0:
    s_list.reverse()
print(''.join(s_list))

Submission status: Time Limit Exceeded

Solution code:
#D
s_list=list(input())
q=int(input())

for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        s_list.reverse()
    else:
        if ope[1]=='1':
            s_list=list(ope[2])+s_list
        else:
            s_list=s_list+list(ope[2])
print(''.join(s_list))

Submission status: Time Limit Exceeded

Solution code:
#D
s_list=list(input())
q=int(input())

for i in range(q):
    ope=input().split()
    if ope[0]=='1':
        s_list.reverse()
    else:
        if ope[1]=='1':
            s_list.insert(0,ope[2])
        else:
            s_list.append(ope[2])
print(''.join(s_list))

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
q = int(input())
ls = [list(map(str,input().split())) for _ in range(q)]
def rev(ls):
    return ls[::-1]
def addi_f(s,t):
    return t+s
def addi_b(s,t):
    return s+t
for i in range(q):
    if ls[i] == ["1"]:
        s = rev(s)
    else:
        if ls[i][1] == "1":
            s = addi_f(s,ls[i][2])
        else:
            s = addi_b(s,ls[i][2])
print(s)

Submission status: Time Limit Exceeded

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

flag_swap = False
for i in range(q):
  query = list(input().split())
  if query[0] == "1":
    flag_swap = not flag_swap
  else:
    if (query[1] == "1" and not flag_swap) or (query[1] == "2" and flag_swap):
      s = query[2] + s
    else:
      s += query[2]

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

Submission status: Time Limit Exceeded

Solution code:
string = list(input())
inp = string
n = int(input())
flag = 1
for i in range(n):
    j = input().split()
    if len(j) == 3 and j[1] == "1" and flag == 1:
        inp.insert(0,j[2])
    elif len(j) == 3 and j[1] == "2" and flag == 1:
        inp.append(j[2])
    elif len(j) == 3 and j[1] == "2" and flag == -1:
        inp.insert(0,j[2])
    elif len(j) == 3 and j[1] == "1" and flag == -1:
        inp.append(j[2])
    else:
        flag *= -1
if flag == -1:
    inp = inp[::-1]
s = ""
for j in inp:
    s = s + j
print(s)

Submission status: Time Limit Exceeded

Solution code:
string = list(input())
n = int(input())
while n:
    n-=1
    inp = input().split()
    if inp[0]=='1':
        string =string[::-1]
    else:
        if inp[1]=='1':
            string.insert(0,inp[2])
        else:
            string.append(inp[2])
s=''
for i in range(len(string)):
    s+=string[i]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
c=0
for i in range(q):
    s1=input()
    if s1[0]=='1':
        c+=1
    else:
        if s1[2]=='1':
            if c%2==0:
                s=s1[4]+s
            else:
                s+=s1[4]
        else:
            if c%2==0:
                s+=s1[4]
            else:
                s=s1[4]+s
if c%2==0:
    print(s)
else:
    print(s[::-1])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
rev=False
count=0
while(q):
    q-=1
    t=list(input().split())
    if t[0]=='2':
        f=t[1]
        if rev and count%2==1:
            if f=='1':
                f='2'
            else:
                f='1'
        c=t[2]
        if f=='1':
            s=c+s
        else:
            s=s+c
    else:
        count+=1
        rev=True
    #print(s)
if count%2!=0:
    for i in range(len(s)-1,-1,-1):
      print(s[i],end="")
else:
	print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
rev=False
count=0
while(q):
    q-=1
    t=list(input().split())
    if t[0]=='2':
        f=t[1]
        if rev and count%2==1:
            if f=='1':
                f='2'
            else:
                f='1'
        c=t[2]
        if f=='1':
            s=c+s
        else:
            s=s+c
    else:
        count+=1
        rev=True
    #print(s)
if count%2!=0:
    s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
while(q):
    q-=1
    t=list(input().split())
    if t[0]=='2':
        f=t[1]
        c=t[2]
        if f=='1':
            s=c+s
        else:
            s=s+c
    else:
        s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from _collections import deque

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip()
q = int(input())
query = []
rev = 0
for i in range(q):
    query.append(list(map(str, input().split())))
for i in range(q):
    if len(query[i]) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if query[i][1] == '1':
                s = query[i][2] + s
            else:
                s = s + query[i][2]
        else:
            if query[i][1] == '2':
                s = query[i][2] + s
            else:
                s = s + query[i][2]
if rev == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip()
q = int(input())
query = []
rev = 0
for i in range(q):
    query.append(list(map(str, input().split())))
for i in range(q):
    if len(query[i]) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if int(query[i][1]) == 1:
                s = query[i][2] + s
            else:
                s = s + query[i][2]
        else:
            if int(query[i][1]) == 2:
                s = query[i][2] + s
            else:
                s = s + query[i][2]
if rev == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query = []
rev = 0
for i in range(q):
    query.append(list(map(str, input().split())))
for i in range(q):
    if len(query[i]) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if int(query[i][1]) == 1:
                s = query[i][2] + s
            else:
                s = s + query[i][2]
        else:
            if int(query[i][1]) == 2:
                s = query[i][2] + s
            else:
                s = s + query[i][2]
if rev == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
rev = 0
for i in range(q):
    query = list(map(str, input().split()))
    if len(query) == 1:
        rev = (rev + 1) % 2
    else:
        if rev == 0:
            if int(query[1]) == 1:
                s = query[2] + s
            else:
                s = s + query[2]
        else:
            if int(query[1]) == 2:
                s = query[2] + s
            else:
                s = s + query[2]
if rev == 1:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline()
s = s[:-1]
q = int(input())

rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = ~rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            s = s + ci
        else:
            s = ci + s

if rv:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = ~rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            s.append(ci)
        else:
            s.insert(0, ci)

if rv:
    for i in range(1, len(s)+1):
        print(s[-i], end='')
    print('')
else:
    for si in s:
        print(si, end='')
    print('')

Submission status: Time Limit Exceeded

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

rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = ~rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            s = s + ci
        else:
            s = ci + s
if rv:
    for i in range(1, len(s)+1):
        print(s[-i], end='')
    print('')
else:
    print(s)

Submission status: Time Limit Exceeded

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

rv = False
for i in range(q):
    line = input()
    if line[0] == '1':
        rv = ~rv
    else:
        ti, fi, ci = line.split()
        if (fi == '1' and rv) or (fi == '2' and not rv):
            s = s + ci
        else:
            s = ci + s
if rv:
    print(s[::-1])
else:
    print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput().split()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt: #反転の状態
        if f == "1": s.append(c)
        if f == "2": s.insert(0,c)
    else: #ノーマル状態
        if f == "1": s.insert(0,c)
        if f == "2": s.append(c)
    #print(s)

s = "".join(s)
s = s[::-1] if cnt else s
print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput().split()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt: #反転の状態
        if f == "1": s = s + [c]
        if f == "2": s = [c] + s
    else: #ノーマル状態
        if f == "1": s = [c] + s
        if f == "2": s = s + [c]
    #print(s)

s = "".join(s)
s = s[::-1] if cnt else s
print(s)

Submission status: Time Limit Exceeded

Solution code:

import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]


s = sinput()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt:
        if f == "1": s = s + c
        if f == "2": s = c + s
    else:
        if f == "1": s = c + s
        if f == "2": s = s + c
    #print(s)

s = s[::-1] if cnt else s
print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput()
q = iinput()
cnt = 0
for i in range(q):
    try: t,f,c = sarr()
    except: cnt += 1; cnt %= 2; continue
    if cnt:
        if f == "1": s = s + c
        if f == "2": s = c + s
    else:
        if f == "1": s = c + s
        if f == "2": s = s + c
    #print(s)

s = s[::-1] if cnt else s
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    read = sys.stdin.buffer.read
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines

    S = readline().decode().strip()
    Q = int(readline())

    cnt = 0

    #空の文字列にqueryで追加する文字を加えていく
    ss = " "
    for _ in range(Q):
        query = list(input().split())
        if query[0] == "1":
            cnt += 1
        else:
            if (query[1] == "1" and cnt%2==0) or (query[1] == "2" and cnt%2==1):
                ss = "".join([query[2],ss])
            else:
                ss = "".join([ss,query[2]])

    #反転回数が奇数の場合、元の文字列とqueryにより作成した文字列を反転
    if cnt%2 == 1:
        S = S[::-1]
        ss = ss[::-1]

    #スペースに元の文字列を追加
    print(ss.replace(" ", S))

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

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

    cnt = 0

    #空の文字列にqueryで追加する文字を加えていく
    ss = " "
    for _ in range(Q):
        query = list(input().split())
        if query[0] == "1":
            cnt += 1
        else:
            if (query[1] == "1" and cnt%2==0) or (query[1] == "2" and cnt%2==1):
                ss = "".join([query[2],ss])
            else:
                ss = "".join([ss,query[2]])

    #反転回数が奇数の場合、元の文字列とqueryにより作成した文字列を反転
    if cnt%2 == 1:
        S = S[::-1]
        ss = ss[::-1]

    #スペースに元の文字列を追加
    print(ss.replace(" ", S))

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
a=1
for i in range(Q):
  if ls[i][0]=="1":
    a*=(-1)
  else:
    if (ls[i][1]=="1" and a==1) or (ls[i][1]=="2" and a==-1):
      S.insert(0,ls[i][2])
    else:
      S.append(ls[i][2])
print("".join(S) if a==1 else "".join(reversed(S)))

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
Q=int(input())
ls=[list(input().split()) for _ in range(Q)]
for i in range(Q):
  if ls[i][0]=="1":
    S.reverse()
  else:
    if ls[i][1]=="1":
      S.insert(0,ls[i][2])
    else:
      S.append(ls[i][2])
print("".join(S))

Submission status: Time Limit Exceeded

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

d = 1
S1 = ''
S2 = ''
for i in range(n):
    Q = list(input().split())
    if Q[0] == '1':
        d *= -1
    else:
        f = Q[1]
        c = Q[2]
        if f == '1':
            if d == 1:
                S1 += c
            else:
                S2 += c
        else:
            if d == 1:
                S2 += c
            else:
                S1 += c
if d == 1:
    ans = S1[::-1] + S + S2
else:
    ans = S2[::-1] + S[::-1] + S1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
querys=[0]*q
for i in range(q):
  x = input().split()
  if len(x) == 1:
    querys[i] = [int(x[0])]
  else:
    querys[i] = [int(x[0]), int(x[1]), x[2]]
trans=0
for query in querys:
  if query[0]==1:
    trans += 1
  elif (query[1] & 1) ^ (trans & 1):
    s = query[2] + s
  else: # 後
    s += query[2]
res = ''
if trans % 2 == 0:
  res = s
else:
  res = ''.join([s[len(s)-i-1] for i in range(len(s))])
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

ANS = deque(S)

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        ANS.reverse()
    else:
        f, c = query[1:]
        if f == '1':
            ANS.appendleft(c)
        else:
            ANS.append(c)

print(''.join(ANS))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

ANS = deque(S)

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        ANS = list(ANS)
        ANS = ANS[::-1]
        ANS = deque(ANS)
    else:
        f, c = query[1:]
        if f == '1':
            ANS.appendleft(c)
        else:
            ANS.append(c)

print(''.join(ANS))

Submission status: Time Limit Exceeded

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

ANS = S

for _ in range(Q):
    query = input().split(' ')
    if len(query) == 1:
        ANS = ANS[::-1]
    else:
        f, c = query[1:]
        if f == '1':
            ANS = c + ANS
        else:
            ANS = ANS + c

print(ANS)