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;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/83301634
今日推荐