STL UVA 11995 I Can Guess the Data Structure!
题意:训练指南P186
分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法
#include <bits/stdc++.h> using namespace std; int main(void) { int n; while (scanf ("%d", &n) == 1) { stack<int> sta; queue<int> que; priority_queue<int, vector<int>, less<int> > pque; bool flag1 = true, flag2 = true, flag3 = true; for (int op, x, y, i=1; i<=n; ++i) { scanf ("%d%d", &op, &x); if (op == 1) { sta.push (x); que.push (x); pque.push (x); } else { if (sta.empty ()) flag1 = false; else { y = sta.top (); sta.pop (); if (y != x) flag1 = false; } if (que.empty ()) flag2 = false; else { y = que.front (); que.pop (); if (y != x) flag2 = false; } if (pque.empty ()) flag3 = false; else { y = pque.top (); pque.pop (); if (y != x) flag3 = false; } } } if (!flag1 && !flag2 && !flag3) puts ("impossible"); else if ((flag1 && flag2) || (flag1 && flag3) || (flag2 && flag3)) puts ("not sure"); else if (flag1) puts ("stack"); else if (flag2) puts ("queue"); else puts ("priority queue"); } return 0; }
编译人生,运行世界!