/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 3ms 532.0 KiB
#5 Accepted 5ms 356.0 KiB
#6 Accepted 4ms 572.0 KiB
#7 Accepted 4ms 532.0 KiB
#8 Accepted 5ms 580.0 KiB
#9 Accepted 4ms 536.0 KiB
#10 Accepted 9ms 864.0 KiB
#11 Accepted 2ms 532.0 KiB
#12 Accepted 21ms 2.93 MiB
#13 Accepted 3ms 536.0 KiB
#14 Accepted 177ms 32.578 MiB
#15 Accepted 19ms 344.0 KiB

Code

/*
 *   Copyright (c) 2025 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
int n;
vector<string> v;
string s;
void bracket(int i,string &s,int cnt1,int cnt2)
{
    if(i==n)
    {
        v.push_back(s);
        //cout<<s<<'\n';
        return;
    }
    int rem = n-i-1;
    if(cnt1+cnt2+1 <= rem)
    {
        s.push_back('(');
        bracket(i+1,s,cnt1,cnt2+1);
        s.pop_back();
    }
    if(cnt2)
    {
        s.push_back(')');
        bracket(i+1,s,cnt1,cnt2-1);
        s.pop_back();
    }

    if(cnt1+cnt2+1 <= rem)
    {
        s.push_back('[');
        bracket(i+1,s,cnt1+1,cnt2);
        s.pop_back();
    }
    if(cnt1) 
    {
        s.push_back(']');
        bracket(i+1,s,cnt1-1,cnt2);
        s.pop_back();
    }
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n;
    bracket(0,s,0,0);
    sort(v.begin(),v.end());
    for(auto e:v) cout<<e<<'\n';
}

Information

Submit By
Type
Submission
Problem
P1170 Regular bracket sequence
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-22 19:18:50
Judged At
2025-02-22 19:18:50
Judged By
Score
100
Total Time
177ms
Peak Memory
32.578 MiB