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 = str(input())
q = int(input())
query = [[str(i) for i in input().split()] for i in range(q)]
direction = 0
for i in range(q):
    if query[i] == ['1']:
        direction += 1
    else:
        if (int(query[i][1]) + direction) % 2 == 0:
            s += query[i][2]
        else:
            s = query[i][2] + s
if direction % 2 == 1:
    s = ''.join(list(reversed(s)))
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=int(input())
u=[input().split()for i in range(t)]
a=0
for i in range(t):
  if u[i][0]=='1':
    a+=1
  else:
    if u[i][1]=='1':
      if a%2==0:
        s=u[i][2]+s
      else:
        s=s+u[i][2]
    else:
      if a%2==0:
        s=s+u[i][2]
      else:
        s=u[i][2]+s
if a%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]
  else:
    a, F, C = q.split()
    if F == '1':
      S = C + S
    else:
      S = S + C
      
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

S = input()
Q = int(input())
QUERY = sys.stdin.readlines()

state = 0
q1 = ''
q2 = ''
for i in range(Q):
    query = QUERY[i]
    if query[0] == '1':
        state ^= 1
    else:
        _, F, C = query.split()
        if (state + int(F)) % 2:
            q1 += C
        else:
            q2 += C

ans = ''
if state == 0:
    print(q1[::-1], end='')
    print(S, end='')
    print(q2)
else:
    print(q2[::-1], end='')
    print(S[::-1], end='')
    print(q1)

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
flag = True  # flagがTrueのとき、偶数回数の反転が行われている
tmp = []
cnt = 0
for _ in range(Q):
    query = list(map(str, input().split()))
    # tmp.append(query)
    if len(query) == 1:  # 1のqueryのとき
        cnt += 1
        if flag:
            flag = False
        else:
            flag = True
    else:
        f, c = query[1], query[2]
        if f == "1":  # 先頭に追加するもの
            if flag:
                s = c + s  # 偶数回の反転なら素直に先頭に追加する
            else:
                s += c  # 奇数会の反転なら後ろに追加する
        else:  # 末尾に追加する
            if flag:
                s += c
            else:
                s = c + s

# 最後に反転が必要かどうか
# if flag:
#     cnt = 0
# else:
#     cnt = 1
# tmp.reverse()
# for t in tmp:
#     if len(t) == 1:
#         cnt += 1
#     else:
#         break
if cnt % 2 == 1 and cnt != 0:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for x in range(Q):
    o = input()
    if o == "1":
        S = S[::-1]
        continue
    k, F, C = o.split()
    if F == "1":
        S = C + S
    else:
        S += C


print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
for i in range(Q):
    arr = list(input().split())
    if len(arr) == 1:
        count += 1
    else:
        if arr[1] == '1':
            if count%2==0:
                S = arr[2] + S
            else:
                S = S + arr[2]
        else:
            if count%2!=0:
                S = arr[2] + S
            else:
                S = S + arr[2]
if count%2 == 0:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    S = input()
    Q = int(input())
    #Q_list = []
    #tmp_list = []

    for i in range(Q):
        #Q_list = []
        Q_list = input().split()
        if Q_list[0] == '1':
            S = S[::-1]
            #print(' 1 :' + S)

        else:
            F = Q_list[1]
            C = Q_list[2]

            if F == '1':
                S = C + S
            else:
                S = S + C
            #print(' 2 :' + S)

    print(S)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query_list = []
for i in range(Q):
    query_list.append(input())
#print(S, Q, query_list)

result = S

for i in range(Q):
    #print(i + 1)
    q = query_list[i]
    #print(q)
    t = int(q[0])
    if t == 1:
        result = result[::-1]
    else:
        t, f, c = q.split()
        f = int(f)
        if f == 1:
            result = c + result
        else:
            result = result + c
    #print(t, f, c, result)
    #print("--------")
print(result)

Submission status: Time Limit Exceeded

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

total = 0
num = 0
for i in range(Q):
    tmp = input().split()

    if tmp[0] == "1":
        # S = S[::-1]
        total += 1
        num += 1

    elif tmp[0] == "2":
        num = num % 2
        dada = num + int(tmp[1])
        if dada == 1 or dada == 3:
            S = tmp[2] + S
        else:
            S = S + tmp[2]

        # if tmp[1] == "1":
        #     S = tmp[2] + S

        # elif tmp[1] == "2":
        #     S = S + tmp[2]

if total % 2 == 1:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# print('input >>')
S = input()
Q = int(input())
queries = []
for _ in range(Q):
    queries.append(input())

is_reverse = False

for q in queries:
    ql = list(q.split())
    if len(ql) == 1:
        is_reverse = not is_reverse
    else:
        if ql[1] == '1': # 先頭に加える
            if not is_reverse:
                S = ql[2] + S
            else:
                S = S + ql[2]
        else: # 末尾に加える
            if not is_reverse:
                S = S + ql[2]
            else:
                S = ql[2] + S

# print('-----output-----')
if not is_reverse:
    print(S)
else:
    print(S[::-1])

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

D = deque(input())

for i in range(int(input())):
  q = input().split()
  
  if int(q[0])==1:
    D = deque(reversed(D))
  else:
    if int(q[1])==1:
      D.appendleft(q[2])
    else:
      D.append(q[2])

print(*D,sep="")

Submission status: Time Limit Exceeded

Solution code:
def main():
  S = input()
  Q = int(input())
  import sys
  que = [list(sys.stdin.readline().split()) for _ in range(Q)]
  judge = False
  for l in que:
    if int(l[0])==1:
      if judge:
        judge = False
      else:
        judge = True

    elif int(l[0])==2:
      f,c = int(l[1]),l[2]
      if judge:
        if f==1:
          f=2
        elif f==2:
          f=1
      if f==1:
        S = ''.join([c,S])
      elif f==2:
        S = ''.join([S,c])

  if judge:
    S=S[::-1]
  print(S)
  
main()

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
ans=0
for i in range(q):
    query=input().split()
    if query==['1']:
        ans+=1
    else:
        if query[1]=='1' and ans%2==0:
            s.insert(0,query[2])
        elif query[1]=='1' and ans%2==1:
            s.append(query[2])
        elif query[1]=='2' and ans%2==0:
            s.append(query[2])
        else:
            s.insert(0,query[2])
if ans%2==1:
    s.reverse()
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
lis = []
for _ in range(q):
    i = input()
    if i == '1':
        s = ''.join(list(reversed(s)))
    else:
        a,b,c = i.split()
        if b == '1':
            s = c + s
        else:
            s = s + c
print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    x = input().split()
    if len(x)==1:
        S = S[::-1]
    else:
        x2,string = x[1],x[2]
        if int(x2)==2:
            S = S + string
        else:
            S = string + S
print(S)

Submission status: Time Limit Exceeded

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

for i in range(Q):
  query = input()
  if query=='1':
    S = S[::-1]
  else:
    _, F, C = query.split()
    if F=='1':
      S = C+S
    else:
      S = S+C

print(S)

Submission status: Time Limit Exceeded

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


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

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=int(input())
for i in range(n):
    e=list(map(str,input().split()))
    q=int(e[0])
    if len(e)!=1:
        f=int(e[1])
        c=e[2]
    if q==2:
        if f==1:
            s=c+s
        else:
            s=s+c
    else:
        x=list(reversed(s))
        y=''.join(x)
        s=y
print(s)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
x=y=z=""
i=0
for _ in range(q):
    a=list(input().split())
    if a[0]=="1":
        i+=1
        x,z=z[::-1],x[::-1]
    else:
        if a[1]=="1":x="".join([a[2],x])
        else:z="".join([z,a[2]])
print(x+s[::-1]+z if i%2 else x+s+z)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
moj = str(input())
kai = int(input())
kyu = []
for i in range(kai):
    kyu = []
    kyu = list(input().split(" "))
    if kyu[0] == "1":
        moj = moj[::-1]
    elif kyu[0] == "2":
        if kyu[1] == "1":
            moj = kyu[2] + moj
        elif kyu[1] == "2":
            moj = moj + kyu[2]
            
print(moj)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
query = []
for i in range(q):
    a = input().split()
    query.append(list(a))
for i in query:
    q1 = int(i[0])
    if q1 == 1:
        s.reverse()
    else:
        q2 = int(i[1])
        q3 = i[2]
        if q2 == 1:
            s.reverse()
            s.append(q3)
            s.reverse()
        else:
            s.append(q3)
print(''.join(s))

Submission status: Time Limit Exceeded

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

flag = 0
for i in range(n):
    q = list(map(str,input().split()))
    if len(q) == 1 and q[0] == "1":
        flag = 1 - flag 
    elif q[1] == "1":
        if flag == 0:
            s = q[2] + s
        elif flag == 1:
            s = s + q[2]
    elif q[1] == "2":
        if flag == 0:
            s = s + q[2]
        elif flag == 1:
            s = q[2] + s


if flag == 0:
    print(s)
elif flag == 1:
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
now = 1
q = int(input())
#Query = [ list(map(str,input().split())) for _ in range(q)]
#print(*Query)
adhere = ""
back = ""
for i in range(q):
    query = input()
    if len(query) == 5:
        #t = int(Query[i][0])
        f = int(query[2])
        c = query[4]
        if f == 1:
            adhere = c + adhere
        else:
            back += c
    else:
        now *= -1
        tmp_adhere = back
        back = adhere[::-1]
        adhere = tmp_adhere[::-1]
        
if now == -1:
    ans = adhere + s[::-1] + back
    #print(-1)
else:
    ans = adhere + s + back
    #print(1)
print(ans)

Submission status: Time Limit Exceeded

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

t = [0] * q
f = [0] * q
c = [""] * q

for i in range(q):
    lis = list(input().split())
    t[i] = int(lis[0])
    if t[i] == 2:
        f[i] = int(lis[1])
        c[i] = lis[2]

idx = 0
while idx < q:
    if t[idx] == 1:
        flip = True
        while idx + 1 < q and t[idx + 1] == 1:
            idx += 1
            flip = not flip
        if flip:
            s = s[::-1]
    else:
        if f[idx] == 1:
            s = c[idx] + s
        else:
            s = s + c[idx]
    idx += 1

print(s)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
q=int(input())
sc=0
for _ in range(q):
  query=input()
  if query=="1":
    sc+=1
  else:
    t,f,c=query.split()
    if f=="1":
      if sc%2==0:
        s=[c]+s
      else:
        s=s+[c]
    else:
      if sc%2==0:
        s=s+[c]
      else:
        s=[c]+s
if sc%2!=0:
  s.reverse()
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    for _ in range(q):
        qu = [x for x in input().split()]
        if len(qu) == 1:
            s = s[::-1]
        else:
            if qu[1] == '1':
                s = qu[2] + s
            else:
                s = s + qu[2]
    print(s)
main()

Submission status: Time Limit Exceeded

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

reverse_status = False

for i in range(q):
  query = list(map(format, input().split()))
  if int(query[0]) == 1:
    if reverse_status:
      reverse_status = False
    else:
      reverse_status = True
  else:
    if int(query[1]) == 1:
      if reverse_status:
        s = s + query[2]
      else:
        s = query[2] + s
    else:
      if reverse_status:
        s = query[2] + s
      else:
        s = s + query[2]

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

Submission status: Time Limit Exceeded

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

rev = False
for i in range(Q):
  if query[i][0] == "1":
    rev = not rev
    pass
  else:
    if (not rev and query[i][1] == "1") or (rev and query[i][1] == "2"):
      S = query[i][2] + S
    else:
      S += query[i][2]
  
if rev:
  S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
# N = int(input())
S = input()
Q = int(input())
T = [input().split() for i in range(Q)]

for t in T:
    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()
q = int(input())
t = 1
f = 0
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)

Submission status: Time Limit Exceeded

Solution code:
letter = input()
num = int(input())

for kk in range(num):
    lis = list(input().split())
    if int(lis[0]) == 1:
        letter = letter[::-1]
    else:
        if int(lis[1]) == 1:
            letter = lis[2] + letter
        else:
            letter = letter + lis[2]

print(letter)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    a = input()
    a = list(a)
    q = int(input())
    
    for _ in range(q):
        _q = [i for i in input().split()]
        t, f, c =0, 0, 0
        t = int(_q[0])
        if t == 2:
            f, c = int(_q[1]), _q[2]
        
        if t == 1:
            a = list(reversed(a))
        elif t == 2:
            if f == 1:
                a.insert(0, c)
            elif f == 2:
                a.append(c)
    
    print("".join(a))


if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
length = len(s)
que = [[''] * 3 for i in range(q)]
for i in range(q):
    c = input()
    if c[0] == '1':
        que[i][0] = 1
    else:
        n, f, c = map(str, c.split())
        que[i][0], que[i][1], que[i][2] = int(n), int(f), c
for com in que:
    if com[0] == 1:
        s = s[::-1]
    else:
        if com[1] == 1:
            li = [com[2], s]
            s = ''.join(li)
        else:
            tmp = s
            li = [s, com[2]]
            s = ''.join(li)
print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
S=list(input())
Q=int(input())
re=1
for i in range(Q):
    q=input()
    if q=="1":
        re*=-1
    else:
        two,F,C=q.split()
        if re==1:
            if F=="1":
                S.insert(0,C)
            else:
                S.append(C)
        else:
            if F=="2":
                S.insert(0,C)
            else:
                S.append(C)

if re==-1:
    S.reverse()

ans="".join(S)
print(ans)

Submission status: Time Limit Exceeded

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

def main():
    s = input().replace('\n','')
    q = int(input())
    for i in range(q):
        tmp = input().replace('\n', '').split(' ')
        # print(tmp)
        # print(s)
        if tmp[0] == '1':
            s = ''.join(list(reversed(s)))
        else:
            if tmp[1] == '1':
                s = tmp[2] + s
            else:
                s = s + tmp[2]
    print(s)


main()

Submission status: Time Limit Exceeded

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

for i in range(q):
  temp = query[i].split()
  if int(temp[0]) == 1:
    s = s[::-1]
  else:
    if int(temp[1]) == 1:
      s = '{}{}'.format(temp[2], s)
    else:
      s = '{}{}'.format(s, temp[2])

print(s)

Submission status: Time Limit Exceeded

Solution code:

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

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

    if (len(q) == 1):
        S = S[::-1]
    else:
        judge = q[1]
        cha = q[2]
        
        if (int(judge) == 1):
            S = cha + S
        else:
            S = S + cha

print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**9)
def main():
    S = input()
    Q = int(input())

    def insert(s,type):
        if type == 1:
            return s + S
        else:
            return S + s

    non_reverse = True
    for _ in range(Q):
        inp = input()
        if inp == "1":
            non_reverse = not non_reverse
        else:
            _,F,C = inp.split()
            F = int(F)
            if non_reverse:
                S = insert(C,F)
            else:
                S = insert(C,1 if F == 2 else 2)

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



if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
#import numpy as np
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
#S = str(input())

S = str(input())
Q = int(input())
#query = [["" for _ in range(3)] for _ in range(Q)]

for _ in range(Q) :
  stmp = input().split()
  #print (stmp)
  if stmp[0] == "1" :
    S = S[::-1]
  else :
    if stmp[1] == '1' :
      S = stmp[2] + S
    else :
      S = S + stmp[2]

print (S)

Submission status: Time Limit Exceeded

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

que = []
frag = True

for i in range(q):
    que = li[i]
    if que[0] == "1":
        if frag:
            frag = False
        else:
            frag = True
    else:
        f = que[1]
        c = que[2]
        if frag:
            if f == "1":
                s = c + s
            else:
                s = s + c
        else:
            if f == "1":
                s = s + c
            else:
                s = c + s

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

Submission status: Time Limit Exceeded

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

S = []
for i in range(len(s)):
    S.append(s[i])
    
back_count = 0
for i in range(N):
    Q = list(map(str, input().split()))
    Q[0] = int(Q[0])
    if Q[0] == 1:
        if back_count == 0:
            back_count = 1
        else:
            back_count = 0
    else:
        Q[1] = int(Q[1])
        if back_count == 0:
            if Q[1] == 1:
                S.insert(0, Q[2])
            else:
                S.append(Q[2])
        else:
            if Q[1] == 1:
                S.append(Q[2])
            else:
                S.insert(0, Q[2])

if back_count == 0:
    print("".join(S))
else:
    S = S[::-1]
    print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
query=[]
for i in range(q):
    query.append(input())
seq=s
rev=0
for i in range(q):
    if query[i][0]=="1":
        rev+=1
    else:
        n,f,c=query[i].split()
        if (rev+int(f))%2==0:
            seq=seq+c
        else:
            seq=c+seq

if rev%2==0:
    print(seq)
else:
    for i in range(len(seq)):
        print(seq[len(seq)-i-1],end="")

Submission status: Time Limit Exceeded

Solution code:
first_step_string = input()
query_num = int(input())
query_li = list()
ans = first_step_string
for _ in range(query_num):
    query = input().split()
    if query[0] == '1':
        ans = ans[::-1]
    elif query[1] == '1':
        ans = query[2] + ans
    else:
        ans += query[2]

print(ans)

Submission status: Time Limit Exceeded

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

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
    s = sys.stdin.readline().strip()
    q = int(sys.stdin.readline().strip())
    for _ in range(q):
        command = sys.stdin.readline().strip().split(" ")
        if command[0] == "1":
            s = s[::-1]
        else:
            if command[1] == "1":
                s = command[2] + s
            else:
                s = s + command[2]
    print(s)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
S = input()
Q = int(input())

for q in range(Q):
    i = input().split()
    if i[0] == "1":
        S = S[::-1]
    elif i[1] == "1":
        S = i[2] + S
    else:
        S = S + i[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
#D
s=[i for i in input()]
q=int(input())
for i in range(q):
    t=input().split()
    if t[0]=='1':
        s.reverse()
    elif t[1]=='1':
        s.insert(0,t[2])
    else:
        s.append(t[2])
        
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = deque(input())
Q = int(input())
query = [input().split() for _ in range(Q)]
for q in query:
    if q[0] == '1':
        S.reverse()
    else:
        if q[1] == '1':
            S.appendleft(q[2])
        else:
            S.append(q[2])

print(''.join(S))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S=input()
Query=[input() for _ in range(int(input()))]
ans=S

for q in Query:
    tmp=ans
    if q=='1':
        tmp=tmp[::-1]
    elif q[0]=='2':
        if q[2]=='1':
            tmp=q[4]+tmp
        else:
            tmp+=q[4]
    ans=tmp

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
queri=[]
rv=0

for i in range(q):
    tmp=list(map(lambda x: x,input().split()))
    queri.append(tmp)
    if tmp[0]=="1": rv+=1
rv=rv%2

if rv==1: s=s[::-1]
ans=[s]

for i in range(q):
    if queri[i][0]=="2":
        rvt=(rv+int(queri[i][1]))%2
        if rvt==1: ans=[queri[i][2]]+ans
        else: s=ans.append(queri[i][2])

    else: rv=(rv+1)%2

for i in range(len(ans)):
    print(ans[i],end="")

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    Q = int(input())
    revFlag = False
    for i in range(Q):
        tmp = list(input().split())
        if tmp[0] == "1":
            if revFlag:
                revFlag = False
            else:
                revFlag = True
        else:
            if tmp[1] == "1":
                if revFlag:
                    S += tmp[2]
                else:
                    S = tmp[2] + S
            else:
                if revFlag:
                    S = tmp[2] + S
                else:
                    S += tmp[2]
    if revFlag:
        S = list(S)
        S.reverse()
        print("".join(S))
    else:
        print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
count = 0
lr = ['', '']
for i in range(Q):
    TFC = input()
    if TFC[0] == '1':
        count += 1
    elif TFC[2] == '1':
        lr[count % 2] += TFC[4]
    else:
        lr[(count+1) % 2] += TFC[4]
if count % 2 == 0:
    print(lr[0][::-1] + S + lr[1])
else:
    print(lr[1][::-1] + S[::-1] + lr[0])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
query = [list(input().split()) for _ in range(Q)]
for x in query:
    if int(x[0]) == 1:
        S = S[::-1]
    elif int(x[0]) == 2:
        if int(x[1]) == 1:
            S = x[2] + S
        elif int(x[1]) == 2:
            S = S + x[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
def main(s,x,q):
    res = s
    for op in q:
        if int(op[0]) == 1:
            res = res[::-1]
        elif int(op[0]) == 2:
            if int(op[1]) == 1:
                res = op[2] + res
            if int(op[1]) == 2:
                res = res + op[2]
    return res

s = input()
x = int(input())
q = [input().split() for _ in range(x)]
print(main(s,x,q))

Submission status: Time Limit Exceeded

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

for q in q_list:
    if q[0] == '1':
        s = s[::-1]
    elif q[1] == '1':
        s = q[2] + s
    else:
        s = s + q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
q = int(input())
c = 0
for i in range(q):
    qy = list(input().split())
    if qy[0] == "1":
        c += 1
    else:
        if (qy[1] == "1" and c % 2 == 0) or (qy[1] == "2" and c % 2 == 1):
            s = [qy[2]] + s
        else:
            s = s + [qy[2]]
if c % 2 == 0:
    print("".join(s))
else:
    print("".join(reversed(s)))

Submission status: Time Limit Exceeded

Solution code:
def odd(S):
    return S[::-1]

def even(S,F,C,c):
    if F==1 and c%2==0 or F==2 and c%2==1:
        C+=S
        return C
    else:
        S+=C
        return S

S=input()
Q=int(input())
c=0
for i in range(Q):
    T=list(map(str,input().split()))
    if len(T)==1:
        c+=1
    else:
        S=even(S,int(T[1]),T[2],c)
        
if c%2==1:
    S=odd(S)

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
  query = input().split()
  if query[0] == '1':
    S = S[::-1]
  else:  # query[0] == 2
    F, C = query[1:]
    if F == '1':
      S = C + S
    else:  # F == '2'
      S = S + C
print(S)

Submission status: Time Limit Exceeded

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

for i in Query:
    if int(i[0]) == 1:
        S = S[::-1]
    elif int(i[0]) == 2:
        if int(i[1]) == 1:
            tmp = [i[2],S]
            S = ''.join(tmp)
        elif int(i[1]) == 2:
            tmp = [S,i[2]]
            S = ''.join(tmp)
print(S)