## Lexicographically Least Substring (Hard)

View as PDF

Points: 15
Time limit: 1.8s
Memory limit: 256M

Problem type
##### Brute Force Practice 2 — Hard Version

You have a string (indexed from ) with no more than lowercase characters. Find the lexicographically least substring with a length of at least . A string is said to be lexicographically smaller than a string if and is a prefix of or and . Here, denotes the length of the string.

#### Input Specification

The first line will have the string.

The second line will have .

#### Output Specification

Print the lexicographically least substring of length at least .

#### Sample Input

iloveprogramming
4

#### Sample Output

ammi

## Comments

• commented on Nov. 29, 2017, 3:48 a.m.

My code TLEs on the 16th case, but ACs on the rest. Any tips?

• commented on May 21, 2021, 3:55 a.m.

In my submission, I had the same thing. I don't want to spoil my solution, but I was sorting a lot of pair's, which maybe made the comparisons take too long (constant factor). I fixed it weirdly by using qsort instead of std::sort in C++.

• commented on Feb. 15, 2015, 4:43 a.m. edited

What should my program output if the size of is larger than the given string.

• commented on Feb. 15, 2015, 5:02 a.m.

You may assume

• commented on Nov. 24, 2014, 5:21 a.m.

IS this a true brute force problem? keep getting memory limits on mostly all the test cases when trying to brute force it

• commented on Nov. 25, 2014, 1:28 a.m.

No, it's not actually brute force -- it's just a harder version of the brute force practice (the name is a bit misleading).

• commented on Nov. 25, 2014, 3:40 a.m.

Ok, Would this be something along the lines of a suffix tree then?

• commented on Nov. 25, 2014, 3:52 a.m. edited

You can theoretically solve it with an suffix tree, but there are algorithms with lower constant factors.

• commented on Nov. 25, 2014, 8:40 p.m.

Mind telling me of such algorithms? I cant think of any :(

• commented on Nov. 25, 2014, 10:12 p.m.

One example is the suffix array. If you're going to use that, you'd need a very fast algorithm, like DC3 implemented in a very efficient manner.

• commented on Nov. 25, 2014, 11:08 p.m.

Nvm, its not working, still memory limits. Wouldn't using a suffix array still take lots of memory? Because after the suffix array is created, you need to sort it then iterate through the list.

• commented on Nov. 25, 2014, 11:09 p.m. edited

Your algorithm for suffix array is extremely slow naive brute force . There are algorithms for suffix array, such as DC3. You probably have to implement it in C++.

• commented on Nov. 25, 2014, 10:22 p.m.

I originally thought of a suffix array, however how would you find the Lexicographically Least Substring via that. Wouldnt you need to store all possible answers in an array and sort it?

• commented on Nov. 25, 2014, 10:24 p.m.

NVM figured it out myself...... suffix array is sorted initially.

Thanks though!