题目
题意:
如果存在 w i − x = 1 , w i + x = 1 w_{i-x}=1,w_{i+x}=1 wi−x=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 wi−x=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 wi−x=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;
}