Professor Maple teaches mathematics in a university. He have invented a function for the purpose of obtaining the operands from an expression. The function named op(i,e) can be described as follows:
The expression e may be divided into sub-expression_r(s) by the operator, which has the lowest priority in the expression. For example, the expression "a*b+b*c+c*d" should be divided into three sub-expressions "a*b", "b*c" and "c*d", because the operator "+" has the lowest priority. The purpose of this function is to extract the ith sub-expression as the result. So, in the example above, op(2,e)=b*c.
If we regard the sub-expression as the main expression, it might be divided again and again. Obviously, the dividing process is recursive. As you see, the following example is much more complex:
Let p:=a^b*c+(d*c)^f*z+b
op(1,op(1,op(2,p)))=(d*c)
op(1,op(1,op(1,op(2,p))))=d*c
op(2,op(2,p))=z
op(3,p)=b
op(1,op(3,p))=b
Professor Maple is so lazy that he would leave the work to computer rather than do it himself, when the expression is long and complicated. Of course, without your program, the computer won't work out the result automatically.
Input
The input file contains several test cases. The last test case in the input file is followed by a line containing a symbol "*", indicating the end of the input data. Each test case consists of two parts. The first part describes the expression, while the second part contains several questions, which should be calculated according to the expression.
The first line of each test case contains an expression consists of the expression name, ":=" and the content of the expression. The expression name is a lowercase. And the content is composed by lowercases and operators "+", "(", ")", "*" and "^". For example, here is a valid expression, p:=a^b*c+(d*c)^f*z+b. Among those operators, "(" and ")" have the highest priority. The operator "^" has a lower priority, and then "*". The priority of the operator "+" is the lowest.
The second line of each test case contains an integer n indicating n questions based on the above expression. This is followed by n lines. Each of them contains the description of one question, which consists of integers. For example, the question with three integers "2 1 1" describes the function op(1,op(1,op(2,e))). To compute this function, we have to keep to the following sequence: First, according to the first integer 2, divide the expression and extract the 2nd sub-expression. Then, according to the second integer 1, divide the sub-expression and extract the 1st one. Finally, according to the third integer 1, divide the outcome again, and extract the result.
Output
For each test case, display the expression name and a colon on the first line. Then display the result of each question on a line. The layout of the output is shown in the sample output.
You may assume that all expressions and functions are always valid.
Display a blank line between test cases.
Sample Input
p:=a^b*c+(d*c)^f*z+b
4
2 1 1
2 2
3
3 1
a:=(x+y)
3
1
1 2
1 2 1
*
Output for the Sample Input
Expression p:
op(1,op(1,op(2,p)))=(d*c)
op(2,op(2,p))=z
op(3,p)=b
op(1,op(3,p))=b
Expression a:
op(1,a)=x+y
op(2,op(1,a))=y
op(1,op(2,op(1,a)))=y
Source: Asia 2001, Shanghai (Mainland China)
源码:
![ZOJ <wbr>1014 <wbr>Operand ZOJ <wbr>1014 <wbr>Operand](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
![ZOJ <wbr>1014 <wbr>Operand ZOJ <wbr>1014 <wbr>Operand](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
解题报告:
这题是字符串操作题:题意比较好理解,没有固定的模板,算法思想就是对于操作浮,从+,*,^,一个一个遍历字符串,存在其中的一个就说明,以该操作浮将其划分为n个操作数,时间复杂度O(3*n)。需要注意括号的操作,即括号的比配问题。
此处还用到了C语言的模板list 容器,或stack 容器,该处对于输入的格式由于不知道每次的操作步数,所以需要加入cin.peek()!='\n'来判断是否输入结束。因而需要在前后加入cin.ignore() 用来跳过换行浮。