Binary String Reconstruction

Binary String Reconstruction
在这里插入图片描述

题意:

考虑如下的过程。你有一个长度为n的二进制串 w w w 还有一个整数 x x x 。你构建了一个长度为 n n n 的二进制串 s s s 。二进制串 s s s 的第 i i i 个字符串的选择如下:
1.如果 w i − x w_{i−x} wix存在并且等于 1 1 1,那么 s i s_i si则等于 1 1 1
2.如果$w_{i+x}存在并且等于 1 1 1,那么 s i s_i si则等于 1 1 1
3.如果前两种情况都不存在,那么 s i s_i si 则等于 0 0 0

给出字符串 s s s和整数 x x x,构造字符串 w w w

分析:规则告诉我们如果 s i s_i si 1 1 1,那么 w i − x w_{i−x} wix w i + x w_{i+x} wi+x中至少一个为 1 1 1。如果 s i s_i si 0 0 0,那么 w i − x w_{i−x} wix w i + x w_{i+x} wi+x都为 0 0 0。那么我们先把 w w w全都置为 1 1 1,把要求是 0 0 0的地方全都变成 0 0 0。然后检测构造后的这个字符串是否满足要求。可以见代码。

#include <bits/stdc++.h>
 
using namespace std;
const int maxn = 1e5+10;
char s[maxn];
int x;
char str[maxn];
 
int main() {
    int _;
    scanf("%d", &_);
    while (_--) {
        int flag = false;
        scanf("%s%d", s, &x);
        int len = strlen(s);
        for (int i = 0; i < len; i++) {
            str[i] = '1';
        }
        str[len]='\0';
        for (int i = 0; i < len; ++i) {
            if (s[i] == '0' && i - x >= 0) str[i - x] = '0';
            if (s[i] == '0' && i + x < len) str[i + x] = '0';
        }
        for (int i = 0; i < len; ++i) {
            if (s[i] == '0')continue;
            bool flag1 = false;
            if ((i - x >= 0 && str[i - x] == '1') || (i + x < len && str[i + x] == '1')) flag1 = true;
            if (!flag1) {
                flag = true;
                break;
            }
        }
        if (flag) puts("-1"); else printf("%s\n", str);
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值