Bracket Value
InterviewTime limit1sMemory limit128 MB
Parse a bracket string with two bracket types and compute its defined nested value, or output 0 if it is invalid.
Problem
A bracket string is made only from the four characters (, ), [, and ]. A valid bracket string is defined as follows.
- The strings
()and[], each consisting of one matching pair, are valid bracket strings. - If
Xis a valid bracket string, then(X)and[X]are also valid bracket strings. - If both
XandYare valid bracket strings, then their concatenationXYis also a valid bracket string.
For a valid bracket string X, define its value as follows.
- The value of
()is 2. - The value of
[]is 3. - The value of
(X)is2 × value(X). - The value of
[X]is3 × value(X). - If valid bracket strings
XandYare concatenated, thenvalue(XY) = value(X) + value(Y).
For example, (()[[]]) is valid because the inside ()[[]] has value 2 + 3 × 3 = 11, so (()[[]]) has value 2 × 11 = 22. Also, ([]) has value 2 × 3 = 6, so (()[[]])([]) has total value 22 + 6 = 28.
Given one bracket string, compute and print its value according to these rules. If the input string is not a valid bracket string, print 0.
Input
The first line contains a string representing a bracket string. Its length is between 1 and 30, inclusive.
Output
Print one integer: the value of the bracket string. If the input is not a valid bracket string, print 0.