📜  查找字典顺序的下一个平衡括号序列

📅  最后修改于: 2021-05-05 00:24:31             🧑  作者: Mango

给定平衡括号序列为包含字符‘(’‘)’的字符串str ,任务是找到下一个字典顺序平衡序列(如果可能的话,否则打印-1)

例子:

方法:首先找到最右边的开括号,我们可以用一个右括号代替它,以按字典顺序获得更大的括号字符串。更新后的字符串可能不平衡,我们可以按字典上最小的字符串来填充字符串的其余部分:即,首先使用尽可能多的左括号,然后使用右括号填充其余位置。换句话说,我们尝试使尽可能长的前缀保持不变,并且后缀被按字典顺序最小的前缀代替。

要找到此位置,我们可以从右到左遍历字符,并保持左括号和右括号的平衡深度。当我们遇到一个中括号时,我们将减小深度,而当我们遇到一个中括号时,我们将增加深度。如果我们在某个点遇到一个中括号,并且处理完此符号后的余额为正,那么我们发现可以更改的最右边位置。我们更改符号,计算必须添加到右侧的左括号和右括号的数量,并按照字典最小的方式排列它们。

如果找不到合适的位置,则该序列已经是最大可能的序列,并且没有答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the lexicographically
// next balanced bracket
// expression if possible
string next_balanced_sequence(string& s)
{
    string next = "-1";
    int length = s.size();
    int depth = 0;
    for (int i = length - 1; i >= 0; --i) {
  
        // Decrement the depth for
        // every opening bracket
        if (s[i] == '(')
            depth--;
  
        // Increment for the
        // closing brackets
        else
            depth++;
  
        // Last opening bracket
        if (s[i] == '(' && depth > 0) {
            depth--;
            int open = (length - i - 1 - depth) / 2;
            int close = length - i - 1 - open;
  
            // Generate the required string
            next = s.substr(0, i) + ')'
                   + string(open, '(')
                   + string(close, ')');
            break;
        }
    }
    return next;
}
  
// Driver code
int main()
{
    string s = "((()))";
  
    cout << next_balanced_sequence(s);
  
    return 0;
}


Java
// Java implementation of the approach 
class Sol
{
      
// makes a string containing char d
// c number of times
static String string(int c, char d)
{
    String s = "";
    for(int i = 0; i < c; i++)
    s += d;
      
    return s;
}
      
// Function to find the lexicographically 
// next balanced bracket 
// expression if possible 
static String next_balanced_sequence(String s) 
{ 
    String next = "-1"; 
    int length = s.length(); 
    int depth = 0; 
    for (int i = length - 1; i >= 0; --i) 
    { 
  
        // Decrement the depth for 
        // every opening bracket 
        if (s.charAt(i) == '(') 
            depth--; 
  
        // Increment for the 
        // closing brackets 
        else
            depth++; 
  
        // Last opening bracket 
        if (s.charAt(i) == '(' && depth > 0) 
        { 
            depth--; 
            int open = (length - i - 1 - depth) / 2; 
            int close = length - i - 1 - open; 
  
            // Generate the required String 
            next = s.substring(0, i) + ')'
                + string(open, '(') 
                + string(close, ')'); 
            break; 
        } 
    } 
    return next; 
} 
  
// Driver code 
public static void main(String args[])
{ 
    String s = "((()))"; 
  
    System.out.println(next_balanced_sequence(s)); 
}
} 
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the approach 
  
# Function to find the lexicographically 
# next balanced bracket 
# expression if possible 
def next_balanced_sequence(s) : 
  
    next = "-1"; 
    length = len(s); 
    depth = 0; 
      
    for i in range(length - 1, -1, -1) :
          
        # Decrement the depth for 
        # every opening bracket 
        if (s[i] == '(') :
            depth -= 1; 
  
        # Increment for the 
        # closing brackets 
        else :
            depth += 1; 
  
        # Last opening bracket 
        if (s[i] == '(' and depth > 0) :
              
            depth -= 1; 
            open = (length - i - 1 - depth) // 2; 
            close = length - i - 1 - open; 
  
            # Generate the required string 
            next = s[0 : i] + ')' + open * '(' + close* ')'; 
            break; 
              
    return next; 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    s = "((()))"; 
  
    print(next_balanced_sequence(s)); 
  
    # This code is contributed by AnkitRai01


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
// makes a string containing char d
// c number of times
static String strings(int c, char d)
{
    String s = "";
    for(int i = 0; i < c; i++)
    s += d;
      
    return s;
}
      
// Function to find the lexicographically 
// next balanced bracket 
// expression if possible 
static String next_balanced_sequence(String s) 
{ 
    String next = "-1"; 
    int length = s.Length; 
    int depth = 0; 
    for (int i = length - 1; i >= 0; --i) 
    { 
  
        // Decrement the depth for 
        // every opening bracket 
        if (s[i] == '(') 
            depth--; 
  
        // Increment for the 
        // closing brackets 
        else
            depth++; 
  
        // Last opening bracket 
        if (s[i] == '(' && depth > 0) 
        { 
            depth--; 
            int open = (length - i - 1 - depth) / 2; 
            int close = length - i - 1 - open; 
  
            // Generate the required String 
            next = s.Substring(0, i) + ')' + 
                        strings(open, '(') + 
                        strings(close, ')'); 
            break; 
        } 
    } 
    return next; 
} 
  
// Driver code 
public static void Main(String []args)
{ 
    String s = "((()))"; 
  
    Console.WriteLine(next_balanced_sequence(s)); 
}
} 
  
// This code is contributed by Princi Singh


输出:
(()())