码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-26 02:39:56      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   数据   io   for   re   

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;
}


UVA 11995 - I Can Guess the Data Structure! (基本数据结构),布布扣,bubuko.com

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

标签:style   http   color   os   数据   io   for   re   

原文地址:http://blog.csdn.net/accelerator_/article/details/38118745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!