📜  制作平衡序列所需的最少操作

📅  最后修改于: 2021-10-26 06:27:38             🧑  作者: Mango

平衡序列被定义为一个字符串,其中每个左括号都有 2 个连续的右括号。因此, {}}、{{}}}}、{{}}{}}}}是平衡的,而}}{、{}是不平衡的。
现在给定一个括号序列('{‘ 和’}’),您只能对该序列执行一个操作,即在任何位置插入一个左括号或右括号。您必须告诉使给定序列平衡所需的最少操作数。

方法:为了制作一个平衡的序列,每个左括号都需要两个连续的右括号。可能有3种情况:

  1. 当前字符是一个开放支架:如果前面的字符不是一个右括号,然后只需插入左括号到堆栈,否则就需要一个右括号,这将花费一个操作。
  2. 如果堆栈为空并且当前字符是一个右括号:在这种情况下,需要一个左括号,它将花费一个操作并将该左括号插入堆栈。
  3. 如果堆栈不为空但当前字符是一个右括号:这里只需要计算右括号的个数。如果是 2,则从堆栈中删除一个左括号,否则增加右括号的计数。

在字符串的末尾,如果堆栈不为空,则所需的右括号计数为 ((2 * 堆栈的大小) – 当前右括号的计数)。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum operations required
int minOperations(string s, int len)
{
    int operationCnt = 0;
    stack st;
    int cntClosing = 0;
    for (int i = 0; i < len; i++) {
 
        // Condition where we got only one closing
        // bracket instead of 2, here we have to
        // add one more closing bracket to
        // make the sequence balanced
        if (s[i] == '{') {
            if (cntClosing > 0) {
 
                // Add closing bracket that
                // costs us one operation
                operationCnt++;
 
                // Remove the top opening bracket because
                // we got the 1 opening and 2
                // continuous closing brackets
                st.pop();
            }
 
            // Inserting the opening bracket to stack
            st.push(s[i]);
 
            // After making the sequence balanced
            // closing is now set to 0
            cntClosing = 0;
        }
        else if (st.empty()) {
 
            // Case when there is no opening bracket
            // the sequence starts with a closing bracket
            // and one opening bracket is required
            // Now we have one opening and one closing bracket
            st.push('{');
 
            // Add opening bracket that
            // costs us one operation
            operationCnt++;
 
            // Assigning 1 to cntClosing because
            // we have one closing bracket
            cntClosing = 1;
        }
        else {
            cntClosing = (cntClosing + 1) % 2;
 
            // Case where we got two continuous closing brackets
            // Need to pop one opening bracket from stack top
            if (cntClosing == 0) {
                st.pop();
            }
        }
    }
 
    // Condition where stack is not empty
    // This is the case where we have
    // only opening brackets
    // (st.size() * 2) will give us the total
    // closing bracket needed
    // cntClosing is the count of closing
    // bracket that we already have
    operationCnt += st.size() * 2 - cntClosing;
    return operationCnt;
}
 
// Driver code
int main()
{
    string str = "}}{";
    int len = str.length();
 
    cout << minOperations(str, len);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG{
 
// Function to return the
// minimum operations required
static int minOperations(String s, int len)
{
    int operationCnt = 0;
    Stack st = new Stack();
    int cntClosing = 0;
     
    for(int i = 0; i < len; i++)
    {
         
       // Condition where we got only one
       // closing bracket instead of 2,
       // here we have to add one more
       // closing bracket to make the
       // sequence balanced
       if (s.charAt(i) == '{')
       {
           if (cntClosing > 0)
           {
                
               // Add closing bracket that
               // costs us one operation
               operationCnt++;
                
               // Remove the top opening bracket
               // because we got the 1 opening
               // and 2 continuous closing brackets
               st.pop();
           }
            
           // Inserting the opening bracket to stack
           st.add(s.charAt(i));
            
           // After making the sequence balanced
           // closing is now set to 0
           cntClosing = 0;
       }
       else if (st.isEmpty())
       {
            
           // Case when there is no opening
           // bracket the sequence starts
           // with a closing bracket and
           // one opening bracket is required
           // Now we have one opening and one
           // closing bracket
           st.add('{');
            
           // Add opening bracket that
           // costs us one operation
           operationCnt++;
            
           // Assigning 1 to cntClosing because
           // we have one closing bracket
            cntClosing = 1;
       }
       else
       {
           cntClosing = (cntClosing + 1) % 2;
            
           // Case where we got two continuous
           // closing brackets need to pop one
           // opening bracket from stack top
           if (cntClosing == 0)
           {
               st.pop();
           }
       }
    }
 
    // Condition where stack is not empty
    // This is the case where we have only
    // opening brackets (st.size() * 2)
    // will give us the total closing
    // bracket needed cntClosing is the
    // count of closing bracket that
    // we already have
    operationCnt += st.size() * 2 - cntClosing;
 
    return operationCnt;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "}}{";
    int len = str.length();
 
    System.out.print(minOperations(str, len));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation
# of the approach
from queue import LifoQueue 
 
# Function to return the
# minimum operations required
def minOperations(s, len):
   
    operationCnt = 0;
    st = LifoQueue();
    cntClosing = 0;
 
    for i in range(0, len):
 
        # Condition where we got only one
        # closing bracket instead of 2,
        # here we have to add one more
        # closing bracket to make the
        # sequence balanced
        if (s[i] == '{'):
           
            if (cntClosing > 0):
 
                # Add closing bracket that
                # costs us one operation
                operationCnt += 1;
 
                # Remove the top opening bracket
                # because we got the 1 opening
                # and 2 continuous closing brackets
                st.pop();           
 
            # Inserting the opening bracket to stack
            st.put(s[i]);
 
            # After making the sequence balanced
            # closing is now set to 0
            cntClosing = 0;
             
        elif(st.empty()):
           
                # Case when there is no opening
                # bracket the sequence starts
                # with a closing bracket and
                # one opening bracket is required
                # Now we have one opening and one
                # closing bracket
                st.put('{');
     
                # Add opening bracket that
                # costs us one operation
                operationCnt += 1;
     
                # Assigning 1 to cntClosing because
                # we have one closing bracket
                cntClosing = 1;
        else:
            cntClosing = (cntClosing + 1) % 2;
 
            # Case where we got two continuous
            # closing brackets need to pop one
            # opening bracket from stack top
            if (cntClosing == 0):
                st.pop();   
 
    # Condition where stack is not empty
    # This is the case where we have only
    # opening brackets (st.size() * 2)
    # will give us the total closing
    # bracket needed cntClosing is the
    # count of closing bracket that
    # we already have
    operationCnt += st.qsize() * 2 - cntClosing + 1;
 
    return operationCnt;
 
# Driver code
if __name__ == '__main__':
    str = "{";
    print(minOperations(str, 1));
 
# This code is contributed by gauravrajput1


C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to return the
// minimum operations required
static int minOperations(String s,
                         int len)
{
  int operationCnt = 0;
  Stack st = new Stack();
  int cntClosing = 0;
 
  for(int i = 0; i < len; i++)
  {       
    // Condition where we got only one
    // closing bracket instead of 2,
    // here we have to add one more
    // closing bracket to make the
    // sequence balanced
    if (s[i] == '{')
    {
      if (cntClosing > 0)
      {              
        // Add closing bracket that
        // costs us one operation
        operationCnt++;
 
        // Remove the top opening bracket
        // because we got the 1 opening
        // and 2 continuous closing brackets
        st.Pop();
      }
 
      // Inserting the opening bracket to stack
      st.Push(s[i]);
 
      // After making the sequence balanced
      // closing is now set to 0
      cntClosing = 0;
    }
    else if (st.Count != 0)
    {          
      // Case when there is no opening
      // bracket the sequence starts
      // with a closing bracket and
      // one opening bracket is required
      // Now we have one opening and one
      // closing bracket
      st.Push('{');
 
      // Add opening bracket that
      // costs us one operation
      operationCnt++;
 
      // Assigning 1 to cntClosing because
      // we have one closing bracket
      cntClosing = 1;
    }
    else
    {
      cntClosing = (cntClosing + 1) % 2;
 
      // Case where we got two continuous
      // closing brackets need to pop one
      // opening bracket from stack top
      if (cntClosing == 0 &&
          st.Count != 0)
      {
        st.Pop();
      }
    }
  }
 
  // Condition where stack is not empty
  // This is the case where we have only
  // opening brackets (st.Count * 2)
  // will give us the total closing
  // bracket needed cntClosing is the
  // count of closing bracket that
  // we already have
  operationCnt += st.Count * 2 -
                  cntClosing + 1;
 
  return operationCnt;
}
 
// Driver code
public static void Main(String[] args)
{
  String str = "}}{";
  int len = str.Length;
  Console.Write(minOperations(str,
                              len));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
3

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