输入n,打印n对括号中的全部有效组合。(即左右括号正确的配对)。
分析:水题,直接切。
1 /* 2 ID: y1197771 3 PROG: test 4 LANG: C++ 5 */ 6 #include<bits/stdc++.h> 7 #define pb push_back 8 #define FOR(i, n) for (int i = 0; i < (int)n; ++i) 9 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl 10 typedef long long ll; 11 using namespace std; 12 typedef pair<int, int> pii; 13 const int maxn = 1e3 + 10; 14 void dfs(int left, int right, string cur) { 15 if(left == 0 && right == 0) { 16 cout << cur << endl; 17 return; 18 } 19 if(left > 0) { 20 dfs(left - 1, right + 1, cur + "("); 21 } 22 if(right > 0) { 23 dfs(left, right - 1, cur + ")"); 24 } 25 } 26 void solve() { 27 int n; 28 while(cin >> n) { 29 dfs(n, 0, ""); 30 } 31 } 32 int main() { 33 //freopen("test.in", "r", stdin); 34 //freopen("test.out", "w", stdout); 35 solve(); 36 return 0; 37 }