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:
import sys
import math
from functools import reduce
from operator import mul
sys.setrecursionlimit(10**6)

def InMap(): return list(map(int, sys.stdin.readline().split(' ')))
def In(): return int(sys.stdin.readline())
def InS(): return sys.stdin.readline()

S = InS()[:-1:]
Q = In()
flag = 0
cont_one = 0
for _ in range(Q):
    Query = InS()[:-1:].split(' ')
    if(Query[0] == '1'):
        flag += 1
        cont_one += 1
    else:
        cont_one = 0
        if(Query[1] == '1'):
            if(flag & 0x01):
                S = S + Query[2]
            else:
                S = Query[2] + S
        else:
            if(flag & 0x01):
                S = Query[2] + S
            else:
                S = S + Query[2]
flag -= cont_one
if flag & 0x01:
    if(cont_one & 0x01):
        print(S)
    else:
        print(S[::-1])
else:
    if(cont_one & 0x01):
        print(S[::-1])
    else:
        print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())


def main():
    S = INPUT()
    Q = INT()

    flag_order = True
    D_MAP = lambda: map(str, query.split())
    for i in range(Q):
        query = INPUT()
        if len(query.split()) == 1:
            T = 1
        else:
            T = 2
            _, F, C = D_MAP()
            F = int(F)

        if T == 1:
            if flag_order: flag_order = False
            else: flag_order = True
        if T == 2:
            if flag_order:
                if F == 1: S = C + S
                else: S = S + C
            else:
                if F == 1: S = S + C
                else: S = C + S

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

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

# print(S, Q)

queries = []
for i in range(Q):
    query = input()
    queries.append(query)


isReversed = False
for query in queries:
    if query == "1":
        isReversed = not isReversed
        # print("reverse", S)
        # S = S[::-1]
    else:
        t, f, c = query.split()
        # print(t, f, c)
        d = f=="1"
        if isReversed: d = not d
        if d:
            S = "".join([c, S])
        else:
            # print("append c to tail of ", S)
            # S = S + c
            S = "".join([S, c])

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
T = [0] * Q
F = [0] * Q
C = [0] * Q
for i in range(Q):
  inp = input()
  if len(inp) == 1:
    S = S[::-1]
  else:
    T[i], F[i], C[i] = inp.split()
    if int(F[i]) == 1:
      S = C[i] + S
    else:
      S = S + C[i]

print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().strip()
s=str(input())
q=int(input())
for i in range(q):
    j=input()
    if j[0]=="1":
        s=s[::-1]
    else:
        if j[2]=="1":
            s=j[4]+s
        else:
            s+=j[4]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

    r_flg = True

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

        # query 1
        if query[0] == '1':
            r_flg = not r_flg
        
        # query 2 Fi = 1
        elif query[1] == '1':
            if r_flg:
                s = query[2] + s
            else:
                s = s + query[2]

        # query 2 Fi = 2
        else:
            if r_flg:
                s = s + query[2]
            else:
                s = query[2] + s

        # print('cont:{}, s:{}, r_flg:{}'.format(i, s, r_flg))

    if not r_flg:
        s = ''.join(reversed(s))

    print(s)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  s=input()
  q=int(input())
  count=0
  for _ in [0]*q:
    a=list(input().split())
    if len(a)==1:
      count+=1
    else:
      if count%2==1:
        s=s[::-1]
        count=0
      if a[1]=='1':
        s=a[2]+s
      else:
        s=s+a[2] 
  if count%2==1:
    s=s[::-1]
  print(s)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

for i in range(Q):
    query = list(input().split())
    if(query[0] == '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()
k = []
Q = int(input())
now = 0
ans = ""
mode = 0
for i in range(Q):
    K = input().split()
    if K[0] == "1":
        S = S[::-1]
    else:
        if K[1] == "1":
            S = K[2]+S
        else:
            S = S+K[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
Q = int(input())
q = []
f = []
c = []

for _ in range(Q):
    tmp = list(map(str,input().split()))
    if len(tmp) == 1:
        q.append(int(tmp[0]))
        f.append(0)
        c.append('')
    else:
        q.append(int(tmp[0]))
        f.append(int(tmp[1]))
        c.append(tmp[2])

for i in range(Q):
    if q[i] == 1:
        s = s[::-1]
    else:
        if f[i] == 1:
            s = c[i]+s
        else:
            s = s+c[i]

print(s)

Submission status: Time Limit Exceeded

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

for query in queries:
  if len(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:

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

rev = False

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

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

Submission status: Time Limit Exceeded

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

r = 0
for i in range(q):
  tfc = input().split()
  if tfc[0] == '1':
    r += 1
  else:
    if tfc[1] == '1':
      if r % 2 == 0:
        s.insert(0, tfc[2])
      else:
        s.append(tfc[2])
    if tfc[1] == '2':
      if r % 2 == 1:
        s.insert(0, tfc[2])
      else:
        s.append(tfc[2])          
if r % 2 == 0:
  for i in s:
      print(i, end='')
else:
  for i in (reversed(s)):
    print(i, end='')

Submission status: Time Limit Exceeded

Solution code:
# Problem D - String Formation

# input process
S = list(input())
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    # クエリが１の時
    if query_num[0]=="1":
        # 反転処理
        if sento==1:
            sento = 10
        elif sento==10:
            sento = 1
    elif query_num[0]=="2":
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S.insert(0, c)
            elif sento==10:
                S.append(c)
        elif f==2: # 末尾追加処理
            if sento==1:
                S.append(c)
            elif sento==10:
                S.insert(0, c)

# output process
if sento==1:
    for s in S:
        print(s, end="")
else:
    for s in S[::-1]:
        print(s, end="")

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s=list(str(input()))
s=deque(s)
q=int(input())
for i in range(q):
    que=list(input().split())
    if len(que)==1:
        s=list(s)
        s=s[::-1]
        s=deque(s)
    else:
        if int(que[1])==1:
            s.appendleft(que[2])
        elif int(que[1])==2:
            s.append(que[2])
s=list(s)
print(''.join(s))

Submission status: Time Limit Exceeded

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

for q in Q:
    if int(q[0]) == 1:
        s = s[::-1]
    elif int(q[1]) == 1:
        s = q[2] + s
    else:
        s = s + q[2]

print(s)

Submission status: Time Limit Exceeded

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

qs = [input().split() for i in range(Q)]

for q in qs:
    if q[0] == '1':
        S = S[::-1]
    else:
        if q[1] == '1':
            S = q[2] + S
        else:
            S = S + q[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
for i in range(n):
    qq = list(input().split())
    if qq[0] == '1':
        s = s[::-1]
    else:
        if qq[1] == '1':
            s = qq[2] + s
        else:
            s = s + qq[2]

print(s)

Submission status: Time Limit Exceeded

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

query_list = [input().split() for _ in range(Q)]

for query in query_list:
    if query[0] == '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()
Q = int(input())
#query = [input().split() for _ in range(Q)]

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

Submission status: Time Limit Exceeded

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

for i in query:
  if i[0] == '2':
    if i[1] == '1':
      s = '{}{}'.format(i[2], s)
    else:
      s = '{}{}'.format(s, i[2])
  else:
    s = ''.join(reversed(s))
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
for i in range(q):
    com = list(map(str, input().split()))
    #print(s)
    if com[0] == "1":
        s = s[::-1]
    else:
        c = int(com[1])
        if c == 1:
            s = com[2]+s
        else:
            s += com[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
#At coder 2020/3/8 C
import math
S=input()
Q=int(input())
''' for ii in range(Q):
    aa=list(map(str,input().split())) 
    if '1'==aa[0]:
        S=S[::-1]
    else:
        if '1'==aa[1]:
                S=aa[2]+S
        else:
                S=S+aa[2] '''
jj=0
for ii in range(Q):
    aa=list(map(str,input().split())) 
    if '1'==aa[0] and jj==1:
        jj=0
    elif '1'==aa[0] and jj==0:
        jj=1
    else:
        if ('1'==aa[1] and jj==0) or ('2'==aa[1] and jj==1):
                S=aa[2]+S
        else:
                S=S+aa[2]
if jj==0:
    ans=S
else:
    ans=S[::-1]
    
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
  Q = [i for i in input().split()]
  if Q[0] =='1':
    if condition ==True:
      condition=False
    else:
      condition=True
  else:
    if Q[1]=='1':
      if condition ==True:
        S = Q[2]+S
      else:
        S =S+Q[2]
    else:
      if condition==True:
        S =S+Q[2]
      else:
        S = Q[2]+S
    #print(S)

if condition ==False:
  S=S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
l=[]
for c in range(q):
    l.append(input())
for c in range(q):
    if l[c]=="1":
        s=s[::-1]
    else:
        j=l[c].split()
        if j[1]=="1":
            s=j[2]+s
        else:
            s=s+j[2]
print(s)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    Query = input().split()
    if Query[0] == '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()
q=int(input())

flg=True #正順
for i in range(q):
    qi=list(map(str,input().split()))
    if qi[0]=="1":
        flg=not flg
    else:
        f,c=qi[1],qi[2]
        if f=="1":
            if flg:
                s=c+s
            else:
                s=s+c
        else:
            if not flg:
                s=c+s
            else:
                s=s+c
                

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

Submission status: Time Limit Exceeded

Solution code:
import sys
a = sys.stdin.read().split('\n')[:-1]
s = a[0]
q = int(a[1])
f = 1
for i in range(0, q):
    if a[2 + i][0] == '1':
        f = 1 - f
    if a[2 + i][0] == '2':
        if a[2 + i][2] == '21'[f]:
            s = a[2 + i][4] + s
        else:
            s += a[2 + i][4]
if f == 1:
    print(s)
else:
    b = ''
    for c in reversed(s):
        b += c
    print(b)

Submission status: Time Limit Exceeded

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

r = False
for i in range(Q):
    x = input().split()
    
    if x[0] == "1":
        r = not r
        
    else:
        if x[1] == "1":
            if r == False :
                S = x[2] + S
            else:
                S = S + x[2]
        else:
            if r == True :
                S = x[2] + S
            else:
                S = S + x[2]
        
if r:
    print(S[::-1])
else:
    print(S)

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
num=int(input())
for i in range(num):
    t=input().split(' ')
    if int(t[0])==1:
        s=s[::-1]
    else:
        if int(t[1])==1:
            s.insert(0,t[2])
        else:
            s.append(t[2])
print("".join(s))

Submission status: Time Limit Exceeded

Solution code:
s=input()
for i in range(int(input())):
    query = input().split()
    if len(query)==1:
        s = "".join(list(reversed(s)))
        continue

    _,f,c = query
    s = c+s if f=="1" else s+c

print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
tmp = s
flag = 0
for _ in range(q):
    _s = input()
    if _s == "1":
        flag += 1
    else:
        f = _s[2]
        c = _s[4]
        r = flag % 2
        if f == "1" and r == 0:
            tmp = c + tmp
        elif f == "1" and r == 1:
            tmp = tmp + c
        elif f == "2" and r == 0:
            tmp = tmp + c
        elif f == "2" and r == 1:
            tmp = c + tmp
if flag % 2 == 0:
    print(tmp)
else:
    print(tmp[::-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.append(c)
    else:
        now *= -1
        ##tmp_adhere = back
        #back = adhere[::-1]
        #adhere = tmp_adhere[::-1]
        back,adhere = adhere[::-1],back[::-1]
if now == -1:
    ans = "".join(adhere) + s[::-1] + "".join(back)
    #print(-1).join
else:
    ans = "".join(adhere) + s + "".join(back)
    #print(1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def S(): return input().rstrip()
def LIR(n): return [S().split() for i in range(n)]

def main():
    s = S()
    Q = I()
    Qu = LIR(Q)
    for que in Qu:
        if que[0] == '1':
            s = ''.join(list(reversed(s)))
        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())
X = [list(input().split()) for _ in range(q)]

for x in X:
    if int(x[0]) == 1:
        s = s[::-1]
        continue
    if int(x[1]) == 1:
        s = x[2] +s
    else:
        s = s +x[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input(""))
Q = int(input())
c = 0
for i in range(Q):
    t = input()
    if len(t) == 1:
        c += 1
    elif t[2] == "1":
        if c%2 == 0:
            s.insert(0,t[4])
        else:
            s.append(t[4])
    else:
        if c%2 == 0:
            s.append(t[4])
        else:
            s.insert(0,t[4])

if c%2 == 0:
    for i in s:
        print(i,end="")
else:
    for i in s[::-1]:
        print(i,end="")

Submission status: Time Limit Exceeded

Solution code:
S = [s for s in input()]
Q = int(input())
front = []
end = []
cnt = 0
for _ in range(Q):
    T = input().split()
    if int(T[0]) == 1:
        cnt += 1
    else:
        F = int(T[1])
        C = [s for s in T[2]]
        if F == 1:
            if cnt%2==0:
                front = C+front
            else:
                end = end+C
        else:
            if cnt%2==0:
                end = end+C
            else:
                front=C+front
if cnt % 2 == 0:
    S = front+S+end
else:
    S.reverse()
    end.reverse()
    front.reverse()
    S = end+S+front

print(''.join(S))

Submission status: Time Limit Exceeded

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

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

if mode==-1:
  S = S[::-1]

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:
        s=s[::-1]
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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:
                d = deque(S)
                d.appendleft(Q[2])
                S = list(d)
            else:
                S.append(Q[2])
        else:
            if Q[1] == 1:
                S.append(Q[2])
            else:
                d = deque(S)
                d.appendleft(Q[2])
                S = list(d)

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

Submission status: Time Limit Exceeded

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

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=S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

roll = False

for i in range(q) :
    inp = input()
    if inp[:1] == '1' :
        roll = not roll
        #print(roll)
    else :
        t, f, c = inp.split()
        #print(t, f, c)
        if (f == '1' and (not roll)) or (f == '2' and roll) :
            s = c + s
        else :
            s += c

if roll :
    s = s[::-1]

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    q = int(input())
    r = s[::-1]
    for i in range(q):
        query = input().split()
        if query[0] == "1":
            s,r = r,s
        else:
            if query[1] == "1":
                s = query[2] + s
                r = r + query[2]
            if query[1] == "2":
                s = s + query[2]
                r = query[2] + r
    print(s)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#[int(i) for i in input().split()]
s = input()
q = int(input())
check = 1
cnt = 0
for i in range(q):
    tmp = [j for j in input().split()]
    if tmp[0] == "2":
        if check == 1:
            if tmp[1] == "1":
                s = tmp[2] + s
            else:
                s += tmp[2]
        else:
            if tmp[1] == "1":
                s += tmp[2]
            else:
                s = tmp[2] + s
    else:
        check *= -1
        cnt += 1


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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
sys.setrecursionlimit(int(1e6))

S = input()
Q = int(input())
ans = S
rev = 1
for i in range(Q):
    query = input().split()
    if query[0] == '1':
        rev = -1 * rev
    elif query[1] == '1':
        if rev > 0:
            ans = query[2] + ans
        else:
            ans = ans +query[2]
    else:
        if rev > 0:
            ans = ans +query[2]
        else:
            ans = query[2] + ans

if rev > 0:
    print(ans)
else:
    print(ans[::-1])

Submission status: Time Limit Exceeded

Solution code:
def do(s, query, isReverse):
    if(query[0] == "1"):
        return s, isReverse * -1
    else:
        if(query[1] == "1"):
            if(isReverse == 1):
                return "".join([s, query[2]]), isReverse
            else:
                return "".join([query[2], s]), isReverse
        else:
            if(isReverse == 1):
                return "".join([query[2], s]), isReverse
            else:
                return "".join([s, query[2]]), isReverse

s = input()
q = int(input())
isReverse = -1
for i in range(q):
    query = input().split()
    s, isReverse = do(s, query, isReverse)
if(isReverse == -1):
    print(s)
else:
    print(s[::-1])

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]
forward=""
back=""
for i in range(q):
    if queri[i][0]=="2":
        rvt=(rv+int(queri[i][1]))%2
        if rvt==1: forward+=queri[i][2]
        else: back+=queri[i][2]

    else: rv=(rv+1)%2

print(forward[::-1],end="")
print(s,end="")
print(back)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = input()
  n = int(input())
  
  reverse_count = 0
  pre_s = ''
  after_s = ''
  for _ in range(n):
    t = list(input_list_str())
    if len(t) == 1:
      reverse_count += 1
      s = s[::-1]
      continue
    q, f, c = t
    if int(f) == 1:
      pre_s += c
      s = c + s
    else:
      after_s += c
      s = s + c
  
  print(s)
  exit()
  a = pre_s + s + after_s
  if reverse_count % 2 != 0:
    a = a[::-1]
  print(a)
    
def input_list():
  return map(int, input().split())

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

import math
import fractions
from functools import reduce
main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
  T = input().split()
  #print("S", S)
  #print("T", T)
  if T[0] == '1':
    S = S[::-1]
  elif T[0] == '2':
    if T[1] == '1':
      S = T[2] + S 
    elif T[1] == '2':
      S = S + T[2]
print(S)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
flag = 0
for i in range(int(input())):
	X = sys.stdin.readline().split()
	if int(X[0]) == 1:
		flag = flag^1	
	elif (int(X[1])-1)^flag == 0:
		s = X[2] + s
	else:
		s = s + X[2]
if flag == 1:
	print(s[::-1])
else:
	print(s)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
Q = int(input())
for i in range(Q):
  #print(S)
  q = input()
  if q == "1":
    S.reverse()
    #print("1",S)
  else:
    q,f,c = q.split()
    if f == "1":
      S = [c]+S
    else:
      S = S+[c]
print("".join(S))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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


def main():
    s = "".join(input().split())
    q = int(input())
    queries = [list(input().split()) for _ in range(q)]
    for query in queries:
        if query[0] == "1":
            lst = list(s)
            lst.reverse()
            s = "".join(lst)
            continue
        if query[1] == "1":
            s = query[2] + s
        else:
            s = s + query[2]
    print(s)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
q=int(input())
for _ in range(q):
	k=input()
	if k[0] == "1":
		s = s[::-1]
	else:
		if k[2] == "1":
			s = "".join([k[4:],s])
		else:
			s = "".join([s,k[4:]])
print(s)

Submission status: Time Limit Exceeded

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

main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
S = stdin.readline()[:-1]
Q = int(stdin.readline())
for _ in range(Q):
    command = stdin.readline()
    try:
        a, b, char, = command.split()
        if b == '1': S = char + S
        else: S = S + char
    except: S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
q = int(input())
toggle = "ok"


for i in range(q):
    si = input().split()
    if si[0] == "1":
        if toggle == "ok":
            toggle = "re"
        else:
            toggle = "ok"
    else:
        if si[1] == "1" and toggle == "ok":
            s = si[2] + s
        elif si[1] == "2" and toggle == "re":
            s = si[2] + s
        elif si[1] == "1" and toggle == "re":
            s = s + si[2]
        elif si[1] == "2" and toggle == "ok":
            s = s + si[2]
if toggle == "ok":
    print(s)
else :
    print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
f = ''
b = ''
r = False
for i in range(Q):
    q = input()
    if q[0] == '1':
        r = not r
    else:
        T, F, C = q.split()
        if (F == '1') ^ r:
            f = C + f
        else:
            b = b + C
S = f + S + b
S = ''.join(list(reversed(S))) if r else S
print(S)