Binary String Constructing(CodeForces 1003B)

本文介绍了一种算法,用于构造一个长度为n的二进制字符串s,该字符串包含a个0,b个1,并且恰好有x个位置使得相邻字符不同。文章提供了解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given three integers aa, bb and xx. Your task is to construct a binary string ss of length n=a+bn=a+bsuch that there are exactly aa zeroes, exactly bb ones and exactly xx indices ii (where 1≤i<n1≤i<n) such that si≠si+1si≠si+1. It is guaranteed that the answer always exists.

For example, for the string "01010" there are four indices ii such that 1≤i<n1≤i<n and si≠si+1si≠si+1 (i=1,2,3,4i=1,2,3,4). For the string "111001" there are two such indices ii (i=3,5i=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 aa, bb and xx (1≤a,b≤100,1≤x<a+b)1≤a,b≤100,1≤x<a+b).

Output

Print only one string ss, where ss 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

题解:做的时候疯狂wa7(1 100 2),最后发现水过一组a<b就没考虑了,太菜了!

代码如下:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 1007
#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
using namespace std;
typedef long long ll;


int main()
{
    int a,b,x;
    cin>>a>>b>>x;
    if(a>=b)
    {
        if(x%2==0)
        {
            for(int i=0; i<a-x/2; i++)
                printf("0");
            for(int i=0; i<x-1; i++)
                if(i%2!=0)
                    printf("0");
                else
                    printf("1");
            for(int i=x/2; i<b; i++)
                printf("1");
            printf("0");
        }
        else
        {
            for(int i=0; i<a-x/2; i++)
                printf("0");
            for(int i=0; i<x; i++)
            {
                if(i%2==0)
                    printf("1");
                else
                    printf("0");
            }
            for(int i=x/2+1; i<b; i++)
                printf("1");
        }
    }
    else
    {
        swap(a,b);
        if(x%2==0)
        {
            for(int i=0; i<a-x/2; i++)
                printf("1");
            for(int i=0; i<x-1; i++)
                if(i%2!=0)
                    printf("1");
                else
                    printf("0");
            for(int i=x/2; i<b; i++)
                printf("0");
            printf("1");
        }
        else
        {
            for(int i=0; i<a-x/2; i++)
                printf("1");
            for(int i=0; i<x; i++)
            {
                if(i%2==0)
                    printf("0");
                else
                    printf("1");
            }
            for(int i=x/2+1; i<b; i++)
                printf("0");
        }
    }
    return 0;
}

上面代码a,b讨论后输出过程是一样的,可以变为如下程序:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 1007
#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
using namespace std;
typedef long long ll;


int main()
{
    int a,b,x,n;
    char u,v;
    cin>>a>>b>>x;
    n=a+b;
    if(a>b) u='0',v='1';
    else
    {
        u='1',v='0';
        swap(a,b);
    }
    for(int i=0; i<x/2; i++)
    {
        cout<<u<<v;
        a--,b--;
    }
    if(x%2==0)
    {
        while(b--)
            cout<<v;
        while(a--)
            cout<<u;
    }
    else
    {
        while(a--)
            cout<<u;
        while(b--)
            cout<<v;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值