UVA 11995

I Can Guess the Data Structure!

Time limit: 1.000 seconds

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

考查基本数据结构定义,实现判断即可。

/*
* @author  Panoss
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<ctime>
#include<stack>
#include<queue>
#include<list>
using namespace std;
#define DBG 0
#define fori(i,a,b) for(int i = (a); i < (b); i++)
#define forie(i,a,b) for(int i = (a); i <= (b); i++)
#define ford(i,a,b) for(int i = (a); i > (b); i--)
#define forde(i,a,b) for(int i = (a); i >= (b); i--)
#define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i])
#define mset(a,v) memset(a, v, sizeof(a))
#define mcpy(a,b) memcpy(a, b, sizeof(a))
#define dout  DBG && cerr << __LINE__ << " >>| "
#define checkv(x) dout << #x"=" << (x) << " | "<<endl
#define checka(array,a,b) if(DBG) { \
    dout << #array"[] | " << endl;     forie(i, a, b) cerr << "[" << i << "]=" << array[i] << " |" << ((i - (a)+1) % 5 ? " " : "\n"); if (((b)-(a)+1) % 5) cerr << endl; }
#define redata(T, x) T x; cin >> x
#define MIN_LD -2147483648
#define MAX_LD  2147483647
#define MIN_LLD -9223372036854775808
#define MAX_LLD  9223372036854775807
#define MAX_INF 18446744073709551615
inline int  reint() { int d; scanf("%d", &d); return d; }
inline long relong() { long l; scanf("%ld", &l); return l; }
inline char rechar() { scanf(" "); return getchar(); }
inline double redouble() { double d; scanf("%lf", &d); return d; }
inline string restring() { string s; cin >> s; return s; }

struct Cmd
{
    int cmd;
    int x;
};

vector<Cmd> C;

bool is_stack()
{
    stack<int> S;
    fori(i,0,C.size())
    {
        if(C[i].cmd == 1)
            S.push(C[i].x);
        else
        {
            if(S.empty()) return false;
            int value = S.top();
            S.pop();
            if(value != C[i].x) return false;
        }
    }
    return true;
}

bool is_queue()
{
    queue<int> Q;
    fori(i,0,C.size())
    {
        if(C[i].cmd == 1)
            Q.push(C[i].x);
        else
        {
            if(Q.empty()) return false;
            int value = Q.front();
            Q.pop();
            if(value != C[i].x) return false;
        }
    }
    return true;
}

bool is_pri_queue()
{
    priority_queue<int> Q;
    fori(i,0,C.size())
    {
        if(C[i].cmd == 1)
            Q.push(C[i].x);
        else
        {
            if(Q.empty()) return false;
            int value = Q.top();
            Q.pop();
            if(value != C[i].x) return false;
        }
    }
    return true;
}

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        C.clear();
        forie(i,1,n)
        {
            Cmd command;
            scanf("%d%d",&command.cmd,&command.x);
            C.push_back(command);
        }
        bool is_st = is_stack();
        bool is_qu = is_queue();
        bool is_pri_qu = is_pri_queue();
        if(!is_st&&!is_qu&&!is_pri_qu) puts("impossible");
        else if(is_st&&is_qu&&is_pri_qu) puts("not sure");
        else if((is_st&&is_qu&&!is_pri_qu)||(is_st&&!is_qu&&is_pri_qu)||(!is_st&&is_qu&&is_pri_qu)) puts("not sure");
        else if(is_st&&!is_qu&&!is_pri_qu) puts("stack");
        else if(!is_st&&is_qu&&!is_pri_qu) puts("queue");
        else if(!is_st&&!is_qu&&is_pri_qu) puts("priority queue");
    }

    return 0;
}

UVA 11995

时间: 2024-07-30 10:19:24

UVA 11995的相关文章

【map离散化+打表】UVA 11995 I Can Guess the Data Structure!

[map离散化+打表]UVA 11995 I Can Guess the Data Structure! map关联容器:有序 + 映射,查找的复杂度O(nlogn) 题目大意 给你n个数构成的数组,求数v第k次出现的下标值(下标从1开始) – 说一下思路 这题很显然要打表预处理,关键是怎么打这张表 1.首先我们观察到v很大,开一个二维数组data[v][k]肯定存储不了,所以用map离散化(自动有序编号,避免空间浪费) 2.data[v]肯定是一个变长的数组,存储数据v出现0,1,2次--的下

uva 11995 I Can Guess the Data Structure 数据结构

// uva 11995 数据结构 // 给你一些操作,确定是队列还是栈还是优先队列(数值大的优先级大) // 简单题,练练基础吧相当于 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <c

UVA 11995 - I Can Guess the Data Structure! (基本数据结构)

UVA 11995 - I Can Guess the Data Structure! 题目链接 题意:给定一堆的操作,问这个数据结构是什么 思路:水题,稍微模拟一下就可以了 代码: #include <cstdio> #include <cstring> #include <stack> #include <queue> using namespace std; const int N = 1005; int n, q[N][2]; bool is_sta

【模拟+数据结构】UVA 11995 I Can Guess the Data Structure!

[模拟+数据结构]UVA 11995 I Can Guess the Data Structure! 题目大意 给出一系列操作,包含操作数和操作码,判断符合这一系列操作返回值的数据结构类型(栈.队列.优先队列) – 说一下思路 拿这三种数据结构去模拟一下就可以了 [注意]栈顶 stack.top() 队首 queue.front() 堆顶 priority_queue.top() 堆又叫做优先队列heap == priority_queue 参考代码 #include<bits/stdc++.h

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

[UVA] 11995 - I Can Guess the Data Structure! [STL应用]

11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds 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

uva 11995 I Can Guess the Data Structure stack,queue,priority_queue

题意:给你n个操做,判断是那种数据结构. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 int n; 8 int v[1010],u[1010]; 9 10 int ck_q() 11 { 12 //cout<<"!!"&

uva 11995(stl)

题意:1 x,表示放进x元素,2表示拿出一个元素,给出n条指令,然后2 x表示取出的数据是什么,问可以从输入输出判断出是哪种数据结构(栈,队列,优先队列),如果有多种满足,就输出not sure,都不是就输出impossible. 题解:直接定义三个数据结构的stl变量,然后模拟放入数据,到拿出数据时和三种比对判断,可以知道是哪种数据结构. #include <stdio.h> #include <queue> #include <stack> using namesp

UVA 11995 STL 使用

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-I