对于一个二进制字符串
s
s
s,以及一个给定的
x
x
x,你可以通过一下操作来得到字符串
w
w
w:
对于字符串
s
s
s的第
i
i
i位,
1. 如果
i
−
x
i-x
i−x有意义并且
s
[
i
−
x
]
=
=
1
s[i-x]==1
s[i−x]==1那么
w
[
i
]
=
1
w[i]=1
w[i]=1 ;
2.如果
i
+
x
i+x
i+x有意义并且
s
[
i
+
x
]
=
=
1
s[i+x]==1
s[i+x]==1,那么
w
[
i
]
=
1
w[i]=1
w[i]=1;
如果上面两条都不符合,那么
w
[
i
]
=
0
w[i]=0
w[i]=0.
现在题目给出你字符串
w
w
w和
x
x
x,让你找出符合要求的字符串
s
s
s,如果不存在这样的字符串
s
s
s那么输出
−
1
-1
−1。
思路:
对于给出的字符串
w
w
w,如果
w
[
i
]
=
=
1
w[i]==1
w[i]==1,那么必定有
s
[
i
−
x
]
=
0
,
s
[
i
+
1
]
=
0
s[i-x]=0,s[i+1]=0
s[i−x]=0,s[i+1]=0。
将其他非
0
0
0的位置补为
1
1
1。然后按照上面给出的操作再由
s
s
s得到
w
1
w_1
w1,如果
w
=
w
1
w=w_1
w=w1那么
s
s
s就是答案,否则没有答案输出
−
1
-1
−1。
之所以需要由
s
s
s再得到一遍
w
1
w_1
w1然后用
w
w
w和
w
1
w_1
w1进行比较,原因在于:我们设
w
w
w的一个位置
p
=
x
p=x
p=x,若
w
[
2
x
]
=
0
w[2x]=0
w[2x]=0,那么在
w
[
2
x
]
w[2x]
w[2x]处就会有
s
[
x
]
=
0
,
s
[
3
x
]
=
0
s[x]=0,s[3x]=0
s[x]=0,s[3x]=0,而如果
w
[
0
]
=
1
w[0]=1
w[0]=1,那么就要求
s
[
x
]
=
1
s[x]=1
s[x]=1,一个位置不可能有两个值,而在上面由
w
w
w得到
s
s
s的过程中无法检查出这个问题。
#include <cstdio>
#include <cstring>
#include <algorithm>
const int Maxn = 100005;
char str[Maxn];
int s[Maxn], w[Maxn], x;
void solve() {
scanf("%s %d", str, &x);
int len = strlen(str);
for (int i = 0; i < len; i++) {
w[i] = str[i] - '0';
}
std::fill(s, s + len, 1);
for (int i = 0; i < len; i++) {
if (w[i] == 0) {
if (i - x >= 0) {
s[i - x] = 0;
}
if (i + x < len) {
s[i + x] = 0;
}
}
}
bool flag = true;
for (int i = 0; i < len; i++) {
int t = 0;
if ((i - x >= 0 && s[i - x] == 1) || (i + x) < len && s[i + x] == 1) {
t = 1;
}
if (w[i] != t) {
flag = false;
break;
}
}
if (flag) {
for (int i = 0; i < len; i++) {
printf("%d", s[i]);
}
printf("\n");
} else {
printf("-1\n");
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}