Problem 588
Score of Parentheses
Medium · 20 pts · String, Stack
Problem
The score of a balanced parentheses string is defined recursively: () has score 1, AB (two balanced strings side by side) has score A + B, and (A) has score 2 × A. Compute the score of a given balanced string. A neat linear solution: every () pair at depth d (the number of brackets enclosing it) contributes 2^d, so add 2^d each time an opening bracket is immediately followed by a closing one.
Input
A single balanced parentheses string S.
Output
Print the score of S.
Constraints
2 <= |S| <= 50; S is balanced
Example input
(()(()))
Example output
6
Explanation
(()(())) is (A) where A = ()(()). A scores 1 + 2 × 1 = 3, so the whole string scores 2 × 3 = 6.