欢迎访问https://blog.csdn.net/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~
Binary String Constructing(CodeForces - 1003B)
Description
You are given three integers aa, bb and xx. Your task is to construct a binary string ss of length n=a+bn=a+bsuch that there are exactly aa zeroes, exactly bb ones and exactly xx indices ii (where 1≤i<n1≤i<n) such that si≠si+1si≠si+1. It is guaranteed that the answer always exists.
For example, for the string "01010" there are four indices ii such that 1≤i<n1≤i<n and si≠si+1si≠si+1 (i=1,2,3,4i=1,2,3,4). For the string "111001" there are two such indices ii (i=3,5i=3,5).
Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1.
Input
The first line of the input contains three integers aa, bb and xx (1≤a,b≤100,1≤x<a+b)1≤a,b≤100,1≤x<a+b).
Output
Print only one string ss, where ss is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
Examples
Input
2 2 1
Output
1100
Input
3 3 3
Output
101100
Input
5 3 6
Output
01010100
Note
All possible answers for the first example:
- 1100;
- 0011.
All possible answers for the second example:
- 110100;
- 101100;
- 110010;
- 100110;
- 011001;
- 001101;
- 010011;
- 001011.
题意:
给出a,b,x,构造一个只含0和1的串,要求有a个0,b个1,并且不多不少刚刚好有x个 a [ i ] 使得a [ i ] ! = a [ i + 1 ] 。
思路:
输入o代表0的个数,e代表1的个数,x代表a[i] != a[i+1]的个数,(因为o看起来像0,e读起来像1,防止弄乱出错嘛嘻嘻......)注意字符串最后一个是‘\0’,所以可以先给a清零。然后先看0和1哪个个数居多,就先将第一个设为哪个数,然后优先按照01010101或者10101010顺序排列,根据x的数量将不等的先排了,保证后来不再多增加不相等的个数,所以让a[k-1]和a[k]相等然后一定注意最后剩余的0和1还有一次不等。此处还有一个要注意的地方,就是a数组如果设成int容易出错,最好按字符串处理。最后输出时的判断条件也不要设成 i < e+o,因为e+o已经减没了。
代码:
#include<cmath>
#include<deque>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-10
using namespace std;
int main()
{
int n,o,e,x,k=1;
scanf("%d%d%d",&o,&e,&x); //o代表0的个数,e代表1的个数,x代表a[i] != a[i+1]的个数
char a[o+e];
n=o+e;
memset(a,0,sizeof(a)); //给a清零,注意字符串最后一个是‘\0’
if(o<=e){
a[0]='1';
e--;
}
else{ //看0和1哪个个数居多,就先将第一个设为哪个数
a[0]='0';
o--;
}
while(x>1) //注意最后剩余的0和1还有一次不等,所以此处不能取等
{
x--;
if(a[k-1]=='1'){
a[k++]='0'; //根据x的数量,优先按照01010101或者10101010顺序排列
o--;
}
else{
a[k++]='1';
e--;
}
}
if(a[k-1]=='1'){ //保证后来不再多增加不想等的个数,所以让a[k-1]和a[k]相等
while(e--)
a[k++]='1';
while(o--)
a[k++]='0';
}
else{
while(o--)
a[k++]='0';
while(e--)
a[k++]='1';
}
for(int i=0;i<=n-1;i++)
printf("%c",a[i]);
printf("\n");
return 0;
}
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~