时间限制:1.000秒
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3146
给你一个未知的数据结构,支持两个操作:
</pre><pre name="code" class="plain">1 x:向这个数据结构内放入一个元素
2 x:从这个数据结构中拿出一个元素,返回值是x
输入是多组数据,每组操作数目最多为1000,数据结构是栈(stack)、队列(queue)、优先队列(priority_queue)的一种,求给出的是哪一种数据结构,结果可能为:
stack:确定数据结构是栈
queue:确定数据结构是队列.
priority queue:确定数据结构是优先队列
impossible:不是这三种数据结构中的任何一种,或者这种数据结构不存在
not sure:不能够确定是这三种数据结构中的哪一种,存在多种可能
#include
#include
#include
using namespace std;
int main() {
int n;
while(~scanf("%d", &n)) {
stack
a; queue
b; priority_queue
c; bool ok_a = true, ok_b = true, ok_c = true; int cmd, d; for(int i = 0; i != n; ++i) { scanf("%d%d", &cmd, &d); if(cmd == 1) { if(ok_a) a.push(d); if(ok_b) b.push(d); if(ok_c) c.push(d); } else { if(ok_a) { if(a.empty()) ok_a = false; else { if(d != a.top()) ok_a = false; else a.pop(); } } if(ok_b) { if(b.empty()) ok_b = false; else { if(d != b.front()) ok_b = false; else b.pop(); } } if(ok_c) { if(c.empty()) ok_c = false; else { if(d != c.top()) ok_c = false; else c.pop(); } } } } int num = 0; if(ok_a) ++num; if(ok_b) ++num; if(ok_c) ++num; if(!num) printf("impossible\n"); else if(num > 1) printf("not sure\n"); else { if(ok_a) printf("stack\n"); else if(ok_b) printf("queue\n"); else if(ok_c) printf("priority queue\n"); } } }