Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void addParenthesis(char* str, const int& pos, const int& left, const int& right)
  6. {
  7.     if (left < 0 || right < left)
  8.         return;
  9.     if (left == 0 && right == 0)
  10.     {
  11.         cout << str << '\n';
  12.         return;
  13.     }
  14.     str[pos] = '(';
  15.     addParenthesis(str, pos + 1, left - 1, right);
  16.     str[pos] = ')';
  17.     addParenthesis(str, pos + 1, left, right - 1);
  18. }
  19.  
  20. int main()
  21. {
  22.     ios_base::sync_with_stdio(0);
  23.     cin.tie(0);
  24.  
  25.     int n = 0;
  26.     cin >> n;
  27.     char* str = new char[2*n+1];
  28.     str[2*n] = '\0';
  29.     addParenthesis(str, 0, n, n);
  30.  
  31.     delete []str;
  32.     return 0;
  33. }
  34.  
  35. /*
  36. 2
  37.  
  38. (())
  39. ()()
  40.  
  41.  
  42. 3
  43.  
  44. ((()))
  45. (()())
  46. (())()
  47. ()(())
  48. ()()()
  49. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement