Codeforces1003B——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 that1≤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.

这题借鉴了一下同学的思路 结果我先a了 他还没a
就是给一定数量的0或1 然后要求构造出01串满足相邻不同的数量等于所给的x
首先可以看出
01—1个不同
0101—3个不同
010101—5个不同
所以我们先判断x是奇是偶,如果是偶数,先减1变成奇数
所以有x个不同 就有x/2 个01串
然后现在串左边就是0,右边就是1,再判断剩下的情况,
如果x本来就是奇数的,这时候已经构造完成了,剩下的就是把剩下的0放左边 1放右边
(这里今天又get到了c++ string的用法 原来也可以用+来连接字符串)
然后就是x本来是偶数的情况了,又分为三种情况
0 1 都剩有:
因为还缺一个不同,所以先把所有0放左边,然后再把所有1也放左边,那这样又产生了一个01串
只剩下0:
0全部放左边
只剩下1:
1全部放右边

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
const int MAXN=205;
int main(void){
    int a,b,x;
    string res="";
    scanf("%d%d%d",&a,&b,&x);
    int f=0;
    if(x%2==0){
        x--;
        f=1;
    }
    int t=(x+1)/2;
    while(t--){
        res+="01";
        a--;
        b--;
    }
    if(f){
        if(a && b){
            while(a--){
                res="0"+res;
            }
            while(b--){
                res="1"+res;
            }
        }
        else if(a){
            while(a--){
                res+="0";
            }
        }
        else if(b){
            while(b--){
                res="1"+res;
            }
        }
    }
    else{
        while(a--){
            res="0"+res;
        }
        while(b--){
            res+="1";
        }
    }
    cout << res << endl;
    return 0;
}
全部评论

相关推荐

06-13 12:13
已编辑
东北大学 射频工程师
26毕业的,日常实习还能找到吗
求实习的青提很想去大厂:目前应该还有hc吧,腾讯感觉还有hc,最近捞了我好几次,因为目前有offer,所以不准备面了,可以再找找,不行的话就找找中小厂试试,因为我之前也找了好久,准备放弃了,结果有个岗位流程特别顺利,然后就oc,只能说坚持下试试,万一呢💪
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务