You are given a string of length , which consists of digits from to . You can apply the following operation to the string:
- Choose an integer with and apply for each .
For example, if , then choosing and applying the operation yields the string .
The prefix of string of length is string . A prefix of length is called good if . Find the length of the longest good prefix that can be obtained in string by applying the given operation maximum times.
Input Format
- The first line of input contains an integer , denoting the number of test cases. The test cases then follow:
- The first line of each test case contains two space-separated integers .
- The second line of each test case contains the string .
Output Format
For each test case, output in a single line the length of the longest good prefix that can be obtained in string by applying the given operation maximum times.
Constraints
- contains digits from to
- Sum of over all test cases does not exceed .
Subtasks
- Subtask 1 (100 points): Original constraints
Sample Input 1
6
3 5
380
3 9
380
4 2
0123
5 13
78712
6 10
051827
8 25
37159725
Sample Output 1
0
3
1
3
2
5
Explanation
Test case : There is no way to obtain zeros on the prefix of the string by applying the given operation maximum times.
Test case : The optimal strategy is: choose and apply the operation twice, resulting in , then choose and apply the operation times, resulting in .
Test case : One of the possible sequence of operations is the following:
- Choose and apply the operation thrice, resulting in .
- Choose and apply the operation times, resulting in .
- Choose and apply the operation once, resulting i
- Solution
No comments:
Post a Comment