Basic Data Structure

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

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

Hint

In the first sample: during the first query, the stack contains only one element 1, so the answer is 1. then in the second query, the stack contains 0, l
(from bottom to top), so the answer to the second is also 1. In the third query, there is no element in the stack, so you should output Invalid.

 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5928 5927 
/*
双向队列,记录从开头开始到第一个0的位置的1有多少个,因为0与任何nand都是1 比赛的时候竟然想不起来双向队列.......愣是用一个数组加了两个指针模拟了一个双向队列。
*/
#include<bits/stdc++.h>
#define N 500000
using namespace std;
int s[N];
deque<int >q;//用来存放所有0的位置
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
int t,n;
char op[];
scanf("%d",&t);
int Case=;
while(t--)
{
memset(s,-,sizeof s);
int f=;
scanf("%d",&n);
int r=;
int l=r-;
int fa=;///记录栈里面的总数
q.clear();
printf("Case #%d:\n",Case++);
while(n--)
{
scanf("%s",op);
int a;
if(op[]=='P'&&op[]=='U')
{
scanf("%d",&a);
if(f)
{
s[r]=a;
if(!a)
q.push_back(r);
r++;
}
else
{
s[l]=a;
if(!a)
q.push_front(l);
l--;
}
fa++;
}
else if(op[]=='P'&&op[]=='O')
{
if(!fa)
continue;
if(f)
{
if(s[r-]==)
q.pop_back();
r--;
}
else
{
if(s[l+]==)
q.pop_front();
l++;
}
fa--;
}
else if(op[]=='Q')
{
//cout<<"cur="<<cur<<endl;
int cur=;
if(fa==)
{
cout<<"Invalid."<<endl;
}
else if(fa==)
{
cout<<s[l+]<<endl;
}
else
{
if(f)
{
if(q.empty())
cur=fa;
else
{
cur=q.front()==r-?fa-:q.front()-l;
}
}
else
{
if(q.empty())
cur=fa;
else
{
cur=q.back()==l+?fa-:r-q.back();
} }
if(cur%==)
cout<<""<<endl;
else
cout<<""<<endl;
}
}
else
{
f^=;
}
}
}
return ;
}

Basic Data Structure的更多相关文章

  1. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  2. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. Basic Data Structure HDU - 5929 (这个模拟我要报警了)

    Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operati ...

  5. hdu 5929 Basic Data Structure

    ゲート 分析: 这题看出来的地方就是这个是左结合的,不适用结合律,交换律. 所以想每次维护答案就不怎么可能了.比赛的时候一开始看成了异或,重读一遍题目了以后就一直去想了怎么维护答案...... 但是很 ...

  6. HDU 5929 Basic Data Structure(模拟 + 乱搞)题解

    题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...

  7. 【推导】【线段树】hdu5929 Basic Data Structure

    题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bo ...

  8. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  9. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 使用VMware Workstation Pro 12 虚拟机安装Mac OS系统教程 全程图解

    导读:使用虚拟机安装Windows.Linux或者Ubuntu系统大家或许看了很多,但如何使用VMware Workstation Pro 12安装Mac OS,的确需要好好研究一番:否则无法下手,因 ...

  2. Jmeter的安装和启动时出现unable to access jarfile apachejmeter.jar error value=1错误处理

    Jmeter是纯Java开发的, 能够运行Java程序的系统一般都可以运行Jmeter, 如:Windows. Linux. mac等. 由于是由Java开发,所以自然需要jdk环境. Windows ...

  3. 兼容低版本JS的Array.map方法

    前几天去别的公司面试遇到个这样的问题,兼容IE7下的Array.map方法,一脸蒙蔽.后面回来查了下资料发现.Array.map方法是ECMA-262 标准中新添加的方法,在低版本的JS中是木有的. ...

  4. 考了3年,工作四年,零基础在职终于拿到CFA证书

    大家都知道CFA Charterholder是独有的全球公认的投资管理从业人员高职业水平和道德水准的有力证明,是金融界卓越专业成就的象征:CFA资格强调和遵循极其严格的职业操守和道德准则,世界各主要发 ...

  5. Go语言备忘录:反射的原理与使用详解

    目录: 预备知识 reflect.Typeof.reflect.ValueOf Value.Type 动态调用 通过反射可以修改原对象 实现类似“泛型”的功能   1.预备知识: Go的变量都是静态类 ...

  6. Python实战之SocketServer模块

    文章出处:http://www.cnblogs.com/wupeiqi/articles/5040823.html SocketServer内部使用 IO多路复用 以及 "多线程" ...

  7. SpringMVC拦截器Interceptor

    SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter. SpringMVC 中的Interceptor 拦截请求是通过Ha ...

  8. Springmvc+Spring+Mybatis实现员工登录注册功能

    ssm实现用户注册以及登录功能..转载请标明出处  http://www.cnblogs.com/smfx1314/p/smfx1314.html 前端bootstrap 所使用的IDE是eclips ...

  9. 【转载】使用CSS将图片转换成黑白(灰色、置灰)

    文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=2547原文摘要: . ...

  10. Thrift总结(三)Thrift框架

    1.数据类型 基本类型: bool:布尔值,true 或 false,对应 Java 的 boolean byte:8 位有符号整数,对应 Java 的 byte i16:16 位有符号整数,对应 J ...