当前位置:网站首页>C. Binary String Reconstruction
C. Binary String Reconstruction
2022-08-09 21:55:00 【秦小咩】
C. Binary String Reconstruction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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
=========================================================================
我们先满足必须是0的位置,然后为了尽可能满足其余1的情况,我们让剩余位置全是1即可,然后在进行无解的判断,条件就按照题目给的写就够了
# include<iostream>
# include<string.h>
using namespace std;
char s[100000+10],t[100000+10];
int main ()
{
int n;
cin>>n;
while(n--)
{
scanf("%s",s+1);
int x;
cin>>x;
for(int i=1;i<=strlen(s+1);i++)
{
t[i]='1';
}
for(int i=1;i<=strlen(s+1);i++)
{
if(s[i]=='0')
{
if(i-x>=1)
t[i-x]='0';
if(i+x<=strlen(s+1))
t[i+x]='0';
}
}
int flag=0;
for(int i=1;i<=strlen(s+1);i++)
{
if(s[i]=='1')
{
if(!((i-x>=1&&t[i-x]=='1')||(i+x<=strlen(s+1)&&t[i+x]=='1')))
{
flag=1;
break;
}
}
else
{
if((i-x>=1&&t[i-x]=='1')||(i+x<=strlen(s+1)&&t[i+x]=='1'))
flag=1;
}
}
if(flag)
{
cout<<"-1"<<endl;
}
else
{
for(int i=1;i<=strlen(s+1);i++)
{
cout<<t[i];
}
cout<<endl;
}
}
return 0;
}
边栏推荐
- OKR 锦囊妙计
- json case
- 孙正义亏掉1500亿:当初投贵了
- 电脑系统重装后怎么用打印机扫描出文件?
- laravel table migration error [easy to understand]
- AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
- MySQL——JDBC
- Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
- 肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!
- 在“企业通讯录”的盲区,融云的边界与分寸
猜你喜欢

shell学习

Swift 需求 如何防止把view重复添加到win里面

五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长

Converting angles to radians

2022年中国第三方证券APP创新专题分析

The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition

FileZilla搭建FTP服务器图解教程

一文让你快速了解隐式类型转换【整型提升】!

力扣 1413. 逐步求和得到正数的最小值

Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
随机推荐
【微服务~Nacos】Nacos服务提供者和服务消费者
Rust 解引用
js十五道面试题(含答案)
电脑系统重装后怎么用打印机扫描出文件?
[Microservice~Nacos] Configuration Center of Nacos
NodeJS使用JWT
金山云地震,震源在字节?
openGauss数据库基本操作(超详细)
js数组对象去重
Swift 需求 如何防止把view重复添加到win里面
从源码方面来分析Fragment管理中 Add() 方法
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
Sudoku | Backtrack-7
一本通2074:【21CSPJ普及组】分糖果(candy)
2.1.5 大纲显示问题
Easyui 表单验证「建议收藏」
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
工作经验-组件封装(拖拽排序组件)
你的 Link Button 能让用户选择新页面打开吗?
面试官:Redis 大 key 要如何处理?