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 ;
}
/*
// //
*/

HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)的更多相关文章

  1. HDU 5929 Basic Data Structure 模拟

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

  2. HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  6. HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟

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

  8. 2016CCPC东北地区大学生程序设计竞赛 (2018年8月22日组队训练赛)

    题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2016CCPC%B6%AB%B1%B1%B5%D8%C7%F8%B4%F3%D ...

  9. 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929

    链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...

随机推荐

  1. 百度知道的php爬虫

    原文地址:百度知道的php爬虫作者:好宏杰软件 <?php class spider  {    private $content ;    private $contentlen ;    p ...

  2. javascript的面向对象编程

    面象对象编程技术的核心理念:封装.继承.多态:在一些主流的高级编程语言中,比如:C#,VB.NET,JAVA,PHP等都是很容易实现的,而如果要在javascript中实现面象对象编程,可就不那么直接 ...

  3. Bootstrap--全局CSS样式之排版

    Bootstrap的排版样式大致和html基本元素一样,没什么大的区别,就是对元素加了样式. (1)标题 HTML 中的所有标题标签,<h1> 到 <h6> 均可使用.另外,还 ...

  4. [原创]ie6,7中td和img之间有间隙

    情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...

  5. C#中的委托范例学习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  6. 倒置字符串s中各字符的位置

    倒置字符串s中各字符的位置 其中reverse函数可以写成更紧凑的形式 void reverse(char s[]){ int c,i,j; ,j=strlen(s)-;i<j;i++,j--) ...

  7. python模块学习 hashlib

    一.hashlib概述 涉及加密服务:14. Cryptographic Services 其中 hashlib是涉及安全散列和消息摘要,提供多个不同的加密算法借口,如SHA1.SHA224.SHA2 ...

  8. 复选框字段数组拆分后循环选项值,if判断根据选项值,前端输出html

    {php $specials = explode(',',$r[special]);} <div class="special"> {loop $specials $s ...

  9. [转] CSS direction属性简介与实际应用 ---张鑫旭

    一.用的少并不代表没有用 至少,在我接触的这么多项目里,没有见到使用过CSS direction属性做实际开发的. 为什么呢?是因为direction长得丑吗? 虽然说direction确实其貌不扬, ...

  10. js学习--DOM操作详解大全二(window对象)

    一.window - 计时器 1、setTimeout()可以用来在指定的时间之后单次调用函数.setTimeount(f,1000);//一秒后调用函数fclearTimeout();取消函数的执行 ...