ASTRING - Editorial
<h1>PROBLEM LINK</h1>
[Practice](http://www.codechef.com/problems/ASTRING)<br>
[Contest](http://www.codechef.com/LTIME36/problems/ALOST)<br>
**Author:** [Amit Pandey](http://www.codechef.com/users/amitpandeykgp)<br>
**Tester:** [Prateek Gupta](http://www.codechef.com/users/prateekg603)<br>
**Editorialist:** [Prateek Gupta](https://www.codechef.com/users/prateekg603)<br>
<h1>DIFFICULTY:</h1>
Easy
<h1>PREREQUISITES:</h1>
Sets, Implementation
<h1>PROBLEM:</h1>
Find the smallest lexicographical subsequence of length $K$ from a given string of length $N$
<h1>EXPLANATION</h1>
<br>
**Solution for Subtask 1:**<br>
The smallest subtask can indeed be solved by dynamic programming. Basically, you have a choice at every position of the string $S$ i.e, whether to include the current character in final subsequence of $K$ characters or not. Based on this observation, let us define a state as $dp(i, j)$ denoting the smallest lexicographical subsequence of length $j$ formed from first $i$ characters of the original string $S$. Hence, the following equation holds true.
$$dp(i,\ j)\ =\ min(\ dp(i\ -\ 1,\ j)\ ,\ s_i + dp(i\ -\ 1,\ j\ -\ 1)$$
<br>Let's have a look at the pseudo code for the bottom up solution of the above approach.
dp[0][0] = ""
for ( j = 1 to K ) dp[0][j] = "INF"
for ( i = 1 to N )
for ( j = 1 to K )
dp[i][j] = "INF"
if ( dp[i - 1][j] != "INF" ) dp[i][j] = dp[i - 1][j]
if ( dp[i - 1][j - 1] != "INF" )
if ( dp[i][j] == "INF" ) dp[i][j] = dp[i - 1][j - 1] + s[i - 1]
else dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + s[i - 1])
print(dp[N][K])
<br>The same thing can be implemented by recursive memoization solution. You might like to read this blog to know more about [Dynamic Programming][1]. The time complexity for the above approach is $\mathcal{O}(N^{2})$ but the space complexity is around $\mathcal{O}(N^{3})$. Due to the higher space complexity, it won't be possible to implement the same solution for subtask 2 as it can cause memory overflow leading to Runtime Error.
<br>**Solution for subtask 2:**<br>
The approach for this subtask is based on the fact that the subsequence formed at last has to be of length $K$ exactly. Having said that, it is not difficult to realize that the first character of the smallest lexicographical subsequence has to be from substring $s[0,\ n\ -\ k]$. Why? If the first character is not from this substring, then it would not be possible to form a subsequence of length $K$. Similarly, the $i^{th}$ character should be from the substring $s[prev\ + 1,\ n\ -\ k\ +\ i]$, where $prev$ is the position found for $(i - 1)^{th}$ character of the subsequence. Hence, we should always choose the smallest characters in the substrings. And in case, there are two smallest characters, we should choose the one with leftmost position, since it will give a larger substring to choose from, for the rest of the characters in the subsequence. Let us look at the pseudo code for this greedy approach.
subseq = ""
prev_pos = -1
for ( i from 0 to k - 1 )
for ( j from last_pos + 1 to n - k + i ) {
if ( j == last_pos + 1 ) smallest_char = s[j], leftmost_pos = j
else if ( smallest_char > s[j] ) smallest_char = s[j], leftmost_pos = j
prev_pos = leftmost_pos
subseq.append(smallest_char)
}
print(subseq)
Since, for all $K$ characters of the subsequence, it traverses the whole string in the worst case to find the smallest character, the time complexity for this pseudo code is $\mathcal{O}(N*K)$. But, this does not yet suffice to pass all the input files.
<br>**Solution for subtask 3:**<br>
The logical approach for the problem remains the same as mentioned in the subtask $2$. But, the implementation can be optimized to reduce the overall time complexity necessary to pass the final set of input files. For this, we can maintain a data structure that will give the smallest character in a range and in case, there are several smaller characters, it can give us the position of the leftmost smallest character in that range. The data structure which seems most easier at this stage is a set of pairs. Hence, the pairs of (characters, indexes) will be inserted into the set when required and removed when they go out side the current range. The first element of the set will give us the required character and it's corresponding index for that particular position. Suppose, we need to find the $i^{th}$ character of the $K$-length subsequence. Let's say, the $(i\ -\ 1)^{th}$ character was found at position $prev_pos1$. Then your Hence, the range has to be shifted from [prev_pos1, n - k] $[prev1, n\ -\ k]$ to [prev_pos2, n - k n\ - k\ + 1] where prev_pos2 > prev_pos1. Hence, it would be better to remove the pairs from the range[prev_pos1, prev_pos2 - 1] range$[prev1,\ prev2\ -\ 1]$ and insert a new pair of $(s_{n -\ k\ +\ 1},\ n\ -\ k\ +\ 1)$. This would allow you to remove and insert each pair exactly once. Let us now have a look at the pseudo code for the same.
for ( i = 0 to n - k ) Set.insert(make_pair(s[i], i))
subseq.append(Set.top().character)
a = 0, b = Set.top().index
for (i = n - k + 1 to n - 1 )
while ( a <= b ) SET.erase(make_pair(s[a], a)), a++
Set.insert(make_pair(s[i], i))
b = Set.top().second
subseq.append(Set.top().character)
print(subseq)
For more details on the implementation, have a look at the tester's solution.
<h1>COMPLEXITY</h1>
The overall complexity necessary to pass all the input files should be $\mathcal{O}(N*logN)$.
<h1>SOLUTIONS</h1>
Setter<br>
Tester
[1]: https://tp-iiita.quora.com/Dynamic-Programming-Part-1