打印所有可能的括号组合_打印括号的所有组合

打印所有可能的括号组合

Problem statement:

问题陈述:

Given N number of parenthesis (pair of opening and closing parenthesis), you have to print the valid combinations of the parenthesis and print the value.

给定N个圆括号(一对圆括号),必须打印有效的圆括号组合并打印值。

Input:
First-line contains T Testcases,
T no. of lines along with an integer number.

E.g.
3
4
3
5

Constrains:
1≤ T ≤10
1≤ N ≤ 20

Output:
Print the number of possible valid combinations 
of the parenthesis.

Example

T = 3

Input:
4
output:
(((()))), ((()())), ((())()), ((()))(), (()(())), (()()())
(()())(), (())(()), (())()(), ()((())),()(()()), ()(())()
()()(()), ()()()()

Input:
3
Output:
((())), (()()), (())(), ()(()), ()()()


Input:
5
Output:
((((())))), (((()()))), (((())())), (((()))()), (((())))()
((()(()))), ((()()())), ((()())()), ((()()))(), ((())(()))
((())()()), ((())())(), ((()))(()), ((()))()(), (()((())))
(()(()())), (()(())()), (()(()))(), (()()(())), (()()()())
(()()())(), (()())(()), (()())()(), (())((())), (())(()())
(())(())(), (())()(()), (())()()(), ()(((()))), ()((()()))
()((())()), ()((()))(), ()(()(())), ()(()()()), ()(()())()
()(())(()), ()(())()(), ()()((())), ()()(()()), ()()(())()
()()()(()), ()()()()()

Explanation with example:

举例说明:

To generate a valid combination with the parenthesis is a try and error process and we will solve this problem with the recursion approach.

生成带括号的有效组合是一个尝试和错误的过程,我们将使用递归方法解决此问题。

To solve this problem, we will follow these steps,

为了解决这个问题,我们将按照以下步骤操作,

  1. We have to initialize a count variable to zero, and an open variable is for open parenthesis, and a close variable is for close parenthesis.

    我们必须将一个count变量初始化为零,一个open变量用于开括号,一个close变量用于闭括号。

  2. Initialize a vector to store the strings.

    初始化向量以存储字符串。

  3. Whenever the open is less than the number then we add an open parenthesis.

    只要空位数小于数字,我们就会添加一个圆括号。

  4. Whenever the open parenthesis is grater the closing parenthesis then we add closing parenthesis to it.

    只要开放括号圆括号括起来,就要在其上加上封闭括号。

  5. If the value of open parenthesis equals the number then we add the strings into the vector.

    如果开括号的值等于数字,则将字符串添加到向量中。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

void traverse(int open, int close, int n, int& count, string str, vector<string>& v)
{
    if (close == n) {
        v.push_back(str);
        return;
    }
    if (open < n) {
        traverse(open + 1, close, n, count, str + "(", v);
    }
    if (close < open) {
        traverse(open, close + 1, n, count, str + ")", v);
    }
    return;
}

void genarate_parenthesis(int num)
{
    string str = "";
    int open_brace = 0, close_brace = 0, count = 0;
    vector<string> v;
    traverse(open_brace, close_brace, num, count, str, v);
    cout << "Posiible combination : ";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << endl;
    }
}

int main()
{
    int t;

    cout << "TestCase : ";
    cin >> t;

    while (t--) {
        int num;

        cout << "Enter the number: ";
        cin >> num;

        genarate_parenthesis(num);
    }
    
    return 0;
}

Output

输出量

TestCase : 3
Enter the number: 4
(((())))
((()()))
((())())
((()))()
(()(()))
(()()())
(()())()
(())(())
(())()()
()((()))
()(()())
()(())()
()()(())
()()()()
Enter the number: 3
((()))
(()())
(())()
()(())
()()()
Enter the number: 5
((((()))))
(((()())))
(((())()))
(((()))())
(((())))()
((()(())))
((()()()))
((()())())
((()()))()
((())(()))
((())()())
((())())()
((()))(())
((()))()()
(()((())))
(()(()()))
(()(())())
(()(()))()
(()()(()))
(()()()())
(()()())()
(()())(())
(()())()()
(())((()))
(())(()())
(())(())()
(())()(())
(())()()()
()(((())))
()((()()))
()((())())
()((()))()
()(()(()))
()(()()())
()(()())()
()(())(())
()(())()()
()()((()))
()()(()())
()()(())()
()()()(())
()()()()()


翻译自: https://www.includehelp.com/icp/print-all-the-combinations-of-the-parenthesis.aspx

打印所有可能的括号组合

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值