zoukankan      html  css  js  c++  java
  • UVA 11995 I Can Guess the Data Structure! STL

      题目链接: UVA很难登上去吧......

      题目大意: 给你几组输入与输出让你判断是栈,队列, 堆, 还是不确定, 还是哪种也不是

      解题思路: 这题看起来很简单, 搞几个标准STL, 和结果一对比就知道了, 坑点就是栈, 队列或堆在判断.top的时候会溢出造成RE, 在这儿坑了两次, 一开始还以为是不是全局不行

      代码: 

    #include <queue>
    #include <stack>
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int ans[100];
    stack<int> S;
    queue<int> Q;
    priority_queue<int> PQ;
    
    int main() {
        int n;
        while( scanf( "%d", &n ) != EOF ) {
            while( !S.empty() ) {
                S.pop();
            }
            while( !Q.empty() ) {
                Q.pop();
            }
            while( !PQ.empty() ) {
                PQ.pop();
            }
            ans[0] = ans[1] = ans[2] = 1;
            while( n-- ) {
                int op, num;
                scanf( "%d%d", &op, &num );
                if( op == 1 ) {
                    S.push(num);
                    Q.push(num);
                    PQ.push(num);
                }
                else {
                    if( ans[0] && !Q.empty() ) {
                        int temp = Q.front();
                        Q.pop();
                        if( num != temp ) ans[0] = 0;
                    }
                    else ans[0] = 0;
                    if( ans[1] && !S.empty() ) {
                        int temp = S.top();
                        S.pop();
                        if( num != temp ) ans[1] = 0;
                    }
                    else ans[1] = 0;
                    if( ans[2] && !PQ.empty() ) {
                        int temp = PQ.top();
                        PQ.pop();
                        if( num != temp ) ans[2] = 0;
                    }
                    else ans[2] = 0;
                }
            }
            int sum = ans[0] + ans[1] + ans[2];
            if( sum == 1 ) {
                if( ans[0] == 1 ) {
                    printf( "queue\n" );
                }
                else if( ans[1] == 1 ) {
                    printf( "stack\n" );
                }
                else {
                    printf( "priority queue\n" );
                }
            }
            else if( sum == 0 ) {
                printf( "impossible\n" );
            }
            else {
                printf( "not sure\n" );
            }
        }
        return 0;
    }
    View Code

      思考: 这题就是考细心的啊, 还记得去年第一次组队打多校的时候就是遇到了这个坑点, 总RE, 最后花了三个多小时才找到错误, 自己现在却还是不长记性, 所以以后遇到栈等需要取偷拍top或者pop操作的时候, 先判空!

  • 相关阅读:
    查看unity打来的包在手机上面查看日志
    Unity 打包出来动态加载图片丢失问题
    嵌套列表拖拽事件冲突问题
    游戏中实现鼠标拖尾效果
    2048
    面试知识点积累
    ARM处理器架构理论知识
    計算機網絡知識點總結:
    collection
    demo002.链表操作
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7240235.html
Copyright © 2011-2022 走看看