#39886: C++解


316829316829316828z@gmail.com (WA&TLE為AC之母)

學校 : 不指定學校
編號 : 267947
來源 : [61.216.106.5]
最後登入時間 :
2024-05-16 11:40:14
a229. 括號匹配問題 -- 名題精選百則 | From: [203.68.192.201] | 發表日期 : 2024-04-09 15:24

#include <bits/stdc++.h>

using namespace std;

 

int main() {

        ios::sync_with_stdio(false);

        cin.tie(0);

        int n;

        while(cin >> n) {

                auto dfs = [&](auto self, string& s, int lhs, int rhs) -> void {

                        if(lhs < rhs || lhs > n) {

                                return;

                        }

                        if(lhs + rhs == 2 * n) {

                                cout << s << "\n";

                                return;

                        }

                        s[lhs + rhs] = '(';

                        self(self, s, lhs + 1, rhs);

                        s[lhs + rhs] = ')';

                        self(self, s, lhs, rhs + 1);

                };

                string s(2 * n, '_');

                dfs(dfs, s, 0, 0);

                cout << "\n";

        }

        return 0;

}

 
ZeroJudge Forum