当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2792:Brackets Removal

题目描述
Let us consider arithmetic expressions that consist of variables denoted by lower-case letters "a" to "z";

four binary arithmetic operations: addition ("+"), subtraction ("-"), multiplication ("*"), and division("/"); opening ("(") and closing (")") round brackets. The normal order of precedence is used -- multiplication and division have the highest precedence, addition and subtraction have the lowest precedence.

Operations of the same precedence are evaluated from left to right (for example a - b + c = (a - b) + c).

Thus, the grammar for the expressions is the following:
<expression> --> <term> | <expression> + <term> | <expression> - <term>

<term> --> <factor> | <term> * <factor> | <term> / <factor>
<factor> --> <variable> | (<expression>)
<variable> --> a | b | . . . | z

Your task is to rewrite the given expression so that its semantics is not changed, but the resulting expression has the minimal number of round brackets.

You can remove any excessive brackets that do not change the order of evaluation, for example
(a + b) + (c) => a + b + c,
(a * b)/(c) ) => a * b/c,
and you can rewrite expressions using the following rules:
  • If A and B are arbitrary expressions, you can change A + (B) to A + B, for example
    a - g/h + (b + c - d + e * (f + h - i)) => a - g/h + b + c - d + e * (f + h - i).
  • If A and B are arbitrary expressions, you can change A-(B) to A-B' where B' is obtained from B by replacing all top-level "+" operations to "-" operations and vice versa, for example
    a - g/h - (b + c - d + e * (f + h - i)) => a - g/h - b - c + d - e * (f + h - i)).
  • If A and B are arbitrary terms, you can change A * (B) to A * B, for example
    x/(y + z) * (a * (b - c)/d/(e/f)) => x/(y + z) * a * (b - c)/d/(e/f).
  • If A and B are arbitrary terms, you can change A/(B) to A/B', where B' is obtained from B by replacing all top-level "*" operations to "/" operations and vice versa, for example
    x/(y + z)/(a * (b - c)/d/(e/f)) => x/(y + z)/a/(b - c) * d * (e/f).

You can think about these transformations as ones that only use "+" and "*" associativity, the fact that "-" is the reverse operation to "+", "/" is the reverse operation to "*", and nothing else.

You can apply the described transformations and remove excessive brackets as many times as you need to get the expression with the minimal number of round brackets.
输入解释
The input contains a single line with the expression. Expression does not have any leading, trailing, or inner spaces and consists of at most 1000 characters.
输出解释
Write to the output a single line with the same expression that is rewritten with the minimal number of round brackets. Do not write any spaces.
输入样例
((a-b)-(c-d)-(z*z*g/f)/(p*(t))*((y-u)))
输出样例
a-b-c+d-z*z*g/f/p/t*(y-u)

该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-2792

最后修改于 2020-10-29T06:44:21+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536