Function String
InterviewTime limit1sMemory limit1024 MB
Evaluate a prefix-notation string of f, g, and x, where x is 0, g adds 1, and f takes the smaller of two sub-strings, or print -1 if the string is malformed.
Problem
A function string that evaluates to a single integer is defined as follows.
xis a function string. The string above evaluates to .- If
Sis a function string, thengSis also a function string. The string above evaluates to the value ofSplus . - If
SandTare function strings, thenfSTis also a function string. The string above evaluates to the smaller of the values thatSandTevaluate to.
Input
The first line contains a string consisting only of f, g, and x.
Output
On the first line, print the value of the string if it is a function string. Otherwise, print -1.