Algorithm


A. Regular Bracket Sequences
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.

You are given an integer n. Your goal is to construct and print exactly n different regular bracket sequences of length 2n2�.

Input

The first line contains one integer t (1t501≤�≤50) — the number of test cases.

Each test case consists of one line containing one integer n (1n501≤�≤50).

Output

For each test case, print n lines, each containing a regular bracket sequence of length exactly 2n2�. All bracket sequences you output for a testcase should be different (though they may repeat in different test cases). If there are multiple answers, print any of them. It can be shown that it's always possible.

Example
input
Copy
3
3
1
3
output
Copy
()()()
((()))
(()())
()
((()))
(())()
()(())

 



 

 

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;

int main(){
  int t;
  cin>>t;
  int n;
  while(t--){
    cin>>n;
    char arr[2*n];
    for(int i = 0; i <2*n; i++){
      if(i % 2 == 0){
        arr[i] = '(';
        continue;
      }
      arr[i] = ')';
    }
    for(int i = 0; i <2*n; i++){
      cout<<arr[i];
    }
    cout<<endl;
    char temp;
    for(int i = 1; i <= n-1; i++){
      temp = arr[i];
      arr[i] = arr[2*n - 1 - i];
      arr[2*n - 1 - i] = temp;
      for(int j = 0; j < 2*n; j++){
        cout<<arr[j];
      }
      cout<<endl;
    }
  }
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
3
1
3

Output

x
+
cmd
()()()
((()))
(()())
()
((()))
(())()
()(())
Advertisements

Demonstration


Codeforcess Solution 1574-A A. Regular Bracket Sequences ,C++, Java, Js and Python  ,1574-A,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+