Advertisement
999ms

Untitled

Apr 6th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void nxt(string & s){
  5.   int close=0;
  6.   int open=0;
  7.   int itr = (int)s.length();
  8.   for(int i=0;i<(int)s.length();i++){
  9.     if(s[i]=='('){
  10.       if(open > close)
  11.         itr = i;
  12.       open++;
  13.     } else {
  14.       close++;
  15.     }
  16.   }
  17.   if(itr == (int)s.length()){
  18.     s="";
  19.   } else {
  20.     s[itr] = ')';
  21.     open = close = 0;
  22.     for(int i=0;i<=itr;i++){
  23.       if(s[i]=='(') open++;
  24.       else close++;
  25.     }
  26.     open = (int)s.length()/2 - open;
  27.     close = (int)s.length()/2 - close;
  28.     int i,j;
  29.     for(i=itr+1,j=0;j<open;j++,i++){
  30.       s[i]='(';
  31.     }
  32.     for(;i<(int)s.length();i++){
  33.       s[i]=')';
  34.     }
  35.   }
  36. }
  37.  
  38. int main(){
  39.   ios_base::sync_with_stdio(false);cin.tie(nullptr);
  40.   int n;
  41.   cin>>n;
  42.   string s = string(n,'(') + string(n,')');
  43.   do{
  44.     cout<<s<<"\n";
  45.     nxt(s);
  46.   }while(s.length());
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement