Submit | All submissions | Best solutions | Back to list |
BRCKTS2 - Brackets II |
Peter is preparing slides for his lecture on parsing arithmetic expressions. In the first part of the lecture he wants to focus just on parsing brackets. He invented an interesting geometric representation of a correct bracket sequence for his students, because one image is better than a thousand words:
Formally, the definition of the geometric representation looks as follows. The simplest correct bracket sequence () is represented by a 1 * 1 square. If A is a correct bracket sequence and g(A) its represenation, then the representation for (A) is g(A) surrounded by a rectangle two units wider than g(A) and one unit taller than the highest point of g(A). If A and B are two correct bracket sequences and g(A) and g(B) are their representations, then we get g(AB) by placing g(B) one unit to the right of g(A).
After he finished his slides, Peter started to play with the images he prepared. He painted the bounded areas of the images alternately black and white, in such a way that the outer-most areas are all painted black. For the example above this coloring looks as follows:
Problem specification
You are given a correct bracket sequence. Calculate the area that is colored black.
Input specification
The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.
Each test case consists of one line with a correct bracket sequence with length less than 350000. Every line will only contain characters ( and ).
Output specification
For each test case output one line with one integer – the area of the black part of the corresponding geometric representation.
Example
Input: 2 ((())) (())(()(())) Output: 10 20
Added by: | Fudan University Problem Setters |
Date: | 2009-05-31 |
Time limit: | 2s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: C99 ERL JS-RHINO NODEJS PERL6 VB.NET |
Resource: | IPSC 2009 |
hide comments
|
|||||
2016-03-22 09:55:39 Archit Jain
use long long |
|||||
2014-11-01 17:45:47 guru2691
use 64 bit integer for all variables!!! |
|||||
2013-06-15 07:35:33 Ajey Golsangi
Got a SIGSEGV flag with recursion, got AC after converting to a non recursive program. |
|||||
2013-03-31 08:12:43 Tigran
I think, recursion works fine for this problem. And I got AC with 2 recursion function which depths in worst case are 350000 / 2. |
|||||
2012-12-18 21:03:07 Nic Roets
@Brian You are right about the grader. However, when testing on my own Linux box, I had to increase it, e.g. 'ulimit -s 20000' |
|||||
2012-04-15 12:01:40 abhishek kumar
Any recursion can be changed into iteration by suitable using a stack |
|||||
2011-07-12 00:59:01 darryl
yes, this problem can be done without recursion. |
|||||
2010-11-06 08:01:55 no
can this ques be done without using recursion ? |
|||||
2010-11-05 05:34:05 no
are we to print ans subsequently for each test case? |
|||||
2010-09-04 15:25:42 Brian Bi
I disagree; in C/C++, recursion depth is limited only by available stack space. On Windows, I think this is only 8 MB, so it's easy to overflow. On the SPOJ grader, stack space is bounded only by total virtual memory, 256 MB. |