1404A.md (2021B)
1 Source: https://codeforces.com/problemset/problem/1404/A 2 3 A. Balanced Bitstring 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called $k$-balanced if every substring of size $k$ of this bitstring has an equal amount of 0 and 1 characters ($\frac{k}{2}$ of each). 22 23 You are given an integer $k$ and a string $s$ which is composed only of characters 0, 1, and ?. You need to determine whether you can make a $k$-balanced bitstring by replacing every ? characters in $s$ with either 0 or 1. 24 25 A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. 26 27 Input 28 29 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. 30 31 The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 3 \cdot 10^5$, $k$ is even) — the length of the string and the parameter for a balanced bitstring. 32 33 The next line contains the string $s$ ($|s| = n$). It is given that $s$ consists of only 0, 1, and ?. 34 35 It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. 36 37 Output 38 39 For each test case, print YES if we can replace every ? in $s$ with 0 or 1 such that the resulting bitstring is $k$-balanced, or NO if it is not possible. 40 41 Example 42 43 Input 44 45 ``` 46 9 47 6 4 48 100110 49 3 2 50 1?1 51 3 2 52 1?0 53 4 4 54 ???? 55 7 4 56 1?0??1? 57 10 10 58 11??11??11 59 4 2 60 1??1 61 4 4 62 ?0?0 63 6 2 64 ????00 65 ``` 66 67 Output 68 69 ``` 70 YES 71 YES 72 NO 73 YES 74 YES 75 NO 76 NO 77 YES 78 NO 79 ``` 80 81 Note 82 83 For the first test case, the string is already a $4$-balanced bitstring. 84 85 For the second test case, the string can be transformed into 101. 86 87 For the fourth test case, the string can be transformed into 0110. 88 89 For the fifth test case, the string can be transformed into 1100110.