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. ArrayList 和 LinkedList 的实现与区别

    (转载请标明出处) 1.ArrayLis t的实现 2.LinkedLis t的实现 3.ArrayList 和 LinkedList 的区别 ArrayList 的实现: 1.MyArrayList ...

  2. JSP入门 Listener

    实现HttpSessionListener 编写一个OnlineUserListener类 package anni; import java.util.List; import javax.serv ...

  3. 对python编程的初步理解

    一直以来零零散散有听过python,这周终于下定决心学python了.在网上了买个套视频教程,内容分周次学习,有详细的讲解.本人觉得非常好.这里谈谈一下第一周的学习的笔记.望路过的大神给予指正,不胜感 ...

  4. webpack——devtool里的7种SourceMap模式

    我们先来看看文档对这 7 种模式的解释: 模式 解释 eval 每个module会封装到 eval 里包裹起来执行,并且会在末尾追加注释 //@ sourceURL. source-map 生成一个S ...

  5. python识别验证码——一般的数字加字母验证码识别

    1.验证码的识别是有针对性的,不同的系统.应用的验证码区别有大有小,只要处理好图片,利用好pytesseract,一般的验证码都可以识别 2.我在识别验证码的路上走了很多弯路,重点应该放在怎么把图片处 ...

  6. C#微信公众号/订阅号开发 接口源码

    using System; using System.Web; using System.IO; using System.Text; using System.Web.Security; using ...

  7. 百度地图JS调用示例

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. Ricequant-米筐金工-估值因子

    Ricequant米筐金工--因子分析 作者:戴宇.小湖 上一篇介绍了单因子检验是因子分析前重要的一个步骤,是构建因子库.建立因子模型的基础,这篇报告首先对常见估值因子进行初步的检验. 第一篇.估值因 ...

  9. Django安装Xadmin步骤

    在Django中安装Xadmin替换原始的admin,下面介绍两种方法安装 第一种方法:pip安装 第一步: 直接pip安装xadmin pip install xadmin pip会同时安装上面三个 ...

  10. appium python ios 自动化

    mac下搭建appium python selenium来针对ios应用进行自动化测试,并不是官网上的例子,自己程序调试成功. 前言:因为appium的安装前要先确定nodejs的安装.python的 ...