题意:一个只有‘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;
}
}