1711. Expression Evaluator

单点时限: 2.0 sec

内存限制: 256 MB

This problem is about evaluating some C-style expressions. The expressions to be evaluated will contain only simple integer variables and a limited set of operators; there will be no constants in the expressions. There are 26 variables in the program, named by lower case letters a through z. Before evaluation, the initial values of these variables are a = 1, b = 2, …, z = 26.

The operators allowed are addition and subtraction (binary + and -), with their known meaning. So, the expression a + c - d + b has the value 2 (1 + 3 - 4 + 2). Additionally, ++ and – operators are allowed in the input expression too, which are unary operators, and may come before or after variables. If the ++ operator comes before a variable, then that variable’s value is increased (by one) before the variable’s value is used in calculating the value of the whole expression. Thus the value of ++ c - b is 2. When ++ comes after a variable, that variable is increased (by one) after its value is used to calculate the value of the whole expression. So, the value of the c ++ - b is 1, though c is incremented after the value for entire expression is computed; its value will be 4 too. The – operator behaves the same way, except that it decreases the value of its operand.

More formally, an expression is evaluated in the following manner:

  1. Identify every variable that are preceded by ++. Write an assignment statement for incrementing the value of each of them, and omit the ++ from before that variable in the expression.

  2. Do similarly for the variables with ++ after them.

  3. At this point, there is no ++ operator in the expression. Write a statement evaluating the remaining expression after the statements determined in step 1, and before those determined in step 2.

  4. Execute the statements determined in step 1, then those written in step 3, and finally the one written in step 2.

This way, evaluating ++ a + b ++ is the same as computing a = a + 1, result = a + b, and b = b + 1.

输入格式

The first line of the input contains a single integer T which is the number of test cases, followed by T lines each containing the input expression for a test case. Ignore blanks in the input expression. Be sure that no ambiguity is in the input expressions (like a+++b). Similarly, ++ or – operators do not appear both before and after one single variable (like ++a++). You may safely assume each variable appears only once in an expression.

输出格式

For each test case, write each expression as it appears in the input (exactly), then write the value of the complete expression. After this, on separate lines, write the value of each variable after evaluating the expression (write them in sorted order of the variable names). Write only the values of the variables that are used in the expressions. To find out about the output format, follow the style used in the sample output below.

样例

Input
2
a+b
c+f--+--a
Output
Expression: a+b
value = 3
a = 1
b = 2
Expression: c+f--+--a
value = 9
a = 0
c = 3
f = 5

7 人解决,11 人已尝试。

7 份提交通过,共有 25 份提交。

7.0 EMB 奖励。

创建: 17 年,2 月前.

修改: 7 年,2 月前.

最后提交: 3 年,12 月前.

来源: Tehran 2006 Preliminary

题目标签