📌  相关文章
📜  使得长度为N的二进制字符串始终以大小为K的组一起出现0的方式的数目

📅  最后修改于: 2021-05-07 01:19:02             🧑  作者: Mango

给定两个整数N和K ,任务是计算制作长度为N的二进制字符串的方式数,以使0总是在大小为K的组中一起出现。

例子:

使用动态编程可以轻松解决此问题。令dp [i]为满足条件的长度为i的二进制字符串的数量。

从条件可以得出:

  • 对于1 <= i
  • 另外dp [k] = 2,因为长度为K的二进制字符串将是仅为零或仅为1的字符串。
  • 现在,如果我们考虑i> k。如果我们将i字符确定为’1’,则dp [i] = dp [i-1],因为二进制字符串的数量不会改变。但是,如果我们将i字符确定为’0’,那么我们要求前面的k-1个字符也应为’0’,因此dp [i] = dp [ik] 。因此,dp [i]将是这两个值的总和。

因此

dp[i] = dp[i - 1] + dp[i - k]

下面是上述方法的实现:

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
const int mod = 1000000007;
  
// Function to return no of ways to build a binary 
// string of length N such that 0s always occur 
// in groups of size K
int noOfBinaryStrings(int N, int k)
{
    int dp[100002];
    for (int i = 1; i <= k - 1; i++) {
        dp[i] = 1;
    }
  
    dp[k] = 2;
  
    for (int i = k + 1; i <= N; i++) {
        dp[i] = (dp[i - 1] + dp[i - k]) % mod;
    }
  
    return dp[N];
}
  
// Driver Code
int main()
{
    int N = 4;
    int K = 2;
    cout << noOfBinaryStrings(N, K);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
      
static int mod = 1000000007;
  
// Function to return no of ways to build a binary 
// string of length N such that 0s always occur 
// in groups of size K
static int noOfBinaryStrings(int N, int k)
{
    int dp[] = new int[100002];
    for (int i = 1; i <= k - 1; i++) 
    {
        dp[i] = 1;
    }
  
    dp[k] = 2;
  
    for (int i = k + 1; i <= N; i++) 
    {
        dp[i] = (dp[i - 1] + dp[i - k]) % mod;
    }
  
    return dp[N];
}
  
// Driver Code
public static void main(String[] args) 
{
    int N = 4;
    int K = 2;
    System.out.println(noOfBinaryStrings(N, K));
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 iimplementation of the
# above approach 
mod = 1000000007; 
  
# Function to return no of ways to 
# build a binary string of length N
# such that 0s always occur in 
# groups of size K 
def noOfBinaryStrings(N, k) :
    dp = [0] * 100002; 
    for i in range(1, K) : 
        dp[i] = 1; 
      
    dp[k] = 2; 
  
    for i in range(k + 1, N + 1) :
        dp[i] = (dp[i - 1] + dp[i - k]) % mod; 
  
    return dp[N]; 
  
# Driver Code 
if __name__ == "__main__" : 
  
    N = 4; 
    K = 2; 
      
    print(noOfBinaryStrings(N, K)); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG 
{
      
static int mod = 1000000007;
  
// Function to return no of ways to build 
// a binary string of length N such that 
// 0s always occur in groups of size K
static int noOfBinaryStrings(int N, int k)
{
    int []dp = new int[100002];
    for (int i = 1; i <= k - 1; i++) 
    {
        dp[i] = 1;
    }
  
    dp[k] = 2;
  
    for (int i = k + 1; i <= N; i++) 
    {
        dp[i] = (dp[i - 1] + dp[i - k]) % mod;
    }
  
    return dp[N];
}
  
// Driver Code
public static void Main() 
{
    int N = 4;
    int K = 2;
    Console.WriteLine(noOfBinaryStrings(N, K));
}
}
  
/* This code contributed by PrinciRaj1992 */


PHP


输出:
5