编程介的小学生 2017-08-20 15:41 采纳率: 20.5%
浏览 702
已采纳

Next Expression

Sempr is looking for a girlfriend now. On his way of seeking a beauty, he met many difficult problems. Here is one of them.

In order to judge whether an expression can be equal to some value, he has to enumerate the calculation order of the operators. So he changes the expression without parentheses into a regular expression. The regular expression is defined as follows:

E ::= "(" E O E ")" | N
O ::= "+" | "-" | "*" | "/"
N ::= [0-9]+
The regular expression is unambiguous and its calculation order is defined. For example, ((1+2)-(3*4)) is a regular expression. We can first evaluate (1+2) and get 3. Then we evaluate (3*4) and get 12. At last, we evaluate (3-12) and get -9. Note the evaluations are performed from left to right.

Of course, Sempr is so powerful that he can write a program to evaluate a regular expression easily. But enumerating the regular expressions of an expression is a bit difficult for him. So he asks you for help. Your job is to find the smallest regular expression that is larger than the given one in lexicographic order.

A regular expression is lexicographically smaller than another regular expression of the same expression iff the last operator to evaluate that differs between the two regular expressions in the first regular expression is after that in the second regular expression. For example, the evaluation order of regular expression (((6+7)+8)+9) is 1, 2, 3. The evaluation order of ((6+(7+8))+9) is 2, 1, 3. The second operators of them, which are the last operands that differ between them, are 2 and 1. The first one is after the second one. Hence, the first expression is lexicographically smaller than the second one.

Input

The first line of the input contains an integer T (T <= 100), indicating the number of cases.

Each test case contains one line of the expression that is only made up of parentheses ("(", ")"), digits ("0"-"9") and operators ("+", "-", "*", "/"). The length of the expression will be between 1 and 1,000,000, inclusive.

Output

If the given expression is not a regular expression, output "Error". If the expression has no lexicographically larger regular expression, output "-1". Otherwise, output the smallest regular expression that is larger than the given one. NOTE that you need NEITHER to evaluate whether the expression is valid, NOR to omit the leading zeros in the expression that are not needed.

Sample Input

4
1+1
(1+1)
((1+2)*3)
((12+3)/(0+3))
Sample Output

Error
-1
(1+(2*3))
(12+((3/0)+3))

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题