void CParenthesisProblem::Test() { int n = 4; std::cout << "Combination for n = " << n << '\n'; PrintCombinations( n ); std::cout << '\n'; }
void CParenthesisProblem::PrintCombinations( int n ) { char * str = (char *) malloc( 2*n + 1 ); PrintCombinations( n, str, 0, 0 ); free( str ); }
int main(int argc, char** argv) { int num = 3; PrintBinaryAdj(num); PrintCombinationsAdj(num); PrintCombinations(num); return 0; }
void PrintCombinations(std::vector<std::vector<DataType>::size_type>& outputs, std::vector<DataType>::size_type startIdxIn, const std::vector<DataType>& arrayNoDup, std::vector<DataType>::size_type endIdxOut) { for (std::vector<DataType>::size_type idx=startIdxIn; idx<arrayNoDup.size(); ++idx) { outputs[endIdxOut++] = idx; PrintOut(outputs, 0, endIdxOut, arrayNoDup); PrintCombinations(outputs, idx+1, arrayNoDup, endIdxOut); --endIdxOut; } }
void CParenthesisProblem::PrintCombinations( int n, char * str, int open, int close ) { if ( ( open + close ) == 2*n ) { str[ 2*n ] = 0; std::cout << str << '\n'; return; } // check if "(" can be added if ( open < n ) { str[ open + close ] = '('; PrintCombinations( n, str, open + 1, close ); } // check if ")" can be added if ( close < open ) { str[ open + close ] = ')'; PrintCombinations( n, str, open , close + 1 ); } }
void PrintAllCombinations(const std::vector<DataType>& arrayNoDup) { std::vector<std::vector<DataType>::size_type> outputs(arrayNoDup.size()); PrintCombinations(outputs, 0, arrayNoDup, 0); }