-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathbrackets_recursion.cpp
More file actions
55 lines (55 loc) · 1.18 KB
/
brackets_recursion.cpp
File metadata and controls
55 lines (55 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<bits/stdc++.h>
#define lli long long int
#define ff first
#define ss second
#define vi vector<int>
#define REP(i,n) for(int i=0; i<n; i++)
using namespace std;
void solve(string op, int open , int close,vector<string>&v){
if(open==0 && close==0){
// cout<<op<<endl;
v.push_back(op);
return;
}
if(open==close){
op.push_back('(');
open=open-1;
solve(op,open,close,v);
}
else if(open==0 && close!=0){
op=op+')'; close--;
solve(op,open,close,v);
}
else if(open!=0 && close==0){
op=op+'(';
open--;
solve(op,open,close,v);
}
else{
string op1=op+'(';
int first1=open-1; int sec1=close;
string op2=op+')';
int first2=open; int sec2=close-1;
solve(op1,first1,sec1,v);
solve(op2,first2,sec2,v);
}
return;
}
void printvector(vector<string> v){
cout<<"we are inside printvector function"<<endl;
for(auto it:v){
cout<<it<<endl;
}
return;
}
int main(){
int n;
cin>>n;
int open,close;
string op="(";
open=n-1;
close=n;
vector<string> v;
solve(op,open,close,v);
printvector(v);
}