Binary String Constructing
You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1≤i<n) such that si≠si+1
. It is guaranteed that the answer always exists.
For example, for the string "01010" there are four indices i
such that 1≤i<n and si≠si+1 (i=1,2,3,4). For the string "111001" there are two such indices i (i=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 a
, b and x (1≤a,b≤100,1≤x<a+b)
.
Output
Print only one string s
, where s
is any binary string satisfying conditions described above. It is guaranteed that the answer always exists.
Examples
Input
Copy
2 2 1
Output
Copy
1100
Input
Copy
3 3 3
Output
Copy
101100
Input
Copy
5 3 6
Output
Copy
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个0,b个1,输出的序列中要满足x个
思路前x-1个按010101形式输出,第x个在序列后面接上剩余的0和1,比如01010110
补题的时候错了一发,时间超限,原因是数组开小了,所以看题和检查很重要的呀QAQ
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int a,b,x,aa,bb,t[205],i=0,j;
scanf("%d %d %d",&a,&b,&x);
aa=a;
bb=b;
if(a>=b)
{
t[0]=0;
aa--;
}
else
{
t[0]=1;
bb--;
}
if(x>1)
{
for(i=1; i<=a+b; i++)
{
if(t[i-1]==0)
{
t[i]=1;
bb--;
}
else
{
t[i]=0;
aa--;
}
x--;
if(x==1)
break;
}
}
if(t[i]==1)
{
for(j=1; j<=bb; j++)
t[++i]=1;
for(j=1; j<=aa; j++)
t[++i]=0;
}
else
{
for(j=1; j<=aa; j++)
t[++i]=0;
for(j=1; j<=bb; j++)
t[++i]=1;
}
for(j=0; j<=i; j++)
printf("%d",t[j]);
return 0;
}