Codeforces Round #668 (Div. 2) C. Balanced Bitstring

C. Balanced Bitstring

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called 𝑘-balanced if every substring of size 𝑘 of this bitstring has an equal amount of 0 and 1 characters (𝑘2 of each).

You are given an integer 𝑘 and a string 𝑠 which is composed only of characters 0, 1, and ?. You need to determine whether you can make a 𝑘-balanced bitstring by replacing every ? characters in 𝑠 with either 0 or 1.

A string 𝑎 is a substring of a string 𝑏 if 𝑎 can be obtained from 𝑏 by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input

Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). Description of the test cases follows.

The first line of each test case contains two integers 𝑛 and 𝑘 (2≤𝑘≤𝑛≤3⋅105, 𝑘 is even) — the length of the string and the parameter for a balanced bitstring.

The next line contains the string 𝑠 (|𝑠|=𝑛). It is given that 𝑠 consists of only 0, 1, and ?.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 3⋅105.

Output

For each test case, print YES if we can replace every ? in 𝑠 with 0 or 1 such that the resulting bitstring is 𝑘-balanced, or NO if it is not possible.

Example

input

9
6 4
100110
3 2
1?1
3 2
1?0
4 4
????
7 4
1?0??1?
10 10
11??11??11
4 2
1??1
4 4
?0?0
6 2
????00

output

YES
YES
NO
YES
YES
NO
NO
YES
NO

Note

For the first test case, the string is already a 4-balanced bitstring.

For the second test case, the string can be transformed into 101.

For the fourth test case, the string can be transformed into 0110.

For the fifth test case, the string can be transformed into 1100110.

题意

定义平衡字符串为: 该字符串内0和1的个数相等,现出给一个字符串s,s只包含0/1/?,?可被随意替换为0/1,然后给出k,问能否将s变为一个任意长为k的substring都是平衡字符串。

解决

首先用滑动窗口的思想看下问题,如下所示,n = 6, k = 3
1. 100110
2. 100110
由1滑到2时,窗口相当于左边减少了一个1,右边增加了一个1,还是平衡,这点很重要,说明必须满足s[i] == s[i + k],不满足则找不到题目要求的串。根据条件
s[i] == s[i + k]可以引出字符串s如果满足要求,则|s[0]...s[k - 1]|, |s[k]...s[k * 2-1] ...这些字串实际都是相同的,所以现在按照条件只要判断s[0]...s[k-1]
这个串能否找到满足要求的串就可以了。

#include <bits/stdc++.h>

#define MAXN 345678

using namespace std;

int t;
int n, k;
string s;
bool ok;
char tmp;
int one, zero, un;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> t;
  while (t--) {
    ok = true;
    
    cin >> n >> k;
    cin >> s;
    for (int i = 0; i < k; i++) {
      tmp = '?';
      int j = i;
      while(j < n) {
        if (s[j] != '?') {
          tmp = s[j];
          break;
        }
        j += k;
      }
      if (tmp == '?') 
        continue;
      for (int j = i; j < n; j += k) {
        if (s[j] == '?') {
          s[j] = tmp;
        } else {
          if (s[j] != tmp) {
            ok = false;
            break;
          }
        }
      }
    }
    one = 0;
    zero = 0;
    un = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] == '1') {
        one++;
      } else if (s[i] == '0') {
        zero++;
      } else {
        un++;
      }
    }
    if (one == zero) {
      if (un % 2) {
        ok = false;
      }
    } else if (one > zero) {
      if (un < one - zero) {
        ok = false;
      } else {
        if ((un - (one - zero)) % 2) {
          ok = false;
        }
      }
    } else {
      if (un < zero - one) {
        ok = false;
      } else {
        if ((un - (zero - one)) % 2) {
          ok = false;
        }
      }
    }
    puts(ok ? "YES" : "NO");
  }
  return 0;
}
posted @ 2020-09-07 10:47  勿忘初心0924  阅读(329)  评论(0编辑  收藏  举报