UVA - 11995 I Can Guess the Data Structure!

题目大意:给你一个移动方式,判断它是队列,栈,还是优先队列。


解题思路:直接用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;
}


你可能感兴趣的:(UVA - 11995 I Can Guess the Data Structure!)