hdu 5929 Basic Data Structure(找规律+deque模拟)

Basic Data Structure

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


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.
 
题目大意+分析:让你模拟一个特殊的栈,有四种操作,直说QUEYR,别的不说了;从栈顶到栈底进行NAND操作,从给出的运算规则可以看出,任何数和0运算都会为1,抓住这一点,从栈底往上开始找到第一个0,再判定下面1的个数的奇偶性,当然还要判端一下,这个0是不是栈顶元素(很关键),这样就能求出结果了。

详见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
const int maxn = 400010;
using namespace std;
int destack[maxn];
int l,r,flag,n;
deque <int> dq;

void init()//初始化
{
    flag = 1;
    l = n;//逆向往左放
    r = n-1;//正向往右放
    while(!dq.empty()) dq.pop_back();
}

void PUSH()
{
    int x;
    scanf("%d",&x);
    if(flag)//正向
    {
        destack[++r]=x;
        if(x==0)
            dq.push_back(r);
    }
    else
    {
        destack[--l]=x;
        if(x==0)
            dq.push_front(l);
    }
}

void POP()
{
    if(flag)
    {
        if(destack[r]==0)
            dq.pop_back();
        r--;
    }
    else
    {
        if(destack[l]==0)
            dq.pop_front();
        l++;
    }
}
void QUERY()
{
    if(dq.empty())
    {
        if(r<l)
            printf("Invalid.\n");
        else
        {
            int num=r-l+1;
            printf("%d\n",num&1?1:0);
        }
    }
    else
    {
        if(flag)
        {   //判定最后结果需要双重条件:1、运算到最后一个0的位置时的结果:2、剩下的1的个数
            int fr=dq.front();
            int num=fr-l;
            if(num&1)//栈底有奇数个1
            {
                printf("%d\n",fr==r?1:0);//0后面还有数的时候 (即fr<r) 才能保证运算到这一位是1
            }
            else//栈底有偶数个1
            {
                printf("%d\n",fr==r?0:1);//0后面还有数的时候 (即fr<r) 才能保证运算到这一位是1
            }
        }
        else
        {
            int fl=dq.back();
            int num=r-fl;
            if(num&1)
            {
                printf("%d\n",fl==l?1:0);//0后面还有数的时候 (即fr<r) 才能保证运算到这一位是1
            }
            else
            {
                printf("%d\n",fl==l?0:1);
            }
        }
    }
}

int main()
{
    char str[10];
    int t,index;
    index=0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d:\n",++index);
        scanf("%d",&n);
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",&str);
            if(str[0]=='P' && str[1]=='U')
                PUSH();
            else if(str[0]=='P' && str[1]=='O')
                POP();
            else if(str[0]=='Q' && str[1]=='U')
                QUERY();
            else
                flag^=1;
        }
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值