📜  计算括号序列对,以使括号平衡

📅  最后修改于: 2021-06-26 23:36:40             🧑  作者: Mango

给定N个括号序列,任务是通过连接来找到括号序列对的数量,这可以从整体上获得平衡的括号序列。方括号序列只能是一对对的一部分。

例子:

Input: { ")())", ")", "((", "((", "(", ")", ")"}
Output:  2 
Bracket sequence {1, 3} and {5, 6} 

Input: {"()", "(())", "(())", "()"}
Output: 2 
Since all brackets are balanced, hence we can form 2 pairs with 4. 

方法:可以按照以下步骤解决上述问题:

  • 计算所需的个人开括号。
  • 如果所需的右方括号> 0并且左方括号为0,则对方括号的所需结束号进行哈希处理。
  • 同样,如果所需的开括号> 0并且右括号为0,则对括号的所需开号进行哈希处理。
  • 计算平衡的括号序列。
  • 将(平衡括号序列的数量/ 2)添加到对的数量中。
  • 对于每个需要相同数目的括号的序列,将min(hash [open],hash [close])添加到对数中

下面是上述方法的实现:

C++
// C++ program to count the number of pairs
// of balanced parentheses
#include 
using namespace std;
 
// Function to count the number of pairs
int countPairs(string bracks[], int num)
{
 
    // Hashing function to count the
    // opening and closing brackets
    unordered_map open, close;
 
    int cnt = 0;
 
    // Traverse for all bracket sequences
    for (int i = 0; i < num; i++) {
 
        // Get the string
        string s = bracks[i];
 
        int l = s.length();
 
        // Counts the opening and closing required
        int op = 0, cl = 0;
 
        // Traverse in the string
        for (int j = 0; j < l; j++) {
 
            // If it is a opening bracket
            if (s[j] == '(')
                op++;
            else // Closing bracket
            {
 
                // If openings are there, then close it
                if (op)
                    op--;
                else // Else increase count of closing
                    cl++;
            }
        }
 
        // If requirements of openings
        // are there and no closing
        if (op && !cl)
            open[op]++;
 
        // If requirements of closing
        // are there and no opening
        if (cl && !op)
            close[cl]++;
 
        // Perfect
        if (!op && !cl)
            cnt++;
    }
 
    // Divide by two since two
    // perfect makes one pair
    cnt = cnt / 2;
 
    // Traverse in the open and find
    // corresponding minimum
    for (auto it : open)
        cnt += min(it.second, close[it.first]);
 
    return cnt;
}
 
// Driver Code
int main()
{
    string bracks[] = { ")())", ")", "((", "((", "(", ")", ")" };
    int num = sizeof(bracks) / sizeof(bracks[0]);
 
    cout << countPairs(bracks, num);
}


Java
// Java program to count the number of pairs
// of balanced parentheses
import java.util.HashMap;
 
class GFG
{
 
// Function to count the number of pairs
static int countPairs(String[] bracks, int num)
{
 
    // Hashing function to count the
    // opening and closing brackets
    HashMap open = new HashMap<>();
    HashMap close = new HashMap<>();
 
    int cnt = 0;
 
    // Traverse for all bracket sequences
    for (int i = 0; i < num; i++)
    {
 
        // Get the string
        String s = bracks[i];
        int l = s.length();
 
        // Counts the opening and closing required
        int op = 0, cl = 0;
 
        // Traverse in the string
        for (int j = 0; j < l; j++)
        {
 
            // If it is a opening bracket
            if (s.charAt(j) == '(')
                op++;
 
            // Closing bracket
            else
            {
 
                // If openings are there, then close it
                if (op != 0)
                    op--;
 
                // Else increase count of closing
                else
                    cl++;
            }
        }
 
        // If requirements of openings
        // are there and no closing
        if (op != 0 && cl == 0)
            open.put(op, open.get(op) == null ?
                     1 : open.get(op) + 1);
 
        // If requirements of closing
        // are there and no opening
        if (cl != 0 && op == 0)
            close.put(cl, close.get(cl) == null ?
                      1 : close.get(cl) + 1);
 
        // Perfect
        if (op == 0 && cl == 0)
            cnt++;
    }
 
    // Divide by two since two
    // perfect makes one pair
    cnt /= 2;
 
    // Traverse in the open and find
    // corresponding minimum
    for (HashMap.Entry it : open.entrySet())
        cnt += Math.min(it.getValue(),
              close.get(it.getKey()));
 
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
    String[] bracks = { ")())", ")", "((",
                        "((", "(", ")", ")" };
    int num = bracks.length;
 
    System.out.println(countPairs(bracks, num));
}
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 program to count the number of pairs
# of balanced parentheses
import math as mt
 
# Function to count the number of pairs
def countPairs(bracks, num):
 
    # Hashing function to count the
    # opening and closing brackets
    openn=dict()
    close=dict()
 
    cnt = 0
 
    # Traverse for all bracket sequences
    for i in range(num):
 
        # Get the string
        s = bracks[i]
 
        l = len(s)
 
        # Counts the opening and closing required
        op,cl = 0,0
 
        # Traverse in the string
        for j in range(l):
            # If it is a opening bracket
            if (s[j] == '('):
                op+=1
            else: # Closing bracket
             
 
                # If openings are there, then close it
                if (op):
                    op-=1
                else: # Else increase count of closing
                    cl+=1
             
         
 
        # If requirements of openings
        # are there and no closing
        if (op and cl==0):
            if op in openn.keys():
                openn[op]+=1
            else:
                openn[op]=1
                 
 
        # If requirements of closing
        # are there and no opening
        if (cl and op==0):
            if cl in openn.keys():
                close[cl]+=1
            else:
                close[cl]=1
             
 
        # Perfect
        if (op==0 and cl==0):
            cnt+=1
     
 
    # Divide by two since two
    # perfect makes one pair
    cnt = cnt //2
 
    # Traverse in the open and find
    # corresponding minimum
    for it in openn:
        cnt += min(openn[it], close[it])
 
    return cnt
 
 
# Driver Code
bracks= [")())", ")", "((", "((", "(", ")", ")" ]
num = len(bracks)
 
print(countPairs(bracks, num))
 
#This code is contributed by Mohit kumar 29


C#
// C# program to count the number of pairs
// of balanced parentheses
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to count the number of pairs
static int countPairs(string[] bracks, int num)
{
   
    // Hashing function to count the
    // opening and closing brackets
    Dictionary open = new Dictionary();
    Dictionary close = new Dictionary();
  
    int cnt = 0;
  
    // Traverse for all bracket sequences
    for(int i = 0; i < num; i++)
    {
  
        // Get the string
        string s = bracks[i];
        int l = s.Length;
  
        // Counts the opening and closing required
        int op = 0, cl = 0;
  
        // Traverse in the string
        for(int j = 0; j < l; j++)
        {
  
            // If it is a opening bracket
            if (s[j] == '(')
                op++;
  
            // Closing bracket
            else
            {
  
                // If openings are there, then close it
                if (op != 0)
                    op--;
  
                // Else increase count of closing
                else
                    cl++;
            }
        }
  
        // If requirements of openings
        // are there and no closing
        if (op != 0 && cl == 0)
        {
            if (open.ContainsKey(op))
            {
                open[op]++;
            }
            else
            {
                open[op] = 1;
            }
        }
  
        // If requirements of closing
        // are there and no opening
        if (cl != 0 && op == 0)
        {
            if (close.ContainsKey(cl))
            {
                close[cl]++;
            }
            else
            {
                close[cl] = 1;
            }
        }
  
        // Perfect
        if (op == 0 && cl == 0)
            cnt++;
    }
  
    // Divide by two since two
    // perfect makes one pair
    cnt /= 2;
  
    // Traverse in the open and find
    // corresponding minimum
    foreach(KeyValuePair it in open)
    {
        cnt += Math.Min(it.Value, close[it.Value]);
    }
    return cnt;
}
  
// Driver Code
public static void Main(string[] args)
{
    string[] bracks = { ")())", ")", "((",
                        "((", "(", ")", ")" };
   
    int num = bracks.Length;
  
    Console.Write(countPairs(bracks, num));
}
}
 
// This code is contributed by rutvik_56


输出:
2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。