Advertisement
oleg_drawer

Untitled

Apr 5th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int n;
  7. vector<char>arr(0);
  8.  
  9. void gen(int ind, int open){ // open : [ cnt of '(' ] - [ cnt of ')' ]
  10.     if(ind == n){
  11.         for(int i = 0; i < n; i++) cout<<arr[i];
  12.         cout << endl;
  13.         return;
  14.     }
  15.     if(n-ind-1 >= open+1){
  16.         arr[ind] = '(';
  17.         gen(ind+1, open+1);
  18.     }
  19.     if(open > 0){
  20.         arr[ind] = ')';
  21.         gen(ind+1, open-1);
  22.     }
  23. }
  24.  
  25.  
  26. int main(){
  27.  
  28. cin>>n;
  29. n*=2;
  30. arr.resize(n);
  31. gen(0,0);
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement