HDU 5292 Basic Data Structure

本文介绍了一种基于栈的数据结构实现及其操作,包括PUSH、POP、REVERSE和QUERY等,特别关注QUERY操作中NAND逻辑运算的应用及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Basic Data Structure

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


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,atop1,,a1  is corresponding to the element of the Stack from top to the bottom,  value=atop  nand  atop1  nand ... nand  a1 . Note that the Stack  will notchange 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 ( T20 ), which indicates the number of test cases.

For each test case, the first line contains only one integers N ( 2N200000 ), 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.
 
    题意:

    Mr.frog最近在学习栈。栈有一些基础的操作:

PUSH x:将x放入栈中,x必须是0或1

POP:将栈顶元素取出

    这些都太简单,Mr.Frog又增加了两个操作:

REVERSE:将栈中的元素翻转

QUERY:从左到右对栈中元素做NAND运算,输出运算结果,栈空输出“Invalid”。

    另外,NAND是基于二进制的操作:

0 nand 0 = 1

0 nand 1 = 1

1 nand 0 = 1


1 nand 1 = 0

     思路:用模拟栈+deque(双端队列)来解决。

     用普通方法做,果断T了,现学了一下deque的用法,附学习的网址:

deque的使用

   

#include <iostream>
#include <algorithm>
#include <deque>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;
deque<int > q;//存放data[]中0的位置
int data[450000];//模拟栈
int T;//测试数据
int n;//操作数
int Case;
int t, h;//data[]的下标
int main()
{
	scanf("%d",&T);
	Case = 1;
	while(T--)
	{
		q.clear();
		t = 200000;
		h = 200001;
		bool flag = false;//false则不需翻转;true则需要翻转
		int index = 0;//模拟栈中元素的个数
		scanf("%d",&n);
		printf("Case #%d:\n",Case);
		Case++;
		while(n--)
		{
			char str[8];
			scanf("%s",str);
			if(str[2] == 'S')//PUSH
			{
				int value;
				scanf("%d",&value);
				if(flag)//REVERSE
				{
					data[t--] = value;
					if(!value)
						q.push_front(t + 1);
				}
				else
				{
					data[h++] = value;
					if(!value)
						q.push_back(h - 1);
				}
				index++;
			}
			else if(str[2] == 'P')//POP
			{
				index--;
				if(flag)
				{
					if(data[++t] == 0)
						q.pop_front();
				}
				else
				{
					if(data[--h] == 0)
						q.pop_back();
				}					
					
			}
			else if(str[2] == 'V')//REVERSE
				flag = !flag;
			else
			{
				if(index == 0)
					printf("Invalid.\n");
				else if(index == 1)
					printf("%d\n",data[h - 1]);
				else
				{
					int num = 0;
					if(q.empty())
						num = index;
					else
					{
						if(flag)
							num = (q.back() == t+1) ? index - 1 : h - q.back();
						else
							num = (q.front() == h-1) ? index - 1 : q.front() - t;
					}			
					if(num % 2)
						printf("1\n");
					else
						printf("0\n");
				}
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值