题目链接
思路:
字符串s[i]如果为0,那么w[i]两端也一定为0,标记完这些位置之后特判两端长度为x的位置是否有冲突即可。
代码:
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e5+7;
const int M=1e4+5;
const double eps=1e-8;
const int mod=1e9+7;
const int inf=0x7fffffff;
const double pi=3.1415926;
using namespace std;
signed main()
{
int t;
cin>>t;
while(t--)
{
int x,s[N],w[N];
char str[N];
cin>>str+1;
cin>>x;
int n=strlen(str+1);
for (int i=1;i<=n;i++)
{
w[i]=1;
s[i]=str[i]-'0';
}
for (int i=1;i<=n;i++)
{
if (!s[i])
{
if (i+x<=n)
w[i+x]=0;
if (i-x>=1)
w[i-x]=0;
}
}
bool bz=1;
for (int i=1;i<=n;i++)
{
if(s[i])
{
if (((i+x<=n&&!w[i+x])||i+x>n)&&((i-x>=1&&!w[i-x])||i-x<1))
{
bz=0;
break;
}
}
}
if(!bz)
{
cout<<-1<<endl;
}
else
{
for(int i=1;i<=n;i++)
{
cout<<w[i];
}
cout<<endl;
}
}
return 0;
}