CF1003B Binary String Constructing

2024-02-19 14:32

本文主要是介绍CF1003B Binary String Constructing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目描述

You are given three integers a a a , b b b and x x x . Your task is to construct a binary string s s s of length n=a+b n = a + b n=a+b such that there are exactly a a a zeroes, exactly b b b ones and exactly x x x indices i i i (where 1≤i<n 1 \le i < n 1≤i<n ) such that si≠si+1 s_i \ne s_{i + 1} si​≠si+1​ . It is guaranteed that the answer always exists.

For example, for the string "01010" there are four indices i i i such that 1≤i<n 1 \le i < n 1≤i<n and si≠si+1 s_i \ne s_{i + 1} si​≠si+1​ ( i=1,2,3,4 i = 1, 2, 3, 4 i=1,2,3,4 ). For the string "111001" there are two such indices i i i ( i=3,5 i = 3, 5 i=3,5 ).

Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.

输入输出格式

输入格式:

 

The first line of the input contains three integers a a a , b b b and x x x ( 1≤a,b≤100,1≤x<a+b) 1 \le a, b \le 100, 1 \le x < a + b) 1≤a,b≤100,1≤x<a+b) .

 

输出格式:

 

Print only one string s s s , where s s s is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.

 

输入输出样例

输入样例#1: 复制

2 2 1

输出样例#1: 复制

1100

输入样例#2: 复制

3 3 3

输出样例#2: 复制

101100

输入样例#3: 复制

5 3 6

输出样例#3: 复制

01010100

说明

All possible answers for the first example:

  • 1100;
  • 0011.

All possible answers for the second example:

  • 110100;
  • 101100;
  • 110010;
  • 100110;
  • 011001;
  • 001101;
  • 010011;
  • 001011.

思路:对于x,x/2对0和1(n对0和1交错出现可以提供2*n-1个符合题意的ai),然后将剩余的0和剩余的1连续输出(提供1个符合条件的ai)若X为偶数输出顺序与原来相同,否则相反,这样就刚好是x个符合条件的ai了,需要注意的是,我们要优先将个数多的放在前面,例如,有10个1,5个0的话,我们先输出x/2个“10”,否则,输出x/2个“01”)

   


#include "iostream"using namespace std;int main(){int a,b,x,n;char u,v;cin>>a>>b>>x;n=a+b;if(a>b) u='0',v='1';else {u='1',v='0';swap(a,b);}for(int i=0;i<x/2;i++) {cout<<u<<v;a--,b--;}if(x%2==0){while(b--) cout<<v;while(a--) cout<<u;}else{while(a--) cout<<u;while(b--) cout<<v;}return 0;}

 

 

以上代码非本人所写,

下面的代码存在错输暂存

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int a[2],x;
int main()
{string str="";scanf("%d%d%d",&a[0],&a[1],&x);if(x%2==0){if(a[0]<a[1]){while(a[1]--){a[0]--;str=str+"1";if(a[0]>=0)str=str+"0";}}else if(a[0]>=a[1]){while((a[0]--)>=0){a[1]--;if(a[0]==a[1]&&a[1]==0){str=str+"1";str=str+"0";break;}if(a[0]>=0)str=str+"0";if(a[1]>=0)str=str+"1";}}}else{if(a[0]<a[1]){while(a[0]--||a[1]--){str=str+"1";if(a[0]==1){str=str+"1";a[1]--;}if(a[0]>=0)str=str+"0";}}else if(a[0]>=a[1]){while((a[0]--)>=0){a[1]--;if(a[0]>0)str=str+"0";if(a[1]==1){str=str+"0";a[0]--;}if(a[1]>=0)str=str+"1";}}}cout<<str<<endl;return 0;
}

 

这篇关于CF1003B Binary String Constructing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/724971

相关文章

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

java String.join()的使用小结

《javaString.join()的使用小结》String.join()是Java8引入的一个实用方法,用于将多个字符串按照指定分隔符连接成一个字符串,本文主要介绍了javaString.join... 目录1. 方法定义2. 基本用法2.1 拼接多个字符串2.2 拼接集合中的字符串3. 使用场景和示例3

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

IDEA如何将String类型转json格式

《IDEA如何将String类型转json格式》在Java中,字符串字面量中的转义字符会被自动转换,但通过网络获取的字符串可能不会自动转换,为了解决IDEA无法识别JSON字符串的问题,可以在本地对字... 目录问题描述问题原因解决方案总结问题描述最近做项目需要使用Ai生成json,可生成String类型

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

string字符会调用new分配堆内存吗

gcc的string默认大小是32个字节,字符串小于等于15直接保存在栈上,超过之后才会使用new分配。

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro