codeforces C. Binary String Reconstruction

本文深入探讨了一种用于检查序列是否符合特定条件的算法。通过分析序列元素与相邻元素的关系,该算法能够确定序列是否满足预设规则。核心思想在于利用0元素定位并验证序列的连续性,确保所有1元素周围至少有一个对应位置的1元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

题目

题意:

如果存在 w i − x = 1 , w i + x = 1 w_{i-x}=1,w_{i+x}=1 wix=1,wi+x=1其中一个的话,那么此时 w i = 1 w_{i}=1 wi=1否则 w i = 0 w_i=0 wi=0,给你一个序列,问这个序列是否这个条件。

思路:

因为出现 0 0 0的情况说明 w i − x = w i + x = 0 w_{i-x}=w_{i+x}=0 wix=wi+x=0,所以我们可以通过 0 0 0将所有的为 0 0 0的情况都计算出来,然后我们判断 w i = 1 w_{i}=1 wi=1的时候,是否出现 w i − x = w i + x = 1 w_{i-x}=w_{i+x}=1 wix=wi+x=1其中一个,如果出现,那么就说明这个可以是 0 0 0,否则就不符合条件了。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
const int maxn = 1e5 + 10;
char s[maxn] = {0}, t[maxn];
int main() {
    int T; read(T); while (T--) {
        scanf("%s", s + 1);
        int x; read(x);
        int len = strlen(s + 1);
        for (int i = 1; i <= len; i++) t[i] = '1';
        for (int i = 1; i <= len; i++) {
            if (s[i] == '0') {
                if (i > x) t[i - x] = '0';
                if (i + x <= len) t[i + x] = '0';
            }
        }
        bool flag = true;
        for (int i = 1; i <= len; i++) {
            if (s[i] == '1') {
                if (i > x) {
                    if (t[i - x] == '1') continue;
                }
                if (i + x <= len) {
                    if (t[i + x] == '1') continue;
                }
                flag = false;
                break;
            }
        }
        if (!flag) printf("-1\n");
        else {
            for (int i = 1; i <= len; i++) printf("%c", t[i]);
            printf("\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值