CF 1003B 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
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.

【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int N = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/*
对于x,我们很容易就可以想到先输出x/2对0和1(n对0和1交错出现可以提供2*n-1个符合题意的ai)
然后将剩余的0和剩余的1连续输出(提供1个符合条件的ai,这样就刚好是x个符合条件的ai了,
需要注意的是,我们要优先将个数多的放在前面,
例如,有10个1,5个0的话,我们先输出x/2个“10”,否则,输出x/2个“01”)。
*/
int main()
{
int a,b,x;
while(cin>>a>>b>>x)
{
if(x%2==0)//5 2 4
{
if(a>b)
{
for(int i=0;i<x/2;i++)
cout<<"01";
cout<<string(b-x/2,'1');
cout<<string(a-x/2,'0');
}
else{
for(int i=0;i<x/2;i++)
cout<<"10";
cout<<string(a-x/2,'0');
cout<<string(b-x/2,'1');
}
}
else
{
if(a>b)//5 2 3
{
for(int i=0;i<x/2;i++)
cout<<"01";
cout<<string(a-x/2,'0');
cout<<string(b-x/2,'1');
}
else
{
for(int i=0;i<x/2;i++)
cout<<"10";
cout<<string(b-x/2,'1');
cout<<string(a-x/2,'0');
}
}
cout<<endl;
}
}
上一篇:java事务(三)


下一篇:p标签中的span标签文字垂直居中对齐