[22]. 括号生成
- 题目
- 算法设计:回溯
- 算法设计:空间换时间
传送门:https://leetcode.cn/problems/generate-parentheses/
括号问题可以分成俩类:
搞懂回溯算法,最重要的是理解回溯框架。
回溯框架:
ans = []
void recall( 路径,选择列表 )
if 满足结束条件:
ans.add( 路径 )
return
for 选择 in 选择列表:
做选择
recall( 路径,选择列表 )
撤销选择
回溯框架,本质是遍历一颗决策树。
核心在于 for 循环里面的递归,在递归之前做选择,在递归之后撤销选择。
问题是,选择和撤销选择是在这颗树上做什么呢?
所以,选择的可视化是在进入树的某一节点前执行,撤销选择的可视化是在离开树的某一节点后执行。
做选择:在进入节点前,从选择列表拿出一个选择,将它放入路径。
撤销选择:在离开节点后,从路径中拿出一个选择,将它恢复到选择列表中。
套用回溯框架,打印所有括号:
int n;
void recall( int x, string& ans ) {
if( x == 2 * n )
print(ans);
return;
for c in ['(', ')']:
ans.push(c);
recall(x + 1, ans);
ans.pop(c);
}
不过,这题不是生成所有括号,而是生成所有合法括号。
那么合法的定义是什么?
对于 2n
个括号,必然有 n
个左括号、n
个右括号
这里把穷举位置 x
,改成用 l
记录还可以使用多少个左括号,用 r
记录还可以使用多少个右括号。
class Solution {
vector<string> res; // 记录答案
public:
vector<string> generateParenthesis(int n) {
if( n == 0 ) return res;
recall(n , n, ""); // 当前可用左括号和右括号数量为 n
return res;
}
void recall(int l, int r, string str) {
if( l < 0 || r < 0 ) return; // 剪枝:数量小于0,不合法
if( r < l ) return; // 剪枝:左括号剩下的多,不合法
if( l == 0 && r == 0 ) { // 所有括号恰好用完,得到一个合法括号
res.push_back(str);
return;
}
str.push_back('('); // 选择左括号
recall(l-1, r, str); // 进入下一层
str.pop_back(); // 撤销选择
str.push_back(')');
recall(l, r-1, str);
str.pop_back();
}
};
回溯算法的时间复杂度太高了。
因为这题的输入数据 < 8,所以可以采用空间换时间策略。
class Solution {
public:
vector<string> generateParenthesis(int n) {
switch(n){
case 1:
return vector<string>{"()"};
case 2:
return vector<string>{"()()","(())"};
case 3:
return vector<string>{"((()))","(()())","(())()","()(())","()()()"};
case 4:
return vector<string>{"(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"};
case 5:
return vector<string>{"((((()))))","(((()())))","(((())()))","(((()))())","(((())))()","((()(())))","((()()()))","((()())())","((()()))()","((())(()))","((())()())","((())())()","((()))(())","((()))()()","(()((())))","(()(()()))","(()(())())","(()(()))()","(()()(()))","(()()()())","(()()())()","(()())(())","(()())()()","(())((()))","(())(()())","(())(())()","(())()(())","(())()()()","()(((())))","()((()()))","()((())())","()((()))()","()(()(()))","()(()()())","()(()())()","()(())(())","()(())()()","()()((()))","()()(()())","()()(())()","()()()(())","()()()()()"};
case 6:
return vector<string>{"(((((())))))","((((()()))))","((((())())))","((((()))()))","((((())))())","((((()))))()","(((()(()))))","(((()()())))","(((()())()))","(((()()))())","(((()())))()","(((())(())))","(((())()()))","(((())())())","(((())()))()","(((()))(()))","(((()))()())","(((()))())()","(((())))(())","(((())))()()","((()((()))))","((()(()())))","((()(())()))","((()(()))())","((()(())))()","((()()(())))","((()()()()))","((()()())())","((()()()))()","((()())(()))","((()())()())","((()())())()","((()()))(())","((()()))()()","((())((())))","((())(()()))","((())(())())","((())(()))()","((())()(()))","((())()()())","((())()())()","((())())(())","((())())()()","((()))((()))","((()))(()())","((()))(())()","((()))()(())","((()))()()()","(()(((()))))","(()((()())))","(()((())()))","(()((()))())","(()((())))()","(()(()(())))","(()(()()()))","(()(()())())","(()(()()))()","(()(())(()))","(()(())()())","(()(())())()","(()(()))(())","(()(()))()()","(()()((())))","(()()(()()))","(()()(())())","(()()(()))()","(()()()(()))","(()()()()())","(()()()())()","(()()())(())","(()()())()()","(()())((()))","(()())(()())","(()())(())()","(()())()(())","(()())()()()","(())(((())))","(())((()()))","(())((())())","(())((()))()","(())(()(()))","(())(()()())","(())(()())()","(())(())(())","(())(())()()","(())()((()))","(())()(()())","(())()(())()","(())()()(())","(())()()()()","()((((()))))","()(((()())))","()(((())()))","()(((()))())","()(((())))()","()((()(())))","()((()()()))","()((()())())","()((()()))()","()((())(()))","()((())()())","()((())())()","()((()))(())","()((()))()()","()(()((())))","()(()(()()))","()(()(())())","()(()(()))()","()(()()(()))","()(()()()())","()(()()())()","()(()())(())","()(()())()()","()(())((()))","()(())(()())","()(())(())()","()(())()(())","()(())()()()","()()(((())))","()()((()()))","()()((())())","()()((()))()","()()(()(()))","()()(()()())","()()(()())()","()()(())(())","()()(())()()","()()()((()))","()()()(()())","()()()(())()","()()()()(())","()()()()()()"};
case 7:
return vector<string>{"((((((()))))))","(((((()())))))","(((((())()))))","(((((()))())))","(((((())))()))","(((((()))))())","(((((())))))()","((((()(())))))","((((()()()))))","((((()())())))","((((()()))()))","((((()())))())","((((()()))))()","((((())(()))))","((((())()())))","((((())())()))","((((())()))())","((((())())))()","((((()))(())))","((((()))()()))","((((()))())())","((((()))()))()","((((())))(()))","((((())))()())","((((())))())()","((((()))))(())","((((()))))()()","(((()((())))))","(((()(()()))))","(((()(())())))","(((()(()))()))","(((()(())))())","(((()(()))))()","(((()()(()))))","(((()()()())))","(((()()())()))","(((()()()))())","(((()()())))()","(((()())(())))","(((()())()()))","(((()())())())","(((()())()))()","(((()()))(()))","(((()()))()())","(((()()))())()","(((()())))(())","(((()())))()()","(((())((()))))","(((())(()())))","(((())(())()))","(((())(()))())","(((())(())))()","(((())()(())))","(((())()()()))","(((())()())())","(((())()()))()","(((())())(()))","(((())())()())","(((())())())()","(((())()))(())","(((())()))()()","(((()))((())))","(((()))(()()))","(((()))(())())","(((()))(()))()","(((()))()(()))","(((()))()()())","(((()))()())()","(((()))())(())","(((()))())()()","(((())))((()))","(((())))(()())","(((())))(())()","(((())))()(())","(((())))()()()","((()(((())))))","((()((()()))))","((()((())())))","((()((()))()))","((()((())))())","((()((()))))()","((()(()(()))))","((()(()()())))","((()(()())()))","((()(()()))())","((()(()())))()","((()(())(())))","((()(())()()))","((()(())())())","((()(())()))()","((()(()))(()))","((()(()))()())","((()(()))())()","((()(())))(())","((()(())))()()","((()()((()))))","((()()(()())))","((()()(())()))","((()()(()))())","((()()(())))()","((()()()(())))","((()()()()()))","((()()()())())","((()()()()))()","((()()())(()))","((()()())()())","((()()())())()","((()()()))(())","((()()()))()()","((()())((())))","((()())(()()))","((()())(())())","((()())(()))()","((()())()(()))","((()())()()())","((()())()())()","((()())())(())","((()())())()()","((()()))((()))","((()()))(()())","((()()))(())()","((()()))()(())","((()()))()()()","((())(((()))))","((())((()())))","((())((())()))","((())((()))())","((())((())))()","((())(()(())))","((())(()()()))","((())(()())())","((())(()()))()","((())(())(()))","((())(())()())","((())(())())()","((())(()))(())","((())(()))()()","((())()((())))","((())()(()()))","((())()(())())","((())()(()))()","((())()()(()))","((())()()()())","((())()()())()","((())()())(())","((())()())()()","((())())((()))","((())())(()())","((())())(())()","((())())()(())","((())())()()()","((()))(((())))","((()))((()()))","((()))((())())","((()))((()))()","((()))(()(()))","((()))(()()())","((()))(()())()","((()))(())(())","((()))(())()()","((()))()((()))","((()))()(()())","((()))()(())()","((()))()()(())","((()))()()()()","(()((((())))))","(()(((()()))))","(()(((())())))","(()(((()))()))","(()(((())))())","(()(((()))))()","(()((()(()))))","(()((()()())))","(()((()())()))","(()((()()))())","(()((()())))()","(()((())(())))","(()((())()()))","(()((())())())","(()((())()))()","(()((()))(()))","(()((()))()())","(()((()))())()","(()((())))(())","(()((())))()()","(()(()((()))))","(()(()(()())))","(()(()(())()))","(()(()(()))())","(()(()(())))()","(()(()()(())))","(()(()()()()))","(()(()()())())","(()(()()()))()","(()(()())(()))","(()(()())()())","(()(()())())()","(()(()()))(())","(()(()()))()()","(()(())((())))","(()(())(()()))","(()(())(())())","(()(())(()))()","(()(())()(()))","(()(())()()())","(()(())()())()","(()(())())(())","(()(())())()()","(()(()))((()))","(()(()))(()())","(()(()))(())()","(()(()))()(())","(()(()))()()()","(()()(((()))))","(()()((()())))","(()()((())()))","(()()((()))())","(()()((())))()","(()()(()(())))","(()()(()()()))","(()()(()())())","(()()(()()))()","(()()(())(()))","(()()(())()())","(()()(())())()","(()()(()))(())","(()()(()))()()","(()()()((())))","(()()()(()()))","(()()()(())())","(()()()(()))()","(()()()()(()))","(()()()()()())","(()()()()())()","(()()()())(())","(()()()())()()","(()()())((()))","(()()())(()())","(()()())(())()","(()()())()(())","(()()())()()()","(()())(((())))","(()())((()()))","(()())((())())","(()())((()))()","(()())(()(()))","(()())(()()())","(()())(()())()","(()())(())(())","(()())(())()()","(()())()((()))","(()())()(()())","(()())()(())()","(()())()()(())","(()())()()()()","(())((((()))))","(())(((()())))","(())(((())()))","(())(((()))())","(())(((())))()","(())((()(())))","(())((()()()))","(())((()())())","(())((()()))()","(())((())(()))","(())((())()())","(())((())())()","(())((()))(())","(())((()))()()","(())(()((())))","(())(()(()()))","(())(()(())())","(())(()(()))()","(())(()()(()))","(())(()()()())","(())(()()())()","(())(()())(())","(())(()())()()","(())(())((()))","(())(())(()())","(())(())(())()","(())(())()(())","(())(())()()()","(())()(((())))","(())()((()()))","(())()((())())","(())()((()))()","(())()(()(()))","(())()(()()())","(())()(()())()","(())()(())(())","(())()(())()()","(())()()((()))","(())()()(()())","(())()()(())()","(())()()()(())","(())()()()()()","()(((((())))))","()((((()()))))","()((((())())))","()((((()))()))","()((((())))())","()((((()))))()","()(((()(()))))","()(((()()())))","()(((()())()))","()(((()()))())","()(((()())))()","()(((())(())))","()(((())()()))","()(((())())())","()(((())()))()","()(((()))(()))","()(((()))()())","()(((()))())()","()(((())))(())","()(((())))()()","()((()((()))))","()((()(()())))","()((()(())()))","()((()(()))())","()((()(())))()","()((()()(())))","()((()()()()))","()((()()())())","()((()()()))()","()((()())(()))","()((()())()())","()((()())())()","()((()()))(())","()((()()))()()","()((())((())))","()((())(()()))","()((())(())())","()((())(()))()","()((())()(()))","()((())()()())","()((())()())()","()((())())(())","()((())())()()","()((()))((()))","()((()))(()())","()((()))(())()","()((()))()(())","()((()))()()()","()(()(((()))))","()(()((()())))","()(()((())()))","()(()((()))())","()(()((())))()","()(()(()(())))","()(()(()()()))","()(()(()())())","()(()(()()))()","()(()(())(()))","()(()(())()())","()(()(())())()","()(()(()))(())","()(()(()))()()","()(()()((())))","()(()()(()()))","()(()()(())())","()(()()(()))()","()(()()()(()))","()(()()()()())","()(()()()())()","()(()()())(())","()(()()())()()","()(()())((()))","()(()())(()())","()(()())(())()","()(()())()(())","()(()())()()()","()(())(((())))","()(())((()()))","()(())((())())","()(())((()))()","()(())(()(()))","()(())(()()())","()(())(()())()","()(())(())(())","()(())(())()()","()(())()((()))","()(())()(()())","()(())()(())()","()(())()()(())","()(())()()()()","()()((((()))))","()()(((()())))","()()(((())()))","()()(((()))())","()()(((())))()","()()((()(())))","()()((()()()))","()()((()())())","()()((()()))()","()()((())(()))","()()((())()())","()()((())())()","()()((()))(())","()()((()))()()","()()(()((())))","()()(()(()()))","()()(()(())())","()()(()(()))()","()()(()()(()))","()()(()()()())","()()(()()())()","()()(()())(())","()()(()())()()","()()(())((()))","()()(())(()())","()()(())(())()","()()(())()(())","()()(())()()()","()()()(((())))","()()()((()()))","()()()((())())","()()()((()))()","()()()(()(()))","()()()(()()())","()()()(()())()","()()()(())(())","()()()(())()()","()()()()((()))","()()()()(()())","()()()()(())()","()()()()()(())","()()()()()()()"};
case 8:
return vector<string>{"(((((((())))))))","((((((()()))))))","((((((())())))))","((((((()))()))))","((((((())))())))","((((((()))))()))","((((((())))))())","((((((()))))))()","(((((()(()))))))","(((((()()())))))","(((((()())()))))","(((((()()))())))","(((((()())))()))","(((((()()))))())","(((((()())))))()","(((((())(())))))","(((((())()()))))","(((((())())())))","(((((())()))()))","(((((())())))())","(((((())()))))()","(((((()))(()))))","(((((()))()())))","(((((()))())()))","(((((()))()))())","(((((()))())))()","(((((())))(())))","(((((())))()()))","(((((())))())())","(((((())))()))()","(((((()))))(()))","(((((()))))()())","(((((()))))())()","(((((())))))(())","(((((())))))()()","((((()((()))))))","((((()(()())))))","((((()(())()))))","((((()(()))())))","((((()(())))()))","((((()(()))))())","((((()(())))))()","((((()()(())))))","((((()()()()))))","((((()()())())))","((((()()()))()))","((((()()())))())","((((()()()))))()","((((()())(()))))","((((()())()())))","((((()())())()))","((((()())()))())","((((()())())))()","((((()()))(())))","((((()()))()()))","((((()()))())())","((((()()))()))()","((((()())))(()))","((((()())))()())","((((()())))())()","((((()()))))(())","((((()()))))()()","((((())((())))))","((((())(()()))))","((((())(())())))","((((())(()))()))","((((())(())))())","((((())(()))))()","((((())()(()))))","((((())()()())))","((((())()())()))","((((())()()))())","((((())()())))()","((((())())(())))","((((())())()()))","((((())())())())","((((())())()))()","((((())()))(()))","((((())()))()())","((((())()))())()","((((())())))(())","((((())())))()()","((((()))((()))))","((((()))(()())))","((((()))(())()))","((((()))(()))())","((((()))(())))()","((((()))()(())))","((((()))()()()))","((((()))()())())","((((()))()()))()","((((()))())(()))","((((()))())()())","((((()))())())()","((((()))()))(())","((((()))()))()()","((((())))((())))","((((())))(()()))","((((())))(())())","((((())))(()))()","((((())))()(()))","((((())))()()())","((((())))()())()","((((())))())(())","((((())))())()()","((((()))))((()))","((((()))))(()())","((((()))))(())()","((((()))))()(())","((((()))))()()()","(((()(((()))))))","(((()((()())))))","(((()((())()))))","(((()((()))())))","(((()((())))()))","(((()((()))))())","(((()((())))))()","(((()(()(())))))","(((()(()()()))))","(((()(()())())))","(((()(()()))()))","(((()(()())))())","(((()(()()))))()","(((()(())(()))))","(((()(())()())))","(((()(())())()))","(((()(())()))())","(((()(())())))()","(((()(()))(())))","(((()(()))()()))","(((()(()))())())","(((()(()))()))()","(((()(())))(()))","(((()(())))()())","(((()(())))())()","(((()(()))))(())","(((()(()))))()()","(((()()((())))))","(((()()(()()))))","(((()()(())())))","(((()()(()))()))","(((()()(())))())","(((()()(()))))()","(((()()()(()))))","(((()()()()())))","(((()()()())()))","(((()()()()))())","(((()()()())))()","(((()()())(())))","(((()()())()()))","(((()()())())())","(((()()())()))()","(((()()()))(()))","(((()()()))()())","(((()()()))())()","(((()()())))(())","(((()()())))()()","(((()())((()))))","(((()())(()())))","(((()())(())()))","(((()())(()))())","(((()())(())))()","(((()())()(())))","(((()())()()()))","(((()())()())())","(((()())()()))()","(((()())())(()))","(((()())())()())","(((()())())())()","(((()())()))(())","(((()())()))()()","(((()()))((())))","(((()()))(()()))","(((()()))(())())","(((()()))(()))()","(((()()))()(()))","(((()()))()()())","(((()()))()())()","(((()()))())(())","(((()()))())()()","(((()())))((()))","(((()())))(()())","(((()())))(())()","(((()())))()(())","(((()())))()()()","(((())(((())))))","(((())((()()))))","(((())((())())))","(((())((()))()))","(((())((())))())","(((())((()))))()","(((())(()(()))))","(((())(()()())))","(((())(()())()))","(((())(()()))())","(((())(()())))()","(((())(())(())))","(((())(())()()))","(((())(())())())","(((())(())()))()","(((())(()))(()))","(((())(()))()())","(((())(()))())()","(((())(())))(())","(((())(())))()()","(((())()((()))))","(((())()(()())))","(((())()(())()))","(((())()(()))())","(((())()(())))()","(((())()()(())))","(((())()()()()))","(((())()()())())","(((())()()()))()","(((())()())(()))","(((())()())()())","(((())()())())()","(((())()()))(())","(((())()()))()()","(((())())((())))","(((())())(()()))","(((())())(())())","(((())())(()))()","(((())())()(()))","(((())())()()())","(((())())()())()","(((())())())(())","(((())())())()()","(((())()))((()))","(((())()))(()())","(((())()))(())()","(((())()))()(())","(((())()))()()()","(((()))(((()))))","(((()))((()())))","(((()))((())()))","(((()))((()))())","(((()))((())))()","(((()))(()(())))","(((()))(()()()))","(((()))(()())())","(((()))(()()))()","(((()))(())(()))","(((()))(())()())","(((()))(())())()","(((()))(()))(())","(((()))(()))()()","(((()))()((())))","(((()))()(()()))","(((()))()(())())","(((()))()(()))()","(((()))()()(()))","(((()))()()()())","(((()))()()())()","(((()))()())(())","(((()))()())()()","(((()))())((()))","(((()))())(()())","(((()))())(())()","(((()))())()(())","(((()))())()()()","(((())))(((())))","(((())))((()()))","(((())))((())())","(((())))((()))()","(((())))(()(()))","(((())))(()()())","(((())))(()())()","(((())))(())(())","(((())))(())()()","(((())))()((()))","(((())))()(()())","(((())))()(())()","(((())))()()(())","(((())))()()()()","((()((((()))))))","((()(((()())))))","((()(((())()))))","((()(((()))())))","((()(((())))()))","((()(((()))))())","((()(((())))))()","((()((()(())))))","((()((()()()))))","((()((()())())))","((()((()()))()))","((()((()())))())","((()((()()))))()","((()((())(()))))","((()((())()())))","((()((())())()))","((()((())()))())","((()((())())))()","((()((()))(())))","((()((()))()()))","((()((()))())())","((()((()))()))()","((()((())))(()))","((()((())))()())","((()((())))())()","((()((()))))(())","((()((()))))()()","((()(()((())))))","((()(()(()()))))","((()(()(())())))","((()(()(()))()))","((()(()(())))())","((()(()(()))))()","((()(()()(()))))","((()(()()()())))","((()(()()())()))","((()(()()()))())","((()(()()())))()","((()(()())(())))","((()(()())()()))","((()(()())())())","((()(()())()))()","((()(()()))(()))","((()(()()))()())","((()(()()))())()","((()(()())))(())","((()(()())))()()","((()(())((()))))","((()(())(()())))","((()(())(())()))","((()(())(()))())","((()(())(())))()","((()(())()(())))","((()(())()()()))","((()(())()())())","((()(())()()))()","((()(())())(()))","((()(())())()())","((()(())())())()","((()(())()))(())","((()(())()))()()","((()(()))((())))","((()(()))(()()))","((()(()))(())())","((()(()))(()))()","((()(()))()(()))","((()(()))()()())","((()(()))()())()","((()(()))())(())","((()(()))())()()","((()(())))((()))","((()(())))(()())","((()(())))(())()","((()(())))()(())","((()(())))()()()","((()()(((())))))","((()()((()()))))","((()()((())())))","((()()((()))()))","((()()((())))())","((()()((()))))()","((()()(()(()))))","((()()(()()())))","((()()(()())()))","((()()(()()))())","((()()(()())))()","((()()(())(())))","((()()(())()()))","((()()(())())())","((()()(())()))()","((()()(()))(()))","((()()(()))()())","((()()(()))())()","((()()(())))(())","((()()(())))()()","((()()()((()))))","((()()()(()())))","((()()()(())()))","((()()()(()))())","((()()()(())))()","((()()()()(())))","((()()()()()()))","((()()()()())())","((()()()()()))()","((()()()())(()))","((()()()())()())","((()()()())())()","((()()()()))(())","((()()()()))()()","((()()())((())))","((()()())(()()))","((()()())(())())","((()()())(()))()","((()()())()(()))","((()()())()()())","((()()())()())()","((()()())())(())","((()()())())()()","((()()()))((()))","((()()()))(()())","((()()()))(())()","((()()()))()(())","((()()()))()()()","((()())(((()))))","((()())((()())))","((()())((())()))","((()())((()))())","((()())((())))()","((()())(()(())))","((()())(()()()))","((()())(()())())","((()())(()()))()","((()())(())(()))","((()())(())()())","((()())(())())()","((()())(()))(())","((()())(()))()()","((()())()((())))","((()())()(()()))","((()())()(())())","((()())()(()))()","((()())()()(()))","((()())()()()())","((()())()()())()","((()())()())(())","((()())()())()()","((()())())((()))","((()())())(()())","((()())())(())()","((()())())()(())","((()())())()()()","((()()))(((())))","((()()))((()()))","((()()))((())())","((()()))((()))()","((()()))(()(()))","((()()))(()()())","((()()))(()())()","((()()))(())(())","((()()))(())()()","((()()))()((()))","((()()))()(()())","((()()))()(())()","((()()))()()(())","((()()))()()()()","((())((((())))))","((())(((()()))))","((())(((())())))","((())(((()))()))","((())(((())))())","((())(((()))))()","((())((()(()))))","((())((()()())))","((())((()())()))","((())((()()))())","((())((()())))()","((())((())(())))","((())((())()()))","((())((())())())","((())((())()))()","((())((()))(()))","((())((()))()())","((())((()))())()","((())((())))(())","((())((())))()()","((())(()((()))))","((())(()(()())))","((())(()(())()))","((())(()(()))())","((())(()(())))()","((())(()()(())))","((())(()()()()))","((())(()()())())","((())(()()()))()","((())(()())(()))","((())(()())()())","((())(()())())()","((())(()()))(())","((())(()()))()()","((())(())((())))","((())(())(()()))","((())(())(())())","((())(())(()))()","((())(())()(()))","((())(())()()())","((())(())()())()","((())(())())(())","((())(())())()()","((())(()))((()))","((())(()))(()())","((())(()))(())()","((())(()))()(())","((())(()))()()()","((())()(((()))))","((())()((()())))","((())()((())()))","((())()((()))())","((())()((())))()","((())()(()(())))","((())()(()()()))","((())()(()())())","((())()(()()))()","((())()(())(()))","((())()(())()())","((())()(())())()","((())()(()))(())","((())()(()))()()","((())()()((())))","((())()()(()()))","((())()()(())())","((())()()(()))()","((())()()()(()))","((())()()()()())","((())()()()())()","((())()()())(())","((())()()())()()","((())()())((()))","((())()())(()())","((())()())(())()","((())()())()(())","((())()())()()()","((())())(((())))","((())())((()()))","((())())((())())","((())())((()))()","((())())(()(()))","((())())(()()())","((())())(()())()","((())())(())(())","((())())(())()()","((())())()((()))","((())())()(()())","((())())()(())()","((())())()()(())","((())())()()()()","((()))((((()))))","((()))(((()())))","((()))(((())()))","((()))(((()))())","((()))(((())))()","((()))((()(())))","((()))((()()()))","((()))((()())())","((()))((()()))()","((()))((())(()))","((()))((())()())","((()))((())())()","((()))((()))(())","((()))((()))()()","((()))(()((())))","((()))(()(()()))","((()))(()(())())","((()))(()(()))()","((()))(()()(()))","((()))(()()()())","((()))(()()())()","((()))(()())(())","((()))(()())()()","((()))(())((()))","((()))(())(()())","((()))(())(())()","((()))(())()(())","((()))(())()()()","((()))()(((())))","((()))()((()()))","((()))()((())())","((()))()((()))()","((()))()(()(()))","((()))()(()()())","((()))()(()())()","((()))()(())(())","((()))()(())()()","((()))()()((()))","((()))()()(()())","((()))()()(())()","((()))()()()(())","((()))()()()()()","(()(((((()))))))","(()((((()())))))","(()((((())()))))","(()((((()))())))","(()((((())))()))","(()((((()))))())","(()((((())))))()","(()(((()(())))))","(()(((()()()))))","(()(((()())())))","(()(((()()))()))","(()(((()())))())","(()(((()()))))()","(()(((())(()))))","(()(((())()())))","(()(((())())()))","(()(((())()))())","(()(((())())))()","(()(((()))(())))","(()(((()))()()))","(()(((()))())())","(()(((()))()))()","(()(((())))(()))","(()(((())))()())","(()(((())))())()","(()(((()))))(())","(()(((()))))()()","(()((()((())))))","(()((()(()()))))","(()((()(())())))","(()((()(()))()))","(()((()(())))())","(()((()(()))))()","(()((()()(()))))","(()((()()()())))","(()((()()())()))","(()((()()()))())","(()((()()())))()","(()((()())(())))","(()((()())()()))","(()((()())())())","(()((()())()))()","(()((()()))(()))","(()((()()))()())","(()((()()))())()","(()((()())))(())","(()((()())))()()","(()((())((()))))","(()((())(()())))","(()((())(())()))","(()((())(()))())","(()((())(())))()","(()((())()(())))","(()((())()()()))","(()((())()())())","(()((())()()))()","(()((())())(()))","(()((())())()())","(()((())())())()","(()((())()))(())","(()((())()))()()","(()((()))((())))","(()((()))(()()))","(()((()))(())())","(()((()))(()))()","(()((()))()(()))","(()((()))()()())","(()((()))()())()","(()((()))())(())","(()((()))())()()","(()((())))((()))","(()((())))(()())","(()((())))(())()","(()((())))()(())","(()((())))()()()","(()(()(((())))))","(()(()((()()))))","(()(()((())())))","(()(()((()))()))","(()(()((())))())","(()(()((()))))()","(()(()(()(()))))","(()(()(()()())))","(()(()(()())()))","(()(()(()()))())","(()(()(()())))()","(()(()(())(())))","(()(()(())()()))","(()(()(())())())","(()(()(())()))()","(()(()(()))(()))","(()(()(()))()())","(()(()(()))())()","(()(()(())))(())","(()(()(())))()()","(()(()()((()))))","(()(()()(()())))","(()(()()(())()))","(()(()()(()))())","(()(()()(())))()","(()(()()()(())))","(()(()()()()()))","(()(()()()())())","(()(()()()()))()","(()(()()())(()))","(()(()()())()())","(()(()()())())()","(()(()()()))(())","(()(()()()))()()","(()(()())((())))","(()(()())(()()))","(()(()())(())())","(()(()())(()))()","(()(()())()(()))","(()(()())()()())","(()(()())()())()","(()(()())())(())","(()(()())())()()","(()(()()))((()))","(()(()()))(()())","(()(()()))(())()","(()(()()))()(())","(()(()()))()()()","(()(())(((()))))","(()(())((()())))","(()(())((())()))","(()(())((()))())","(()(())((())))()","(()(())(()(())))","(()(())(()()()))","(()(())(()())())","(()(())(()()))()","(()(())(())(()))","(()(())(())()())","(()(())(())())()","(()(())(()))(())","(()(())(()))()()","(()(())()((())))","(()(())()(()()))","(()(())()(())())","(()(())()(()))()","(()(())()()(()))","(()(())()()()())","(()(())()()())()","(()(())()())(())","(()(())()())()()","(()(())())((()))","(()(())())(()())","(()(())())(())()","(()(())())()(())","(()(())())()()()","(()(()))(((())))","(()(()))((()()))","(()(()))((())())","(()(()))((()))()","(()(()))(()(()))","(()(()))(()()())","(()(()))(()())()","(()(()))(())(())","(()(()))(())()()","(()(()))()((()))","(()(()))()(()())","(()(()))()(())()","(()(()))()()(())","(()(()))()()()()","(()()((((())))))","(()()(((()()))))","(()()(((())())))","(()()(((()))()))","(()()(((())))())","(()()(((()))))()","(()()((()(()))))","(()()((()()())))","(()()((()())()))","(()()((()()))())","(()()((()())))()","(()()((())(())))","(()()((())()()))","(()()((())())())","(()()((())()))()","(()()((()))(()))","(()()((()))()())","(()()((()))())()","(()()((())))(())","(()()((())))()()","(()()(()((()))))","(()()(()(()())))","(()()(()(())()))","(()()(()(()))())","(()()(()(())))()","(()()(()()(())))","(()()(()()()()))","(()()(()()())())","(()()(()()()))()","(()()(()())(()))","(()()(()())()())","(()()(()())())()","(()()(()()))(())","(()()(()()))()()","(()()(())((())))","(()()(())(()()))","(()()(())(())())","(()()(())(()))()","(()()(())()(()))","(()()(())()()())","(()()(())()())()","(()()(())())(())","(()()(())())()()","(()()(()))((()))","(()()(()))(()())","(()()(()))(())()","(()()(()))()(())","(()()(()))()()()","(()()()(((()))))","(()()()((()())))","(()()()((())()))","(()()()((()))())","(()()()((())))()","(()()()(()(())))","(()()()(()()()))","(()()()(()())())","(()()()(()()))()","(()()()(())(()))","(()()()(())()())","(()()()(())())()","(()()()(()))(())","(()()()(()))()()","(()()()()((())))","(()()()()(()()))","(()()()()(())())","(()()()()(()))()","(()()()()()(()))","(()()()()()()())","(()()()()()())()","(()()()()())(())","(()()()()())()()","(()()()())((()))","(()()()())(()())","(()()()())(())()","(()()()())()(())","(()()()())()()()","(()()())(((())))","(()()())((()()))","(()()())((())())","(()()())((()))()","(()()())(()(()))","(()()())(()()())","(()()())(()())()","(()()())(())(())","(()()())(())()()","(()()())()((()))","(()()())()(()())","(()()())()(())()","(()()())()()(())","(()()())()()()()","(()())((((()))))","(()())(((()())))","(()())(((())()))","(()())(((()))())","(()())(((())))()","(()())((()(())))","(()())((()()()))","(()())((()())())","(()())((()()))()","(()())((())(()))","(()())((())()())","(()())((())())()","(()())((()))(())","(()())((()))()()","(()())(()((())))","(()())(()(()()))","(()())(()(())())","(()())(()(()))()","(()())(()()(()))","(()())(()()()())","(()())(()()())()","(()())(()())(())","(()())(()())()()","(()())(())((()))","(()())(())(()())","(()())(())(())()","(()())(())()(())","(()())(())()()()","(()())()(((())))","(()())()((()()))","(()())()((())())","(()())()((()))()","(()())()(()(()))","(()())()(()()())","(()())()(()())()","(()())()(())(())","(()())()(())()()","(()())()()((()))","(()())()()(()())","(()())()()(())()","(()())()()()(())","(()())()()()()()","(())(((((())))))","(())((((()()))))","(())((((())())))","(())((((()))()))","(())((((())))())","(())((((()))))()","(())(((()(()))))","(())(((()()())))","(())(((()())()))","(())(((()()))())","(())(((()())))()","(())(((())(())))","(())(((())()()))","(())(((())())())","(())(((())()))()","(())(((()))(()))","(())(((()))()())","(())(((()))())()","(())(((())))(())","(())(((())))()()","(())((()((()))))","(())((()(()())))","(())((()(())()))","(())((()(()))())","(())((()(())))()","(())((()()(())))","(())((()()()()))","(())((()()())())","(())((()()()))()","(())((()())(()))","(())((()())()())","(())((()())())()","(())((()()))(())","(())((()()))()()","(())((())((())))","(())((())(()()))","(())((())(())())","(())((())(()))()","(())((())()(()))","(())((())()()())","(())((())()())()","(())((())())(())","(())((())())()()","(())((()))((()))","(())((()))(()())","(())((()))(())()","(())((()))()(())","(())((()))()()()","(())(()(((()))))","(())(()((()())))","(())(()((())()))","(())(()((()))())","(())(()((())))()","(())(()(()(())))","(())(()(()()()))","(())(()(()())())","(())(()(()()))()","(())(()(())(()))","(())(()(())()())","(())(()(())())()","(())(()(()))(())","(())(()(()))()()","(())(()()((())))","(())(()()(()()))","(())(()()(())())","(())(()()(()))()","(())(()()()(()))","(())(()()()()())","(())(()()()())()","(())(()()())(())","(())(()()())()()","(())(()())((()))","(())(()())(()())","(())(()())(())()","(())(()())()(())","(())(()())()()()","(())(())(((())))","(())(())((()()))","(())(())((())())","(())(())((()))()","(())(())(()(()))","(())(())(()()())","(())(())(()())()","(())(())(())(())","(())(())(())()()","(())(())()((()))","(())(())()(()())","(())(())()(())()","(())(())()()(())","(())(())()()()()","(())()((((()))))","(())()(((()())))","(())()(((())()))","(())()(((()))())","(())()(((())))()","(())()((()(())))","(())()((()()()))","(())()((()())())","(())()((()()))()","(())()((())(()))","(())()((())()())","(())()((())())()","(())()((()))(())","(())()((()))()()","(())()(()((())))","(())()(()(()()))","(())()(()(())())","(())()(()(()))()","(())()(()()(()))","(())()(()()()())","(())()(()()())()","(())()(()())(())","(())()(()())()()","(())()(())((()))","(())()(())(()())","(())()(())(())()","(())()(())()(())","(())()(())()()()","(())()()(((())))","(())()()((()()))","(())()()((())())","(())()()((()))()","(())()()(()(()))","(())()()(()()())","(())()()(()())()","(())()()(())(())","(())()()(())()()","(())()()()((()))","(())()()()(()())","(())()()()(())()","(())()()()()(())","(())()()()()()()","()((((((()))))))","()(((((()())))))","()(((((())()))))","()(((((()))())))","()(((((())))()))","()(((((()))))())","()(((((())))))()","()((((()(())))))","()((((()()()))))","()((((()())())))","()((((()()))()))","()((((()())))())","()((((()()))))()","()((((())(()))))","()((((())()())))","()((((())())()))","()((((())()))())","()((((())())))()","()((((()))(())))","()((((()))()()))","()((((()))())())","()((((()))()))()","()((((())))(()))","()((((())))()())","()((((())))())()","()((((()))))(())","()((((()))))()()","()(((()((())))))","()(((()(()()))))","()(((()(())())))","()(((()(()))()))","()(((()(())))())","()(((()(()))))()","()(((()()(()))))","()(((()()()())))","()(((()()())()))","()(((()()()))())","()(((()()())))()","()(((()())(())))","()(((()())()()))","()(((()())())())","()(((()())()))()","()(((()()))(()))","()(((()()))()())","()(((()()))())()","()(((()())))(())","()(((()())))()()","()(((())((()))))","()(((())(()())))","()(((())(())()))","()(((())(()))())","()(((())(())))()","()(((())()(())))","()(((())()()()))","()(((())()())())","()(((())()()))()","()(((())())(()))","()(((())())()())","()(((())())())()","()(((())()))(())","()(((())()))()()","()(((()))((())))","()(((()))(()()))","()(((()))(())())","()(((()))(()))()","()(((()))()(()))","()(((()))()()())","()(((()))()())()","()(((()))())(())","()(((()))())()()","()(((())))((()))","()(((())))(()())","()(((())))(())()","()(((())))()(())","()(((())))()()()","()((()(((())))))","()((()((()()))))","()((()((())())))","()((()((()))()))","()((()((())))())","()((()((()))))()","()((()(()(()))))","()((()(()()())))","()((()(()())()))","()((()(()()))())","()((()(()())))()","()((()(())(())))","()((()(())()()))","()((()(())())())","()((()(())()))()","()((()(()))(()))","()((()(()))()())","()((()(()))())()","()((()(())))(())","()((()(())))()()","()((()()((()))))","()((()()(()())))","()((()()(())()))","()((()()(()))())","()((()()(())))()","()((()()()(())))","()((()()()()()))","()((()()()())())","()((()()()()))()","()((()()())(()))","()((()()())()())","()((()()())())()","()((()()()))(())","()((()()()))()()","()((()())((())))","()((()())(()()))","()((()())(())())","()((()())(()))()","()((()())()(()))","()((()())()()())","()((()())()())()","()((()())())(())","()((()())())()()","()((()()))((()))","()((()()))(()())","()((()()))(())()","()((()()))()(())","()((()()))()()()","()((())(((()))))","()((())((()())))","()((())((())()))","()((())((()))())","()((())((())))()","()((())(()(())))","()((())(()()()))","()((())(()())())","()((())(()()))()","()((())(())(()))","()((())(())()())","()((())(())())()","()((())(()))(())","()((())(()))()()","()((())()((())))","()((())()(()()))","()((())()(())())","()((())()(()))()","()((())()()(()))","()((())()()()())","()((())()()())()","()((())()())(())","()((())()())()()","()((())())((()))","()((())())(()())","()((())())(())()","()((())())()(())","()((())())()()()","()((()))(((())))","()((()))((()()))","()((()))((())())","()((()))((()))()","()((()))(()(()))","()((()))(()()())","()((()))(()())()","()((()))(())(())","()((()))(())()()","()((()))()((()))","()((()))()(()())","()((()))()(())()","()((()))()()(())","()((()))()()()()","()(()((((())))))","()(()(((()()))))","()(()(((())())))","()(()(((()))()))","()(()(((())))())","()(()(((()))))()","()(()((()(()))))","()(()((()()())))","()(()((()())()))","()(()((()()))())","()(()((()())))()","()(()((())(())))","()(()((())()()))","()(()((())())())","()(()((())()))()","()(()((()))(()))","()(()((()))()())","()(()((()))())()","()(()((())))(())","()(()((())))()()","()(()(()((()))))","()(()(()(()())))","()(()(()(())()))","()(()(()(()))())","()(()(()(())))()","()(()(()()(())))","()(()(()()()()))","()(()(()()())())","()(()(()()()))()","()(()(()())(()))","()(()(()())()())","()(()(()())())()","()(()(()()))(())","()(()(()()))()()","()(()(())((())))","()(()(())(()()))","()(()(())(())())","()(()(())(()))()","()(()(())()(()))","()(()(())()()())","()(()(())()())()","()(()(())())(())","()(()(())())()()","()(()(()))((()))","()(()(()))(()())","()(()(()))(())()","()(()(()))()(())","()(()(()))()()()","()(()()(((()))))","()(()()((()())))","()(()()((())()))","()(()()((()))())","()(()()((())))()","()(()()(()(())))","()(()()(()()()))","()(()()(()())())","()(()()(()()))()","()(()()(())(()))","()(()()(())()())","()(()()(())())()","()(()()(()))(())","()(()()(()))()()","()(()()()((())))","()(()()()(()()))","()(()()()(())())","()(()()()(()))()","()(()()()()(()))","()(()()()()()())","()(()()()()())()","()(()()()())(())","()(()()()())()()","()(()()())((()))","()(()()())(()())","()(()()())(())()","()(()()())()(())","()(()()())()()()","()(()())(((())))","()(()())((()()))","()(()())((())())","()(()())((()))()","()(()())(()(()))","()(()())(()()())","()(()())(()())()","()(()())(())(())","()(()())(())()()","()(()())()((()))","()(()())()(()())","()(()())()(())()","()(()())()()(())","()(()())()()()()","()(())((((()))))","()(())(((()())))","()(())(((())()))","()(())(((()))())","()(())(((())))()","()(())((()(())))","()(())((()()()))","()(())((()())())","()(())((()()))()","()(())((())(()))","()(())((())()())","()(())((())())()","()(())((()))(())","()(())((()))()()","()(())(()((())))","()(())(()(()()))","()(())(()(())())","()(())(()(()))()","()(())(()()(()))","()(())(()()()())","()(())(()()())()","()(())(()())(())","()(())(()())()()","()(())(())((()))","()(())(())(()())","()(())(())(())()","()(())(())()(())","()(())(())()()()","()(())()(((())))","()(())()((()()))","()(())()((())())","()(())()((()))()","()(())()(()(()))","()(())()(()()())","()(())()(()())()","()(())()(())(())","()(())()(())()()","()(())()()((()))","()(())()()(()())","()(())()()(())()","()(())()()()(())","()(())()()()()()","()()(((((())))))","()()((((()()))))","()()((((())())))","()()((((()))()))","()()((((())))())","()()((((()))))()","()()(((()(()))))","()()(((()()())))","()()(((()())()))","()()(((()()))())","()()(((()())))()","()()(((())(())))","()()(((())()()))","()()(((())())())","()()(((())()))()","()()(((()))(()))","()()(((()))()())","()()(((()))())()","()()(((())))(())","()()(((())))()()","()()((()((()))))","()()((()(()())))","()()((()(())()))","()()((()(()))())","()()((()(())))()","()()((()()(())))","()()((()()()()))","()()((()()())())","()()((()()()))()","()()((()())(()))","()()((()())()())","()()((()())())()","()()((()()))(())","()()((()()))()()","()()((())((())))","()()((())(()()))","()()((())(())())","()()((())(()))()","()()((())()(()))","()()((())()()())","()()((())()())()","()()((())())(())","()()((())())()()","()()((()))((()))","()()((()))(()())","()()((()))(())()","()()((()))()(())","()()((()))()()()","()()(()(((()))))","()()(()((()())))","()()(()((())()))","()()(()((()))())","()()(()((())))()","()()(()(()(())))","()()(()(()()()))","()()(()(()())())","()()(()(()()))()","()()(()(())(()))","()()(()(())()())","()()(()(())())()","()()(()(()))(())","()()(()(()))()()","()()(()()((())))","()()(()()(()()))","()()(()()(())())","()()(()()(()))()","()()(()()()(()))","()()(()()()()())","()()(()()()())()","()()(()()())(())","()()(()()())()()","()()(()())((()))","()()(()())(()())","()()(()())(())()","()()(()())()(())","()()(()())()()()","()()(())(((())))","()()(())((()()))","()()(())((())())","()()(())((()))()","()()(())(()(()))","()()(())(()()())","()()(())(()())()","()()(())(())(())","()()(())(())()()","()()(())()((()))","()()(())()(()())","()()(())()(())()","()()(())()()(())","()()(())()()()()","()()()((((()))))","()()()(((()())))","()()()(((())()))","()()()(((()))())","()()()(((())))()","()()()((()(())))","()()()((()()()))","()()()((()())())","()()()((()()))()","()()()((())(()))","()()()((())()())","()()()((())())()","()()()((()))(())","()()()((()))()()","()()()(()((())))","()()()(()(()()))","()()()(()(())())","()()()(()(()))()","()()()(()()(()))","()()()(()()()())","()()()(()()())()","()()()(()())(())","()()()(()())()()","()()()(())((()))","()()()(())(()())","()()()(())(())()","()()()(())()(())","()()()(())()()()","()()()()(((())))","()()()()((()()))","()()()()((())())","()()()()((()))()","()()()()(()(()))","()()()()(()()())","()()()()(()())()","()()()()(())(())","()()()()(())()()","()()()()()((()))","()()()()()(()())","()()()()()(())()","()()()()()()(())","()()()()()()()()"};
}
return {};
}
};