编程介的小学生 2017-06-06 08:08 采纳率: 20.5%
浏览 778
已采纳

Basic Data Structure

Problem Description
Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack:

∙ PUSH x: put x on the top of the stack, x must be 0 or 1.
∙ POP: throw the element which is on the top of the stack.

Since it is too simple for Mr. Frog, a famous mathematician who can prove "Five points coexist with a circle" easily, he comes up with some exciting operations:

∙REVERSE: Just reverse the stack, the bottom element becomes the top element of the stack, and the element just above the bottom element becomes the element just below the top elements... and so on.
∙QUERY: Print the value which is obtained with such way: Take the element from top to bottom, then do NAND operation one by one from left to right, i.e. If atop,atop−1,⋯,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop−1 nand ... nand a1. Note that the Stack will not change after QUERY operation. Specially, if the Stack is empty now,you need to print ”Invalid.”(without quotes).

By the way, NAND is a basic binary operation:

∙ 0 nand 0 = 1
∙ 0 nand 1 = 1
∙ 1 nand 0 = 1
∙ 1 nand 1 = 0

Because Mr. Frog needs to do some tiny contributions now, you should help him finish this data structure: print the answer to each QUERY, or tell him that is invalid.

Input
The first line contains only one integer T (T≤20), which indicates the number of test cases.

For each test case, the first line contains only one integers N (2≤N≤200000), indicating the number of operations.

In the following N lines, the i-th line contains one of these operations below:

∙ PUSH x (x must be 0 or 1)
∙ POP
∙ REVERSE
∙ QUERY

It is guaranteed that the current stack will not be empty while doing POP operation.

Output
For each test case, first output one line "Case #x:w, where x is the case number (starting from 1). Then several lines follow, i-th line contains an integer indicating the answer to the i-th QUERY operation. Specially, if the i-th QUERY is invalid, just print "Invalid."(without quotes). (Please see the sample for more details.)

Sample Input
2
8
PUSH 1
QUERY
PUSH 0
REVERSE
QUERY
POP
POP
QUERY
3
PUSH 0
REVERSE
QUERY

Sample Output
Case #1:
1
1
Invalid.
Case #2:
0

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-06-06 15:52
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?