您的位置:首页 > 产品设计 > UI/UE

UVA - 11995 I Can Guess the Data Structure! STL模板

2015-03-22 20:28 393 查看
题目大意:有n个操作,操作x y

x为1时表示放入1个数y

x为2时表示取出一个数并判断取出的数是否为y

完成n次操作后,要求判断这是什么数据结构

解题思路:用STL模板,记得判断是否为空

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

int n;
void solve() {
    int ans = 7, x, y, t, mark_S = 1, mark_Q = 1, mark_P = 1;
    priority_queue<int > pri_que;
    queue<int > q;
    stack<int > s;

    for(int i = 0; i < n; i++) {
        scanf("%d%d", &x, &y);
        if(x == 1) {
            s.push(y);
            pri_que.push(y);
            q.push(y);  
        }
        else if(x == 2) {
            if(mark_S) {
                if(s.empty()) {
                    mark_S = 0;
                }
                else {
                    t = s.top();
                    s.pop();
                    if(t != y) 
                        mark_S = 0;
                }
            }

            if(mark_Q) {
                if(q.empty())  
                    mark_Q = 0;
                else {
                    t = q.front();
                    q.pop();
                    if(t != y)
                        mark_Q = 0;
                }
            }
            if(mark_P) {
                if(pri_que.empty())
                    mark_P = 0;
                else {
                    t = pri_que.top();
                    pri_que.pop();
                    if(t != y)
                        mark_P = 0;
                }       
            }
        }
    }
    if(mark_P && !mark_S && !mark_Q) 
        printf("priority queue\n");
    else if(mark_Q && !mark_S && !mark_P)
        printf("queue\n");
    else if(mark_S && !mark_P && !mark_Q)
        printf("stack\n");
    else if(!mark_S && !mark_P && !mark_Q)
        printf("impossible\n");
    else
        printf("not sure\n");
}

int main() {
    while(scanf("%d", &n) == 1) {
        solve();    
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: