Basic Data Structure


Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1303    Accepted Submission(s): 320


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


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


题意: 给一个栈.有push,pop,query ,reverse这些操作,对于每个询问输出这个栈从栈顶到底进行题目给的这个运算后的结果;sh=

题解:

 双向队列,存0和相邻1的的个数

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <deque>

using namespace std;

int main()
{
int m,i, j;
int K;
int n, x, k;
char str[10];
int h= 0;
scanf("%d", &K);
while(K--)
{
int flag = 0;
printf("Case #%d:\n", ++h);
scanf("%d", &n);
deque<int>q;
while(n--)
{
scanf("%s", str);
if(strcmp(str, "PUSH") == 0)
{
scanf("%d", &x);
if(q.empty())
{
q.push_front(x);
continue;
}
if(flag == 0)
{
if(x == 0)//0直接压进去
{
q.push_front(0);
}
else
{
int e = q.front();
if(e == 0)
{
q.push_front(1);//前一个为0,直接把1压进
}
else //前一个不为0,前一个出来加1再压进去,
{
q.pop_front();
e++;
q.push_front(e);
}
}
}
else
{
if(x == 0)
{
q.push_back(0);
}
else
{
int e = q.back();
if(e == 0)
{
q.push_back(1);
continue;
}
else
{
q.pop_back();
e++;
q.push_back(e);
}
}
}
}
else if(strcmp(str, "QUERY") == 0)
{
if(q.empty())
{
printf("Invalid.\n");
continue;
}
if(flag == 1)
{
int e1;
int e = q.front();
e1 = e;
q.pop_front();
if(e == 0)
{
if(q.empty())
printf("0\n");
else
printf("1\n");
q.push_front(e);
continue;
}
else if(!q.empty())
{
int ee = q.front();
q.pop_front();
if(ee == 0 && !q.empty())
e1++;
q.push_front(ee);
}
q.push_front(e);
printf("%d\n",e1&1);
}
else
{
int e1;
int e = q.back();
e1 = e;
q.pop_back();
if(e == 0)
{
if(q.empty())
printf("0\n");
else
printf("1\n");
q.push_back(e);
continue;
}
else if(!q.empty())
{
int ee = q.back();
q.pop_back();
if(ee == 0 && !q.empty())
e1++;
q.push_back(ee);
}
q.push_back(e);
printf("%d\n",e1&1);
}
}
else if(strcmp(str, "REVERSE") == 0)
{
if(flag == 0)
flag =1;
else
flag = 0;
}
else if(strcmp(str, "POP") == 0)
{
if(q.empty())
{
continue;
}
if(flag == 0)
{
int e = q.front();
q.pop_front();
if(e)
if(e -1 > 0)
{
e--;
q.push_front(e);
}
}
else
{
int e = q.back();
q.pop_back();
if(e)
if(e -1 > 0)
{
e--;
q.push_back(e);
}
}
}
}
}
return 0;
}