--- title: 7A 括弧組合 tags: solution --- # A. 括弧組合 與2A題的概念相當,不過目的是在於將「所有的組合」都給生成出來。 可以利用簡單的DFS,將每個狀況皆列舉出來後,再判斷當前組合是否無法形成括弧組合的方式。 ```cpp= # include <bits/stdc++.h> using namespace std; void paren(int n, int l, string s){ if(l<0 || n<0) return; if(n == 0 && l == 0){ cout<<s<<'\n'; return; } paren(n-1, l+1, s+'('); paren(n-1, l-1, s+')'); } int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int n; while(cin>>n){ paren(n*2,0,""); cout<<'\n'; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up