Under construction

Still being built. Things may change

Pretty Good Proportion (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Given a binary string and a target fraction F, find the start index of the contiguous substring whose share of 1s is closest to F.
Level

Medium4 of 10

Topics
Brute force, Prefix sum
Solved
No attempts yet

Problem

You have a string of NN binary digits. Find a contiguous substring whose fraction of 1s is as close as possible to a target value FF. A substring whose fraction equals FF exactly may not exist, so take the one with the smallest difference.

The fraction of 1s in a substring is the number of 1s in it divided by its length. Only substrings of length 1 or more count. Report the starting index of a substring that minimizes ∣fraction−F∣|\text{fraction} - F|.

Input

The first line contains the number of test cases TT. Each test case takes two lines. The first line contains NN and FF, separated by a single space. FF is a decimal fraction between 0 and 1 inclusive with exactly 6 digits after the decimal point. The second line contains NN digits, each 0 or 1, with no separators.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤10001 \le N \le 1000
  • 0≤F≤10 \le F \le 1
  • FF has exactly 6 digits after the decimal point.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the starting index of the substring whose fraction of 1s is closest to FF. Indices are 0-based. If several substrings achieve the smallest error, print the smallest starting index among them.

Hint

Take the string 001001010111 with F=0.666667F = 0.666667. No substring has a fraction of 1s equal to 666667/1000000666667/1000000. The closest reachable value is 2/32/3, and five substrings reach it. The ones of length 3 start at indices 5, 7, and 8 (101, 101, 011), and the ones of length 6 start at indices 5 and 6 (101011, 010111). The smallest of those starting indices is 5.

Examples1

  1. Example 1

    Input
    5
    12 0.666667
    001001010111
    11 0.400000
    10000100011
    9 0.000000
    111110111
    5 1.000000
    00000
    15 0.333333
    000000000011000
    
    Expected output
    Case #1: 5
    Case #2: 5
    Case #3: 5
    Case #4: 0
    Case #5: 6