uva 11995 I Can Guess the Data Structure!

简介: 点击打开链接uva 11995 思路: STL模拟 分析: 1 分别用三种STL去模拟这些操作,然后进行判断输出 2 比较简单 代码: #include#include#include#include#include#i...

点击打开链接uva 11995

思路: STL模拟
分析:
1 分别用三种STL去模拟这些操作,然后进行判断输出
2 比较简单


代码:

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

stack<int> stk;
queue<int> que;
priority_queue<int> p_que;
int n;
bool is_stack , is_queue , is_Pqueue;

void init(){
    while(!stk.empty())
        stk.pop();
    while(!que.empty())
        que.pop();
    while(!p_que.empty())
        p_que.pop();
    is_stack = is_queue = is_Pqueue = true;
}

void insert(int x){
    stk.push(x);
    que.push(x);
    p_que.push(x);
}

void pop(int x){
    if(is_stack){
       if(!stk.empty() && x == stk.top()) 
           stk.pop();
       else
           is_stack = false;
    }      
    if(is_queue){
       if(!que.empty() && x == que.front()) 
           que.pop();
       else
           is_queue = false;
    }      
    if(is_Pqueue){
       if(!p_que.empty() && x == p_que.top()) 
           p_que.pop();
       else
           is_Pqueue = false;
    }    
}

void output(){
    if(is_stack && !is_queue && !is_Pqueue)
        puts("stack");
    else if(!is_stack && is_queue && !is_Pqueue)
        puts("queue");
    else if(!is_stack && !is_queue && is_Pqueue)
        puts("priority queue");
    else if(!is_stack && !is_queue && !is_Pqueue)
        puts("impossible");
    else
        puts("not sure");
}

int main(){
    int x , y;
    while(scanf("%d" , &n) != EOF){
        init(); 
        for(int i = 0 ; i < n ; i++){
            scanf("%d%d" , &x , &y);  
            if(x == 1)
                insert(y);
            else
                pop(y);
        }
        output();
    }    
    return 0;
}



目录
相关文章
UVa11565 - Simple Equations
UVa11565 - Simple Equations
54 0
UVa1583 - Digit Generator
UVa1583 - Digit Generator
53 0
|
存储 索引
LeetCode 73. Set Matrix Zeroes
给定一个m * n 的矩阵,如果当前元是0,则把此元素所在的行,列全部置为0. 额外要求:是否可以做到空间复杂度O(1)?
104 0
LeetCode 73. Set Matrix Zeroes
|
存储 NoSQL 关系型数据库
Three industries you didn't realize are benefitting from big data
Big data and analytics have long since transitioned from being buzzwords to delivering tangible business results.
1893 0
[LeetCode]--73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution us
986 0
[LeetCode]--71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, =&gt; “/home” path = “/a/./b/../../c/”, =&gt; “/c” click to show corner cases. Corner Cases:
1068 0
[LeetCode]--40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the
1291 0
|
算法
[LeetCode]--39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimit
1030 0