📌  相关文章
📜  长度为N的二进制字符串的计数,偶数设置为偶数,且最多为K个连续的1s

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

给定两个整数NK ,任务是查找长度为N的偶数个1的二进制字符串的数量,其中小于K的是连续的。
例子:

方法:
这个问题可以通过动态编程来解决。
让我们考虑一个3D表dp [] [] []存储每个子问题的解,这样dp [n] [i] [s]表示长度为n的具有i个连续1的二进制字符串的数量和1的= s 。因为仅需要检查1的总数是否为偶数,所以我们存储s%2 。因此, dp [n] [i] [s]可以计算如下:

  1. 如果将0放置在第n位置,则1的数量保持不变。因此, dp [n] [i] [s] = dp [n – 1] [0] [s]
  2. 如果将1放在第n位置,则dp [n] [i] [s] = dp [n – 1] [i +1] [(s +1)%2]
  3. 从以上两点可以看出,形成的递归关系为:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include  
using namespace std;
  
// Table to store solution
// of each subproblem
int dp[100001][20][2];
  
// Function to calculate
// the possible binary
// strings
int possibleBinaries(int pos,
                     int ones,
                     int sum,
                     int k)
{
    // If number of ones
    // is equal to K
    if (ones == k)
        return 0;
  
    // pos: current position
    // Base Case: When n
    // length is traversed
    if (pos == 0)
  
        // sum: count of 1's
        // Return the count
        // of 1's obtained
        return (sum == 0) ? 1 : 0;
  
    // If the subproblem has already
    // been solved
    if (dp[pos][ones][sum] != -1)
        // Return the answer
        return dp[pos][ones][sum];
  
    // Recursive call when current
    // position is filled with 1
    int ret = possibleBinaries(pos - 1,
                               ones + 1,
                               (sum + 1) % 2,
                               k)
              // Recursive call when current
              // position is filled with 0
              + possibleBinaries(pos - 1, 0,
                                 sum, k);
  
    // Store the solution
    // to this subproblem
    dp[pos][ones][sum] = ret;
  
    return dp[pos][ones][sum];
}
  
// Driver Code
int main()
{
    int N = 3;
    int K = 2;
  
    // Initialising the
    // table with -1
    memset(dp, -1, sizeof dp);
  
    cout << possibleBinaries(N, 0, 0, K);
}


Java
// Java program for the above approach
class GFG{
  
// Table to store solution
// of each subproblem
static int [][][]dp = new int[100001][20][2];
  
// Function to calculate
// the possible binary
// Strings
static int possibleBinaries(int pos, int ones,
                            int sum, int k)
{
      
    // If number of ones
    // is equal to K
    if (ones == k)
        return 0;
  
    // pos: current position
    // Base Case: When n
    // length is traversed
    if (pos == 0)
  
        // sum: count of 1's
        // Return the count
        // of 1's obtained
        return (sum == 0) ? 1 : 0;
  
    // If the subproblem has already
    // been solved
    if (dp[pos][ones][sum] != -1)
      
        // Return the answer
        return dp[pos][ones][sum];
  
    // Recursive call when current
    // position is filled with 1
    int ret = possibleBinaries(pos - 1,
                              ones + 1,
                              (sum + 1) % 2, k) + 
                                
              // Recursive call when current
              // position is filled with 0
              possibleBinaries(pos - 1, 0,
                               sum, k);
                                 
    // Store the solution
    // to this subproblem
    dp[pos][ones][sum] = ret;
  
    return dp[pos][ones][sum];
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    int K = 2;
  
    // Initialising the
    // table with -1
    for(int i = 0; i < 100001; i++)
    {
        for(int j = 0; j < 20; j++)
        {
            for(int l = 0; l < 2; l++)
                dp[i][j][l] = -1;
        }
    }
  
    System.out.print(possibleBinaries(N, 0, 0, K));
}
}
  
// This code is contributed by Rohit_ranjan


Python3
# Python3 program for the above approach 
import numpy as np 
  
# Table to store solution 
# of each subproblem 
dp = np.ones(((100002, 21, 3)))
dp = -1 * dp
  
# Function to calculate 
# the possible binary 
# strings 
def possibleBinaries(pos, ones, sum, k):
      
    # If number of ones 
    # is equal to K 
    if (ones == k):
        return 0
  
    # pos: current position 
    # Base Case: When n 
    # length is traversed 
    if (pos == 0):
  
        # sum: count of 1's 
        # Return the count 
        # of 1's obtained 
        return 1 if (sum == 0) else 0
  
    # If the subproblem has already 
    # been solved 
    if (dp[pos][ones][sum] != -1):
          
        # Return the answer 
        return dp[pos][ones][sum] 
  
    # Recursive call when current 
    # position is filled with 1 
    ret = (possibleBinaries(pos - 1,
                           ones + 1, 
                           (sum + 1) % 2, k) + 
      
           # Recursive call when current 
           # position is filled with 0 
           possibleBinaries(pos - 1, 0, sum, k)) 
  
    # Store the solution 
    # to this subproblem 
    dp[pos][ones][sum] = ret 
  
    return dp[pos][ones][sum] 
  
# Driver Code 
N = 3
K = 2
              
print(int(possibleBinaries(N, 0, 0, K)))
  
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Table to store solution
// of each subproblem
static int [,,]dp = new int[100001, 20, 2];
  
// Function to calculate the
// possible binary Strings
static int possibleBinaries(int pos, int ones,
                            int sum, int k)
{
      
    // If number of ones
    // is equal to K
    if (ones == k)
        return 0;
  
    // pos: current position
    // Base Case: When n
    // length is traversed
    if (pos == 0)
  
        // sum: count of 1's
        // Return the count
        // of 1's obtained
        return (sum == 0) ? 1 : 0;
  
    // If the subproblem has already
    // been solved
    if (dp[pos, ones, sum] != -1)
      
        // Return the answer
        return dp[pos, ones, sum];
  
    // Recursive call when current
    // position is filled with 1
    int ret = possibleBinaries(pos - 1,
                                ones + 1,
                                (sum + 1) % 2, k) + 
              // Recursive call when current
              // position is filled with 0
              possibleBinaries(pos - 1, 0,
                               sum, k);
                                 
    // Store the solution
    // to this subproblem
    dp[pos, ones, sum] = ret;
  
    return dp[pos, ones, sum];
}
  
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
    int K = 2;
  
    // Initialising the
    // table with -1
    for(int i = 0; i < 100001; i++)
    {
        for(int j = 0; j < 20; j++)
        {
            for(int l = 0; l < 2; l++)
                dp[i, j, l] = -1;
        }
    }
    Console.Write(possibleBinaries(N, 0, 0, K));
}
}
  
// This code is contributed by Amit Katiyar


输出:
2

时间复杂度: O(2 * N * K)