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).
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
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).
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
Sample Output
queue
not sure
impossible
stack
priority queue
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
struct node
{
int x;
bool operator<(node a)const
{
return x<a.x;
}
} a;
priority_queue <node> P;
queue<node> Q;
stack<node> S;
int main()
{
int n,k,x;
while(~scanf("%d",&n))
{
while(!Q.empty())Q.pop();
while(!S.empty())S.pop();
while(!P.empty())P.pop();
int kp,kq,ks;
kp=kq=ks=1;
while(n--)
{
scanf("%d%d",&k,&a.x);
if(k==1)
{
P.push(a);
Q.push(a);
S.push(a);
}
else
{
node r;
if(!P.empty()&&kp)
{
r = P.top();
P.pop();
if(r.x!=a.x)
kp=0;
}
else
kp=0;
if(!Q.empty()&&kq)
{
r = Q.front();
Q.pop();
if(r.x!=a.x)
kq=0;
}
else
kq = 0;
if(!S.empty()&&ks)
{
r = S.top();
S.pop();
if(r.x!=a.x)
ks=0;
}
else
ks = 0;
}
}
if(ks&&!kq&&!kp)
puts("stack");
else if(!ks&&kq&&!kp)
puts("queue");
else if(!ks&&!kq&&kp)
puts("priority queue");
else if(!ks&&!kq&&!kp)
puts("impossible");
else
puts("not sure");
}
return 0;
}
本文深入探讨了数据结构如栈、队列、优先队列的特性与操作,并通过实例展示了如何通过序列操作来判断数据结构类型,涉及算法与数据结构的基础理论与实践应用。

571

被折叠的 条评论
为什么被折叠?



