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
function pre_process ( substrings , s ) { var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { var dup = " " ; for ( var j = i ; j < n ; j ++ ) { dup += s [ j ] ; substrings . push ( dup ) ; } } substrings . sort ( ) ; } var s = " " ; var substrings = [ ] ; pre_process ( substrings , s ) ; var queries = [ 1 , 5 , 10 ] ; var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) document . write ( substrings [ queries [ i ] - 1 ] + " " ) ;