Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.4 kB
14
Indexable
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define REP(i, b) for(int i = 0; i < b; i++)
#define PER(i, b) for(int i = b - 1; i >= 0; i--)
#define fi first
#define se second
#ifdef JASPER2
#include "debug.h"
#else
#define debug(...) 166
#endif

// #define int long long
using pii = pair < int, int >;
const int INF = 1e9;
const ll MOD = 998244353;
const int N = 2e5 + 5;

int n;
vector <string> ans;
string str;

bool isValid(string expr)
{
    // Declare a stack to hold the previous brackets.
    stack<char> temp;
    for (int i = 0; i < (int) expr.length(); i++) {
        if (temp.empty()) {
             
            // If the stack is empty 
            // just push the current bracket
            temp.push(expr[i]);
        }
        else if ((temp.top() == '(' && expr[i] == ')')
                 || (temp.top() == '{' && expr[i] == '}')
                 || (temp.top() == '[' && expr[i] == ']')) {
             
            // If we found any complete pair of bracket
            // then pop
            temp.pop();
        }
        else {
            temp.push(expr[i]);
        }
    }
    if (temp.empty()) {
         
        // If stack is empty return true
        return true;
    }
    return false;
}

void solve(int i, int opn, int cls) {
    if (i == n) {
        if (isValid(str))
            ans.push_back(str);
        return;
    }

    // Them dau ngoac mo truoc
    if (opn < n / 2) {
        for (auto x : {'(', '[', '{'}) {
            str.push_back(x);
            solve(i + 1, opn + 1, cls);
            str.pop_back();
        }
    }
    // Them ngoac dong
    if (cls < opn) {
        for (auto x : {')', ']', '}'}) {
            str.push_back(x);
            solve(i + 1, opn, cls + 1);
            str.pop_back();
        }
    }
}

void run_case() {
    cin >> n;
    if (n & 1) {
        cout << "NOT FOUND\n";
        return;
    }

    solve(0, 0, 0);
    sort(ans.begin(), ans.end());
    for (auto x : ans) cout << x << " ";
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    #ifdef JASPER2
        freopen("in1", "r", stdin);
    #endif

    int Test = 1;
    // cin >> Test;
    for (int test = 1; test <= Test; test++){

        run_case();
    }
}