B. Binary String Constructing

#include<bits/stdc++.h>//构造,不断重复固定的结构缩小问题的范围,分类讨论(奇偶,01的多少)
using namespace std;//抓住主线,深入思考,不用怕wa,事实证明只要一直做下去总会是对的
int cnt[2];//分类构造题--通过举例子来简化思考,这样可以避免过度抽象引起思维混乱
string ans;
int main()
{
    int a,b,x;
    scanf("%d%d%d",&a,&b,&x);
    if(a>b)
    {
       if(x%2==1)
       {
             for(int i=1;i<=x/2;i++)
             {
                ans+="01";
             a--;
             b--;
          }
          while(a)
          {
              ans+=‘0‘;
              a--;
          }
          while(b)
          {
              ans+=‘1‘;
              b--;
          }
       }
       else
       {
             for(int i=1;i<=x/2;i++)
             {
                 ans+="01";
              a--;
              b--;
          }
          while(b)
          {
              ans+=‘1‘;
              b--;
          }
          while(a)
          {
              ans+=‘0‘;
              a--;
          }
       }
       cout<<ans<<endl;
    }
    else
    {
        for(int i=1;i<=x/2;i++)
        {
            ans+="10";
            b--;
            a--;
        }
        if(x%2==1)
        {
            while(b)
            {
                ans+=‘1‘;
                b--;
            }
            while(a)
            {
                ans+=‘0‘;
                a--;
            }
        }
        else
        {
            while(a)
            {
                ans+=‘0‘;
                a--;
            }
            while(b)
            {
                ans+=‘1‘;
                b--;
            }
        }
        cout<<ans<<endl;
    }
}

如果用string,insert函数会简单很多

原文地址:https://www.cnblogs.com/lishengkangshidatiancai/p/10343164.html

时间: 2024-10-12 20:15:29

B. Binary String Constructing的相关文章

CF 1003B 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

NYOJ 5 Binary String Matching【string find的运用】

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

NYOJ 5 Binary String Matching (kmp 字符串匹配)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ 5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

nyoj5 Binary String Matching(KMP)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

How to convert a byte to its binary string representation

How to convert a byte to its binary string representation For example, the bits in a byte B are 10000010, how can I assign the bits to the string strliterally, that is, str = "10000010". byte b1 = (byte) 129; String s1 = String.format("%8s&

CF 1107 E. Vasya and Binary String

E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为一个点.设f[i][j][k]表示消完区间[i,j]和这段区间后面k个元素最大值,其中k个元素的颜色与点j的颜色相同. 转移:可以首先将j和后面k个元素消除,然后消除[i,j-1].也可以枚举一个和j颜色相同的点m,然后分别先消除[m+1,r-1],剩下的区间就和后面k个连在一起了,再递归求出. 代

[Swift]LeetCode761. 特殊的二进制序列 | Special Binary String

Special binary strings are binary strings with the following two properties: The number of 0's is equal to the number of 1's. Every prefix of the binary string has at least as many 1's as 0's. Given a special string S, a move consists of choosing two