HDU 5929 数据结构模拟

来源:互联网 发布:怎么样做淘宝服装模特 编辑:程序博客网 时间:2024/06/05 03:17
#include<bits/stdc++.h>using namespace std;const int MAX=400105;const int BASE=200010;int T,N,Start,End,Flag,temp;char Str[100];set<int> S;             //记录0的位置.set<int>::iterator it;/*先分析一下NAND操作:0 NAND 0 = 10 NAND 1 = 11 NAND 0 = 11 NAND 1 = 0因为只有 1 NAND 1 = 0 ,我们记录当前栈中最后一个0的位置,然后看它后面有几个1.如果有奇数个1,则结果为1,如果有偶数个1,则几个为0.*/int main(){    scanf("%d",&T);    for (int cases=1; cases<=T; cases++)    {        scanf("%d",&N);        printf("Case #%d:\n",cases);        S.clear();        Start=End=BASE,Flag=0;        while (N--)        {            scanf("%s",Str);            switch (Str[2])            {            case 'S': //PUSH                scanf("%d",&temp);                if (temp==0)                    S.insert(End);                if (Flag)                    End--;                else                    End++;                break;            case 'P': //POP                if (Flag)                {                    if (!S.empty())                    {                        it=S.begin();                        if (*it<=End+1)                            S.erase(it);                    }                    End++;                }                else                {                    if (!S.empty())                    {                        it=S.end();                        it--;                        if (*it>=End-1)                            S.erase(it);                    }                    End--;                }                break;            case 'V': //REVERSE                if (Flag)                {                    temp=Start;                    Start=End+1;                    End=temp+1;                    Flag=0;                }                else                {                    temp=Start;                    Start=End-1;                    End=temp-1;                    Flag=1;                }                break;            case 'E': //QUERY                if (Start==End)                    printf("Invalid.\n");                else if (S.empty())                    printf("%d\n",abs((End-Start)%2));                else if (Flag)                {                    it=S.end();                    it--;                    temp=*it;                    int Ans=Start-temp;                    if (temp>End+1)                        Ans++;                    printf("%d\n",Ans%2);                }                else                {                    it=S.begin();                    temp=*it;                    int Ans=temp-Start;                    if (temp<End-1)                        Ans++;                    printf("%d\n",Ans%2);                }                break;            }        }    }    return 0;}
0 0
原创粉丝点击