Submit | All submissions | Best solutions | Back to list |
GNY07F - Monkey Vines |
Deep in the Amazon jungle, exceptionally tall trees grow that support a rich biosphere of figs and juniper bugs, which happen to be the culinary delight of brown monkeys.
Reaching the canopy of these trees requires the monkeys to perform careful navigation through the tall tree’s fragile vine system. These vines operate like a see-saw: an unbalancing of weight at any vine junction would snap the vine from the tree, and the monkeys would plummet to the ground below. The monkeys have figured out that if they work together to keep the vines properly balanced, they can all feast on the figs and juniper bugs in the canopy of the trees.
A vine junction supports exactly two sub-vines, each of which must contain the same number of monkeys, or else the vine will break, leaving a pile of dead monkeys on the jungle ground. For purposes of this problem, a vine junction is denoted by a pair of matching square brackets [ ], which may contain nested information about junctions further down its sub-vines. The nesting of vines will go no further than 25 levels deep.
You will write a program that calculates the minimum number of monkeys required to balance a particular vine configuration. There is always at least one monkey needed, and, multiple monkeys may hang from the same vine.
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing a vine configuration consisting of a string of [ and ] characters as described above. The length of the string of [ and ] will be greater than or equal to zero, and less than or equal to 150.
Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the minimum number of monkeys required to reach the canopy successfully. Assume that all the hanging vines are reachable from the jungle floor, and that all monkeys jump on the vines at the same time.
Example
Input: 3 [] [[][[]]] Output: 1 2 2 1 3 8
Added by: | Marco Gallotta |
Date: | 2008-03-11 |
Time limit: | 12.85s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ERL JS-RHINO NODEJS PERL6 VB.NET |
Resource: | ACM Greater New York Regionals 2007 |
hide comments
2020-03-20 09:20:22
Create your own exponentiation function, pow() will give unnecessary WA. |
|
2017-04-01 06:54:09
Very easy question, ac in one go |
|
2016-06-01 11:11:49
how to input empty line in C/C++ Last edit: 2016-07-05 09:28:20 |
|
2014-06-20 21:56:45 Jaiwardhan Swarnakar
@khalid545 : as far as i know i got AC without having considered your case. the correct answer is the second one you showed. |
|
2014-05-17 18:00:52 Khalid545
For input: 2 [] An accepted solution answers: 1 2 2 2 But the correct answer is: 1 2 2 1 This problem occurs whenever the last testcase is a blank line. |
|
2012-05-13 11:56:56 m@j!n {Amit Gupta}
Some more test cases plz ... |