Bracket Expression
Anonymous User
1368

Given a balanced bracket expression, find the following in the forest representation of it:

1.Number of nodes
2.Number of levels
3.Maximum number of nodes at any level
4.Number of levels which have maximum nodes
Input Format
First line of input consists of an integer t denoting the number of test cases. First line of each test case consists of an integer n denoting the length of the expression. Second line consists of the bracket expression.

Output Format
For each test case print the four values asked above separated by space.

Constraints
1 <= t <= 10000

n = 2 * m where 1 <= m <= 1000

Sample Input
6
32
[[[[][[[]]]][][][[]]]][[[[][]]]]
18
[[[[[]]]][[[]]]][]
16
[[[[[]][[]]]][]]
10
[][][][][]
8
[[]][[]]
2
[]
Sample Output
16 6 5 2
9 5 2 4
8 5 2 3
5 1 5 1
4 2 2 2
1 1 1 1
Explanation
The expression [[[[][[[]]]][][][[]]]][[[[][]]]] has the following representation

[]
└── []
├── []
│ ├── []
│ └── []
│ └── []
│ └── []
├── []
├── []
└── []
└── []
[]
└── []
└── []
├── []
└── []
There are 16 nodes and 6 levels. Maximum nodes on any level is 5 and there are 2 such levels where maximum is achieved i.e. level 3 and 4. Hence answer is 16 6 5 2.

For the expression [[[[[]]]][[[]]]][] the representation is

[]
├── []
│ └── []
│ └── []
│ └── []
└── []
└── []
└── []
[]
There are 9 nodes and 5 levels. Maximum nodes on any level is 2 and there are 4 such levels where maximum is achieved i.e. level 1, 2, 3 and 4. Hence answer is 9 5 2 4.

Comments (1)