QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 512 MB Total points: 100

#13090. 括号

Statistics

运算符和操作数之间的对应关系可以通过括号来明确。例如,在 C 语言程序中,表达式 a-b-c 可以明确为 (a-b)-c,因为减法运算符是左结合的。

括号可以用来覆盖运算符的默认优先级和结合性。例如,在表达式 a-(b-c) 中,减法运算符的左结合性被括号覆盖了。

一位 C 语言初学者 Dennis 对记忆运算符的优先级和结合性感到非常头疼。因此,他发明了一种名为 ICPC(I can parenthesize C)的新语言,在这种语言中,运算符与操作数之间的对应关系必须通过括号完全明确;除此之外,所有其他特性与 C 语言相同。例如,应该写成 (a-b)-c 而不是 a-b-c,写成 a+(b*c) 而不是 a+b*c

然而,在某些情况下,括号的使用可能会过多。对于表达式 a-b-c,写成 (a-b)-c 就足够了,但也可以写成 (a-(b))-c 或者 ((a-b)-c) 其中下划线标出的括号对是多余的。

Dennis 想要将 C 语言表达式转换为 ICPC 表达式,其中括号对的使用应恰到好处。你需要帮助 Dennis。为简化起见,你可以假设输入的 C 语言表达式仅包含五个二元算术运算符(+-*/%)、左括号 ( 和右括号 ),以及单个小写字母操作数。给定这样一个 C 语言表达式,编写一个程序来确定它是否为 ICPC 表达式。

如果该表达式在 ICPC 中不是错误的,那么它在 C 语言中也不应是错误的。一旦它在 C 语言中不是错误的,就应该检查括号的使用情况,以确定它是否为 ICPC 中的规范表达式。如果表达式的括号使用不规范,即括号的数量不是 ICPC 所需的精确数量,则将其视为不规范(improper)。

请注意,某些输入的 C 语言表达式最初可能是错误的。例如,a%/b 是错误的,因为它在 %/ 之间还需要一个操作数才能有效。再举一个例子,a b + c 也是错误的,因为它在 ab 之间还需要一个运算符。

输入格式

程序从标准输入读取数据。输入由包含一个 C 语言表达式的单行组成。该表达式是由单个小写字母、特殊符号(包括左括号和右括号以及五个二元算术运算符 +-*/%)以及空格组成的字符串。输入行至少包含一个操作数。输入行的长度(字符数)不超过 1000,包括空格和末尾的单个换行符。

输出格式

程序输出到标准输出。仅打印一行。该行应包含以下单词之一:errorproperimproper。如果输入的 C 语言表达式有误,则打印 error。如果它没有错误,则根据表达式的括号状态打印 properimproper:如果它被正确地加上了 ICPC 所需的精确数量的括号,则打印 proper,否则打印 improper

样例

输入 1

a + a

输出 1

proper

输入 2

(b+( a+c )) + b

输出 2

proper

输入 3

c + ((b) + a)

输出 3

improper

输入 4

c+(a%/b)

输出 4

error

输入 5

x + ((y + z)

输出 5

error

输入 6

a b + (c + b)

输出 6

error

输入 7

x + y + z

输出 7

improper

Figure 1. Example of an ICPC expression

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.