I Can Guess the Data Structure!
There is a bag-like data structure, supporting two operations:
1 x1 x: Throw an element xx into the bag.
22: 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 nn (1≤n≤10001≤n≤1000). Each of the next nn lines is either a type-1 command, or an integer 22 followed by an integer xx. This means that executing the type-2 command returned the element xx. The value of xx is always a positive integer not larger than 100100. 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 1 | Sample Output 1 |
---|---|
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 1 2 1 |
queue not sure impossible stack priority queue impossible |
题意
模拟栈stack,队列queue,和优先队列priority queue的进出,判断属于哪一个
思路
用stl模拟
代码
#include<bits/stdc++.h> using namespace std; int main() { int n, a, x; while(cin >> n) { stack<int> s; queue<int> q; priority_queue<int> pq; bool iss = 1, isq = 1, ispq = 1; while(n--) { cin >> a >> x; if(a == 1) { s.push(x); q.push(x); pq.push(x); } else { if(s.empty() || s.top() != x) iss = 0; if(q.empty() || q.front() != x) isq = 0; if(pq.empty() || pq.top() != x) ispq = 0; if(!s.empty()) s.pop(); if(!q.empty()) q.pop(); if(!pq.empty()) pq.pop(); } } if(iss + isq + ispq > 1) puts("not sure"); else if(iss) puts("stack"); else if(isq) puts("queue"); else if(ispq) puts("priority queue"); else puts("impossible"); } }