Parenthesis Combination

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

/* Here
        p = Position;
        n = Full Parenthesis Number;
        l = Left Parenthesis;
        r =  Right Parenthesis;*/

void combination(int p, int n, int l, int r)
{
    static char s[N];
    if(r == n)
    {
        cout << s << endl;
        return;
    }
    else
    {
        if(l > r)
        {
            s[p] = ']';
            combination(p+1, n, l, r+1);
        }
        if(l < n)
        {
            s[p] = '[';
            combination(p+1, n, l+1, r);
        }
    }
}

void findcombination(int n)
{
    if(n > 0)
        combination(0, n, 0, 0);
}

int main()
{
    int n;
    while(cin >> n)
    {
        findcombination(n);
    }
    return 0;
}

Comments