Queries to answer the X | Function to pre - process the sub - strings in sorted order ; Generate all substrings ; Iterate to find all sub - strings ; Store the sub - string in the vector ; Sort the substrings lexicographically ; Driver code ; To store all the sub - strings ; Perform queries
def pre_process ( substrings , s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT dup = " " ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT dup += s [ j ] ; NEW_LINE substrings . append ( dup ) ; NEW_LINE DEDENT DEDENT substrings . sort ( ) ; NEW_LINE return substrings ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geek " ; NEW_LINE substrings = [ ] ; NEW_LINE substrings = pre_process ( substrings , s ) ; NEW_LINE queries = [ 1 , 5 , 10 ] ; NEW_LINE q = len ( queries ) ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( substrings [ queries [ i ] - 1 ] ) ; NEW_LINE DEDENT DEDENT