Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- void nxt(string & s){
- int close=0;
- int open=0;
- int itr = (int)s.length();
- for(int i=0;i<(int)s.length();i++){
- if(s[i]=='('){
- if(open > close)
- itr = i;
- open++;
- } else {
- close++;
- }
- }
- if(itr == (int)s.length()){
- s="";
- } else {
- s[itr] = ')';
- open = close = 0;
- for(int i=0;i<=itr;i++){
- if(s[i]=='(') open++;
- else close++;
- }
- open = (int)s.length()/2 - open;
- close = (int)s.length()/2 - close;
- int i,j;
- for(i=itr+1,j=0;j<open;j++,i++){
- s[i]='(';
- }
- for(;i<(int)s.length();i++){
- s[i]=')';
- }
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);cin.tie(nullptr);
- int n;
- cin>>n;
- string s = string(n,'(') + string(n,')');
- do{
- cout<<s<<"\n";
- nxt(s);
- }while(s.length());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement