题目
传送门
Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows:
if the character wi−x exists and is equal to 1, then si is 1 (formally, if i>x and wi−x= 1, then si= 1);
if the character wi+x exists and is equal to 1, then si is 1 (formally, if i+x≤n and wi+x= 1, then si= 1);
if both of the aforementioned conditions are false, then si is 0.
You are given the integer x and the resulting string s. Reconstruct the original string w.
Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string s (2≤|s|≤105, each character of s is either 0 or 1). The second line contains one integer x (1≤x≤|s|−1).
The total length of all strings s in the input does not exceed 105.
Output
For each test case, print the answer on a separate line as follows:
if no string w can produce the string s at the end of the process, print −1;
otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them.
输入
3
101110
2
01
1
110
1
输出
111011
10
-1
题意:t组样例,每次输入一个串s和一个数m,这个串由一个原串w经过下面操作得到
若w【i+m】或w【i-m】等于一,s[i]=1;否则s[i]=0;
问:是否能通过s求出w,可以的话输出w否则输出-1;
思路,我们先初始化一个数组为1,然后如果a[i]==‘0’,我们把该数组相应位置变为零,判断由该数组是否满足条件,满足的话输出,否则输出-1;
AC code
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<sstream>
#include<queue>
#include<stack>
using namespace std;
char a[120000];
int b[120000];
solve()
{
int m;
cin>>a>>m;
int k=strlen(a);
for(int i=0;i<k;i++)b[i]=1;//初始化
for(int i=0;i<k;i++)
if(a[i]=='0')//把相应位置变为0
{
if(i>=m)b[i-m]=0;
if(i+m<k)b[i+m]=0;
}
int flag=1;
for(int i=0;i<k;i++)
if(a[i]=='1'&&!((i>=m&&b[i-m])||(i+m<k&&b[i+m])))//判断是否满足条件
{
flag=0;
printf("-1\n");
break;
}
if(flag)
{
for(int i=0;i<k;i++)printf("%d",b[i]);
printf("\n");
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}