Last active
December 21, 2015 09:18
-
-
Save ygabo/6283798 to your computer and use it in GitHub Desktop.
Generate N proper pairs of parenthesis.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void parn( int index, int max, vector<set<string>> &all){ | |
string *me, *one, *two, *three; | |
set<string> x; | |
if (index < 0 || index >= max ) return; | |
else if (index == 0){ | |
string *d = new string("()"); | |
all[index].insert(*d); | |
parn(index+1, max, all); | |
} | |
else{ | |
// cout << index << endl; | |
for( auto i = all[0].begin(); i != all[0].end(); ++i) | |
//cout << *i << "-"; | |
for( auto i = all[index-1].begin(); i != all[index-1].end(); ++i){ | |
me = new string(*i); | |
// cout << "ME->" << *me << endl; | |
one = new string("(" + *me + ")"); | |
// cout << *one << endl; | |
two = new string(*me+ "()"); | |
// cout << *two << endl; | |
three = new string("()" +*me); | |
//cout << *three << endl; | |
x.insert( *one ); | |
// cout << "ONE PUSH " << endl; | |
x.insert( *two ); | |
// cout << "TWO PUSH " << endl; | |
x.insert( *three ); | |
//cout << "DONE PUSH " << endl; | |
} | |
all[index] = x; | |
//cout << "HERE" << endl; | |
parn(index+1, max, all); | |
} | |
} | |
int main(){ | |
int pairs = 3; | |
vector<set<string>> p(pairs, set<string>()); | |
parn(0, pairs, p); | |
for( auto i = p.begin(); i != p.end(); ++i){ | |
for( auto j = i->begin(); j != i->end(); ++j){ | |
cout << *j << " "; | |
} | |
cout << endl; | |
} | |
cout << endl <<"done" <<endl; | |
cin.get(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <math.h> | |
#include <map> | |
#include <fstream> | |
#include < ctime > | |
#include <algorithm> | |
#include <numeric> | |
#include <set> | |
#include <queue> | |
using namespace std; | |
void par(int left, int right, int &pairs, string &x){ | |
int level = left-right; | |
if ( level < 0 ) return; | |
if( x.length() > 0 && level == 0) cout << x << endl; | |
if ( left == pairs && right == pairs ){ | |
return; | |
} | |
string a = x+"("; | |
string b= x+")"; | |
if ( left < pairs ) | |
par(left+1, right, pairs, a); | |
par(left, right+1, pairs, b); | |
} | |
int main(){ | |
string d = ""; | |
int n = 4; | |
par(0,0,n,d); | |
cout << endl <<"done" <<endl; | |
cin.get(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment