#20465: 求救!


s009 (jessie)

學校 : 不指定學校
編號 : 87583
來源 : [140.118.82.10]
最後登入時間 :
2022-04-28 17:31:25
a229. 括號匹配問題 -- 名題精選百則 | From: [220.133.146.64] | 發表日期 : 2020-01-27 20:52

試了無數種解法,但一直都TLE

以下是我寫出來最快的程式碼,已經比一開始快很多了,但還是TLE@@

 

#include <bits/stdc++.h>
using namespace std;

int n;
string ans;
void dfs(string s, int l, int r){
    if(r == n){
        ans += s + '\n';
        return;
    }
    if(l < n) dfs(s + "(", l + 1, r);
    if(l > r) dfs(s + ")", l, r + 1);
    return;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    while(cin >> n) dfs("", 0, 0);
    cout << ans;
    return 0;
}

 
ZeroJudge Forum