📌  相关文章
📜  第 K 个字典序最小的二进制字符串,具有 A 0 和 B 1

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

第 K 个字典序最小的二进制字符串,具有 A 0 和 B 1

给定三个正整数ABK ,任务是找到恰好包含A0B1第 K字典序最小二进制字符串

例子:

方法:上述问题可以通过使用动态规划来解决。请按照以下步骤解决此问题:

  • 初始化一个二维数组dp[][]使得dp[i][j]将表示二进制字符串的总数,其中i0j1
  • 除了dp[0][0] = 1表示空字符串之外,所有 dp 表值最初都用零填充。
  • 现在, dp[i][j]可以计算为以0结尾的字符串总数(使用 dp 状态为dp[i – 1][j] )和以 1 结尾的字符串(使用 dp状态为dp[i][j – 1] )。因此,当前 dp 状态计算为dp[i][j] = dp[i – 1][j] + dp[i][j – 1]
  • 填满这个 dp 表后,可以使用递归函数计算第 K字典序最小的二进制字符串。
  • 因此,定义一个具有参数A、B、Kdp的函数KthString
  • 现在,在这个递归函数的每次调用中:
    • 如果dp[A][B] 的值至少为 K ,则在第 K 个字典序最小的二进制字符串中的该位置只能出现 '0',然后递归调用状态(A – 1, B)的函数。
    • 否则此处存在“1”并递归调用状态(A, B – 1)的函数。
  • 根据以上观察打印答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Recursive function to find the Kth
// smallest binary string
string KthString(int A, int B, long long K,
                 vector >& dp)
{
    // Base Case
    if (A == 0) {
 
        // Return string of all 1's
        // of length B
        return string(B, '1');
    }
    if (B == 0) {
 
        // Return string of all 0's
        // of length A
        return string(A, '0');
    }
 
    if (K <= dp[A - 1][B]) {
        return "0" + KthString(
                         A - 1, B, K, dp);
    }
 
    else {
        return "1"
               + KthString(
                     A, B - 1,
                     K - dp[A - 1][B], dp);
    }
}
 
// Function to find the Kth lexicographically
// smallest binary string with exactly
// A zeroes and B ones
int KthStringUtil(int A, int B, int K)
{
    // Stores the recurring states
    vector > dp(
        A + 1, vector(B + 1));
 
    // Calculate the dp values iteratively
    dp[0][0] = 1;
    for (int i = 0; i <= A; ++i) {
        for (int j = 0; j <= B; ++j) {
 
            if (i > 0) {
 
                // The last character was '0'
                dp[i][j] += dp[i - 1][j];
            }
            if (j > 0) {
 
                // The last character was '1'
                dp[i][j] += dp[i][j - 1];
            }
        }
    }
 
    // Print the binary string obtained
    cout << KthString(A, B, K, dp);
 
    return 0;
}
 
// Driver Code
int main()
{
    int A = 3, B = 3, K = 7;
    KthStringUtil(A, B, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Recursive function to find the Kth
    // smallest binary string
    static String KthString(int A, int B, long K, int[][] dp)
    {
       
        // Base Case
        if (A == 0) {
 
            // Return string of all 1's
            // of length B
            String ans = "";
            for (int i = 0; i < B; i++) {
                ans += '1';
            }
            return ans;
        }
        if (B == 0) {
 
            // Return string of all 0's
            // of length A
            String ans = "";
            for (int i = 0; i < A; i++) {
                ans += '0';
            }
            return ans;
        }
 
        if (K <= dp[A - 1][B]) {
            return "0" + KthString(A - 1, B, K, dp);
        }
 
        else {
            return "1"
                + KthString(A, B - 1, K - dp[A - 1][B], dp);
        }
    }
 
    // Function to find the Kth lexicographically
    // smallest binary string with exactly
    // A zeroes and B ones
    static int KthStringUtil(int A, int B, int K)
    {
        // Stores the recurring states
        int[][] dp = new int[A + 1][B + 1];
 
        // Calculate the dp values iteratively
        dp[0][0] = 1;
        for (int i = 0; i <= A; ++i) {
            for (int j = 0; j <= B; ++j) {
 
                if (i > 0) {
 
                    // The last character was '0'
                    dp[i][j] += dp[i - 1][j];
                }
                if (j > 0) {
 
                    // The last character was '1'
                    dp[i][j] += dp[i][j - 1];
                }
            }
        }
 
        // Print the binary string obtained
        System.out.println(KthString(A, B, K, dp));
 
        return 0;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 3, B = 3, K = 7;
        KthStringUtil(A, B, K);
    }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python Program to implement
# the above approach
 
# Recursive function to find the Kth
# smallest binary string
def KthString(A, B, K, dp):
   
    # Base Case
    if (A == 0):
 
        # Return string of all 1's
        # of length B
        str = ""
        for i in range(B):
            str += '1'
         
        return str
     
    if (B == 0):
 
        # Return string of all 0's
        # of length A
        str = ""
        for i in range(A):
            str += '0'
        return str
 
    if (K <= dp[A - 1][B]):
        return "0" + KthString( A - 1, B, K, dp)
 
    else:
        return "1" + KthString( A, B - 1, K - dp[A - 1][B], dp)
     
 
# Function to find the Kth lexicographically
# smallest binary string with exactly
# A zeroes and B ones
def KthStringUtil(A, B, K):
   
    # Stores the recurring states
    dp = [0] * (A + 1)
 
    for i in range(len(dp)):
        dp[i] = [0] * (B + 1)
     
 
    # Calculate the dp values iteratively
    dp[0][0] = 1
    for i in range(A + 1):
        for j in range(B + 1):
 
            if (i > 0):
 
                # The last character was '0'
                dp[i][j] += dp[i - 1][j]
             
            if (j > 0):
 
                # The last character was '1'
                dp[i][j] += dp[i][j - 1]
         
 
    # Print the binary string obtained
    print(KthString(A, B, K, dp))
 
# Driver Code
A = 3
B = 3
K = 7
KthStringUtil(A, B, K)
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
class GFG {
 
    // Recursive function to find the Kth
    // smallest binary string
    static string KthString(int A, int B, long K,
                            int[, ] dp)
    {
 
        // Base Case
        if (A == 0) {
 
            // Return string of all 1's
            // of length B
            string ans = "";
            for (int i = 0; i < B; i++) {
                ans += '1';
            }
            return ans;
        }
        if (B == 0) {
 
            // Return string of all 0's
            // of length A
            string ans = "";
            for (int i = 0; i < A; i++) {
                ans += '0';
            }
            return ans;
        }
 
        if (K <= dp[A - 1, B]) {
            return "0" + KthString(A - 1, B, K, dp);
        }
 
        else {
            return "1"
                + KthString(A, B - 1, K - dp[A - 1, B], dp);
        }
    }
 
    // Function to find the Kth lexicographically
    // smallest binary string with exactly
    // A zeroes and B ones
    static int KthStringUtil(int A, int B, int K)
    {
        // Stores the recurring states
        int[, ] dp = new int[A + 1, B + 1];
 
        // Calculate the dp values iteratively
        dp[0, 0] = 1;
        for (int i = 0; i <= A; ++i) {
            for (int j = 0; j <= B; ++j) {
 
                if (i > 0) {
 
                    // The last character was '0'
                    dp[i, j] += dp[i - 1, j];
                }
                if (j > 0) {
 
                    // The last character was '1'
                    dp[i, j] += dp[i, j - 1];
                }
            }
        }
 
        // Print the binary string obtained
        Console.WriteLine(KthString(A, B, K, dp));
 
        return 0;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int A = 3, B = 3, K = 7;
        KthStringUtil(A, B, K);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
010110

时间复杂度: O(A*B)
辅助空间: O(A*B)