QOJ.ac

QOJ

حد الوقت: 1 s حد الذاكرة: 512 MB مجموع النقاط: 100

#3419. 中缀表达式转前缀表达式

الإحصائيات

Jaap 编写了一个实验作业的解决方案。任务很简单:将中缀表达式转换为波兰(前缀)表达式。在中缀表示法中,运算符写在操作数之间(例如 $12 + 5$),而前缀表示法将运算符置于其操作数之前(例如 $+ 12 5$)。

这是 Jaap 需要转换的表达式语法:

$$ \begin{aligned} Expression &::= Number \\ &| \text{‘(’ } Expression \ Op \ Expression \text{ ‘)’} \\ &| \text{‘(’ ‘-’ } Expression \text{ ‘)’} \\ Op &::= \text{‘+’ | ‘-’} \\ Number &::= Digit \ | \ Number \ Digit \\ Digit &::= \text{‘0’ | ‘1’ | ‘2’ | ‘3’ | ‘4’ | ‘5’ | ‘6’ | ‘7’ | ‘8’ | ‘9’} \end{aligned} $$

如果一个 $Number$ 超过一位数字,它不会以 ‘0’ 开头。一个 $Number$ 最多有 9 位数字。

此时我们必须承认,该作业的规范说明得并不好。更具体地说,结果表达式的语法没有给出。因此,Jaap 不得不自己做一些决定——而他做出了错误的决定。

这是他的第一个错误:他认为在前缀表示法中空格是多余的。这在中缀表示法中是正确的,因为两个数字之间总会有一个运算符。然而,在前缀表示法中,数字必须彼此分开。像 Jaap 那样在前缀表示法中省略空格,会导致像 $+1234$ 这样的表达式,它有三种不同的解释。(练习:画出这三种不同的语法树。)

这是 Jaap 的第二个错误:他认为在前缀表示法中括号是多余的。只有在运算符的元数(arity)固定的情况下,不带括号的前缀表示法才是无歧义的。例如,在同时存在一元减号和二元减号的情况下,就会出现歧义。表达式 $--34$ 可以被解读为 $(- (- 3 4))$,计算结果为 $1$;或者解读为 $(- (- 3) 4)$,计算结果为 $-7$;甚至可以解读为 $(- (- 34))$,计算结果为 $34$。

我们不要求你重构 Jaap 的程序。我们要求你找出他的输出有多大的歧义性。

输入格式

每个测试用例由单行组成,包含一个长度 $\le 1000$ 的非空字符串。该字符串仅包含字符 ‘+’ 和 ‘-’ 以及数字 ‘0’ 到 ‘9’。该字符串是 Jaap 程序输出的结果。

输出格式

对于每个测试用例,打印一行,包含两个数字:通过对输入表达式的不同解释所能得到的最小值和最大值。

样例

输入 1

--34

输出 1

-7 34

输入 2

+1234

输出 2

46 235

输入 3

--09

输出 3

-9 9

输入 4

+111111111111

输出 4

222222 111111222

输入 5

--0071

输出 5

-71 -71

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.