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\n");  

        else if(num > 1)  

            printf("not sure\n");  

        else  

        {  

            if(f[0] == 1)  

                printf("stack\n");  

            else if(f[1] == 1)  

                printf("queue\n");  

            else  

                printf("priority queue\n");  

        }  

    }  

    return 0;  

}

最新文章

  1. 一键部署mono 免费空间支持ASP.NET MVC 再也不担心伙食费换空间了
  2. Kafka0.10的新特性一览
  3. SQL Server 中字符串中包含字符串变量的表示方法
  4. Linux 浅谈Linux 操作系统的安全设置
  5. XTUOJ 1248 TC or CF 搜索
  6. linux 最大文件描述符fd
  7. octopress command memo
  8. vld(Visual Leak Detector) 内存泄露检测工具
  9. dbg调试
  10. storm 1.0版本滑动窗口的实现及原理
  11. alex python of day2
  12. 计蒜客NOIP模拟赛4 D1T1 小X的质数
  13. 《HelloGitHub月刊》第 05 期
  14. ETF计划Q&amp;A
  15. DOS的重定向命令及在安全方面的应用
  16. regular exception
  17. mothur trim.seqs 去除PCR引物
  18. hdu2088
  19. Pytest - 进阶功能fixture
  20. js事件的捕获和冒泡阶段

热门文章

  1. Linux centos5.6版本下mysql5.6主从环境安装配置
  2. Redis持久化的几种方式——深入解析RDB
  3. AngularJS: Error reports on $injector:modulerr
  4. tomcat 部署springboot 项目
  5. MemoryStream相关知识分享
  6. linux网络测试命令
  7. 解构ffmpeg(二)
  8. 迁移桌面程序到MS Store(12)——WPF使用UWP InkToolbar和InkCanvas
  9. 以太网驱动的流程浅析(一)-Ifconfig主要流程【原创】
  10. Excel导入数据库(php版)