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 ...
随机推荐
- scrapy异步的爬虫框架简单的使用
scrapy异步的爬虫框架 异步的爬虫框架 高性能的数据解析,持久化存储,全栈数据的爬取,中间件,分布式 框架:就是一个集成好了各种功能且具有很强通用性的一个项目模板. 环境安装: Linux: pi ...
- python3多进程 进程池 协程并发
一.进程 我们电脑的应用程序,都是进程,进程是资源分配的单位.进程切换需要的资源最大,效率低. 进程之间相互独立 cpu密集的时候适合用多进程 #多 ...
- 风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf
风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf
- 实现一个List集合中的某个元素的求和
List<User> userlist = userService.findAll();Integer sum= userlist .stream().collect(Collectors ...
- 获取当前文件路径 import 原理 一般把模块组成的集合称为包(package)
获取当前文件路径 testpath.py import sysprint(sys.path) [root@d mapReduceLog]# python testpath.py['/data/mapR ...
- NodeJS入门学习教程
一.概念 1.什么是nodejs Node.js是JavaScript 运行时环境,通俗易懂的讲,Node.js是JavaScript的运行平台 Node.js既不是语言,也不是框架,它是一个平台 2 ...
- luogu p2622
题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...
- POSTGIS
https://blog.csdn.net/qq_35732147/article/details/85256640 官方文档:http://www.postgis.net/docs/ST_Buffe ...
- Spring Boot 基础,理论,简介
Spring Boot 基础,理论,简介 1.SpringBoot自动装配 1.1 Spring装配方式 1.2 Spring @Enable 模块驱动 1.3 Spring 条件装配 2.自动装配正 ...
- jQuery基础介绍
最近在学习JavaScript,当学习Javascript之后,不得不学习的肯定是jQuery了,所以开始利用网络的便捷浏览各大博客寻找可学习的资源.这篇博客关于jQuery的学习让我有很多收获,也明 ...