@jitendra
#include<iostream>
using namespace std;
void parens(int pairs)
{
int i, j, s, n = 2*(pairs - 1);
for (i = 0; i < 1 << n; i++) {
for (s = 1, j = 0; (j < n) && (s >= 0) && (s <= pairs); j++)
s += ((i >> j) & 1) ? 1 : -1;
if ((j != n) || (s != 1))
continue;
cout<<"(";
for (j = 0; j < n; j++)
((i >> j) & 1) ? putchar('(') : putchar(')');
cout<<")"<<endl;
}
}
int main()
{
parens(3);
cin.sync();
cin.get();
return 0;
}
On Thu, Jun 3, 2010 at 1:45 PM, Jitendra Kushwaha
<[email protected]>wrote:
> The question is that you have to print all the valid permutations of
> the given number of brackets
> for example for input 3 we have the output
>
> 1 ((()))
> 2 (()())
> 3 (())()
> 4 ()(())
> 5 ()()()
>
> total five valid permutation
>
> this can be solved in O( 2^(2n) ) where n is number of brackets .
> Algo will be like this
> 1. Try '(' and ')' in every position and print the correct
> permutation.Neglect all other permutation
>
> As catalon number is far less then exponential growth ,can anybody
> suggest some better algorithm
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
yezhu malai vaasa venkataramana Govinda Govinda
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.