#P1557. Kruscal Expression Evaluation

    ID: 14843 Type: Default 1000ms 256MiB

Kruscal Expression Evaluation

Kruscal Expression Evaluation

This problem is based on a new way to represent arithmetic expressions as introduced by Kruscal. In the original arithmetic expression, a term such as +15*3 can be transformed into one of the following valid forms:

  • Implicit multiplier notation: +++15
  • Explicit multiplier notation: +(3)15

The rule is that if a term is preceded by a '+' (or '-') then the number of consecutive identical sign characters (in the implicit format) indicates the multiplier. In other words, the implicit form encodes a multiplier m by writing the operator m times. Formally, if you have an operator op then

$$m = \mbox{number of consecutive } op \mbox{ characters} $$

and the term's value is computed as:

$$\mbox{value} = \mbox{sign} \times m \times (\mbox{number}) $$

Alternatively, a term can use the explicit multiplier notation where immediately after the operator a multiplier is given in parentheses. For example, +(3)15 represents the same as +++15 (here the multiplier is 3) and -(3)15 represents -15*3 as ---15.

Additional rules include:

  • The explicit multiplier (in parentheses) must directly follow the operator and no extra operator characters are allowed before it. For example, ++(2)15 is not allowed.
  • The multiplier must be a positive integer (zero or negative values are not allowed).
  • The very first term in the expression may either appear as a bare number (e.g. 23) or with an operator as in +23 or +(1)23. However, forms without a leading operator such as (1)23 are not permitted.
  • The expression is formed by concatenating one or more terms. For example, the original expression 23+15*3-2 can be validly written as any one of the following: 23+++15-2, 23+(3)15-2, +23+++15-2, +23+(3)15-2, or +(1)23+(3)15-(1)2.

Your task is to read a string representing a Kruscal expression and output its evaluated integer result. Each term's contribution is computed as the product of its multiplier and its numeric value, with the appropriate sign. For a term:

  • If the term is written in implicit form, and the operator op appears consecutively m times, then the effective value is op ? (m * number) (with the sign determined by op). Note that if only one op appears, it means no multiplication factor (i.e. multiplier 1).
  • If the term is written in explicit form (e.g. +(3)15), the multiplier is as given inside the parentheses.

For instance, the expression 23+++15-2 is evaluated as:

  • First term: 23 (value = 23)
  • Second term: +++15 → operator '+' with 3 consecutive '+' so multiplier 3, value = + (3*15) = 45
  • Third term: -2 → operator '-' with implicit multiplier 1, value = - (1*2) = -2

The final result is 23 + 45 - 2 = 66.

inputFormat

A single line containing a non-empty string which is a valid Kruscal expression. The expression consists of one or more terms. Each term is either:

  • a number without a leading operator (only for the first term), or
  • an operator (+ or -) followed immediately either by an optional explicit multiplier in the form (n) or an implicit multiplier encoded as consecutive operator characters, followed by a positive integer.

You may assume that the input is a valid representation according to the rules described above.

outputFormat

Output a single integer, which is the result of evaluating the expression.

sample

23+++15-2
66