11995 - I Can Guess the Data Structure!


思路:即对数据结构进行模拟,若是其中一个,则输出对应数据结构,若可能是其中的两个,则输出not sure 若都不是则输出impossible。
代码:
#include<stdio.h>
#include<stack>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
//0: queue 1:stack 2:Priority Queue
int type[3];
//操作结构 保存操作的类型与操作数
struct oper {
	int op, d;
}o[1005];
int main() {
	int n;
	while (~scanf("%d", &n)){
		memset(type, 0, sizeof type);
		for (int i = 0; i < n; i++)
			scanf("%d%d", &o[i].op, &o[i].d);
		queue<int>q;
		stack<int>s;
		priority_queue<int>pq;
		for (int i = 0; i < n; i++) {
			if (o[i].op == 1) {
				q.push(o[i].d);
			}
			else {
				if (q.empty()) break;
				int e = q.front();
				q.pop();
				if (o[i].d != e) break;
			}
			if (i == n - 1) type[0] = 1;
		}
		for (int i = 0; i < n; i++) {
			if (o[i].op == 1)
				s.push(o[i].d);
			else {
				if (s.empty()) break;
				int e = s.top();
				s.pop();
				if (e != o[i].d) break;
			}
			if (i == n - 1) type[1] = 1;
		}
		for (int i = 0; i < n; i++) {
			if (o[i].op == 1)
				pq.push(o[i].d);
			else {
				if (pq.empty()) break;
				int e = pq.top();
				pq.pop();
				if (e != o[i].d) break;
			}
			if (i == n - 1) type[2] = 1;
		}
		int sum = 0;
		for (int i = 0; i < 3; i++)
			sum += type[i];
		if (sum == 0)
			printf("impossible\n");
		else if (sum > 1)
			printf("not sure\n");
		else if (type[0] == 1)
			printf("queue\n");
		else if (type[1] == 1)
			printf("stack\n");
		else if (type[2] == 1)
			printf("priority queue\n");
		
		
	}
	return 0;
}







猜你喜欢

转载自blog.csdn.net/Titanium_S/article/details/79198494