📜  平衡表达式,使得给定的位置有左括号

📅  最后修改于: 2021-09-17 07:38:20             🧑  作者: Mango

给定一个整数 n 和一个位置数组 ‘position[]’ (1 <= position[i] <= 2n),找出长度为 2n 的合适括号表达式的方式数,使得给定的位置有左括号.
例子 :

Input : n = 3, position[] = [2}
Output : 3 
Explanation : 
The proper bracket sequences of length 6 and 
opening bracket at position 2 are:
[ [ ] ] [ ] 
[ [ [ ] ] ]
[ [ ] [ ] ]

Input : n = 2, position[] = {1, 3}
Output : 1
Explanation: The only possibility is:
[ ] [ ]

方法:这个问题可以通过动态规划解决 .
DP i, j是填充前 i 个位置的有效方式的数量,这样“[”类型的括号比“]”类型的括号多 j。有效的方式意味着它是匹配括号表达式的前缀,并且强制执行“[”括号的位置已经满足。不难看出,DP 2N, 0是最终答案。
DP 的基本情况是, DP 0, 0 = 1。我们需要用'[‘括号填充第一个位置,这是唯一的方法。
如果该位置有一个可以用散列数组标记的左括号序列,则重复发生为:

if(j != 0) dpi, j = dpi-1, j-1
else  dpi, j =  0; 

如果该位置没有左括号序列,则重复发生如下:

if(j != 0) dpi, j = dpi - 1, j - 1 + dpi - 1, j + 1
 else  dpi, j = dpi - 1, j + 1

答案将是DP 2n, 0
下面给出了上述方法的实现:

C++
// CPP code to find number of ways of
// arranging bracket with proper expressions
#include 
using namespace std;
 
#define N 1000
 
// function to calculate the number
// of proper bracket sequence
long long arrangeBraces(int n, int pos[], int k)
{
 
    // hash array to mark the
    // positions of opening brackets
    bool h[N];
 
    // dp 2d array
    int dp[N][N];
 
    memset(h, 0, sizeof h);
    memset(dp, 0, sizeof dp);
 
    // mark positions in hash array
    for (int i = 0; i < k; i++)
        h[pos[i]] = 1;
 
    // first position marked as 1
    dp[0][0] = 1;
 
    // iterate and formulate the recurrences
    for (int i = 1; i <= 2 * n; i++) {
        for (int j = 0; j <= 2 * n; j++) {
 
            // if position has a opening bracket
            if (h[i]) {
                if (j != 0)
                    dp[i][j] = dp[i - 1][j - 1];
                else
                    dp[i][j] = 0;
            }
            else {
                if (j != 0)
                    dp[i][j] = dp[i - 1][j - 1] +
                               dp[i - 1][j + 1];
                else
                    dp[i][j] = dp[i - 1][j + 1];
            }
        }
    }
 
    // return answer
    return dp[2 * n][0];
}
 
// driver code
int main()
{
    int n = 3;
 
    // positions where opening braces
    // will be placed
    int pos[] = { 2 };
    int k = sizeof(pos)/sizeof(pos[0]);
 
    cout << arrangeBraces(n, pos, k);
    return 0;
}


Java
// Java code to find number of ways of
// arranging bracket with proper expressions
 
public class GFG {
 
    static final int N = 1000;
 
// function to calculate the number
// of proper bracket sequence
    static long arrangeBraces(int n, int pos[], int k) {
 
        // hash array to mark the
        // positions of opening brackets
        boolean h[] = new boolean[N];
 
        // dp 2d array
        int dp[][] = new int[N][N];
 
        // mark positions in hash array
        for (int i = 0; i < k; i++) {
            h[pos[i]] = true;
        }
 
        // first position marked as 1
        dp[0][0] = 1;
 
        // iterate and formulate the recurrences
        for (int i = 1; i <= 2 * n; i++) {
            for (int j = 0; j <= 2 * n; j++) {
 
                // if position has a opening bracket
                if (h[i]) {
                    if (j != 0) {
                        dp[i][j] = dp[i - 1][j - 1];
                    } else {
                        dp[i][j] = 0;
                    }
                } else if (j != 0) {
                    dp[i][j] = dp[i - 1][j - 1]
                            + dp[i - 1][j + 1];
                } else {
                    dp[i][j] = dp[i - 1][j + 1];
                }
            }
        }
 
        // return answer
        return dp[2 * n][0];
    }
// Driver code
 
    public static void main(String[] args) {
        int n = 3;
 
        // positions where opening braces
        // will be placed
        int pos[] = {2};
        int k = pos.length;
        System.out.print(arrangeBraces(n, pos, k));
    }
}
// This code is contributed by 29AjayKumar


Python3
# Python 3 code to find number of ways of
# arranging bracket with proper expressions
N = 1000
 
# function to calculate the number
# of proper bracket sequence
def arrangeBraces(n, pos, k):
     
    # hash array to mark the
    # positions of opening brackets
    h = [False for i in range(N)]
 
    # dp 2d array
    dp = [[0 for i in range(N)]
             for i in range(N)]
 
    # mark positions in hash array
    for i in range(k):
        h[pos[i]] = 1
 
    # first position marked as 1
    dp[0][0] = 1
 
    # iterate and formulate the recurrences
    for i in range(1, 2 * n + 1):
        for j in range(2 * n + 1):
             
            # if position has a opening bracket
            if (h[i]):
                if (j != 0):
                    dp[i][j] = dp[i - 1][j - 1]
                else:
                    dp[i][j] = 0
            else:
                if (j != 0):
                    dp[i][j] = (dp[i - 1][j - 1] +
                                dp[i - 1][j + 1])
                else:
                    dp[i][j] = dp[i - 1][j + 1]
                     
    # return answer
    return dp[2 * n][0]
 
# Driver Code
n = 3
 
# positions where opening braces
# will be placed
pos = [ 2 ,];
k = len(pos)
print(arrangeBraces(n, pos, k))
 
# This code is contributed
# by sahishelangia


C#
// C# code to find number of ways of
// arranging bracket with proper expressions
using System;
   
class GFG
{
    static int N = 1000;
   
    // function to calculate the number
    // of proper bracket sequence
    public static long arrangeBraces(int n, int[] pos, int k)
    {
       
        // hash array to mark the
        // positions of opening brackets
        bool[] h = new bool[N];
       
        // dp 2d array
        int[,] dp = new int[N,N];
       
        for(int i = 0; i < N; i++)
            h[i] = false;
         
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                dp[i,j] = 0;
       
        // mark positions in hash array
        for (int i = 0; i < k; i++)
            h[pos[i]] = true;
       
        // first position marked as 1
        dp[0,0] = 1;
       
        // iterate and formulate the recurrences
        for (int i = 1; i <= 2 * n; i++) {
            for (int j = 0; j <= 2 * n; j++) {
       
                // if position has a opening bracket
                if (h[i]) {
                    if (j != 0)
                        dp[i,j] = dp[i - 1,j - 1];
                    else
                        dp[i,j] = 0;
                }
                else {
                    if (j != 0)
                        dp[i,j] = dp[i - 1,j - 1] +
                                   dp[i - 1,j + 1];
                    else
                        dp[i,j] = dp[i - 1,j + 1];
                }
            }
        }
       
        // return answer
        return dp[2 * n,0];
    }
       
    // driver code
    static void Main()
    {
        int n = 3;
         
        // positions where opening braces
        // will be placed
        int[] pos = new int[]{ 2 };
        int k = pos.Length;
       
        Console.Write(arrangeBraces(n, pos, k));
    }
    //This code is contributed by DrRoot_
}


Javascript


输出 :

3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程