Basic Data Structure

本文介绍了一种涉及栈的基本数据结构操作问题,包括PUSH、POP、REVERSE及QUERY等操作,特别是QUERY操作中使用NAND逻辑运算来计算特定值。通过双向队列实现高效查询,提供了一个具体的解决方案。

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,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 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 ( 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.
 

 

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[20];
    scanf("%d",&t);
    int Case=1;
    while(t--)
    {
        memset(s,-1,sizeof s);
        int f=1;
        scanf("%d",&n);
        int r=250001;
        int l=r-1;
        int fa=0;///记录栈里面的总数
        q.clear();
        printf("Case #%d:\n",Case++);
        while(n--)
        {
            scanf("%s",op);
            int a;
            if(op[0]=='P'&&op[1]=='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[0]=='P'&&op[1]=='O')
            {
                if(!fa)
                    continue;
                if(f)
                {
                    if(s[r-1]==0)
                        q.pop_back();
                    r--;
                }
                else
                {
                    if(s[l+1]==0)
                        q.pop_front();
                    l++;
                }
                fa--;
            }
            else if(op[0]=='Q')
            {
                //cout<<"cur="<<cur<<endl;
                int cur=0;
                if(fa==0)
                {
                    cout<<"Invalid."<<endl;
                }
                else if(fa==1)
                {
                    cout<<s[l+1]<<endl;
                }
                else
                {
                    if(f)
                    {
                        if(q.empty())
                            cur=fa;
                        else
                        {
                            cur=q.front()==r-1?fa-1:q.front()-l;
                        }
                    }
                    else
                    {
                        if(q.empty())
                            cur=fa;
                        else
                        {
                            cur=q.back()==l+1?fa-1:r-q.back();
                        }

                    }
                    if(cur%2==0)
                        cout<<"0"<<endl;
                    else
                        cout<<"1"<<endl;
                }
            }
            else
            {
                f^=1;
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/6005096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值