📜  平衡的表达式,使给定的位置带有左括号|套装2

📅  最后修改于: 2021-05-06 23:38:49             🧑  作者: Mango

给定一个整数n和一个位置’position []’(1 <= length(position [])<= 2n)的数组,找到可以由长度2n形成的适当括号表达式的方式数,以使给定位置具有开括号。注意: position []数组以(基于1的索引)[0,1,1,0]的形式给出。此处的1表示应放置开口支架的位置。在值为0的位置,可以放置打开和关闭支架中的任何一个。

例子

这个问题的动态编程方法已经在这里讨论过了。在这篇文章中,将讨论使用记忆方法的递归和递归。

算法

  1. 在给定的数组adj中将所有位置用方括号标记为1。
  2. 运行递归循环,这样–
    • 如果方括号总数(从方括号中减去的方括号小于零),则返回0。
    • 如果索引一直到n,并且如果方括号总数为0,那么将获得一个解并返回1,否则返回0。
    • 如果索引已预先分配了1个索引,则以index + 1递归返回该函数并增加总括号。
    • 否则,递归地返回该函数,方法是在该索引处插入方括号,然后将总方括号增加1 +在该索引处插入封闭方括号,然后将总方括号减少1,然后移至下一个索引,直到n。

以下是上述算法的递归解决方案

C++
// C++ implementation of above 
// approach using Recursion
#include 
using namespace std;
  
// Function to find Number of
// proper bracket expressions
int find(int index, int openbrk, int n, int adj[])
{
    // If open-closed brackets < 0
    if (openbrk < 0)
        return 0;
  
    // If index reaches the end of expression
    if (index == n) {
  
        // IF brackets are balanced
        if (openbrk == 0)
            return 1;
        else
            return 0;
    }
  
    // If the current index has assigned open bracket
    if (adj[index] == 1) {
  
        // Move forward increasing the
        // length of open brackets
        return find(index + 1, openbrk + 1, n, adj);
    }
  
    else {
  
        // Move forward by inserting open as well
        // as closed brackets on that index
        return find(index + 1, openbrk + 1, n, adj)
               + find(index + 1, openbrk - 1, n, adj);
    }
}
// Driver Code
int main()
{
  
    int n = 2;
  
    // Open brackets at position 1
    int adj[4] = { 1, 0, 0, 0 };
  
    // Calling the find function to calculate the answer
    cout << find(0, 0, 2 * n, adj) << endl;
  
  return 0;
}


Java
// Java implementation of above 
// approach using Recursion 
  
class Test {
// Function to find Number of 
// proper bracket expressions 
  
    static int find(int index, int openbrk,
            int n, int[] adj) {
        // If open-closed brackets < 0 
        if (openbrk < 0) {
            return 0;
        }
  
        // If index reaches the end of expression 
        if (index == n) {
  
            // IF brackets are balanced 
            if (openbrk == 0) {
                return 1;
            } else {
                return 0;
            }
        }
  
        // If the current index has assigned open bracket 
        if (adj[index] == 1) {
  
            // Move forward increasing the 
            // length of open brackets 
            return find(index + 1, openbrk + 1, n, adj);
        } else {
  
            // Move forward by inserting open as well 
            // as closed brackets on that index 
            return find(index + 1, openbrk + 1, n, adj)
                    + find(index + 1, openbrk - 1, n, adj);
        }
    }
  
// Driver Code 
    public static void main(String[] args) {
        int n = 2;
  
        // Open brackets at position 1 
        int[] adj = {1, 0, 0, 0};
  
        // Calling the find function to calculate the answer 
        System.out.print(find(0, 0, 2 * n, adj));
    }
} 
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of above 
# approach using memoizaion 
N = 1000
  
# Function to find Number 
# of proper bracket expressions 
def find(index, openbrk, n, dp, adj): 
      
    # If open-closed brackets<0 
    if (openbrk < 0): 
        return 0
  
    # If index reaches the end of expression 
    if (index == n):
          
        # If brackets are balanced 
        if (openbrk == 0): 
            return 1
        else:
            return 0
              
    # If already stored in dp 
    if (dp[index][openbrk] != -1): 
        return dp[index][openbrk] 
  
    # If the current index has assigned 
    # open bracket 
    if (adj[index] == 1):
          
        # Move forward increasing the 
        # length of open brackets 
        dp[index][openbrk] = find(index + 1, 
                                  openbrk + 1, n, dp, adj) 
    else:
          
        # Move forward by inserting open as 
        # well as closed brackets on that index 
        dp[index][openbrk] = (find(index + 1, openbrk + 1, 
                                             n, dp, adj) + 
                              find(index + 1, openbrk - 1, 
                                              n, dp, adj))
    # return the answer 
    return dp[index][openbrk]
  
# Driver Code 
  
# DP array to precompute the answer 
dp=[[-1 for i in range(N)]
        for i in range(N)]
n = 2; 
  
# Open brackets at position 1 
adj = [ 1, 0, 0, 0 ] 
  
# Calling the find function to 
# calculate the answer 
print(find(0, 0, 2 * n, dp, adj))
  
# This code is contributed by sahishelangia


C#
// C# implementation of above 
// approach using Recursion
using System;
  
class GFG
{
// Function to find Number of
// proper bracket expressions
static int find(int index, int openbrk,
                int n, int[] adj)
{
    // If open-closed brackets < 0
    if (openbrk < 0)
        return 0;
  
    // If index reaches the end of expression
    if (index == n)
    {
  
        // IF brackets are balanced
        if (openbrk == 0)
            return 1;
        else
            return 0;
    }
  
    // If the current index has assigned open bracket
    if (adj[index] == 1)
    {
  
        // Move forward increasing the
        // length of open brackets
        return find(index + 1, openbrk + 1, n, adj);
    }
  
    else
    {
  
        // Move forward by inserting open as well
        // as closed brackets on that index
        return find(index + 1, openbrk + 1, n, adj)
            + find(index + 1, openbrk - 1, n, adj);
    }
}
  
// Driver Code
public static void Main()
{
  
    int n = 2;
  
    // Open brackets at position 1
    int[] adj = { 1, 0, 0, 0 };
  
    // Calling the find function to calculate the answer
    Console.WriteLine(find(0, 0, 2 * n, adj));
}
}
  
// This code is contributed by Akanksha Rai


PHP


C++
// C++ implementation of above 
// approach using memoizaion
#include 
using namespace std;
  
#define N 1000
  
// Function to find Number
// of proper bracket expressions
int find(int index, int openbrk, int n,
         int dp[N][N], int adj[])
{
    // If open-closed brackets<0
    if (openbrk < 0)
        return 0;
  
    // If index reaches the end of expression
    if (index == n) {
  
        // If brackets are balanced
        if (openbrk == 0)
            return 1;
  
        else
            return 0;
    }
  
    // If already stored in dp
    if (dp[index][openbrk] != -1)
        return dp[index][openbrk];
  
    // If the current index has assigned open bracket
    if (adj[index] == 1) {
  
        // Move forward increasing the
        // length of open brackets
        dp[index][openbrk] = find(index + 1,
                                  openbrk + 1, n, dp, adj);
    }
    else {
        // Move forward by inserting open as
        // well as closed brackets on that index
        dp[index][openbrk] = find(index + 1, openbrk + 1, n, dp, adj)
                             + find(index + 1, openbrk - 1, n, dp, adj);
    }
    // return the answer
    return dp[index][openbrk];
}
  
// Driver Code
int main()
{
    // DP array to precompute the answer
    int dp[N][N];
    int n = 2;
  
    memset(dp, -1, sizeof(dp));
  
    // Open brackets at position 1
    int adj[4] = { 1, 0, 0, 0 };
  
    // Calling the find function to calculate the answer
    cout << find(0, 0, 2 * n, dp, adj) << endl;
  
  return 0;
}


Java
// Java implementation of above 
// approach using memoizaion 
  
public class GFG {
  
    static final int N = 1000;
  
// Function to find Number 
// of proper bracket expressions 
    static int find(int index, int openbrk, int n,
            int dp[][], int adj[]) {
        // If open-closed brackets<0 
        if (openbrk < 0) {
            return 0;
        }
  
        // If index reaches the end of expression 
        if (index == n) {
  
            // If brackets are balanced 
            if (openbrk == 0) {
                return 1;
            } else {
                return 0;
            }
        }
  
        // If already stored in dp 
        if (dp[index][openbrk] != -1) {
            return dp[index][openbrk];
        }
  
        // If the current index has assigned open bracket 
        if (adj[index] == 1) {
  
            // Move forward increasing the 
            // length of open brackets 
            dp[index][openbrk] = find(index + 1,
                    openbrk + 1, n, dp, adj);
        } else {
            // Move forward by inserting open as 
            // well as closed brackets on that index 
            dp[index][openbrk] = find(index + 1, openbrk + 1, n, dp, adj)
                    + find(index + 1, openbrk - 1, n, dp, adj);
        }
        // return the answer 
        return dp[index][openbrk];
    }
  
// Driver code 
    public static void main(String[] args) {
        // DP array to precompute the answer 
        int dp[][] = new int[N][N];
        int n = 2;
  
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                dp[i][j] = -1;
            }
        }
  
        // Open brackets at position 1 
        int adj[] = {1, 0, 0, 0};
  
        // Calling the find function to calculate the answer 
        System.out.print(find(0, 0, 2 * n, dp, adj));
    }
}
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of above approach
# using memoizaion
N = 1000;
dp = [[-1 for x in range(N)] 
          for y in range(N)];
            
# Open brackets at position 1
adj = [ 1, 0, 0, 0 ];
  
# Function to find Number of proper 
# bracket expressions
def find(index, openbrk, n):
      
    # If open-closed brackets<0
    if (openbrk < 0):
        return 0;
  
    # If index reaches the end of expression
    if (index == n): 
  
        # If brackets are balanced
        if (openbrk == 0):
            return 1;
  
        else:
            return 0;
  
    # If already stored in dp
    if (dp[index][openbrk] != -1):
        return dp[index][openbrk];
  
    # If the current index has assigned 
    # open bracket
    if (adj[index] == 1):
  
        # Move forward increasing the
        # length of open brackets
        dp[index][openbrk] = find(index + 1, 
                                  openbrk + 1, n);
    else:
          
        # Move forward by inserting open as
        # well as closed brackets on that index
        dp[index][openbrk] = (find(index + 1, openbrk + 1, n) + 
                              find(index + 1, openbrk - 1, n));
    # return the answer
    return dp[index][openbrk];
  
# Driver Code
  
# DP array to precompute the answer
n = 2;
  
# Calling the find function to 
# calculate the answer
print(find(0, 0, 2 * n));
  
# This code is contributed by mits


C#
// C# implementation of above 
// approach using memoizaion 
using System;
  
class GFG 
{
    static readonly int N = 1000;
  
    // Function to find Number 
    // of proper bracket expressions 
    static int find(int index, int openbrk, int n,
            int [,]dp, int []adj)
    {
        // If open-closed brackets<0 
        if (openbrk < 0) 
        {
            return 0;
        }
  
        // If index reaches the end of expression 
        if (index == n) 
        {
  
            // If brackets are balanced 
            if (openbrk == 0)
            {
                return 1;
            } 
            else 
            {
                return 0;
            }
        }
  
        // If already stored in dp 
        if (dp[index,openbrk] != -1) 
        {
            return dp[index, openbrk];
        }
  
        // If the current index has assigned open bracket 
        if (adj[index] == 1) 
        {
  
            // Move forward increasing the 
            // length of open brackets 
            dp[index, openbrk] = find(index + 1,
                    openbrk + 1, n, dp, adj);
        } 
        else 
        {
            // Move forward by inserting open as 
            // well as closed brackets on that index 
            dp[index, openbrk] = find(index + 1, openbrk + 1, n, dp, adj)
                    + find(index + 1, openbrk - 1, n, dp, adj);
        }
          
        // return the answer 
        return dp[index,openbrk];
    }
  
    // Driver code 
    public static void Main() 
    {
          
        // DP array to precompute the answer 
        int [,]dp = new int[N,N];
        int n = 2;
  
        for (int i = 0; i < N; i++) 
        {
            for (int j = 0; j < N; j++) 
            {
                dp[i, j] = -1;
            }
        }
  
        // Open brackets at position 1 
        int []adj = {1, 0, 0, 0};
  
        // Calling the find function to calculate the answer 
        Console.WriteLine(find(0, 0, 2 * n, dp, adj));
    }
}
  
// This code is contributed by PrinciRaj1992


PHP


输出:
2

记忆方法:可以通过记忆来优化上述算法的时间复杂度。唯一要做的就是使用数组存储先前迭代的结果,这样,如果已经计算出该值,则无需多次递归调用同一函数。

以下是所需的实现:

C++

// C++ implementation of above 
// approach using memoizaion
#include 
using namespace std;
  
#define N 1000
  
// Function to find Number
// of proper bracket expressions
int find(int index, int openbrk, int n,
         int dp[N][N], int adj[])
{
    // If open-closed brackets<0
    if (openbrk < 0)
        return 0;
  
    // If index reaches the end of expression
    if (index == n) {
  
        // If brackets are balanced
        if (openbrk == 0)
            return 1;
  
        else
            return 0;
    }
  
    // If already stored in dp
    if (dp[index][openbrk] != -1)
        return dp[index][openbrk];
  
    // If the current index has assigned open bracket
    if (adj[index] == 1) {
  
        // Move forward increasing the
        // length of open brackets
        dp[index][openbrk] = find(index + 1,
                                  openbrk + 1, n, dp, adj);
    }
    else {
        // Move forward by inserting open as
        // well as closed brackets on that index
        dp[index][openbrk] = find(index + 1, openbrk + 1, n, dp, adj)
                             + find(index + 1, openbrk - 1, n, dp, adj);
    }
    // return the answer
    return dp[index][openbrk];
}
  
// Driver Code
int main()
{
    // DP array to precompute the answer
    int dp[N][N];
    int n = 2;
  
    memset(dp, -1, sizeof(dp));
  
    // Open brackets at position 1
    int adj[4] = { 1, 0, 0, 0 };
  
    // Calling the find function to calculate the answer
    cout << find(0, 0, 2 * n, dp, adj) << endl;
  
  return 0;
}

Java

// Java implementation of above 
// approach using memoizaion 
  
public class GFG {
  
    static final int N = 1000;
  
// Function to find Number 
// of proper bracket expressions 
    static int find(int index, int openbrk, int n,
            int dp[][], int adj[]) {
        // If open-closed brackets<0 
        if (openbrk < 0) {
            return 0;
        }
  
        // If index reaches the end of expression 
        if (index == n) {
  
            // If brackets are balanced 
            if (openbrk == 0) {
                return 1;
            } else {
                return 0;
            }
        }
  
        // If already stored in dp 
        if (dp[index][openbrk] != -1) {
            return dp[index][openbrk];
        }
  
        // If the current index has assigned open bracket 
        if (adj[index] == 1) {
  
            // Move forward increasing the 
            // length of open brackets 
            dp[index][openbrk] = find(index + 1,
                    openbrk + 1, n, dp, adj);
        } else {
            // Move forward by inserting open as 
            // well as closed brackets on that index 
            dp[index][openbrk] = find(index + 1, openbrk + 1, n, dp, adj)
                    + find(index + 1, openbrk - 1, n, dp, adj);
        }
        // return the answer 
        return dp[index][openbrk];
    }
  
// Driver code 
    public static void main(String[] args) {
        // DP array to precompute the answer 
        int dp[][] = new int[N][N];
        int n = 2;
  
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                dp[i][j] = -1;
            }
        }
  
        // Open brackets at position 1 
        int adj[] = {1, 0, 0, 0};
  
        // Calling the find function to calculate the answer 
        System.out.print(find(0, 0, 2 * n, dp, adj));
    }
}
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation of above approach
# using memoizaion
N = 1000;
dp = [[-1 for x in range(N)] 
          for y in range(N)];
            
# Open brackets at position 1
adj = [ 1, 0, 0, 0 ];
  
# Function to find Number of proper 
# bracket expressions
def find(index, openbrk, n):
      
    # If open-closed brackets<0
    if (openbrk < 0):
        return 0;
  
    # If index reaches the end of expression
    if (index == n): 
  
        # If brackets are balanced
        if (openbrk == 0):
            return 1;
  
        else:
            return 0;
  
    # If already stored in dp
    if (dp[index][openbrk] != -1):
        return dp[index][openbrk];
  
    # If the current index has assigned 
    # open bracket
    if (adj[index] == 1):
  
        # Move forward increasing the
        # length of open brackets
        dp[index][openbrk] = find(index + 1, 
                                  openbrk + 1, n);
    else:
          
        # Move forward by inserting open as
        # well as closed brackets on that index
        dp[index][openbrk] = (find(index + 1, openbrk + 1, n) + 
                              find(index + 1, openbrk - 1, n));
    # return the answer
    return dp[index][openbrk];
  
# Driver Code
  
# DP array to precompute the answer
n = 2;
  
# Calling the find function to 
# calculate the answer
print(find(0, 0, 2 * n));
  
# This code is contributed by mits

C#

// C# implementation of above 
// approach using memoizaion 
using System;
  
class GFG 
{
    static readonly int N = 1000;
  
    // Function to find Number 
    // of proper bracket expressions 
    static int find(int index, int openbrk, int n,
            int [,]dp, int []adj)
    {
        // If open-closed brackets<0 
        if (openbrk < 0) 
        {
            return 0;
        }
  
        // If index reaches the end of expression 
        if (index == n) 
        {
  
            // If brackets are balanced 
            if (openbrk == 0)
            {
                return 1;
            } 
            else 
            {
                return 0;
            }
        }
  
        // If already stored in dp 
        if (dp[index,openbrk] != -1) 
        {
            return dp[index, openbrk];
        }
  
        // If the current index has assigned open bracket 
        if (adj[index] == 1) 
        {
  
            // Move forward increasing the 
            // length of open brackets 
            dp[index, openbrk] = find(index + 1,
                    openbrk + 1, n, dp, adj);
        } 
        else 
        {
            // Move forward by inserting open as 
            // well as closed brackets on that index 
            dp[index, openbrk] = find(index + 1, openbrk + 1, n, dp, adj)
                    + find(index + 1, openbrk - 1, n, dp, adj);
        }
          
        // return the answer 
        return dp[index,openbrk];
    }
  
    // Driver code 
    public static void Main() 
    {
          
        // DP array to precompute the answer 
        int [,]dp = new int[N,N];
        int n = 2;
  
        for (int i = 0; i < N; i++) 
        {
            for (int j = 0; j < N; j++) 
            {
                dp[i, j] = -1;
            }
        }
  
        // Open brackets at position 1 
        int []adj = {1, 0, 0, 0};
  
        // Calling the find function to calculate the answer 
        Console.WriteLine(find(0, 0, 2 * n, dp, adj));
    }
}
  
// This code is contributed by PrinciRaj1992

的PHP


输出:
2

时间复杂度:O(N 2 )