📌  相关文章
📜  长度最多为 N 且设置位计数为 K 的倍数的二进制字符串的计数

📅  最后修改于: 2022-05-13 01:56:04.412000             🧑  作者: Mango

长度最多为 N 且设置位计数为 K 的倍数的二进制字符串的计数

给定两个整数NK ,任务是找到最多为N长度的二进制字符串的计数,这些二进制字符串可以形成使得连续1的计数始终是K的倍数。

例子:

方法:给定的问题可以在动态编程的帮助下使用记忆化来解决。请按照以下步骤解决给定的问题:

  • 创建一个递归函数cntStrings(N, K) ,它返回长度为N的字符串的数量,其中连续的 1 是K的倍数。这可以通过将 1 分配给当前索引的下一个K连续索引并递归调用剩余的字符串或将 0 分配给当前索引并递归调用剩余的字符串来完成。
  • 创建一个数组dp[]来存储上述递归函数的记忆值。
  • [1, N]范围内i的所有可能值调用函数cntStrings(i, K)并将它们的总和存储在变量cnt中。
  • 存储在cnt中的值是所需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
int dp[1001];
 
// Recursive function to find the count
// of valid binary strings of length n
int cntString(int n, int k)
{
    // Base Case
    if (n == 0) {
        return 1;
    }
 
    // If current value is already calculated
    if (dp[n] != -1) {
        return dp[n];
    }
 
    // Stores the current answer
    int ans = 0;
 
    // Case for element at next k indices as 1
    if (n >= k) {
        ans += cntString(n - k, k);
    }
 
    // Case for element at current index as 0
    ans += cntString(n - 1, k);
 
    // Return ans with storing it in dp[]
    return dp[n] = ans;
}
 
// Function to find the count of valid
// binary strings of atmost N length
int cntStringAll(int N, int K)
{
    // Initializing all elements with -1
    memset(dp, -1, sizeof(dp));
 
    // Stores the final answer
    int cnt = 0;
 
    // Iterate and calculate the total
    // possible binary strings of each
    // length in the range [1, N]
    for (int i = 1; i <= N; i++) {
        cnt += cntString(i, K);
    }
 
    // Return Answer
    return cnt;
}
 
// Driver Code
int main()
{
    int N = 5;
    int K = 4;
 
    cout << cntStringAll(N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
    static int dp[] = new int[1001];
 
    // Recursive function to find the count
    // of valid binary strings of length n
    static int cntString(int n, int k)
    {
        // Base Case
        if (n == 0) {
            return 1;
        }
 
        // If current value is already calculated
        if (dp[n] != -1) {
            return dp[n];
        }
 
        // Stores the current answer
        int ans = 0;
 
        // Case for element at next k indices as 1
        if (n >= k) {
            ans += cntString(n - k, k);
        }
 
        // Case for element at current index as 0
        ans += cntString(n - 1, k);
 
        // Return ans with storing it in dp[]
        return dp[n] = ans;
    }
 
    // Function to find the count of valid
    // binary strings of atmost N length
    static int cntStringAll(int N, int K)
    {
        // Initializing all elements with -1
        for (int i = 0; i < 1001; i++)
            dp[i] = -1;
 
        // Stores the final answer
        int cnt = 0;
 
        // Iterate and calculate the total
        // possible binary strings of each
        // length in the range [1, N]
        for (int i = 1; i <= N; i++) {
            cnt += cntString(i, K);
        }
 
        // Return Answer
        return cnt;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 5;
        int K = 4;
 
        System.out.println(cntStringAll(N, K));
    }
}
 
// This code is contributed by dwivediyash


Python3
# python program for the above approach
dp = [-1 for _ in range(1001)]
 
# Recursive function to find the count
# of valid binary strings of length n
def cntString(n, k):
 
        # Base Case
    if (n == 0):
        return 1
 
        # If current value is already calculated
    if (dp[n] != -1):
        return dp[n]
 
        # Stores the current answer
    ans = 0
 
    # Case for element at next k indices as 1
    if (n >= k):
        ans += cntString(n - k, k)
 
        # Case for element at current index as 0
    ans += cntString(n - 1, k)
 
    # Return ans with storing it in dp[]
    dp[n] = ans
 
    return dp[n]
 
 
# Function to find the count of valid
# binary strings of atmost N length
def cntStringAll(N, K):
 
        # Stores the final answer
    cnt = 0
 
    # Iterate and calculate the total
    # possible binary strings of each
    # length in the range [1, N]
    for i in range(1, N + 1):
        cnt += cntString(i, K)
 
    # Return Answer
    return cnt
 
# Driver Code
if __name__ == "__main__":
 
    N = 5
    K = 4
 
    print(cntStringAll(N, K))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
    static int []dp = new int[1001];
 
    // Recursive function to find the count
    // of valid binary strings of length n
    static int cntString(int n, int k)
    {
       
        // Base Case
        if (n == 0) {
            return 1;
        }
 
        // If current value is already calculated
        if (dp[n] != -1) {
            return dp[n];
        }
 
        // Stores the current answer
        int ans = 0;
 
        // Case for element at next k indices as 1
        if (n >= k) {
            ans += cntString(n - k, k);
        }
 
        // Case for element at current index as 0
        ans += cntString(n - 1, k);
 
        // Return ans with storing it in dp[]
        return dp[n] = ans;
    }
 
    // Function to find the count of valid
    // binary strings of atmost N length
    static int cntStringAll(int N, int K)
    {
       
        // Initializing all elements with -1
        for (int i = 0; i < 1001; i++)
            dp[i] = -1;
 
        // Stores the final answer
        int cnt = 0;
 
        // Iterate and calculate the total
        // possible binary strings of each
        // length in the range [1, N]
        for (int i = 1; i <= N; i++) {
            cnt += cntString(i, K);
        }
 
        // Return Answer
        return cnt;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 5;
        int K = 4;
 
        Console.WriteLine(cntStringAll(N, K));
    }
}
 
// This code is contributed by AnkThon


Javascript


输出
8

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