Basic Data Structure

Basic Data Structure

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


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.



模拟题,调了好长时间。。。。。。。。。


通过真值表我们很快就能发现规律,若栈头元素是0,栈中只有一个元素,则值为0,否则为1;

若栈头元素是1,如果栈中没有0,则根据栈中1的个数判断,若个数是奇数则为1,为偶数是0,否则,找到与栈头最近元素是0的位置,若该位置是栈尾位置,则根据栈尾到栈头之间的1的个数来判断,若个数是奇数则为1,为偶数是0,其他,根据栈尾到栈头之间的1的个数+1来判断,若个数是奇数则为1,为偶数是0


代码如下:

#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int N=6000000+6;
int a[N];
int o[N];
int main()
{
    char s[30];
    int t,count=0;
    int n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int et;
        int st=et=3000000;
        int st1,et1;
        st1=et1=3000000;
        int flag=0;
        printf("Case #%d:\n",++count);
        int ans;
        while(n--)
        {
            scanf("%s",s);
            if(strcmp(s,"PUSH")==0)
            {
                scanf("%d",&m);
                if(flag==0)
                {
                    a[et++]=m;
                    if(m==0)
                        o[et1++]=et-1;
                }
                else
                {
                    a[--st]=m;
                    if(m==0)
                        o[--st1]=st;
                }
            }
            else if(strcmp(s,"QUERY")==0)
            {
                if(st==et)
                    printf("Invalid.\n");
                else
                {
                    if(!flag)
                    {
                        if(a[st]==0)
                        {
                            if(et-st==1)
                            {
                                ans=0;
                                printf("%d\n",ans);
                            }
                            else
                            {
                                ans=1;
                                printf("%d\n",ans);
                            }
                        }
                        else
                        {
                            if(st1==et1)
                            {
                                int m=et-st;
                                if(m%2==0)
                                    ans=0;
                                else
                                    ans=1;
                                printf("%d\n",ans);
                            }
                            else
                            {
                                int x=o[st1],m;//m 1
                                if(x==et-1)
                                    m=x-st;
                                else
                                    m=x-st+1;
                                if(m%2==0)
                                    ans=0;
                                else
                                    ans=1;
                                printf("%d\n",ans);
                            }

                        }
                    }
                    else
                    {
                        if(a[et-1]==0)
                        {
                            if(et-st==1)
                            {
                                ans=0;
                                printf("%d\n",ans);
                            }
                            else
                            {
                                ans=1;
                                printf("%d\n",ans);
                            }
                        }
                        else
                        {
                            if(st1==et1)
                            {
                                int m=et-st;
                                if(m%2==0)
                                    ans=0;
                                else
                                    ans=1;
                                printf("%d\n",ans);
                            }
                            else
                            {

                                int x=o[et1-1],m;//m 1
                                if(x==st)
                                    m=et-x-1;
                                else
                                    m=et-x;
                                if(m%2==0)
                                    ans=0;
                                else
                                    ans=1;
                                printf("%d\n",ans);
                            }
                        }

                    }

                }
            }
            else if(strcmp(s,"REVERSE")==0)
                if(flag==1)
                    flag=0;
                else
                    flag=1;
            else if(strcmp(s,"POP")==0)
            {
                if(st!=et)
                {
                    if(!flag)
                    {
                        if(o[et1-1]>=et-1)
                            et1--;
                        et--;
                    }
                    else
                    {
                        if(o[st1]<=st)
                           st1++;
                        st++;
                    }
                }
            }
        }
    }
    return 0;
}

简化代码:

#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int N=600000+6;
int a[N];
int o[N];
int main()
{
    char s[30];
    int t,count=0;
    int n,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int et=300000;
        int st=300000-1;
        int et1=300000;
        int st1=300000-1;
        int flag=0;
        printf("Case #%d:\n",++count);
        int len=0,le=0;
        int ans;
        while(n--)
        {
            scanf("%s",s);
            if(strcmp(s,"PUSH")==0)
            {
                scanf("%d",&m);
                if(flag==0)
                {
                    a[et++]=m;
                    if(m==0)
                        o[et1++]=et-1,le++;
                }
                else
                {
                    a[st--]=m;
                    if(m==0)
                        o[st1--]=st+1,le++;
                }
                len++;
            }
            else if(strcmp(s,"QUERY")==0)
            {
                if(st+1==et)
                    printf("Invalid.\n");
                else if(le<=0)
                    printf("%d\n",len%2);
                   else if(!flag)
                {
                    int sl=o[st1+1];
                    if(sl==et-1)
                        printf("%d\n",(sl-st+1)%2);
                    else  printf("%d\n",(sl-st)%2);
                }
                else
                {
                    int sr=o[et1-1];
                    if(sr==st+1)
                        printf("%d\n",(et-sr+1)%2);
                    else printf("%d\n",(et-sr)%2);
                }
            }
            else if(strcmp(s,"REVERSE")==0)
                flag^=1;
            else if(strcmp(s,"POP")==0)
            {
                if(st!=et)
                {
                    if(!flag)
                    {
                        if(a[et-1]==0)
                            et1--,le--;
                        et--;
                    }
                    else
                    {
                        if(a[st+1]==0)
                            st1++,le--;
                        st++;
                    }
                }
                len--;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值