Problem:


Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq Q \leq 2 \times 10^5

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy



Sample input:
a
4
2 1 p
1
2 2 c
1


Sample output:
cpa

Submission status: Time Limit Exceeded

Solution code:
def rev(s):
    new_s = list(reversed(s))
    return new_s

S = list(input())
N = int(input())
r_flag=False
for _ in range(N):
    Q = input()
    if Q.isdigit():
        r_flag=not r_flag
    else:
        q = Q.split()
        if r_flag:
            if q[1]=="1":
                S.append(q[2])
            else:
                S.insert(0,q[2])
        else:
            if q[1]=="1":
                S.insert(0,q[2])
            else:
                S.append(q[2])
if r_flag:
    print(''.join(reversed(S)))
else:print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
def rev(s):
    new_s = list(reversed(s))
    return new_s

S = list(input())
N = int(input())
r_flag=False
for _ in range(N):
    Q = input()
    if Q.isdigit():
        if r_flag:
            r_flag=False
        else:r_flag=True
    else:
        q = Q.split()
        if r_flag:
            if q[1]=="1":
                S.append(q[2])
            else:
                S.insert(0,q[2])
        else:
            if q[1]=="1":
                S.insert(0,q[2])
            else:
                S.append(q[2])
if r_flag:
    print(''.join(reversed(S)))
else:print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
def rev(s):
    new_s = list(reversed(s))
    return new_s

S = list(input())
N = int(input())
for _ in range(N):
    Q = input()
    if Q.isdigit():
        #反転
        S = rev(S)
    else:
        q = Q.split()
        if q[1]=="1":
            S.insert(0,q[2])
        else:
            S.append(q[2])


print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
import collections
import math
import operator as op
from functools import reduce
from collections import Counter
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    S = get_input()
    Q = get_input(int)
    for _ in range(Q):
        query = get_input()
        if query[0] == "1":
            S = S[::-1]
        else:
            _, F, C = query.split(" ")
            if F == "1":
                S = C + S
            else:
                S = S + C
    print(S)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

guuki=0
ANS=S
for i in range(Q):
  if A[i][0]=="1":
    guuki+=1
  elif int(A[i][1])*guuki%2==1 or (A[i][1]=="2" and guuki%2==0):
    ANS=ANS+A[i][2]
  else:
    ANS=A[i][2]+ANS
re_ans=ANS
if guuki%2==1:
  re_ans=""
  for j in range(len(ANS)):
    re_ans+=ANS[len(ANS)-j-1]
print(re_ans)

Submission status: Time Limit Exceeded

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

S_normal = S
S_inverse = S[::-1]
for q in range(Q):
    tmp1 = []
    tmp2 = []
    query = list(map(str, input().split()))
    if int(query[0]) == 1:
        S = S_inverse
        S_inverse = S_normal
        S_normal = S
    else:
        if int(query[1]) == 1:
            tmp1.append(query[2])
            tmp1.append(S)
            S = ''.join(tmp1)
            S_normal = S
            tmp2.append(S_inverse)
            tmp2.append(query[2])
            S_inverse = ''.join(tmp2)
        else:
            tmp2.append(S)
            tmp2.append(query[2])
            S = ''.join(tmp2)
            S_normal = S
            tmp1.append(query[2])
            tmp1.append(S_inverse)
            S_inverse = ''.join(tmp1)

print(S)

Submission status: Time Limit Exceeded

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

for q in range(Q):
    query = list(map(str, input().split()))
    tmp = []
    if int(query[0]) == 1:
        S = S[::-1]
    else:
        if int(query[1]) == 1:
            tmp.append(query[2])
            tmp.append(S)
            S = ''.join(tmp)
        else:
            tmp.append(S)
            tmp.append(query[2])
            S = ''.join(tmp)

print(S)

Submission status: Time Limit Exceeded

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

S_normal = S
S_inverse = S[::-1]
for q in range(Q):
    query = list(map(str, input().split()))
    if int(query[0]) == 1:
        S = S_inverse
        S_inverse = S_normal
        S_normal = S
    else:
        if int(query[1]) == 1:
            S = query[2]+S
            S_normal = S
            S_inverse += query[2]
        else:
            S += query[2]
            S_normal = S
            S_inverse = query[2]+S_inverse

print(S)

Submission status: Time Limit Exceeded

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

for q in range(Q):
    query = list(map(str, input().split()))
    if int(query[0]) == 1:
        S = S[::-1]
    else:
        if int(query[1]) == 1:
            S = query[2]+S
        else:
            S += query[2]

print(S)

Submission status: Time Limit Exceeded

Solution code:
def main():

    st=input()
    N = int(input())
    sw=True
    for i in range(N):
        t= input().split()
        if t[0]=='1':
            sw=not sw
        else:
            if (sw and t[1]=='2') or (not sw and t[1] == '1'):
                    st=''.join([st,t[2]])
            else:
                st=''.join([t[2],st])

    if not sw:
        print(st[::-1])
    else:
        print(st)

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = deque(i for i in input())
n = int(input())
l = [[i for i in input().split()] for _ in range(n)]

for i in l:
    if i[0] == '1':
        s.reverse()
    elif i[1] == '1':
        s.appendleft(i[2])
    else:
        s.append(i[2])
            

print(*s,sep = '')

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

def add_h(s,c):
    return c+s

def add_t(s,c):
    return s+c

s = input()
Q = int(input())
query = []
[query.append(input().split()) for _ in range(Q)]

inversion = -1

for i in range(Q):
    if query[i][0] == "1":
        inversion *= -1
        continue

    c = str(query[i][2])
    if query[i][1] == "1" and inversion == -1:
        s = c + s
    elif query[i][1] == "1" and inversion == 1:
        s = s + c
    elif query[i][1] == "2" and inversion == -1:
        s = s+ c
    else:
        s = c + s


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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    Query = [i for i in input().split()]
    if Query[0] == '1':
        S = S[::-1]
    else:
        if Query[1] == '1':
            S = ''.join([Query[2], S])
        else:
            S = ''.join([S, Query[2]])
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for _ in range(Q):
    Query = [i for i in 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:
import sys
input = sys.stdin.readline
         
S = input().rstrip()	;#1st input	
num = int(input())		;#2nd input

inv_flag = 1 

##type of valiable##
for i in range(num):
  in_val =input().split()
  if in_val[0] == str(1):
    inv_flag = -1 * inv_flag
  elif in_val[0] == str(2):
    if in_val[1] == str(1) and inv_flag == 1:
      S = in_val[2] + S
    if in_val[1] == str(1) and inv_flag == -1:
      S = S + in_val[2]
    if in_val[1] == str(2) and inv_flag == 1:
      S = S + in_val[2]
    if in_val[1] == str(2) and inv_flag == -1:
      S = in_val[2] + S

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

Submission status: Time Limit Exceeded

Solution code:
S = str(input())		;#1st input	
num = int(input())		;#2nd input

inv_flag = 1 

##type of valiable##
for i in range(num):
  in_val =input().split()
  if in_val[0] == str(1):
    inv_flag = -1 * inv_flag
  elif in_val[0] == str(2):
    if in_val[1] == str(1) and inv_flag == 1:
      S = in_val[2] + S
    if in_val[1] == str(1) and inv_flag == -1:
      S = S + in_val[2]
    if in_val[1] == str(2) and inv_flag == 1:
      S = S + in_val[2]
    if in_val[1] == str(2) and inv_flag == -1:
      S = in_val[2] + S

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

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]
    elif 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())

s = list(s)


def query_1():
    global s
    s = s[-1::-1]


def query_2(s, a, b):
    if a == 2:
        s.append(b)
    elif a == 1:
        s.insert(0, b)


for i in range(q):
    array = list(input().split())
    if len(array) == 1:
        query_1()
    else:
        query_2(s, int(array[1]), array[2])

for i in s:
    print(i, end='')

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readline().strip()
Q = int(sys.stdin.readline().strip())
Query = [(input().split()) for qn in range(Q)]

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

Submission status: Time Limit Exceeded

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

rev = False
for _ in range(q):
    query = list(input().split())
    if query[0] == "1":
        if rev:
            rev = False
        else:
            rev = True
    elif query[1] == "1":
        if rev:
            s += query[2]
        else:
            s = query[2] + s
    else:
        if rev:
            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())

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
s = sys.stdin.readline().strip()
Q = int(sys.stdin.readline().strip())
#s = str(input())
#Q = int(input())

for _ in range(Q):
	qu = input()
	if qu == '1':
		s = s[::-1]
	else:
		_, fi, ci = qu.split()
		if fi == '1':
			s = ci + s
		if fi == '2':
			s += ci
print(s)

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
S = ''
head = True

for i in range(q):
    li = input().split()
    if li[0] == '1':
        head = not head
    else:
        if head:
            if li[1] == '1':
                s = li.pop() + s
            else:
                s = s + li.pop()
        else:
            if li[1] == '2':
                s = li.pop() + s
            else:
                s = s + li.pop()

if not head:
    s = s[::-1]
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(input())
    q = int(input())
    S = ''
    for i in range(q):
        li = input().split()
        if li[0] == '1':
            s.reverse()
        else:
            if li[1] == '1':
                s = [li[2]]  + s
            else:
                s = s + [li[2]]
    for i in s:
        S += i
    print(S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for _ in range(Q):
	qu = input()
	if qu == '1':
		s = s[::-1]
	else:
		_, fi, ci = qu.split()
		if fi == '1':
			s = ci + s
		if fi == '2':
			s += ci
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
for i in range(Q):
    T =  input()
    if len(T) == 1:
        S = S[::-1]
    else:
        a, F, C = T.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())


for i in range(n):
    ss=input()
    if len(ss)==1:
        s=s[::-1]
    else:
        a=list(ss.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(n):
    q = input().split()
    if len(q) == 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()
c=0
tem=''
te=''
for tc in range(int(input())):
    q=input()
    if q=='1':
        s=s[::-1]
    else:
        q=q.split()
        if q[1]=='1':
            s=q[2]+s
        else:
            s+=q[2]
print(s)

Submission status: Time Limit Exceeded

Solution code:
# 23:22

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

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from sys import exit, stdin
S = deque(stdin.readline().rstrip())
Q = int(stdin.readline().rstrip())
query = [stdin.readline().rstrip().split() for _ in range(Q)]
for q in query:
    if q[0] == '1':
        S.reverse()
    elif q[0] == '2':
        if q[1] == '1':
            S.appendleft(q[2])
        elif q[1] == '2':
            S.append(q[2])
print(''.join(S))

Submission status: Time Limit Exceeded

Solution code:
def string_formation():
    s = str(input().strip())
    q = int(input())
    flag = 0
    for _ in range(q):
        line = input()
        if len(line) == 1:
            flag = (flag + 1) % 2
        else:
            if (line[2] == '1' and flag == 1) \
            or (line[2] == '2' and flag == 0):
                s += line[-1]
            else:
                s = line[-1] + s
    if flag: return ''.join(reversed(s))

    return s
print(string_formation())

Submission status: Time Limit Exceeded

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

mode = 1

def prob_d(s, q):
    ans = s
    for i in range(q):
        ans = ope(ans)
    if mode == -1:
        ans = ans[::-1]
    print(ans)

def reverse():
    global mode
    mode *= -1

def add_head(s, c):
    if mode == -1:
        return s + c
    return c + s

def add_tail(s, c):
    if mode == -1:
        return c + s
    return s + c

def ope(s):
    op = input().split()
    if len(op) == 1 and op[0] == '1':
        reverse()
    elif len(op) == 3 and op[0] == '2':
        if op[1] == "1":
            s = add_head(s, op[2])
        elif op[1] == "2":
            s = add_tail(s, op[2])
        else:
            pass
    else:
        pass
    return s

if __name__ == '__main__':
    s = input()
    q = int(input())
    prob_d(s, q)

Submission status: Time Limit Exceeded

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

def prob_d(s, q):
    ans = s
    for i in range(q):
        ans = ope(ans)
    print(ans)

def reverse(s):
    return s[::-1]

def add_head(s, c):
    return c + s

def add_tail(s, c):
    return s + c

def ope(s):
    op = input().split()
    if len(op) == 1 and op[0] == '1':
        s = reverse(s)
    elif len(op) == 3 and op[0] == '2':
        if op[1] == "1":
            s = add_head(s, op[2])
        elif op[1] == "2":
            s = add_tail(s, op[2])
        else:
            pass
    else:
        pass
    return s

if __name__ == '__main__':
    s = input()
    q = int(input())
    prob_d(s, q)

Submission status: Time Limit Exceeded

Solution code:
def string_formation():
    s = str(input().strip())
    q = int(input())
    for _ in range(q):
        line = input().strip().split()
        op = int(line[0])
        if op == 1:
            s = ''.join(reversed(s))
        elif op == 2:
            pos = int(line[1])
            if pos == 2:
                s += line[2]
            elif pos == 1:
                s = line[2] + s
    return s

print(string_formation())

Submission status: Time Limit Exceeded

Solution code:
from collections import deque as dq
s=list(input())
Q=int(input())

s=dq(s)
flag=1

for i in range(Q):
  Query = input().split()
  if(Query[0]=="1"):
    flag=flag*(-1)
    #tmp=[]
    #for j in range(s_len):
      #tmp.append(s[s_len-j-1])
    #s=tmp
  elif((Query[1]=="1" and flag ==1) or (Query[1]=="2" and flag ==-1)):
    s.appendleft(Query[2])
  else:
    s.append(Query[2])
  #print(s,s_len)

  

s_len=len(s)  
out=""
if(flag==1):
  for j in range(s_len):
    out=out+s[j]
else:
  for j in range(s_len):
    out=out+s[s_len-j-1]
    
print(out)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque as dq
s=list(input())
Q=int(input())

s=dq(s)
s_len=len(s)
flag=1

for i in range(Q):
  Query = input().split()
  if(Query[0]=="1"):
    flag=flag*(-1)
    #tmp=[]
    #for j in range(s_len):
      #tmp.append(s[s_len-j-1])
    #s=tmp
  elif((Query[1]=="1" and flag ==1) or (Query[1]=="2" and flag ==-1)):
    s.appendleft(Query[2])
    s_len+=1
  else:
    s.append(Query[2])
    s_len+=1
  #print(s,s_len)
    
out=""
if(flag==1):
  for j in range(s_len):
    out=out+s[j]
else:
  for j in range(s_len):
    out=out+s[s_len-j-1]
    
print(out)

Submission status: Time Limit Exceeded

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

s_len=len(s)
flag=1

for i in range(Q):
  Query = input().split()
  if(Query[0]=="1"):
    flag=flag*(-1)
    #tmp=[]
    #for j in range(s_len):
      #tmp.append(s[s_len-j-1])
    #s=tmp
  elif((Query[1]=="1" and flag ==1) or (Query[1]=="2" and flag ==-1)):
    s.insert(0,Query[2])
    s_len+=1
  else:
    s.append(Query[2])
    s_len+=1
  #print(s,s_len)
    
out=""
if(flag==1):
  for j in range(s_len):
    out=out+s[j]
else:
  for j in range(s_len):
    out=out+s[s_len-j-1]
    
print(out)

Submission status: Time Limit Exceeded

Solution code:
n = list(input())
a = int(input())
r = 0
count = 0
for i in range(a):
    l = input().split(" ")
    if l[0] == "1":
        count += 1
        if r == 0:
            r = 1
        else:
            r = 0
    else:
        if l[1] == '1':
            if r == 0:
                n.insert(0,l[2])
            else:
                n.append(l[2])
        else:
            if r == 0:
                n.append(l[2])
            else:
                n.insert(0,l[2])
if count % 2 == 1:
    n.reverse()
print("".join(n))

Submission status: Time Limit Exceeded

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

s_len=len(s)

for i in range(Q):
  Query = input().split()
  if(Query[0]=="1"):
    tmp=[]
    for j in range(s_len):
      tmp.append(s[s_len-j-1])
    s=tmp
  elif(Query[1]=="1"):
    s.insert(0,Query[2])
    s_len+=1
  else:
    s.append(Query[2])
    s_len+=1
  #print(s,s_len)
    
out=""
for j in range(s_len):
  out=out+s[j]
    
print(out)

Submission status: Time Limit Exceeded

Solution code:
n = list(input())
a = int(input())
for i in range(a):
    l = input().split(" ")
    if l[0] == "1":
        n.reverse()
    else:
        if l[1] == '1':
            n.insert(0,l[2])
        else:
            n.append(l[2])
print("".join(n))

Submission status: Time Limit Exceeded

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

jun = True
for i in range(nq):
  query = input().split()
  if query[0] == '1':
    jun = not jun
    continue
  if jun:
    if query[1] == '1':
      s = query[2] + s
    else:
      s = s + query[2]
  else:
    if query[1] == '1':
      s = s + query[2]
    else:
      s = query[2] + s
if jun:
  print(s)
else:
  print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
s_is_reversed = False
for j in range(Q):
    if Query[j][0] == '1':
        s_is_reversed = not s_is_reversed
    elif Query[j][1] == '1':
        if s_is_reversed == False:
            s.insert(0,Query[j][2])
        else:
            s.append(Query[j][2])
    else:
        if s_is_reversed == False:
            s.append(Query[j][2])
        else:
            s.insert(0,Query[j][2])
if s_is_reversed == True:
    s.reverse()
printed = ''.join(s)
print(printed)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 09:27:00 2020

@author: azikausy
"""


S = list(input())
Q = int(input())
Query = {}
#TFC = []
perm = 0
for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        #Query[i] = [l[0], 0, 0]
        #S.reverse()
        perm += 1
    else:
        #Query[i] = l
        if (int(l[1])+perm) % 2 == 0:
            S.append(l[2])
        else:
            S.insert(0, l[2])
if perm % 2 != 0:
    S.reverse()
ans = ""
for i in range(0, len(S)):
    ans = ans + S[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 22 09:27:00 2020

@author: azikausy
"""


S = list(input())
Q = int(input())
Query = {}
#TFC = []
for i in range(0, Q):
    l = input().split()
    if len(l) == 1:
        #Query[i] = [l[0], 0, 0]
        S.reverse() 
    else:
        #Query[i] = l
        if l[1] == "2":
            S.append(l[2])
        else:
            S.insert(0, l[2])

ans = ""
for i in range(0, len(S)):
    ans = ans + S[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Query = [input().split() for _ in range(Q)]
s = list(S)
for j in range(Q):
    if Query[j][0] == '1':
        s.reverse()
    elif Query[j][1] == '1':
        s.insert(0,Query[j][2])
    else:
        s.append(Query[j][2])
printed = ''.join(s)
print(printed)

Submission status: Time Limit Exceeded

Solution code:
def main():

    st=input()
    N = int(input())
    sw=True
    for i in range(N):
        t= input().split()
        if t[0]=='1':
            sw=not sw

        elif (sw and t[1]=='2') or (not sw and t[1] == '1'):

            st=''.join([st,t[2]])
        else:
            st=''.join([t[2],st])

    if not sw:
        print(st[::-1])
    else:
        print(st)
main()

Submission status: Time Limit Exceeded

Solution code:
#import time
def main():

    st=input()
    N = int(input())
    sw=0
#    t1=time.time()
    for i in range(N):
        t= input().split()
        if t[0]=='1':
            if sw==0:
                sw=1
            elif sw==1:
                sw=0
        elif sw==0 and t[0]=='2':
            if t[1]=='1':
                st=''.join([t[2],st])
            elif t[1]=='2':
                st=''.join([st,t[2]])
        elif sw==1 and t[0]=='2':
            if t[1]=='1':
                st=''.join([st,t[2]])
            elif t[1]=='2':
                st=''.join([t[2],st])

    if sw==1:
        print(st[::-1])
    else:
        print(st)
#    t2=time.time()
#    print(t2-t1)

main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
flag = True
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        flag = not flag

                        
    if flag: #1
        # print("regular")
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
    else: #2
        # print("rev")
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = S + T[2]
            elif int(T[1]) == 2: 
                S = T[2] + S
if not flag:
    S = S[::-1]
print(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
Q = int(input())
Q_query = []
for _ in range(Q):
    T = input().split()
    if len(T) == 1:
        S = S[::-1]
                        
    else: 
        if int(T[0]) == 2:
            if int(T[1]) == 1:
                S = T[2]+ S
            elif int(T[1]) == 2: 
                S = S + T[2]
print(S)

Submission status: Time Limit Exceeded

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

s = input().rstrip()
n = int(input().rstrip())
q_list = [input().rstrip().split() for i in range(n)]
front = ''
back = ''
count = 1
for i in q_list:
    if i[0] == '1':
        count *= -1
    else:
        if i[1] == '1':
            if count == 1:
                front += i[2]
            else:
                back += i[2]
        else:
            if count == 1:
                back += i[2]
            else:
                front += i[2]
if count == 1:
    print(front[::-1] + s + back)
else:
    print(back[::-1] + s[::-1] + front)

Submission status: Time Limit Exceeded

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

s = input().rstrip()
n = int(input().rstrip())
front = ''
back = ''
count = 1
for i in range(n):
    q = input().rstrip().split()
    if q[0] == '1':
        count *= -1
    else:
        if q[1] == '1':
            if count == 1:
                front += q[2]
            else:
                back += q[2]
        else:
            if count == 1:
                back += q[2]
            else:
                front += q[2]
if count == 1:
    print(front[::-1] + s + back)
else:
    print(back[::-1] + s[::-1] + front)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
q = [input().split() for i in range(n)]
k, l = 0, 1
for i in range(n):
    if q[i][0] == '1':
        k,l = l,k
    else:
        if k == 0:
            if q[i][1] == '1':
                s = q[i][2] + s
            else:
                s += q[i][2]
        else:
            if q[i][1] == '1':
                s += q[i][2]
            else:
                s = q[i][2] + s
print(s if k == 0 else s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
q = int(input())
revflag = 1
for i in range(q):
	tfc = list(map(str, input().split()))
	if tfc[0] == '1':
		revflag *= -1
	elif tfc[1] == '1':
		if revflag > 0:
			s = tfc[2] + s
		else:
			s = s + tfc[2]
	else:
		if revflag > 0:
			s += tfc[2]
		else:
			s = tfc[2] + s
if revflag > 0:
	print(s)
else:
	print(s[::-1])

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = int(input())
q = [input().split() for i in range(n)]
for i in range(n):
    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:
s=input()
q=int(input())
T=[]
for _ in range(q):
    T.append(input())
    
for i in range(q):
    if T[i]=="1":
        s=s[::-1]
    else:
        if T[i][2]=="1":
            s=T[i][4]+s
        else:
            s=s+T[i][4]
        
print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if L == 1:
    print(S)
else:
    print(''.join(list(reversed(S))))

Submission status: Time Limit Exceeded

Solution code:
def main():
    st=input()
    N = int(input())
    t = [0] * N
    x = [0] * N
    y = [0] * N
    sw=0
    for i in range(N):
        try:
            t[i], x[i], y[i] = map(str,input().split())
        except ValueError :
            t[i],x[i],y[i]='1','',''
        if t[i]=='2':
            if sw==0:
                if x[i]=='1':
                    st=''.join([y[i],st])
                elif x[i]=='2':
                    st=''.join([st,y[i]])
            elif sw==1:
                if x[i]=='1':
                    st=''.join([st,y[i]])
                elif x[i]=='2':
                    st=''.join([y[i],st])
        elif t[i]=='1' and sw==0:
            sw=1
        elif t[i]=='1' and sw==1:
            sw=0

    if sw==1:
        print(st[::-1])
    else:
        print(st)

main()