Binary String Reconstruction

题意:一个只有‘0’和‘1’字符串str,当满足i>=x&&arr[i-x]='1’或i+x<str.length()&&arr[i+x]='1’时使str[i]=‘1’,否则令str[i]=‘0’,现在有进行上述操作后的一个新字符串s,问原字符串str是否存在,不存在输出-1,否则输出str

如果新字符串s的某个位置为’1’,则它的后x个位置或前x个位置都可能是’1’,不好确定,但是如果某个位置是’0’,则它的前x个位置和后x个位置一定是’0’,所以我们以为’0’的位置着手。

我们令ss为全为’1’且与s等长的字符串,遍历s,当遇到’0’时,如果前x个位置或后x个位置在范围内则令其为’0’,最后用ss对s进行检验:当s为’1’时,访问其范围内的前x个位置和后半个位置,如果都不为’1’,说明ss不是s的原字符串,进一步可以说明s没有原字符串,输出-1结束,如果对所有位置都满足要求,那么ss即为原字符串str

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<deque>
 
using namespace std;
 
typedef long long ll;
 
deque<int> dqmin, dqmax;
 
const int N = 1e5 + 100;
 
const int mod = 1e9;
 
ll arr[N];
 
int main() {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		string str;
		int x;
		cin >> str>>x;
		int len = str.length();
		bool sign = true;
		string temp = "";
		for (int i = 0; i < len; i++) {
			temp += '1';
		}
		for (int i = 0; i < len; i++) {
			if (str[i] == '0') {
				if (i - x >= 0) temp[i - x] = '0';
				if (i + x < len) temp[i + x] = '0';
			}
		}
		for (int i = 0; i < len; i++) {
			if (str[i] == '1') {
				if (i - x >= 0 && temp[i - x] == '1') continue;
				if (i + x < len && temp[i + x] == '1')continue;
				sign = false;
				break;
			}
		}
		if (sign) cout << temp << endl;
		else cout << -1 << endl;
	}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值