Educational Codeforces Round 94 (Rated for Div. 2) C. Binary String Reconstruction

Title

CodeForces 1400 C. Binary String Reconstruction

Time Limit

2 seconds

Memory Limit

256 megabytes

Problem Description

Consider the following process. You have a binary string (a string where each character is either 0 or 1) w w w of length n n n and an integer x x x. You build a new binary string s s s consisting of n n n characters. The i i i-th character of s s s is chosen as follows:

  • if the character w i − x w_{i-x} wixexists and is equal to 1, then s i s_i si is 1 (formally, if i > x i>x i>x and w i − x w_{i-x} wix= 1, then s i s_i si= 1);
  • if the character w i + x w_{i+x} wi+x exists and is equal to 1, then s i s_i si is 1 (formally, if i + x ≤ n i+x\le n i+xn and w i + x w_{i+x} wi+x= 1, then s i s_i si= 1);
  • if both of the aforementioned conditions are false, then s i s_i si is 0.

You are given the integer x x x and the resulting string s s s. Reconstruct the original string w w w.

Input

The first line contains one integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases.

Each test case consists of two lines. The first line contains the resulting string s s s ( 2 ≤ ∣ s ∣ ≤ 1 0 5 2 \le |s| \le 10^5 2s105, each character of s s s is either 0 or 1). The second line contains one integer x x x ( 1 ≤ x ≤ ∣ s ∣ − 1 1 \le x \le |s| - 1 1xs1).

The total length of all strings s s s in the input does not exceed 1 0 5 10^5 105.

Output

For each test case, print the answer on a separate line as follows:

Sample Input

3
101110
2
01
1
110
1

Sample Onput

111011
10
-1

Source

CodeForces 1400 C. Binary String Reconstruction

题意

给出一个字符串 s s s和一个整数 k k k,要求构造一个字符串 t t t使得

若有一个 t i − x t_{i-x} tix或者 t i + x t_{i+x} ti+x(均不能越界)为1,则 s i = = 1 s_i==1 si==1;否则 s i s_i si为0.

若无则输出-1

思路

很明显零的要求更苛刻,所以我一开始均是特殊字符,表示未做要求,当s[i]为1时,若有未做要求的字符则替换成1,当s[i]为0时,无论是什么,都将相关位置替换成0.这样有可能导致1被覆盖。则最后再check即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 3e5 + 5;
const ll inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--) {
        string s;
        int x;
        cin >> s >> x;
        string ans(s.length(), '*');
        for (int i = 0; i < s.length(); ++i) {
            if (s[i] == '1') {
                if (i + x < s.length() && ans[i + x] == '*') ans[i + x] = '1';
                if (i - x >= 0 && ans[i - x] == '*') ans[i - x] = '1';
            } else {
                if (i + x < s.length()) ans[i + x] = '0';
                if (i - x >= 0) ans[i - x] = '0';
            }
        }
        for (auto &it : ans) {
            if (it == '*') it = '0';
        }
        int ok = 1;
        for (int i = 0; i < ans.length() && ok; ++i) {
            if (s[i] == '1') {
                int l = 0, r = 0;
                if (i + x < s.length()) l = ans[i + x] == '1';
                if (i - x >= 0) r = ans[i - x] == '1';
                if (l + r == 0) ok = 0;
            } else {
                int l = 1, r = 1;
                if (i + x < s.length()) l = ans[i + x] == '0';
                if (i - x >= 0) r = ans[i - x] == '0';
                if (l + r != 2) ok = 0;
            }
        }
        cout << (ok ? ans : "-1") << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值