1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include<bits/stdc++.h> using namespace std;
typedef pair<int, int> PII; typedef long long LL;
int n; vector<string> res;
void dfs(int n, int lcnt, int rcnt, string str) { if (lcnt == n && rcnt == n) res.push_back(str); else { if (lcnt < n) dfs(n, lcnt+1, rcnt, str+'(');
if (rcnt < n && lcnt > rcnt) dfs(n, lcnt, rcnt+1, str+')'); } }
int main() {
cin >> n;
dfs(n, 0, 0, ""); for (auto x : res) cout << x << endl; return 0; }
|