【題解】Codeforces 1400C. Binary String Reconstruction

【題目敘述】http://codeforces.com/contest/1400/problem/C

#include <iostream>
#include <cstring>
using namespace std;
 
int t, x, ans[200005];
string s;
 
int main() {
    cin >> t;
    while (t--){
        cin >> s >> x;
        memset(ans, -1, sizeof(ans));
        bool flag = true;
        for (int i = 0; i < s.length(); i++){
            if (s[i] == '1'){
                if (i-x >= 0 && ans[i-x]) ans[i-x] = 1;
                else if (i+x < s.length()) ans[i+x] = 1;
                else{
                    flag = false;
                    break;
                }
            }
            else{
                if (i-x >= 0){
                    if (ans[i-x] == -1) ans[i-x] = 0;
                    else if (ans[i-x] == 1){
                        flag = false;
                        break;
                    }
                }
                if (i+x < s.length()) ans[i+x] = 0;
            }
        }
        if (!flag) cout << -1 << "\n";
        else{
            for (int i = 0; i < s.length(); i++){
                if (ans[i] == -1) cout << 1;
                else cout << ans[i];
            }
            cout << "\n";
        }
    }
}
分享本文 Share with friends