#C2162. Lexicographically Smallest String After Deletion
Lexicographically Smallest String After Deletion
Lexicographically Smallest String After Deletion
Given a string (s) and an integer (k), you are required to delete exactly (k) characters from (s) so that the resulting string is lexicographically smallest.
In other words, you need to select a subsequence from (s) (by deleting (k) characters) such that the remaining string is as small as possible in dictionary order.
A common approach to solve this problem is to use a greedy algorithm with a stack. At each step, you compare the current character with the top of the stack. If the top character is greater than the current character and you still have deletions available, you pop the stack. This process ensures the result is lexicographically smallest.
For example, if (s = \texttt{'abcde'}) and (k = 3), the resulting string is (\texttt{'ab'}).
inputFormat
The first line contains a single integer (T), the number of test cases.
For each test case, the input consists of two lines:
- The first line contains an integer (k), which is the number of characters to delete.
- The second line contains the string (s).
outputFormat
For each test case, output the lexicographically smallest string obtained by deleting exactly (k) characters from (s). Each answer should be printed on a new line.## sample
3
3
abcde
2
cba
1
abac
ab
a
aac
</p>