您的位置:首页 > 产品设计 > UI/UE

UVA - 11995 I Can Guess the Data Structure!

2015-03-31 20:24 134 查看
题目大意:给你一个移动方式,判断它是队列,栈,还是优先队列。

解题思路:直接用STL里面的队列,栈,和优先队列去模拟,出现矛盾说明不是

#include <cstdio>
#include <queue>
#include <stack>
using namespace std;

int main() {
int n;
while (scanf("%d", &n) != EOF) {
stack<int> st;
queue<int> qu;
priority_queue<int> pq;
int S = 1, Q = 1, P = 1;
for (int i = 0; i < n; i++) {
int c, x;
scanf("%d%d", &c, &x);
if (c == 1) {
st.push(x);
qu.push(x);
pq.push(x);
} else {
if (st.empty()) {
S = Q = P = 0;
continue;
}
if (S)
S = (st.top() == x);
if (Q)
Q = (qu.front() == x);
if (P)
P = (pq.top() == x);
st.pop();
qu.pop();
pq.pop();
}
}

if (S + Q + P > 1)
puts("not sure");
else if (S)
puts("stack");
else if (Q)
puts("queue");
else if (P)
puts("priority queue");
else
puts("impossible");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: