• UVA11995 I Can Guess the Data Structure!


    思路

    简单题,用栈,队列,优先队列直接模拟即可

    代码

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <stack>
    using namespace std;
    queue<int> q;
    stack<int> S;
    priority_queue<int> pq;
    int n;
    int main(){
        while(scanf("%d",&n)==1){
            while(!q.empty())
                q.pop();
            while(!S.empty())
                S.pop();
            while(!pq.empty())
                pq.pop();
            bool isS=true,isq=true,ispq=true;
            for(int i=1;i<=n;i++){
                int opt,x;
                scanf("%d %d",&opt,&x);
                if(opt==1){
                    q.push(x);
                    pq.push(x);
                    S.push(x);
                }
                else{
                    if(q.empty())
                        isq=false;
                    if(pq.empty())
                        ispq=false;
                    if(S.empty())
                        isS=false;
                    if(isq){
                        int t=q.front();
                        if(t!=x)
                            isq=false;
                        q.pop();
                    }
                    if(ispq){
                        int t=pq.top();
                        if(t!=x)
                            ispq=false;
                        pq.pop();
                    }
                    if(isS){
                        int t=S.top();
                        if(t!=x)
                            isS=false;
                        S.pop();
                    }
                }
            }
            if((!ispq)&&(!isq)&&(!isS))
                printf("impossible
    ");
            else if((ispq)&&(!isq)&&(!isS))
                printf("priority queue
    ");
            else if((!ispq)&&(isq)&&(!isS))
                printf("queue
    ");
            else if((!ispq)&&(!isq)&&(isS))
                printf("stack
    ");
            else
                printf("not sure
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    oracle对中文的排序
    sql语句分页
    一些简单的PGSQL 操作
    angularJSngSelect
    angular+ionic返回上一页并刷新
    C语言博客作业02循环结构
    c语言第一次作业顺序、分支结构
    C语言博客作业03函数
    C语言博客作业02循环结构
    第零次作业
  • 原文地址:https://www.cnblogs.com/dreagonm/p/10681208.html
Copyright © 2020-2023  润新知