📌  相关文章
📜  生成不带任何连续0且最多K个连续1的二进制字符串

📅  最后修改于: 2021-04-30 02:11:02             🧑  作者: Mango

给定两个整数NM ,任务是构造一个具有以下条件的二进制字符串:

  • 二进制字符串由N 0和M 1组成
  • 二进制字符串最多具有K个连续的1。
  • 二进制字符串不包含任何相邻的0。

如果不可能构造这样的二进制字符串,则打印-1

例子:

方法:

要构造满足给定属性的二进制字符串,请注意以下几点:

  • 对于没有两个连续的0 ”,它们之间至少应有一个“ 1 ”。
  • 因此,对于N个数字“ 0 ”,应至少存在N-1个1 ”,以生成所需类型的字符串。
  • 由于最多可以放置K个连续的’ 1 ‘,因此对于N 0’s,最大可能为(N + 1)* K’1′
  • 因此,“ 1 ”的数目应在以下范围内:
  • 如果给定值NM不满足上述条件,则打印-1
  • 否则,请按照以下步骤解决问题:
    • 在最后的字符串后面加上’ 0 ‘。
    • 在每对之间插入“ 1 0 ‘s。减去N – 1,如N – 1“1”已经放置。
    • 对于其余的“ 1 ”,将min(K – 1,M)1 ”与每个已放置的“ 1 ”并排放置,以确保最多放置K个“ 1”。
    • 对于任何剩余的1 ,请将其附加到最终字符串的开头和结尾。
  • 最后,打印生成的字符串。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to construct the binary string
string ConstructBinaryString(int N, int M,
                             int K)
{
    // Conditions when string construction
    // is not possible
    if (M < (N - 1) || M > K * (N + 1))
        return "-1";
 
    string ans = "";
 
    // Stores maximum 1's that
    // can be placed in between
    int l = min(K, M / (N - 1));
    int temp = N;
    while (temp--) {
        // Place 0's
        ans += '0';
 
        if (temp == 0)
            break;
 
        // Place 1's in between
        for (int i = 0; i < l; i++) {
            ans += '1';
        }
    }
 
    // Count remaining M's
    M -= (N - 1) * l;
 
    if (M == 0)
        return ans;
 
    l = min(M, K);
    // Place 1's at the end
    for (int i = 0; i < l; i++)
        ans += '1';
 
    M -= l;
    // Place 1's at the beginning
    while (M > 0) {
        ans = '1' + ans;
        M--;
    }
 
    // Return the final string
    return ans;
}
 
// Driver Code
int main()
{
    int N = 5, M = 9, K = 2;
 
    cout << ConstructBinaryString(N, M, K);
}


Java
// Java implementation of
// the above approach
class GFG{
     
// Function to construct the binary string
static String ConstructBinaryString(int N, int M,
                                    int K)
{
     
    // Conditions when string construction
    // is not possible
    if (M < (N - 1) || M > K * (N + 1))
        return "-1";
 
    String ans = "";
 
    // Stores maximum 1's that
    // can be placed in between
    int l = Math.min(K, M / (N - 1));
    int temp = N;
     
    while (temp != 0)
    {
        temp--;
         
        // Place 0's
        ans += '0';
 
        if (temp == 0)
            break;
 
        // Place 1's in between
        for(int i = 0; i < l; i++)
        {
            ans += '1';
        }
    }
 
    // Count remaining M's
    M -= (N - 1) * l;
 
    if (M == 0)
        return ans;
 
    l = Math.min(M, K);
     
    // Place 1's at the end
    for(int i = 0; i < l; i++)
        ans += '1';
 
    M -= l;
     
    // Place 1's at the beginning
    while (M > 0)
    {
        ans = '1' + ans;
        M--;
    }
 
    // Return the final string
    return ans;
}
 
// Driver code   
public static void main(String[] args)
{
    int N = 5, M = 9, K = 2;
     
    System.out.println(ConstructBinaryString(N, M, K));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation of
# the above approach
 
# Function to construct the binary string
def ConstructBinaryString(N, M, K):
 
    # Conditions when string construction
    # is not possible
    if(M < (N - 1) or M > K * (N + 1)):
        return '-1'
 
    ans = ""
 
    # Stores maximum 1's that
    # can be placed in between
    l = min(K, M // (N - 1))
    temp = N
     
    while(temp):
        temp -= 1
 
        # Place 0's
        ans += '0'
 
        if(temp == 0):
            break
 
        # Place 1's in between
        for i in range(l):
            ans += '1'
 
    # Count remaining M's
    M -= (N - 1) * l
 
    if(M == 0):
        return ans
 
    l = min(M, K)
     
    # Place 1's at the end
    for i in range(l):
        ans += '1'
 
    M -= l
     
    # Place 1's at the beginning
    while(M > 0):
        ans = '1' + ans
        M -= 1
 
    # Return the final string
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    N = 5
    M = 9
    K = 2
     
    print(ConstructBinaryString(N, M , K))
 
# This code is contributed by Shivam Singh


C#
// C# implementation of
// the above approach
using System;
class GFG{
      
// Function to construct the binary string
static String ConstructBinaryString(int N, int M,
                                    int K)
{
      
    // Conditions when string construction
    // is not possible
    if (M < (N - 1) || M > K * (N + 1))
        return "-1";
  
    string ans = "";
  
    // Stores maximum 1's that
    // can be placed in between
    int l = Math.Min(K, M / (N - 1));
    int temp = N;
      
    while (temp != 0)
    {
        temp--;
          
        // Place 0's
        ans += '0';
  
        if (temp == 0)
            break;
  
        // Place 1's in between
        for(int i = 0; i < l; i++)
        {
            ans += '1';
        }
    }
  
    // Count remaining M's
    M -= (N - 1) * l;
  
    if (M == 0)
        return ans;
  
    l = Math.Min(M, K);
      
    // Place 1's at the end
    for(int i = 0; i < l; i++)
        ans += '1';
  
    M -= l;
      
    // Place 1's at the beginning
    while (M > 0)
    {
        ans = '1' + ans;
        M--;
    }
  
    // Return the final string
    return ans;
}
  
// Driver code   
public static void Main(string[] args)
{
    int N = 5, M = 9, K = 2;
      
    Console.Write(ConstructBinaryString(N, M, K));
}
}
  
// This code is contributed by Ritik Bansal


输出:
01101101101101


时间复杂度: O(N + M)
辅助空间: O(N + M)