CF 1400C. Binary String Reconstruction

Consider the following process. You have a binary string (a string where each character is either 0 or 1) ww of length nn and an integer xx. You build a new binary string ss consisting of nn characters. The ii-th character of ss is chosen as follows:

  • if the character wi−xwi−x exists and is equal to 1, then sisi is 1 (formally, if i>xi>x and wi−x=wi−x= 1, then si=si= 1);
  • if the character wi+xwi+x exists and is equal to 1, then sisi is 1 (formally, if i+x≤ni+x≤n and wi+x=wi+x= 1, then si=si= 1);
  • if both of the aforementioned conditions are false, then sisi is 0.

You are given the integer xx and the resulting string ss. Reconstruct the original string ww.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

Each test case consists of two lines. The first line contains the resulting string ss (2≤|s|≤1052≤|s|≤105, each character of ss is either 0 or 1). The second line contains one integer xx (1≤x≤|s|−11≤x≤|s|−1).

The total length of all strings ss in the input does not exceed 105105.

Output

For each test case, print the answer on a separate line as follows:

  • if no string ww can produce the string ss at the end of the process, print −1−1;
  • otherwise, print the binary string ww consisting of |s||s| characters. If there are multiple answers, print any of them.

Example

input

Copy

3
101110
2
01
1
110
1

output

Copy

111011
10
-1

 

题解:对于字符串s的0,它在w中位置一定是0。因此我们w初始化为全是1的字符串,并将 s中0对应的位置变为0,再将得到的w串做一次变换,看是否和给出的s串相同。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string.h>
#include<string>
using namespace std;

const int maxn=100000;
string a;
char b[maxn];
int len;
bool flag;
int x;

string get(char s[])
{
	string ans="";
	for (int i=0;i<len;i++)
	{
		if ((i+x<len)&&(s[i+x]=='1') || (i-x>=0)&&(s[i-x]=='1') ) ans+='1'; else ans+='0'; 
	}
	return ans;
}

int main()
{
	int tt;
	cin>>tt;
	for (int t=1;t<=tt;t++)
	{
		cin>>a>>x;
		flag=false;
		len=a.size();
		memset(b,' ',sizeof(b));
		for (int i=0;i<=len-1;i++)
		{
			if (a[i]=='0')
			{
				if (i-x>=0) b[i-x]='0';
				if (i+x<=len-1) b[i+x]='0';
			}
			if ((i+x>len-1)&&(i-x<0)) b[i]='0';
			if (b[i]!='0') b[i]='1';
		}
		if (get(b)!=a)
			cout<<-1<<endl;
		else
		{
			for (int i=0;i<len;i++)
		  		cout<<b[i];
		  	cout<<endl;
		}
	}
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值