[Solution] Sorted Substrings CodeChef Solution
Problem
You have a binary string . You can perform the following operation on :
- Select any substring which is sorted and remove it from . (Both the left and right halves are concatenated after performing the operation)
For e.g. if , we can perform the following operation:
Find the minimum number of operations required so that the final binary string is sorted.
Note that a substring is formed by deleting some (possibly zero) characters from the beginning and some (possibly zero) characters from the end of the string.
Input Format
- The first line contains a single integer — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer — the length of the binary string .
- The second line of each test case contains a binary string of length containing s and s only.
Output Format
For each test case, output the minimum number of operations required so that the final binary string is sorted.
Explanation:
Test case : The binary string is already sorted, so we need no operations.
Test case : We can use one operation to remove the sorted substring . Thus, the remaining string is sorted.
Test case : We can use one operation to remove the sorted substring . Thus, the remaining string is sorted.
No comments:
Post a Comment