📌  相关文章
📜  所有可能的 N 长度平衡二进制字符串的计数

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

所有可能的 N 长度平衡二进制字符串的计数

给定一个数字N ,任务是找到长度为N的平衡二进制字符串的总数。如果满足以下条件,则称二进制字符串是平衡的:

  • 每个二进制字符串中 0 和 1 的数量相等
  • 二进制字符串的任何前缀中 0 的计数始终大于或等于 1 的计数
  • 例如:01 是长度为 2 的平衡二进制字符串,但 10 不是。

例子:

方法:给定的问题可以解决如下:

  1. 如果N是奇数,则不可能有平衡的二进制字符串,因为 0 和 1 的计数相等的条件将失败。
  2. 如果N是偶数,则长度为 N 的二进制字符串将具有N/2 个平衡的 0 和 1 对。
  3. 因此,现在尝试创建一个公式来获得N为偶数时平衡字符串的数量。
  1. 因此,对于任何偶数N ,返回(N/2)的加泰罗尼亚数作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
#define MAXN 500
#define mod 1000000007
 
// Vector to store catalan number
vector cat(MAXN + 1, 0);
 
// Function to get the Catalan Number
void catalan()
{
    cat[0] = 1;
    cat[1] = 1;
 
    for (int i = 2; i < MAXN + 1; i++) {
        long long int t = 0;
        for (int j = 0; j < i; j++) {
            t += ((cat[j] % mod)
                  * (cat[i - 1 - j] % mod)
                  % mod);
        }
        cat[i] = (t % mod);
    }
}
 
int countBalancedStrings(int N)
{
    // If N is odd
    if (N & 1) {
        return 0;
    }
 
    // Returning Catalan number
    // of N/2 as the answer
    return cat[N / 2];
}
 
// Driver Code
int main()
{
    // Precomputing
    catalan();
 
    int N = 4;
    cout << countBalancedStrings(N);
}


Java
// Java program for the above approach
class GFG {
 
    public static int MAXN = 500;
    public static int mod = 1000000007;
 
    // Vector to store catalan number
    public static int[] cat = new int[MAXN + 1];
 
    // Function to get the Catalan Number
    public static void catalan() {
        cat[0] = 1;
        cat[1] = 1;
 
        for (int i = 2; i < MAXN + 1; i++) {
            int t = 0;
            for (int j = 0; j < i; j++) {
                t += ((cat[j] % mod)
                        * (cat[i - 1 - j] % mod)
                        % mod);
            }
            cat[i] = (t % mod);
        }
    }
 
    public static int countBalancedStrings(int N)
    {
       
        // If N is odd
        if ((N & 1) > 0) {
            return 0;
        }
 
        // Returning Catalan number
        // of N/2 as the answer
        return cat[N / 2];
    }
 
    // Driver Code
    public static void main(String args[])
    {
       
        // Precomputing
        catalan();
 
        int N = 4;
        System.out.println(countBalancedStrings(N));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python3 program for the above approach
MAXN = 500
mod = 1000000007
 
# Vector to store catalan number
cat = [0 for _ in range(MAXN + 1)]
 
# Function to get the Catalan Number
def catalan():
     
    global cat
 
    cat[0] = 1
    cat[1] = 1
 
    for i in range(2, MAXN + 1):
        t = 0
        for j in range(0, i):
            t += ((cat[j] % mod) *
                  (cat[i - 1 - j] % mod) % mod)
 
        cat[i] = (t % mod)
 
def countBalancedStrings(N):
 
    # If N is odd
    if (N & 1):
        return 0
 
    # Returning Catalan number
    # of N/2 as the answer
    return cat[N // 2]
 
# Driver Code
if __name__ == "__main__":
 
    # Precomputing
    catalan()
 
    N = 4
    print(countBalancedStrings(N))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
    public static int MAXN = 500;
    public static int mod = 1000000007;
 
    // Vector to store catalan number
    public static int[] cat = new int[MAXN + 1];
 
    // Function to get the Catalan Number
    public static void catalan()
    {
        cat[0] = 1;
        cat[1] = 1;
 
        for (int i = 2; i < MAXN + 1; i++)
        {
            int t = 0;
            for (int j = 0; j < i; j++)
            {
                t += ((cat[j] % mod)
                        * (cat[i - 1 - j] % mod)
                        % mod);
            }
            cat[i] = (t % mod);
        }
    }
 
    public static int countBalancedStrings(int N)
    {
 
        // If N is odd
        if ((N & 1) > 0)
        {
            return 0;
        }
 
        // Returning Catalan number
        // of N/2 as the answer
        return cat[N / 2];
    }
 
    // Driver Code
    public static void Main()
    {
 
        // Precomputing
        catalan();
 
        int N = 4;
        Console.Write(countBalancedStrings(N));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Javascript


输出
2

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