CF1400-C. Binary String Reconstruction
CF1400-C. Binary String Reconstruction
题意:
对于一个二进制字符串\(s\),以及一个给定的\(x\),你可以通过一下操作来得到字符串\(w\):
对于字符串\(s\)的第\(i\)位,
1. 如果\(i-x\)有意义并且\(s[i-x]==1\)那么\(w[i]=1\) ;
2.如果\(i+x\)有意义并且\(s[i+x]==1\),那么\(w[i]=1\);
如果上面两条都不符合,那么\(w[i]=0\).
现在题目给出你字符串\(w\)和\(x\),让你找出符合要求的字符串\(s\),如果不存在这样的字符串\(s\)那么输出\(-1\)。
思路:
对于给出的字符串\(w\),如果\(w[i]==1\),那么必定有\(s[i-x]=0,s[i+1]=0\)。
将其他非\(0\)的位置补为\(1\)。然后按照上面给出的操作再由\(s\)得到\(w_1\),如果\(w=w_1\)那么\(s\)就是答案,否则没有答案输出\(-1\)。
之所以需要由\(s\)再得到一遍\(w_1\)然后用\(w\)和\(w_1\)进行比较,原因在于:我们设\(w\)的一个位置\(p=x\),若\(w[2x]=0\),那么在\(w[2x]\)处就会有\(s[x]=0,s[3x]=0\),而如果\(w[0]=1\),那么就要求\(s[x]=1\),一个位置不可能有两个值,而在上面由\(w\)得到\(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;
}
CF1400-C. Binary String Reconstruction的更多相关文章
- Educational Codeforces Round 94 (Rated for Div. 2) String Similarity、RPG Protagonist、Binary String Reconstruction、Zigzags 思维
题目链接:String Similarity 题意: 首先题目定义了两个串的相似(串的构成是0.1),如果两个串存在对于一个下标k,它们的值一样,那么这两个串就相似 然后题目给你一个长度为2n-1的串 ...
- Educational Codeforces Round 94 (Rated for Div. 2) C. Binary String Reconstruction (构造)
题意:给你一个字符串\(s\),原字符串为\(w\),如果\(i>x\)且\(w_{i-x}=1\),那么\(s_{i}=1\),如果\(i+x\le n\)且\(w_{i+x}=1\),那么\ ...
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- encode_json 会对给定的Perl的数据结构转换为一个UTF-8 encoded, binary string.
use JSON qw/encode_json decode_json/ ; use Encode; my $data = [ { 'name' => 'Ken' , 'age' => 1 ...
- perl encode_json 会产生 UTF-8 (binary) string decode_json 需要一个 UTF-8 (binary) string
encode_json $json_text = encode_json $perl_scalar Converts the given Perl data structure to a UTF-8 ...
- NYOJ 5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
随机推荐
- yum -y install gnuplot
[root@test~]# yum -y install gnuplotLoaded plugins: fastestmirrorLoading mirror speeds from cached h ...
- 【Linux】if中的逻辑运算符怎么在linux的帮助中看到
今天在写shell的时候,突然想查看下if相关的一些逻辑运算的,像-f -d之类的这种 于是man if 或者if --help 可是返回的信息却都无济于事,一点帮助都没有 回想一下,if中调的判断 ...
- kafka(二)基本使用
一.Kafka线上集群部署方案 既然是集群,那必然就要有多个Kafka节点机器,因为只有单台机器构成的kafka伪集群只能用于日常测试之用,根本无法满足实际的线上生产需求. 操作系统: kafka由S ...
- RocketMq消息 demo
参考 https://blog.csdn.net/asdf08442a/article/details/54882769 整理出来的测试 demo 1.produce 生产者 1 package co ...
- Spring学习03
6.Bean的自动装配 6.1 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean的三种装配机 ...
- Spring基于注解开发的注解使用之AOP(部分源代码分析)
AOP底层实现动态代理 1.导入spring-aop包依赖 <!--aopV1--> <dependency> <groupId>org.springframewo ...
- 【IDEA】Lombok--是否值得我们去使用
官网 https://projectlombok.org/ 简介 Project Lombok is a java library that automatically plugs into your ...
- C#高级编程第11版 - 第四章 索引
[1]4.2 继承的类型 1.C#不支持类的多继承,但它支持一个接口继承自多个接口. 2.单继承:单继承允许一个类继承自另外一个基类,C#支持. 3.多级继承:多级继承允许创建一个类继承自它的父类,而 ...
- API服务接口签名代码与设计,如果你的接口不走SSL的话?
在看下面文章之前,我们先问几个问题 rest 服务为什么需要签名? 签名的几种方式? 我认为的比较方便的快捷的签名方式(如果有大神持不同意见,可以交流!)? 怎么实现验签过程 ? 开放式open ap ...
- 不占用额外内存空间能否做到 将图像旋转90度 N × N矩阵表示的图像,其中每个像素的大小为4字节
给定一幅由N × N矩阵表示的图像,其中每个像素的大小为4字节,编写一种方法,将图像旋转90度. 不占用额外内存空间能否做到? 示例 1: 给定 matrix = [ [1,2,3], [4,5,6] ...