HDU - 5929 Basic Data Structure(模拟+规律)

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.
题目大意:关于栈的操作,有push,pop,和反转and查询,反转就是头变成尾部,尾部变成头部,查询时是从顶开始到尾部进行给出的位运算,有0为1,
题解:对于这个模拟操作并不难,用两个数组指针指向头和尾部,用一个标记变量进行区别是反转的情况,主要是进行位运算,可以发现,该位运算只要有0时,结果就为1,那么,在一串数中只要最后一个是0,那么这一串数就为1(除了只有一个0时),而对于全是1的情况,看1的个数即可,偶数为0,奇数为1,那么就可以找到从顶到尾中最后一个是0的位置就可以了,剩下的1的个数在判断其奇偶性就可以了
#include<bits/stdc++.h>
using namespace std;


int top;
int a[620000];
int b[620000];///数组开小了会超时
int main()
{
    int t;
    scanf("%d",&t);
    int w=1;
    while(t--)
    {
        int n;
        char s[20];
        scanf("%d",&n);
        int flag=1;//往右
        int l,r;
        int in=300000;
        int out=300001;
        l=in;
        r=out;
        printf("Case #%d:\n",w++);
        int len=0;
        int le=0;
        while(n--)
        {
            scanf("%s",s);
            if(strcmp(s,"PUSH")==0)
            {
                int x;
                scanf("%d",&x);
                if(flag)
                {
                    a[out]=x;
                    if(x==0)
                    {
                        b[r++]=out;
                        le++;
                    }
                    out++;
                }
                else
                {
                    a[in]=x;
                    if(x==0)
                    {
                        b[l--]=in;
                        le++;
                    }
                    in--;
                }
                len++;
            }
            else if(strcmp(s,"POP")==0)
            {
                if(flag)
                {
                    int y=a[out-1];
                    out--;
                    if(y==0)
                    {
                        r--;
                        le--;
                    }
                }
                else
                {
                    int y=a[in+1];
                    in++;
                    if(y==0)
                    {
                        l++;
                        le--;
                    }
                }
                len--;

            }
            else if(strcmp(s,"REVERSE")==0)
            {
                flag^=1;
            }
            else
            {
                if(len<=0)printf("Invalid.\n");
                else if(le<=0)printf("%d\n",len%2);
                else if(flag)
                {
                    int sl=b[l+1];
                    if(sl==out-1)
                        printf("%d\n",(sl-in+1)%2);
                    else  printf("%d\n",(sl-in)%2);
                }
                else
                {
                    int sr=b[r-1];
                    if(sr==in+1)
                        printf("%d\n",(out-sr+1)%2);
                    else printf("%d\n",(out-sr)%2);
                }
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值