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

时间:2022-05-25 06:21:57

Basic Data Structure

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

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 5925 

Statistic | Submit | Discuss | Note

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5929

题目大意:

  一个栈,N个操作,支持4种操作

  1.压入0或1

  2.弹出栈顶

  3.将栈倒置(底和顶对换)

  4.询问,A[top] nand A[top-1] nand ... nand A[base]。

    0 nand 0=0 nand 1=1 nand 0 = 1, 1 nand 1 =1

题目思路:

  【模拟】

  nand运算的特征是0和任何数nand结果是1,所以当A[i]=0时,如果i上面还有元素,可以全部忽略,ans[i]=1,如果没有元素,ans[i]=0

  所以只要看栈底往上的连续的1个个数,根据奇偶个数判断答案是1还是0,注意只有1个元素的情况、1个0的情况。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 100004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int b,t;
int a[N*][];
char ch[];
void PUSH()
{
int i,j,x;
scanf("%d",&x);
n++;
if(n==)
a[++t][]=x,a[t][]=;
else if(a[t][]==x)
a[t][]++;
else if(b>t)
a[--t][]=x,a[t][]=;
else
a[++t][]=x,a[t][]=;
}
void POP()
{
if(!n)return;
n--;
if(--a[t][]==)
{
if(b>t)t++;
else t--;
}
}
void REVERSE()
{
if(!n)return;
swap(b,t);
}
void QUERY()
{
if(!n)puts("Invalid.");
else if(n==)
printf("%d\n",a[b][]);
else
{
if(a[b][])
{
if(n<=a[b][]+)printf("%d\n",a[b][]&);
else printf("%d\n",a[b][]&^);
}
else puts("");
}
}
int main()
{
#ifndef ONLINE_JUDGEW
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
// for(scanf("%d",&cass);cass;cass--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
printf("Case #%d:\n",cass);
scanf("%d",&m);
n=;
b=N;t=N-;
for(k=;k<=m;k++)
{
scanf("%s",ch);
if(ch[]=='S')PUSH();
else if(ch[]=='P')POP();
else if(ch[]=='V')REVERSE();
else if(ch[]=='E')QUERY();
}
}
return ;
}
/*
// //
*/