SUBLEX - Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:
If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?
After knowing the huge number of questions Kinan will ask, Daniel figured out that he can't do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan's questions.
S = "aaa" (without quotes)
substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:
"a", "aa", "aaa".
In the first line there is Kinan's string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).
Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.
Edited: Some input file contains garbage at the end. Do not process them.
Loved this one! Suffix Array + LCP + Binary Search. O(nlogn + logq).
I got AC supposing that K <= #(distinct substrings), but I think this should explicit in the statement.
Do not deal with multi-cases!!!
@sanzee use O(nlogn) for building suffix array
AC in first go !! :D
Muhammad Rifayat Samee (Sanzee):
getting TLE using suffix array O(n * (lgn)^2) .....
SAM is expected!
What's the possible reason of NZEC error in JAVA?Last edit: 2015-01-23 19:30:24
Buda IM (retired):
I got AC without handling that kind of cases
What do we suppose to answer if K > #(distinct substrings)? It seems like it could happen...Last edit: 2011-11-04 19:30:54