Codeforces Round #668 (Div. 1)A. Balanced Bitstring

题目

传送门
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 (k2 of each).

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.

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.

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

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

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

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

Output
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.

输入
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
输出
YES
YES
NO
YES
YES
NO
NO
YES
NO

题意:给出一个只含有含有“1”,‘0’,‘?’且长度为n的字符串,判断他的每个长度为k的子串是否可以通过把其中的‘?’换成‘1’或‘0’使1的数量等于0的数量(有的长度为k的子串可能没有‘?’,这时直接判断就行)

思路:本来想的是直接判断1或0的数量是否超过k/2再加上k=2的特判,写完之后发现不对…正确的思路应该是:我们可以发现从第一个子串往后的下一个子串少了当时子串的第一个字符多了当时子串的下一个字符,如果我们想满足条件的话a[i]和a[i+k]两个字符应该是相等的,所以我们可以根据这个特性确定第一个长度为k的子串,然后判断每个子串是否满足条件即可,具体看代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
char a[320000],b[320000];
void solve()
{  int n,k;
cin>>n>>k;
cin>>a;
    int flag=1;
    for(int i=0;i<k;i++)
         {
             for(int j=i+k;j<n;j+=k)
    {
        if(a[i]=='?'&&a[j]!='?')//确定第一个子串,‘?’只能代表一个‘0’或一个‘1’
            a[i]=a[j];
        if(a[i]!=a[j]&&a[i]!='?'&&a[j]!='?')//判断每隔k个字符的位置的字母是否相等
        {
            flag=0;
            break;
        }
    }
    if(flag==0)break;}
    int p=0,q=0,l=0,r=k;
    for(int i=0;i<k;i++)
      {
          if(a[i]=='1')p++;
             else if(a[i]=='0')q++;
      }
      if(p>k/2||q>k/2)
           {
               flag=0;
           }//首先判断第一个子串是否满足条件
    while(r<n)//判断剩下的字符是否满足条件
     {
         if(a[r]=='1')p++;
            else if(a[r]=='0')q++;
         r++;
         if(a[l]=='1')p--;
           else if(a[l]=='0')q--;
           l++;
           if(p>k/2||q>k/2)
           {
               flag=0;
               break;
           }
     }
     if(flag)printf("YES\n");
        else printf("NO\n");
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       solve();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值