่งฃๆณไธใDFS
๐น ๅณ้ๅ่
ๅณ้ๅ่ string& s
่ไธๆฏๅณ้ๆดๅๅญไธฒ string s
้ฟๅ
ไธๅฟ
่ฆ็ๅญไธฒ่ค่ฃฝ
โ ๅฎๆดไปฃ็ขผ
่ฉๅ็ตๆ(ๅ่) ๏ผ AC (0.2s, 324KB)
#include <bits/stdc++.h>
using namespace std;
int n;
void dfs(int left, int right, string& s){
if(left+right == 2*n){
cout << s << "\n";
return;
}
if(left < n){
s[left+right] = '(';
dfs(left+1, right, s);
}
if(right < left){
s[left+right] = ')';
dfs(left, right+1, s);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
while(cin >> n){
string str(2*n, '_');
dfs(0, 0, str);
cout << "\n";
}
return 0;
}