網頁

2014年3月1日 星期六

UVa 11995 I Can Guess the Data Structure

想法:
    既然C++提供STL了,那就直接拿來用,模擬實際情況,但取値或pop()的時候要注意該Container是否為empty,否則會Runtime Error。



#include <cstdio>
#include <stack>
#include <queue>
using namespace std;
int main()
{
// freopen("input.txt","rt",stdin);
int N, Command, num;
while (scanf("%d", &N) != EOF) {
stack<int> S;
queue<int> Q;
priority_queue<int> PQ;
bool isStack = 1, isQueue = 1, isPriQueue = 1;
while (N--) {
scanf("%d%d", &Command, &num);
if (Command == 1) {
S.push(num);
Q.push(num);
PQ.push(num);
}
else {
if (S.empty() || S.top() != num) isStack = 0;
if (Q.empty() || Q.front() != num) isQueue = 0;
if (PQ.empty() || PQ.top() != num) isPriQueue = 0;
if (!S.empty()) S.pop();
if (!Q.empty()) Q.pop();
if (!PQ.empty()) PQ.pop();
}
}
if (isStack + isQueue + isPriQueue > 1)
puts("not sure");
else if (isStack) puts("stack");
else if (isQueue) puts("queue");
else if (isPriQueue) puts("priority queue");
else puts("impossible");
}
return 0;
}

沒有留言:

張貼留言