【題解】Codeforces 1404A. Balanced Bitstring

【題目敘述】http://codeforces.com/contest/1404/problem/A

#include <iostream>
using namespace std;
 
int t, n, k, c[300005];
string s;
 
int main() {
    cin >> t;
    while (t--){
        cin >> n >> k;
        cin >> s;
        bool flag = true;
        for (int i = 0; i < k; i++){
            c[i] = -1;
        }
        for (int i = 0; i < n; i++){
            if (s[i] == '?') continue;
            if (c[i%k] == -1) c[i%k] = s[i]-'0';
            if (s[i]-'0' != c[i%k]){
                flag = false;
                break;
            }
        }
        if (!flag){
            cout << "NO\n";
            continue;
        }
        int zero = 0, one = 0;
        for (int i = 0; i < k; i++){
            if (c[i] == 0) zero++;
            else if (c[i] == 1) one++;
        }
        if (zero <= k/2 && one <= k/2) cout << "YES\n";
        else cout << "NO\n";
    }
}
分享本文 Share with friends