Jumbled String Gym - 101933J —— 还原01串

Recall that a subsequence of a string is any string obtained by removing some subset of characters from the string, for instance “string”, “sing”, “i” and “sg” are all subsequences of “string”. If the same subsequence can be obtained in exactly t different ways, by removing different subsets of characters, we say that the subsequence occurs t times.

Jingfei wants to create a nonempty bit string that has the following properties:

the subsequence 00 occurs a times,
the subsequence 01 occurs b times,
the subsequence 10 occurs c times, and
the subsequence 11 occurs d times.
However, Jingfei does not know how to create such a string – or whether it is even possible. Could you help her?
Input
The input consists of a single line with four integers a, b, c, and d (0≤a,b,c,d≤109).

Output
Output a bit string that satisfies the given requirements. If there are several solutions, output any one of them. If there are no solutions, output “impossible”.

Examples
Input
3 4 2 1
Output
01001
Input
5 0 0 5
Output
impossible

题意:

有一个01串,给你00,01,10,11的数量,还原这个01串,若有多个答案输出任意一个0011就是1个00,4个01,1个11.

题解:

从输入可以推出0和1的数量,那我们假设这个字符串一开始全部都是1,然后往里面插0,可以发现插入之后01和10的数量相加是一定的,那只要知道每次插入的位置就好了。判掉不可能的情况。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL o(LL x){
    x*=2;
    LL i;
    for(i=1ll;;i++){
        if(i*(i-1)>x)break;
    }
    i--;
    if(i*(i-1)==x)return i;
    return -1;
}

bool cmp(LL a,LL b){
    return a>b;
}

int main(){
    LL a,b,c,d;
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);

    if(!a&&!b&&!c&&!d)return 0*printf("0\n");

    LL x=o(a),y=o(d);//x 0 y 1
    if(x==-1||y==-1)return 0*printf("impossible\n");
    //cout<<x<<' '<<y<<endl;
    if(x==1&&y==1){
        if(b&&c)printf("impossible\n");
        else if(b==1)printf("01\n");
        else if(c==1)printf("10\n");
        else if(b==0&&c==0) printf("0\n");
        else printf("impossible\n");
        return 0;
    }
    else if(x==1){
        if(b==0&&c==0)x=0;
    }
    else if(y==1){
        if(b==0&&c==0)y=0;
    }


    LL all=x*y;

    if((b+c)!=all)return 0*printf("impossible\n");
    string s(x,'0');
    LL _01=b,_10=c;
    vector<LL>Insert;
    while(y--){
        LL pos=min(x,_01);
        _01-=pos;_10-=x-pos;
        s.insert(pos,"1");
    }
    cout<<s<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值