给定一个不完整的括号序列S。任务是找到使它成为规则括号序列所需的右括号’)’的数量,并打印完整的括号序列。您只能在给定括号序列的末尾添加括号。如果无法完成括号顺序,请打印“不可能”。
让我们通过以下方式定义常规的括号序列:
- 空字符串是规则的括号序列。
- 如果s是常规方括号序列,则(s)是常规方括号序列。
- 如果s&t是正则括号序列,则st是正则括号序列。
例子:
Input : str = “(()(()(”
Output : (()(()()))
Explanation : The minimum number of ) needed to make the sequence regular are 3 which are appended at the end.
Input : str = “())(()”
Output : IMPOSSIBLE
我们需要添加最少数量的右方括号’)’,因此我们将计算不平衡的右方括号的数量,然后添加该右方括号的数量。如果在任何时候,右方括号的数目大于右方括号的数目,那么答案是不可能的。
下面是上述方法的实现:
C++
// C++ program to find number of closing
// brackets needed and complete a regular
// bracket sequence
#include
using namespace std;
// Function to find number of closing
// brackets and complete a regular
// bracket sequence
void completeSuquence(string s)
{
// Finding the length of sequence
int n = s.length();
int open = 0, close = 0;
for (int i = 0; i < n; i++)
{
// Counting opening brackets
if (s[i] == '(')
open++;
else
// Counting closing brackets
close++;
// Checking if at any position the
// number of closing bracket
// is more then answer is impossible
if (close > open)
{
cout << "Impossible" << endl;
return;
}
}
// If possible, print 's' and
// required closing brackets.
cout << s;
for (int i = 0; i < open - close; i++)
cout << ')';
cout << endl;
}
// Driver code
int main()
{
string s = "(()(()(";
completeSuquence(s);
return 0;
}
// This code is contributed by
// sanjeev2552
Java
// Java program to find number of closing
// brackets needed and complete a regular
// bracket sequence
class GFG {
// Function to find number of closing
// brackets and complete a regular
// bracket sequence
static void completeSequence(String s)
{
// Finding the length of sequence
int n = s.length();
int open = 0, close = 0;
for (int i = 0; i < n; i++) {
// Counting opening brackets
if (s.charAt(i) == '(')
open++;
else
// Counting closing brackets
close++;
// Checking if at any position the
// number of closing bracket
// is more then answer is impossible
if (close > open) {
System.out.print("IMPOSSIBLE");
return;
}
}
// If possible, print 's' and required closing
// brackets.
System.out.print(s);
for (int i = 0; i < open - close; i++)
System.out.print(")");
}
// Driver code
public static void main(String[] args)
{
String s = "(()(()(";
completeSequence(s);
}
}
Python 3
# Python 3 program to find number of
# closing brackets needed and complete
# a regular bracket sequence
# Function to find number of closing
# brackets and complete a regular
# bracket sequence
def completeSequence(s):
# Finding the length of sequence
n = len(s)
open = 0
close = 0
for i in range(n):
# Counting opening brackets
if (s[i] == '('):
open += 1
else:
# Counting closing brackets
close += 1
# Checking if at any position the
# number of closing bracket
# is more then answer is impossible
if (close > open):
print("IMPOSSIBLE")
return
# If possible, print 's' and
# required closing brackets.
print(s, end = "")
for i in range(open - close):
print(")", end = "")
# Driver code
if __name__ == "__main__":
s = "(()(()("
completeSequence(s)
# This code is contributed by ita_c
C#
// C# program to find number of closing
// brackets needed and complete a
// regular bracket sequence
using System;
class GFG
{
// Function to find number of closing
// brackets and complete a regular
// bracket sequence
static void completeSequence(String s)
{
// Finding the length of sequence
int n = s.Length;
int open = 0, close = 0;
for (int i = 0; i < n; i++)
{
// Counting opening brackets
if (s[i] == '(')
open++;
else
// Counting closing brackets
close++;
// Checking if at any position the
// number of closing bracket
// is more then answer is impossible
if (close > open)
{
Console.Write("IMPOSSIBLE");
return;
}
}
// If possible, print 's' and
// required closing brackets.
Console.Write(s);
for (int i = 0; i < open - close; i++)
Console.Write(")");
}
// Driver Code
static void Main()
{
String s = "(()(()(";
completeSequence(s);
}
}
// This code is contributed
// by ANKITRAI1
PHP
$open)
{
echo ("IMPOSSIBLE");
return;
}
}
// If possible, print 's' and
// required closing brackets.
echo ($s);
for ($i = 0; $i < $open - $close; $i++)
echo (")");
}
// Driver Code
$s = "(()(()(";
completeSequence($s);
// This code is contributed
// by ajit
?>
输出:
(()(()()))