📌  相关文章
📜  生成一个没有任何连续 0 且至多 K 个连续 1 的二进制字符串

📅  最后修改于: 2021-10-26 06:25:42             🧑  作者: Mango

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

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

如果无法构造这样的二进制字符串,则打印-1

例子:

方法:

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

  • 对于没有连续的两个“ 0 ”,它们之间至少应放置一个“ 1 ”。
  • 因此,对于N个“ 0 ”,对于要生成的所需类型的字符串,至少应存在N-1 个1 ”。
  • 由于不能将多于K个连续的“ 1 ”放在一起,因此对于N 个0,可能存在最大(N+1) * K 个“1 ”。
  • 因此,“ 1 ”的数量应在以下范围内:
  • 如果给定的值NM不满足上述条件,则打印-1
  • 否则,请按照以下步骤解决问题:
    • 将“ 0 ”附加到最终字符串。
    • 在每对之间插入“ 1 0′秒。从M 中减去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


Javascript


输出:
01101101101101

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程