あなたの問題はカタロニア語の数字を使用しています: Cn は、連想式を括弧で囲む方法の数です。これは、n+1 個の葉を持つ可能な完全二分木の数でもあります (完全二分木 = 各ノードが正確に 2 つの子を持つ木、または葉です)。
したがって、再帰を使用してすべての組み合わせを生成できます。
#include <iostream>
#include <string>
#include <vector>
typedef std::vector<std::string> str_vector;
// Combine u and v, and add the result to w
// u and v contains all possible combinations with respectively m and n - m letters
// So w will contain combinations with n letters
void combine(str_vector& w, const str_vector& u, const str_vector& v)
{
for(unsigned int t = 0; t < u.size(); t++)
for(unsigned int i = 0; i < v.size(); i++)
w.push_back("(" + u[t] + v[i] + ")");
}
// The function resolving our problem
// It output an array containing all possible combinations
// with n letters, starting with startLetter
str_vector problem(unsigned int n, char startLetter)
{
if(n == 1)
// Only one combination with one letter.
// Array with 1 string which contains only one character
return str_vector(1,std::string(1,startLetter));
else
{
str_vector w;
// Get all combinations for n-1 letters, and add them to w
for(unsigned int t = 1; t < n; t++)
combine(w, problem(t, startLetter), problem(n - t, startLetter + (char)t));
return w;
}
}
int main()
{
// Get all possible combinations with 4 letters
const str_vector& r = problem(4,'a');
// Print all solutions
for(unsigned int t = 0; t < r.size(); t++)
std::cout << r[t] << "\n";
}
このコードは、最速でも最適化されたものでもありません。でも、とても読みやすいと思います。
組み合わせの数については、式は次のとおりです。

これは次のように簡略化できます。
(2n)!
Cn = ---------
n!² (n+1)
Cn を計算すると、n+1 個の識別子を持つ組み合わせの数が得られます。ただし、解決策は提供されません。