zoukankan      html  css  js  c++  java
  • UVA-11995

    There is a bag-like data structure, supporting two operations:1 x Throw an element x into the bag.2 Take out an element from the bag.Given a sequence of operations with return values, you’re going to guess the data structure. It isa stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out largerelements first) or something else that you can hardly imagine!InputThere are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x.That means after executing a type-2 command, we get an element x without error. The value of xis always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).OutputFor each test case, output one of the following:stack It’s definitely a stack.queue It’s definitely a queue.priority queue It’s definitely a priority queue.impossible It can’t be a stack, a queue or a priority queue.not sure It can be more than one of the three data structures mentionedabove.Sample Input61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4Sample Outputqueuenot sureimpossiblestackpriority queue


    题解:分别定义 stack、queue、priority_queue 判断这个操作序列是不是符合上述结构。

    AC代码为:


    #include<stdio.h>  
    #include<stack>  
    #include<queue>  
    using namespace std;  
    int main()  
    {  
        int n, i, x, y, f[4];  
        while(~scanf("%d",&n))  
        {  
            stack<int> s;  
            queue<int> q;  
            priority_queue<int, vector<int>, less<int> > pq;  
            for(i=0;i<3;i++) 
    f[i]=1;  
            for(i=0;i<n;i++)  
            {  
                scanf("%d%d",&x,&y);  
                if(x == 1)  
                {  
                    s.push(y);  
                    q.push(y);  
                    pq.push(y);  
                }  
                else  
                {  
                    if(!s.empty())  
                    {  
                        if(s.top() != y)  
                            f[0] = 0;  
                        s.pop();  
                    }  
                    else  
                        f[0] = 0;  
      
                    if(!q.empty())  
                    {  
                        if(q.front() != y)  
                            f[1] = 0;  
                        q.pop();  
                    }  
                    else  
                        f[1] = 0;  
      
                    if(!pq.empty())  
                    {  
                        if(pq.top() != y)  
                            f[2] = 0;  
                        pq.pop();  
                    }  
                    else  
                        f[2] = 0;  
                }  
            }  
            int num = 0;  
            for(i = 0; i < 3; i++)  
                if(f[i] == 1)  
                    num++;  
            if(num == 0)  
                printf("impossible ");  
            else if(num > 1)  
                printf("not sure ");  
            else  
            {  
                if(f[0] == 1)  
                    printf("stack ");  
                else if(f[1] == 1)  
                    printf("queue ");  
                else  
                    printf("priority queue ");  
            }  
        }  
        return 0;  
    }  


  • 相关阅读:
    我的本科毕业论文——Messar即时通讯系统
    你为什么不用Flash做程序的表示层呢?
    用于Blog的天气预报服务-改进20050806
    写了个小程序,方便大家编程(QuickDog,快捷键帮手)
    庆祝"上海.NET俱乐部"今天成立,请申请加入的朋友在这里Sign you name
    HTML+CSS+Javascript教学视频【0409更新】
    关于推迟7月9日上海.NET俱乐部第一次技术交流会的通知
    关于“上海.NET俱乐部”第一次技术交流会进展报告
    2005年8月13日 上海.NET俱乐部第一次活动纪实 已经发布,资料提供下载
    喜欢互联网行业,是因为它拥有着无穷的变数
  • 原文地址:https://www.cnblogs.com/csushl/p/9386628.html
Copyright © 2011-2022 走看看