Basic Data Structurek(模拟,双向队列)

来源:互联网 发布:.club域名价值 编辑:程序博客网 时间:2024/05/18 16:40

Basic Data Structure

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


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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
 

Sample Output
Case #1:11Invalid.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.

可以用双向队列模拟,压入队列的数字代表连续1的个数,0的话直接压入。具体见代码,国足又输了55555555555.

#include<iostream>#include<string.h>#include<deque>#include<cstdio>using namespace std;int main(){    int cas;    scanf("%d",&cas);    int nn=0;    while(cas--)    {        deque<int>ideq;        printf("Case #%d:\n",++nn);        int n;        scanf("%d",&n);        char cmd[105];        int num;        int flag=0;        int cc=0;        for(int i=0;i<n;i++)        {            scanf("%s",&cmd);            if(cmd[2]=='S')            {                cin>>num;                if(ideq.empty())                {                    ideq.push_front(num);                    continue;                }                if(flag==0)                {                    if(num==1)                    {                                    cc=ideq.back();                        if(cc==0)                        {                           ideq.push_back(1);                            continue;                        }                            ideq.pop_back();                            cc++;                           ideq.push_back(cc);                                                         }                    else                      ideq.push_back(0);                }                else                {                    if(num==1)                    {                         cc=ideq.front();                        if(cc==0)                        {                          ideq.push_front(1);                            continue;                        }                        cc++;                        ideq.pop_front();                        ideq.push_front(cc);                        }                                            else                      ideq.push_front(0);                }            }            else if(cmd[0]=='Q')            {                int n;                if(flag==1&&!ideq.empty())                {                    int ans;                    int nnn=ideq.back();                    ans=nnn;                    ideq.pop_back();                    if(nnn==0)                    {                        if(!ideq.empty())                        {                            printf("1\n");                        }                        else                        {                            printf("0\n");                        }                        ideq.push_back(nnn);                        continue;                    }                    if(!ideq.empty())                    {                        int tmp=ideq.back();                        ideq.pop_back();                        if((tmp==0&&!ideq.empty()))                            ans++;                        ideq.push_back(tmp);                    }                    ideq.push_back(nnn);                    printf("%d\n",ans&1);                }                else if(flag==0&&!ideq.empty())                {                    int ans;                    int nnn=ideq.front();                    ans=nnn;                    ideq.pop_front();                    if(nnn==0)                    {                        if(!ideq.empty())                        {                            printf("1\n");                        }                        else                        {                            printf("0\n");                        }                        ideq.push_front(nnn);                        continue;                    }                    if(!ideq.empty())                    {                        int tmp=ideq.front();                        ideq.pop_front();                        if((tmp==0&&!ideq.empty()))                            ans++;                        ideq.push_front(tmp);                    }                    ideq.push_front(nnn);                    printf("%d\n",ans&1);                                    }                else                {                    printf("Invalid.\n");                }            }                        else if(cmd[1]=='O')            {                if(ideq.empty())                {                    continue;                }                if(flag==0)                {                    int t=ideq.back();                    ideq.pop_back();                    if(t)                    {                        t--;                            if(t)                        ideq.push_back(t);                    }                                    }                else                {                    int t=ideq.front();                    ideq.pop_front();                    if(t)                    {                        t--;                        if(t)                        ideq.push_front(t);                    }                                    }            }            else if(cmd[0]=='R')            {                flag=!flag;            }        }    }    //system("pause");}




0 0
原创粉丝点击