click below for solution

Saturday, 29 January 2022

Preflix Zeros Solution|codesheff lunchtime 2022 for div 3


You are given a string S of length N, which consists of digits from 0 to 9. You can apply the following operation to the string:

  • Choose an integer L with 1LN and apply Si=(Si+1)mod10 for each 1iL.

For example, if S=39590, then choosing L=3 and applying the operation yields the string S=406_90.

The prefix of string S of length l(1l≤∣S) is string S1S2Sl. A prefix of length l is called good if S1=0,S2=0,,Sl=0. Find the length of the longest good prefix that can be obtained in string S by applying the given operation maximum K times.

Input Format

  • The first line of input contains an integer T, denoting the number of test cases. The T test cases then follow:
  • The first line of each test case contains two space-separated integers N,K.
  • The second line of each test case contains the string S.

Output Format

For each test case, output in a single line the length of the longest good prefix that can be obtained in string S by applying the given operation maximum K times.

Constraints

  • 1T105
  • 1N105
  • 0K109
  • S∣=N
  • S contains digits from 0 to 9
  • Sum of N over all test cases does not exceed 3105.

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 1: There is no way to obtain zeros on the prefix of the string S=380 by applying the given operation maximum 5 times.

Test case 2: The optimal strategy is: choose L=2 and apply the operation twice, resulting in S=500, then choose L=1 and apply the operation 5 times, resulting in S=000.

Test case 4: One of the possible sequence of operations is the following:

  • Choose L=5 and apply the operation thrice, resulting in S=01045.
  • Choose L=2 and apply the operation 9 times, resulting in S=90045.
  • Choose L=1 and apply the operation once, resulting i
  • Solution
   This question solution is updated within contest time. please click on follow button and subscribe our channel.if you follow and subscribe our channel then get notification of answer as soon as possible.

No comments:

Post a Comment