UVA 11995 - I Can Guess the Data Structure! (基本数据结构)

本文介绍了一道名为UVA11995-ICanGuesstheDataStructure!的编程题解。题目要求通过一系列操作判断所使用的数据结构类型。文章提供了一个简单的模拟解决方案,并附带了完整的C++代码实现,该方案能够判断是否为栈、队列或优先队列。

UVA 11995 - I Can Guess the Data Structure!

题目链接

题意:给定一堆的操作,问这个数据结构是什么

思路:水题,稍微模拟一下就可以了

代码:

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

const int N = 1005;

int n, q[N][2];

bool is_stack() {
    stack<int> S;
    for (int i = 0; i < n; i++) {
	if (q[i][0] == 1) {
	    S.push(q[i][1]);
	}
	else {
	    if (S.empty() || S.top() != q[i][1]) return false;
	    S.pop();
	}
    }
    return true;
}

bool is_queue() {
    queue<int> Q;
    for (int i = 0; i < n; i++) {
	if (q[i][0] == 1) {
	    Q.push(q[i][1]);
	}
	else {
	    if (Q.empty() || Q.front() != q[i][1]) return false;
	    Q.pop();
	}
    }
    return true;
}

bool is_priqueue() {
    priority_queue <int> Q;
    for (int i = 0; i < n; i++) {
	if (q[i][0] == 1) {
	    Q.push(q[i][1]);
	}
	else {
	    if (Q.empty() || Q.top() != q[i][1]) return false;
	    Q.pop();
	}
    }
    return true;
}

void solve() {
    int cnt = 0, ans;
    if (is_stack()) ans = 0, cnt++;
    if (is_queue()) ans = 1, cnt++;
    if (is_priqueue()) ans = 2, cnt++;
    if (cnt == 0) printf("impossible\n");
    else if (cnt > 1) printf("not sure\n");
    else {
	if (ans == 0) printf("stack\n");
	else if (ans == 1) printf("queue\n");
	else printf("priority queue\n");
    }
}

int main() {
    while (~scanf("%d", &n)) {
	for (int i = 0; i < n; i++)
	    scanf("%d%d", &q[i][0], &q[i][1]);
	solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值