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:
s = input()
N = int(input())
S = []
order = 1
for _ in range(N):
    res = input().split()
    if res[0] == "1":
        order = order * - 1
    elif res[1] == "1":
        if order == 1:
            s = res[2]+s
        else:
            s = s+res[2]
    else:
        if order == 1:
            s = s+res[2]
        else:
            s = res[2]+s

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

Submission status: Time Limit Exceeded

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

order = 1
for res in S:
    if res[0] == "1":
        order = order * - 1
    elif res[1] == "1":
        if order == 1:
            s = res[2]+s
        else:
            s = s+res[2]
    else:
        if order == 1:
            s = s+res[2]
        else:
            s = res[2]+s

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
dummy = ""
for i in range(q):
  q_list = input().split()
  if q_list[0] == "1":
    s.reverse()
  else:
    if q_list[1] == "1":
      s.insert(0,q_list[2])
    else:
      s.append(q_list[2])
print("".join(s))

Submission status: Time Limit Exceeded

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


flg = True
for i in range(Q):
    que = input()
    if que == "1":
        if flg:
            flg = False
        else:
            flg = True
    else:
        j, F, C = que.split()
        if int(F) == 1:
            if flg:
                S = C + S
            else:
                S = S + C
        else:
            if flg:
                S = S + C
            else:
                S = C + S

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

Submission status: Time Limit Exceeded

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


flg = True
for i in range(Q):
    que = input()
    if que == "1":
        if flg:
            flg = False
        else:
            flg = True
    else:
        j, F, C = que.split()
        if int(F) == 1:
            if flg:
                S = [C] + S
            else:
                S = S + [C]
        else:
            if flg:
                S = S + [C]
            else:
                S = [C] + S

if flg:
    for s in S:
        print(s, end="")
else:
    for s in reversed(S):
        print(s, end="")

Submission status: Time Limit Exceeded

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

for i in range(Q):
    que = input()
    if que == "1":
        S.reverse()
    else:
        j, F, C = que.split()
        if int(F) == 1:
            S = [C] + S
        else:
            S = S + [C]

for s in S:
    print(s, end="")

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
query=[input() for i in range(q)]
for i in range(q):
  if query[i][0]=="1":
    tmp=""
    for j in range(len(s)):
      tmp+=s[-1-j]
    s=tmp
  else:
    a=query[i].split()
    if a[1]=="1":
      s=a[2]+s
    else:
      s=s+a[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=input()
n=int(input())
for i in range(0,n):
  a=list(map(str,input().split()))
  if len(a)==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
rev=0
ST=""
EN=""
S=input()
Q=int(input())
for i in [0]*Q:
 A=list(input().split())
 if int(A[0])==1 :
  rev+=1
 else:
  if (int(A[1])+rev)%2==1:
   ST+=A[2]
  else:
   EN+=A[2]
if rev%2==0:
 print(ST[::-1].strip()+S.strip()+EN.strip())
else:
 print(EN[::-1].strip()+S[::-1].strip()+ST.strip())

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
ls=[input() for _ in range(q)]
be=""
af=""
stri=""
  
for i in ls:
  if i[0]=="1":
    ans=be+ans+af
    for j in range(len(ans)-1,-1,-1):
      stri+=ans[j]
    ans=stri
    be,af,stri="","",""
    
  elif i[2]=="1":
    be=i[4]+be
    
  elif i[2]=="2":
    af+=i[4]
      
print(be+ans+af if be!="" or af!="" else ans)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
ls=[input() for _ in range(q)]
be=""
af=""
  
for i in ls:
  if i[0]=="1":
    ans=(be+ans+af)[::-1]
    be,af="",""
    
  elif i[2]=="1":
    be=i[4]+be
    
  elif i[2]=="2":
    af+=i[4]
      
print(be+ans+af if be!="" or af!="" else ans)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
ls=[]
be=""
af=""

for i in range(q):
  qu=input()
  ls.append(qu)
  
for i in ls:
  if i[0]=="1":
    ans=(be+ans+af)[::-1]
    be,af="",""
    
  elif i[2]=="1":
    be=i[4]+be
    
  elif i[2]=="2":
    af+=i[4]
      
print(be+ans+af if be!="" or af!="" else ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=ans[::-1]
    
  elif qu[2]=="1":
    ans=qu[4]+ans
    
  elif qu[2]=="2":
    ans+=qu[4]
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
be=""
af=""

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=(be+ans+af)[::-1]
    be,af="",""
    
  elif qu[2]=="1":
    be=qu[4]+be
    
  elif qu[2]=="2":
    af+=qu[4]
      
print(be+ans+af if be!="" or af!="" else ans)

Submission status: Time Limit Exceeded

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

order = True
for i in query:
    if i[0] == "1":
        order = not order
    elif i[1] == "1":
        if order:
            s = i[2] + s
        else:
            s += i[2]
    else:
        if order:
            s += i[2]
        else:
            s = i[2] + s
if not order:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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


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

print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query_list = [list(input().split()) for _ in range(q)]
for query in query_list:
    if query[0] == '1':
        #s = ''.join(list(reversed(s)))
        s = s[::-1]
    elif query[0] == '2':
        if query[1] == '1':
            s = query[2] + s
        elif query[1] == '2':
            s = s + query[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S=input()
Q=int(input())
Rev=True
front=''
back=''
for i in range(Q):
    a=input()
    if a=='1':
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == '1':
            if Rev:
                front += x
            else:
                back += x
        else:
            if Rev:
                back += x
            else:
                front += x
if Rev:
    print(front[::-1] + S + back)
else:
    print(back[::-1]+S[::-1]+front)

Submission status: Time Limit Exceeded

Solution code:
import math
S=input()
Q=int(input())
Rev=True
front=''
back=''
for i in range(Q):
    a=input()
    if a=='1':
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == '1':
            if Rev:
                front += x
            else:
                back += x
        else:
            if Rev:
                back += x
            else:
                front += x
if Rev:
    print(front[::-1] + S + back)
else:
    print(back[::-1]+S[::-1]+front)

Submission status: Time Limit Exceeded

Solution code:
import math
S=input()
Q=int(input())
Rev=True
front=""
back=""
for i in range(Q):
    a=input()
    if a=="1":
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == "1":
            if Rev:
                front += x
            else:
                back += x
        else:
            if Rev:
                back += x
            else:
                front += x
if Rev:
    print(front[::-1] + S + back)
else:
    print(back[::-1]+S[::-1]+front)

Submission status: Time Limit Exceeded

Solution code:
import math
S=input()
Q=int(input())
Rev=True
front=""
back=""
for i in range(Q):
    a=input()
    if a=="1":
        Rev=not(Rev)
    else:
        b,c,x=a.split()
        if c == "1":
            if Rev:
                front += x
            else:
                back += x
        if c == "2":
            if Rev:
                back += x
            else:
                front += x
if Rev:
    print(front[::-1] + S + back)
else:
    print(back[::-1]+S[::-1]+front)

Submission status: Time Limit Exceeded

Solution code:
import math
S=input()
Q=int(input())
Rev=1
front=""
back=""
for i in range(Q):
    a=input()
    if a=="1":
        Rev=Rev*-1
    else:
        b,c,x=a.split()
        if (c=="1"and Rev==1) or (c=="2" and Rev==-1):
            front+=x
        else:
            back+=x
if Rev==1:
    print(front[::-1] + S + back)
else:
    print(back[::-1]+S[::-1]+front)

Submission status: Time Limit Exceeded

Solution code:
import math
S=input()
Q=int(input())
Rev=1
front=""
back=""
for i in range(Q):
    a=input().split()
    if a[0]=="1":
        Rev=Rev*-1
        continue
    if (a[1]=="1"and Rev==1) or (a[1]=="2" and Rev==-1):
        front+=a[2]
    else:
        back+=a[2]
if Rev==1:
    #str(front.reverse().extend(S).extend(back))
    print(front[::-1]+S+back)
    #str(front.reverse()+S+back)
else:
    print(back[::-1]+S[::-1]+front)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=input()
q=int(input())
t=[input() for i in range(q)]

l=deque()

for i in range(int(len(s))):
  l.append(s[i])

for i in range(q):

  if int(t[i][0])==1:
    l.reverse()

  else:   
    if int(t[i][2])==1:
      l.appendleft(t[i][4]) 
    
    else:
      l.append(t[i][4])

print(''.join(l))

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
be=""
af=""

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=(be+ans+af)[::-1]
    be,af="",""
    
  else:
    if qu[2]=="1":
      be=qu[4]+be
    else:
      af+=qu[4]
      
print(be+ans+af if be!="" or af!="" else ans)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
be=""
af=""

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=(be+ans+af)[::-1]
    be=""
    af=""
    
  else:
    if qu[2]=="1":
      be=qu[4]+be
    else:
      af+=qu[4]
      
print(be+ans+af if be!="" or af!="" else ans)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
be=""
af=""

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=be+ans+af
    be=""
    af=""
    ans=ans[::-1]
    
  else:
    if qu[2]=="1":
      be=qu[4]+be
    else:
      af+=qu[4]
      
print(be+ans+af)

Submission status: Time Limit Exceeded

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

for i in range(q):
  qu=input()
  
  ans=ans[::-1] if qu[0]=="1" else qu[4]+ans if qu[2]=="1" else ans+qu[4]
  
print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(n):
    i = list(input())

    if i[0] == '1': s=s[::-1]

    else:
        if i[2] == '1':  s=i[4] + s
        else: s+=i[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:

s=input()
n = int(input())

for _ in range(n):
    i = list(input())

    if i[0] == '1': s=s[::-1]

    else:
        if i[2] == '1':  s=i[4] + s
        else: s+=i[4]

print(s)

Submission status: Time Limit Exceeded

Solution code:
def string():
    s = input()
    q = int(input())
    while q > 0:
        t = list(input().strip().split())
        if int(t[0]) == 1:
            s = s[::-1]
        else:
            if int(t[1]) == 2:
                s += t[2]
            else:
                s = t[2] + s
        q -= 1
    print(s)
string()

Submission status: Time Limit Exceeded

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

for i in range(0,Q):
    if q[i][0] == '1':
        s = s[::-1]
    else:
        if q[i][1] == '1':
            s = q[i][2]+ s
        else:
            s += q[i][2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
qu=[input()for _ in range(q)]

for i in range(q):
  
  if qu[i][0]=="1":
    ans=ans[::-1]
      
  else:
    if qu[i][2]=="1":
      ans=qu[i][4]+ans
    else:
      ans=ans+qu[i][4]
      
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=ans[::-1]
      
  else:
    if qu[2]=="1":
      ans=qu[4]+ans
    else:
      ans=ans+qu[4]
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
stri=""

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    ans=ans[::-1]
      
  elif qu[0]=="2":
    if qu[2]=="1":
      ans=qu[4]+ans
    else:
      ans=ans+qu[4]
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s  = input()
q  = int(input())
inv = 1
sfb = [0,"",""]
for i in range(q):
    ls = list(input().split())
    if ls[0] == "1":
        inv *=-1
    else:
        f,c = ls[1:]
        if f == "1":
            sfb[inv] +=c
        else:
            sfb[-inv] +=c

ans = sfb[1][::-1] + s + sfb[-1]
print(ans[::inv])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def main():
    input = stdin.readline
    S = [input()[:-1]]
    Q = int(input())
    L = []
    R = []
    is_turned = False

    for _ in range(Q):
        query = input()[:-1]
        if query == "1":
            is_turned = not is_turned
        else:
            T, F, C = query.split()
            if is_turned ^ (F == "1"):
                L.insert(0, C)
            else:
                R.append(C)

    if (is_turned):
        print("".join(L + S + R)[::-1])
    else:
        print("".join(L + S + R))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def main():
    input = stdin.readline
    S = [input()[:-1]]
    Q = int(input())
    is_turned = False

    for _ in range(Q):
        query = input()[:-1]
        if query == "1":
            is_turned = not is_turned
        else:
            T, F, C = query.split()
            if is_turned ^ (F == "1"):
                S.insert(0, C)
            else:
                S.append(C)

    if (is_turned):
        print("".join(S)[::-1])
    else:
        print("".join(S))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = [input()]
    Q = int(input())
    is_turned = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_turned = not is_turned
        else:
            T, F, C = query.split()
            if is_turned ^ (F == "1"):
                S.insert(0, C)
            else:
                S.append(C)

    if (is_turned):
        print("".join(S)[::-1])
    else:
        print("".join(S))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = [input()]
    Q = int(input())
    is_turned = False

    for _ in range(Q):
        query = input()
        if query == "1":
            is_turned = not is_turned
        else:
            T, F, C = query.split()
            if is_turned ^ (F == "1"):
                S = [C] + S
            else:
                S = S + [C]

    if (is_turned):
        print("".join(S)[::-1])
    else:
        print("".join(S))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

    for _ in range(Q):
        query = input()
        if query == "1":
            is_turned = not is_turned
        else:
            T, F, C = query.split()
            if is_turned ^ (F == "1"):
                S = C + S
            else:
                S = S + C

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# 入力
S = input()
Q = int(input())

counter = 0

for i in range(Q):
    # i+1 回目のinput
    proc = input().split()

    if proc[0] == "1":
        counter += 1
    else:
        # 先頭に文字列を足す場合
        if (counter%2 == 0 and proc[1] == "1") or (counter%2 == 1 and proc[1] == "2"):
            S = proc[2] + S
        else:
            S = S + proc[2]

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

Submission status: Time Limit Exceeded

Solution code:
ans=input()
q=int(input())
stri=""

for i in range(q):
  qu=input()
  
  if qu[0]=="1":
    for j in range(len(ans)-1,-1,-1):
      stri+=ans[j]
    ans=stri
    stri=""
      
  elif qu[0]=="2":
    if qu[2]=="1":
      ans=qu[4]+ans
    else:
      ans=ans+qu[4]
      
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(q):
  que = input()
  if que[0] == str(1):
    s = s[::-1]
  elif que[2] == str(1):
    s = que[4] + s
  else:
    s = s + que[4]
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
rev = False
for _ in range(Q):
    Query = input().split()
    if Query[0] == '1':
        rev = not rev
    else:
        if rev:
            if Query[1] == '1':
                S = S + Query[2]
            else:
                S = Query[2] + S
        else:
            if Query[1] == '1':
                S = Query[2] + S
            else:
                S = S + Query[2]
if rev:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

num = 0
while num < Q:
    T = input().split()
    if len(T) > 1:
        F = int(T.pop(1))
        C = T.pop(1)
        if F == 1:
            S.insert(0, C)
        elif F == 2:
            S.append(C)
    else:
        S.reverse()

    num += 1

print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    n = int(input())
    for _ in range(n):
        head = ''
        tail = ''
        q = input()
        if q[0] == '1':
            s = s[::-1]
        else:
            _, f, c = q.split()
            if f == '1':
                head = c
            else:
                tail = c
        s = head + s + tail
    return s
print(main())

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().strip()
n = int(sys.stdin.readline().strip())
for _ in range(n):
    head = ''
    tail = ''
    q = sys.stdin.readline().strip()
    if q[0] == '1':
        s = s[::-1]
    else:
        _, f, c = q.split()
        if f == '1':
            head = c
        else:
            tail = c
    s = head + s + tail
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for _ in range(n):
    head = ''
    tail = ''
    q = input()
    if q[0] == '1':
        s = s[::-1]
    else:
        _, f, c = q.split()
        if f == '1':
            head = c
        else:
            tail = c
    s = head + s + tail
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

s = deque(input().rstrip())
q = int(input())
for i in range(q):
    query = input()
    if int(query[0]) == 1:
        s.reverse()
    else:
        _, f, c = query.split()
        if int(f) == 1:
            s.appendleft(c)
        else:
            s.append(c)

print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  s = list(input())
  s = s[:-1]
  s = deque(s)
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]

  for i in c:
    if i[0] == '1':
      s = deque(reversed(s))
    else:
      if i[1] == '1':
        s.appendleft(i[2])
      else:
        s.append(i[2])
  print(''.join(s))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  from collections import deque
  s = list(input())
  s = s[:-1]
  s = deque(s)
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]

  for i in c:
    if i[0] == '1':
      s.reverse()
    else:
      if i[1] == '1':
        s.appendleft(i[2])
      else:
        s.append(i[2])
  print(''.join(s))
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  s = input()
  s = s.rstrip('\r\n')
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]

  for i in c:
    if i[0] == '1':
      s = s[::-1]
    else:
      if i[1] == '1':
        s = i[2] + s
      else:
        s = s + i[2]
  print(s)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = input()
  q = int(input())
  c = [[i for i in input().split()] for j in range(q)]

  for i in c:
    if i[0] == '1':
      s = s[::-1]
    else:
      if i[1] == '1':
        s = i[2] + s
      else:
        s = s + i[2]
  print(s)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
query_list = [list(input().split()) for _ in range(q)]
for query in query_list:
    if query[0] == '1':
        s = ''.join(list(reversed(s)))
    elif query[0] == '2':
        if query[1] == '1':
            s = query[2]+s
        elif query[1] == '2':
            s = s + query[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = input()
  q = int(input())
  for i in range(q):
    que = input().split()
    if que[0] == '1':
      s = s[::-1]
    else:
      if que[1] == '1':
        s = que[2] + s
      else:
        s = s + que[2]
  print(s)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

a = 0
for i in range(Q):
    t = input()
    if t == "1":
        if a == 0:
            a = 1
        else:
            a = 0
    else:
        t,f,c = t.split()
        if f == "1":
            if a == 0:
                S = c + S
            else:
                S = S + c
        else:
            if a == 0:
                S = S + c
            else:
                S = c + S
            
if a == 1:
    S = S[::-1]
    
print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    t = input()
    if t == "1":
        S = S[::-1]
    else:
        t,f,c = t.split()
        if f == "1":
            S = c + S
        else:
            S = S + c
            
print(S)

Submission status: Time Limit Exceeded

Solution code:
def RevFlag(flag):
    if flag:
        return False
    else:
        return True

def main():
    S = input()
    N = int(input())
    rev = False
    for i in range(N):
        Q = [i for i in input().split()]
        if len(Q) == 1:
            rev = RevFlag(rev)
        else:
            F = int(Q[1])
            C = Q[2]
            if F == 1:
                if rev:
                    S = '{}{}'.format(S, C)  # 末尾
                else:
                    S = '{}{}'.format(C, S)  # 先頭
            else:
                if rev:
                    S = '{}{}'.format(C, S)  # 先頭
                else:
                    S = '{}{}'.format(S, C)  # 末尾
    if rev:
        S = ''.join(list(reversed(S)))
    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    N = int(input())
    for i in range(N):
        Q = [i for i in input().split()]
        if len(Q) == 1:
            S = ''.join(list(reversed(S)))
        else:
            F = int(Q[1])
            C = Q[2]
            if F == 1:
                S = '{}{}'.format(C, S)
            else:
                S = '{}{}'.format(S, C)
    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(q):
    q_very = input()

    #print(q_very)
    if q_very[0] == '1':
        #print('t is 1')
        cnt += 1
    else:
        instruction = q_very.split()
        # 反転していない
        if cnt % 2 == 0:
            if instruction[1] == '1':
                s = instruction[2] + s
            else:
                s = s + instruction[2]
        else:
            if instruction[1] == '1':
                s = s + instruction[2]
            else:
                s = instruction[2] + s

if cnt % 2 != 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def sousa(s, l):
    if(l[0]=="1"):
        s.reverse()
    if(l[0]=="2"):
        if(l[1]=="1"):
            s.insert(0,l[2])
        elif (l[1]=="2"):
            s.append(l[2])
    return s

s = list(input())
q = int(input())
li = []
for i in range(q):
    li.append(input().split())
    s = sousa(s, li[i])

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
def sousa(s, l):
    if(l[0]=="1"):
        s.reverse()
    if(l[0]=="2"):
        if(l[1]=="1"):
            s.insert(0,l[2])
        if(l[1]=="2"):
            s.append(l[2])
    return s

s = list(input())
q = int(input())
li = []
for i in range(q):
    li.append(input().split())
    s = sousa(s, li[i])

print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
def sousa(s, l):
    if(l[0]=="1"):
        s.reverse()
    if(l[0]=="2"):
        if(l[1]=="1"):
            s.insert(0,l[2])
        if(l[1]=="2"):
            s.append(l[2])

    return s

s = list(input())
q = int(input())
li = []
for i in range(q):
    li.append(input().split())
for i in range(q):
    s = sousa(s, li[i])

print("".join(s))

Submission status: Time Limit Exceeded

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

for _ in range(n):
  query = input().split()
  if query == ["1"]:
    s = s[::-1]
  else:
    if query[1] == "1":
      s = query[2] + s
    else:
      s = s + query[2]

print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
numQ = int(input())
lead = 1

for i in range(numQ):
  Q = input()
  if Q[0] == "1":
    lead *= -1
  else:
    if Q[2] == "1":
      if lead == 1:
        S = Q[4] + S
      else:
        S = S + Q[4]
    else:
      if lead == 1:
        S = S + Q[4]
      else:
        S = Q[4] + S
if lead == -1:
  S = S[::-1]

print(S)

Submission status: Time Limit Exceeded

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

for i in range(QN):
    op=input()
    if op[0]=='1':
        S=S[::-1]
    else:
        if op[2]=='1':
            S=op[4]+S
        else:
            S=S+op[4]

print(S)

Submission status: Time Limit Exceeded

Solution code:
#ABC158.D
S = input()
Q = int(input())
Qy = []
for i in range(Q):
    p = input()
    Qy.append(p)
    if p =='2':
        q = input.split()
        Qy.append(q)
        
    if Qy[i][0]=='1':
        S =S[::-1]
    elif Qy[i][0]=='2' and Qy[i][2]=='1':
        S = str(Qy[i][4])+S
    elif Qy[i][0]=='2' and Qy[i][2]=='2':
        S = S+str(Qy[i][4])
print(S)

Submission status: Time Limit Exceeded

Solution code:
#ABC158.D
S = input()
Q = int(input())
Qy = []
for i in range(Q):
    p = input()
    Qy.append(p)
    if p =='2':
        q = input.split()
        Qy.append(q)
        
for m in range(Q):
    if Qy[m][0]=='1':
        S =S[::-1]
    elif Qy[m][0]=='2' and Qy[m][2]=='1':
        S = str(Qy[m][4])+S
    elif Qy[m][0]=='2' and Qy[m][2]=='2':
        S = S+str(Qy[m][4])
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = 0
for i in range(Q):
    P = [a for a in input().split()]
    if len(P) == 1:
        flag = (flag + 1) % 2
    else:
        if flag == 0:
            if int(P[1]) == 2:
                S += P[2]
            else:
                S = P[2] + S
        else:
            if int(P[1]) == 1:
                S += P[2]
            else:
                S = P[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = 0
for i in range(Q):
    P = [a for a in input().split()]
    if len(P) == 1:
        flag = (flag + 1) % 2
    else:
        if flag == 0:
            if int(P[1]) == 2:
                S = S + P[2]
            else:
                S = P[2] + S
        else:
            if int(P[1]) == 1:
                S = S + P[2]
            else:
                S = P[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    P = [a for a in input().split()]
    if len(P) == 1:
        S = S[::-1]
    else:
        if int(P[1]) == 2:
            S = S + P[2]
        else:
            S = P[2] + S

print(S)