Pretty Good Proportion (Large)
Time limit5sMemory limit512 MB
Given a binary string and a target fraction F, find the smallest starting position among substrings whose proportion of 1s is closest to F.
- Level
Medium7 of 10
- Topics
- Prefix sum, Sorting
- Solved
- No attempts yet
Problem
You have a string of binary digits. You want a substring whose proportion of 1s is exactly some target value, but such a substring may not exist, so a close one will do.
Given a decimal fraction , find a substring whose proportion of 1s is as close to as possible. The proportion of 1s of a substring is the number of 1s in it divided by its length. A substring is a run of consecutive characters and its length is at least 1. Report the smallest starting position among the substrings that come closest.
Input
The first line contains the number of test cases . test cases follow.
Each test case begins with a line containing and , separated by one space. is a decimal fraction between 0 and 1 inclusive, written with exactly 6 digits after the decimal point. The next line contains digits, each 0 or 1, with no spaces.
Limits
- has exactly 6 digits after the decimal point
Output
For each test case, print one line of the form Case #x: y. Here is the test case number starting from 1, and is the starting position of a substring whose proportion of 1s is as close to as possible. Positions are counted from 0. When several starting positions are possible, print the smallest one.
Explanation
Take and the string 001001010111. No substring has a proportion of 1s equal to , and the closest reachable value is . Five substrings reach it: 101, 101, 011 of length 3 starting at positions 5, 7, 8, and 101011, 010111 of length 6 starting at positions 5 and 6. The smallest of those starting positions is 5.