Module: Correct Bracket Sequence (RSP)


Problem

4 /6


PSP generation

Theory Click to read/hide

The generation of correct bracket sequences follows directly from the way the check is done - we just need to add new brackets without violating correctness. This is done by recursive iteration. If you don't know him - BE... ah, no, you can try to understand by reading further. Here is a code sample for one type of brackets:
 

#include <vector>
#include <iostream>

using namespace std;
int n; // Half length 
vector<char> ans; // Our answer 

void rec(int balance) {
if (ans.size() == 2 * n) { // If it does, then we're done < /span>
for (int i = 0; i < 2 * n; i++)
cout << ans[i] << " ";
cout << "\n";
}
if (ans.size() + balance + 2 <= n * 2) { // Check, we'll make it do we close the new opening brace 
// Now watch your hands: we don't need to make a separate vector for each sequence 
ans.push_back('(');
rec(balance + 1);
ans.pop_back(); // To understand this, you need to be aware of recursion. First, we add a parenthesis to the vector, and then we execute all this code again. 
// That is, add a parenthesis again, if we can. 
// And this will happen until we start to leave the recursion - that is, until we reach the desired length. 
// Then the brackets will start to be removed. If you understand this - I congratulate you, you are awesome. 
}
if (balance > 0) { // If we can close a bracket, we close it. 
ans.push_back(')');
rec(balance - 1);
ans.pop_back();
}
}

 int main()
{
cin>> n;
rec(0);

    return 0;
}
And now the time of difficulties - you will have to write the algorithm for several types of brackets YOURSELF! Muahahahahahahahahahahahah!

Problem

British Scientists, Inc.'s most innovative development is a way of finding a solution for any problem that can be solved using the tilde-omega-lambda calculus (that is, for none). To do this, they go through all possible bracket sequences of length x, where x is the first digit of a secret constant used in many of the company's developments. If x is odd, they just add one to it. They then use advanced algorithms using neuro-linguistic programming and Fibonacci computed Googold Catalan numbers to locate the terms. But these algorithms have already been implemented and patented. 

Your task is to implement the iteration algorithm. 


Input
The input is the first digit of the secret constant - x (\(1 <= x <= 9\)). 
 

Output
You need to output all spans of length x (or x+1 if \(x \% 2 ==1\)) in lexicographical order.

 

Examples
# Input Output
1 1
( )
[ ]
{}