C. Binary String Reconstruction——思维题Day2

C. Binary String Reconstruction

题目传送门

题意:

给你结果,要你求源串。
转换方法:
在这里插入图片描述

思路:

先全部置为1。
很简单,转换后0,一定前后x个为0(若存在)。
转换后的1,必然前后x都不为0,若为0则为-1输出。

代码:

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 10;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string str;
        string s;
        int x;
        bool flag = 0;
        cin >> s;
        cin >> x;
        int len = s.size();
       // cout<<len<<endl;
       // cout<<s<<endl;
        for (int i = 0; i < len; i++) {
            str += "1";
        }
        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';
        }
       // cout<<str<<endl;
        for (int i = 0; i < len; ++i) {
            if (s[i] == '0')continue;
            bool flag1 = 0;
            if ((i - x >= 0 && str[i - x] == '1') || (i + x < len && str[i + x] == '1')) flag1 = 1;
            if (!flag1) {
                flag = 1;
                break;
            }
        }
        if (flag) cout << -1 << endl;
        else cout << str << endl;
    }
    return 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值