21277_Parenthesis

2022-5-16 18:19| 发布者: Hocassian| 查看: 37| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-3-8950592432800-Problem List-采集的数据-后羿采集器.html

Pro.ID

21277

Title

Parenthesis

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21277

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 131072/65536 K (Java/Others)
  • 描述

    To a computer, there is no difference between the expression (((x)+(y))(t)) and (x+y)t; but, to a human, the latter is easier to read. When writing automatically generated expressions that a human may have to read, it is useful to minimize the number of parentheses in an expression. We assume expressions consist of only two operations: addition (+) and multiplication (juxtaposition), and these operations act on single lower-case letter variables only. Specifically, here is the grammar for an expression E:

    E : P | P '+' E
    P : F | F  P
    F : V | '(' E ')'
    V : 'a' | 'b' | .. | 'z'

    The addition (+, as in x+y) and multiplication (juxtaposition, as in xy) operators are associative: x+(y+z)=(x+y)+z=x+y+z and x(yz)=(xy)z=xyz. Commutativity and distributivity of these operations should not be assumed. Parentheses have the highest precedence, followed by multiplication and then addition.

    输入

    The input consists of a number of cases. Each case is given by one line that satisfies the grammar above. Each expression is at most 1000 characters long.

    输出

    Description

    To a computer, there is no difference between the expression (((x)+(y))(t)) and (x+y)t; but, to a human, the latter is easier to read. When writing automatically generated expressions that a human may have to read, it is useful to minimize the number of parentheses in an expression. We assume expressions consist of only two operations: addition (+) and multiplication (juxtaposition), and these operations act on single lower-case letter variables only. Specifically, here is the grammar for an expression E:

    E : P | P '+' E
    P : F | F  P
    F : V | '(' E ')'
    V : 'a' | 'b' | .. | 'z'

    The addition (+, as in x+y) and multiplication (juxtaposition, as in xy) operators are associative: x+(y+z)=(x+y)+z=x+y+z and x(yz)=(xy)z=xyz. Commutativity and distributivity of these operations should not be assumed. Parentheses have the highest precedence, followed by multiplication and then addition.

    Input
    The input consists of a number of cases. Each case is given by one line that satisfies the grammar above. Each expression is at most 1000 characters long.
    Output
    For each case, print on one line the same expression with all unnecessary parentheses removed.
    Sample Input
    x
    (x+(y+z))
    (x+(yz))
    (x+y(x+t))
    x+y+xt
    Sample Output
    x
    x+y+z
    x+yz
    x+y(x+t)
    x+y+xt
    Source

    样例输入

    x
    (x+(y+z))
    (x+(yz))
    (x+y(x+t))
    x+y+xt

    样例输出

    x
    x+y+z
    x+yz
    x+y(x+t)
    x+y+xt

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部