Pro.ID22741 TitleStack Machine Executor Title链接http://10.20.2.8/oj/exercise/problem?problem_id=22741 AC0 Submit2 Ratio0.00% 时间&空间限制描述Many ciphers can be computed much faster using various machines and automata. In this problem, we will focus on one particular type of machines called stack machine. Its name comes from the fact that the machine operates with the well-known data structure — stack. The later-stored values are on the top, older values at the bottom. Machine instructions typically manipulate the top of the stack only. Our stack machine is relatively simple: It works with integer numbers only, it has no storage beside the stack (no registers etc.) and no special input or output devices. The set of instructions is as follows:
All binary operations consider the top-most number to be the "right" operand, the second number the "left" one. All of them remove both operands from the stack and place the result on top in place of the original numbers. If there are not enough numbers on the stack for an instruction (one or two), the execution of such an instruction will result into a program failure. A failure also occurs if a divisor becomes zero (for DIV or MOD) or if the result of any operation should be more than 109 in absolute value. This means that the machine only operates with numbers between -1000000000 and 1000000000, inclusive. To avoid ambiguities while working with negative divisors and remainders: If some operand of a division operation is negative, the absolute value of the result should always be computed with absolute values of operands, and the sign is determined as follows: The quotient is negative if (and only if) exactly one of the operands is negative. The remainder has the same sign as the dividend. Thus, 13 div -4 = -3, -13 mod 4 = -1, -13 mod -4 = -1, etc. If a failure occurs for any reason, the machine stops the execution of the current program and no other instructions are evaluated in that program run. 输入The input contains description of several machines. Each machine is described by two parts: the program and the input section. The program is given by a series of instructions, one per line. Every instruction is given by three uppercase letters and there must not be any other characters. The only exception is the NUM instruction, which has exactly one space after the three letters followed by a non-negative integer number between 0 and 109. The only allowed instructions are those defined above. Each program is terminated by a line containing the word "END" (and nothing else). The input section starts with an integer N (0 ≤ N ≤ 10 000), the number of program executions. The next N lines contain one number each, specifying an input value Vi, 0 ≤ Vi ≤ 109. The program should be executed once for each of these values independently, every execution starting with the stack containing one number — the input value Vi. There is one empty line at the and of each machine description. The last machine is followed by a line containing the word "QUIT". No program will contain more than 100000 instructions and no program requires more than 1000 numbers on the stack in any moment during its execution. 输出Description Many ciphers can be computed much faster using various machines and automata. In this problem, we will focus on one particular type of machines called stack machine. Its name comes from the fact that the machine operates with the well-known data structure — stack. The later-stored values are on the top, older values at the bottom. Machine instructions typically manipulate the top of the stack only. Our stack machine is relatively simple: It works with integer numbers only, it has no storage beside the stack (no registers etc.) and no special input or output devices. The set of instructions is as follows:
All binary operations consider the top-most number to be the "right" operand, the second number the "left" one. All of them remove both operands from the stack and place the result on top in place of the original numbers. If there are not enough numbers on the stack for an instruction (one or two), the execution of such an instruction will result into a program failure. A failure also occurs if a divisor becomes zero (for DIV or MOD) or if the result of any operation should be more than 109 in absolute value. This means that the machine only operates with numbers between -1000000000 and 1000000000, inclusive. To avoid ambiguities while working with negative divisors and remainders: If some operand of a division operation is negative, the absolute value of the result should always be computed with absolute values of operands, and the sign is determined as follows: The quotient is negative if (and only if) exactly one of the operands is negative. The remainder has the same sign as the dividend. Thus, 13 div -4 = -3, -13 mod 4 = -1, -13 mod -4 = -1, etc. If a failure occurs for any reason, the machine stops the execution of the current program and no other instructions are evaluated in that program run. Input The input contains description of several machines. Each machine is described by two parts: the program and the input section. The program is given by a series of instructions, one per line. Every instruction is given by three uppercase letters and there must not be any other characters. The only exception is the NUM instruction, which has exactly one space after the three letters followed by a non-negative integer number between 0 and 109. The only allowed instructions are those defined above. Each program is terminated by a line containing the word "END" (and nothing else). The input section starts with an integer N (0 ≤ N ≤ 10 000), the number of program executions. The next N lines contain one number each, specifying an input value Vi, 0 ≤ Vi ≤ 109. The program should be executed once for each of these values independently, every execution starting with the stack containing one number — the input value Vi. There is one empty line at the and of each machine description. The last machine is followed by a line containing the word "QUIT". No program will contain more than 100000 instructions and no program requires more than 1000 numbers on the stack in any moment during its execution. Output For each input value, print one line containing the output value for the corresponding execution, i.e., the one number that will be on the stack after the program executes with the initial stack containing only the input number. If there is a program failure during the execution or if the stack size is incorrect at the end of the run (either empty or there are more numbers than one), print the word "ERROR" instead. Print one empty line after each machine, including the last one. Sample Input DUP Sample Output 3 Source 样例输入DUP 样例输出3 作者 |