Backtracking Code with Advanced Recursion
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define int long long int
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
bool cmp(const pair<char, int>& a, const pair<char, int>& b) {
if (a.second != b.second) {
return a.second > b.second;
}
return a.first < b.first;
}
vector <string> valid;
void generate(string &s,int open, int close)
{
if(open==0 and close==0)
{
valid.push_back(s);
return;
}
if(open>0)
{
s.push_back('(');
generate(s,open-1,close);
s.pop_back();
}
if(close>0)
{
if(open<close)
{
s.push_back(')');
generate(s,open,close-1);
s.pop_back();
}
}
}
signed main() {
FAST
string s ="";
generate(s,2,2);
for(auto it:valid)
{
cout << it << nl;
}
return 0;
}
Comments
Post a Comment