zoukankan      html  css  js  c++  java
  • uva 11995 判别数据类型

    Problem I

    I Can Guess the Data Structure!

    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 is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

    Input

    There 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 x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

    Output

    For 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 mentioned above.

    Sample Input

    6
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    6
    1 1
    1 2
    1 3
    2 3
    2 2
    2 1
    2
    1 1
    2 2
    4
    1 2
    1 1
    2 1
    2 2
    7
    1 2
    1 5
    1 1
    1 3
    2 5
    1 4
    2 4
    

    Output for the Sample Input

    queue
    not sure
    impossible
    stack
    priority queue

    题目大意:根据数据的操作<1>插入<2>删除判别是哪一种数据类型,有栈、队列、优先队列几种类型,如三种
    都不符合输出impossbile,符合两种以上输出not sure,只符合一种的对应输出它的类型名称。

    #include <iostream>
    #include <queue>
    #include <stack>
    #include <cstdio>
    #define Max 1010
    using namespace std;
    
    int N;
    int op[Max],num[Max];
    stack<int> S;
    queue<int> Q;
    priority_queue<int> PQ;
    int check_stack()
    {
        int i,t;
        while(!S.empty()) S.pop();
        for(i=0;i<N;i++)
        {
            if(op[i]==2)
            {
                if(S.empty()) return 0;
                t=S.top();S.pop();
                if(t!=num[i]) return 0;
            }
            else S.push(num[i]);
        }
        return 1;
    }
    int check_queue()
    {
        int i,t;
        while(!Q.empty()) Q.pop();
        for(i=0;i<N;i++)
        {
            if(op[i]==2)
            {
                if(Q.empty()) return 0;
                t=Q.front();Q.pop();
                if(t!=num[i]) return 0;
            }
            else Q.push(num[i]);
        }
        return 1;
    }
    int check_priorityqueue()
    {
        int i,t;
        while(!PQ.empty()) PQ.pop();
        for(i=0;i<N;i++)
        {
            if(op[i]==2)
            {
                if(PQ.empty()) return 0;
                t=PQ.top();PQ.pop();
                if(t!=num[i]) return 0;
            }
            else PQ.push(num[i]);
        }
        return 1;
    }
    int main()
    {
        int i;
        int s,q,pq;
        while(scanf("%d",&N)!=EOF)
        {
            for(i=0;i<N;i++) scanf("%d %d",op+i,num+i);
            s=check_stack();
            q=check_queue();
            pq=check_priorityqueue();
            if(!s && !q && !pq) printf("impossible
    ");
            else if(s && !q && !pq) printf("stack
    ");
            else if(!s && q && !pq) printf("queue
    ");
            else if(!s && !q && pq) printf("priority queue
    ");
            else printf("not sure
    ");
        }
        return 0;
    }

  • 相关阅读:
    汉诺塔学习笔记,有不正确的地方请小伙伴们指正~·~
    梯有N阶,上楼可以一步上一阶,也可以一步上二阶。编写一个程序,计算共有多少中不同的走法?
    HTTP Status 500
    java基础知识
    JAVA多线程和并发基础面试问答
    thymeleaf中的th:remove用法
    thymeleaf:局部变量 th:with
    springboot: thymeleaf 使用详解
    eclipse修改工作目录颜色
    The user specified as a definer ('root'@'%') does not exist
  • 原文地址:https://www.cnblogs.com/xiong-/p/3533962.html
Copyright © 2011-2022 走看看