B. Binary String Constructing 题解(简单思维)

题目链接

题目大意

要你构造一个01串有a个0和b个1,使得恰好存在x个(s_i)使得(s_i !=s_{i+1})

保证答案一定存在

题目思路

其实很简单,但是想记录下

首先输出(frac{x}{2})对01串或者10串

要保证首字母一定是个数多的放在前面,这样可以保证一定可以构成答案

然后这样就有2*x-1个答案了

然后再特判x是奇数还是偶数即可

代码

#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=1e2+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
typedef pair<int,int> pii;
int a,b,x;
signed main(){
    cin>>a>>b>>x;
    int pr1,pr2;
    if(a>b){
        pr1=0;
        pr2=1;
    }else{
        swap(a,b);
        pr1=1;
        pr2=0;
    }
    for(int i=1;i<=x/2;i++){
        cout<<pr1<<pr2;
        a--,b--;
    }
    // a个pr1 b个pr2
    if(x%2==1){
        while(a--) cout<<pr1;
        while(b--) cout<<pr2;
    }else{
        while(b--) cout<<pr2;
        while(a--) cout<<pr1;
    }
    return 0;
}

卷也卷不过,躺又躺不平
原文地址:https://www.cnblogs.com/hunxuewangzi/p/14626995.html