题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3146

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

题意:有一种名叫包的数据类型,要求通过输入输出情况判断是哪种数据类型。

思路:模拟题,将三种数据类型模拟出来,按照操作进行模拟。如果出现不符合的情况,标记出来,最后通过标记分析出来可能的数据结构。

注意:要注意不同情况的初始化,或者将数据类型定义在每种情况的局部。

代码:

UVA 11995 (STL_E题)解题报告UVA 11995 (STL_E题)解题报告
#include<cstdio>
#include<stack>
#include<queue>

using namespace std;
int n;

int main(void){
    
    while(~scanf("%d",&n)){
        int flag[]={};
        stack<int> s;
        queue<int> q;
        priority_queue<int> pq; 
        for(int i =;i<n;i++){
            int a =;int b =;
            scanf("%d %d",&a,&b);
            
            if(a ==){
                s.push(b);
                q.push(b);
                pq.push(b); 
            }else {
                if(!s.empty())  
                {  
                    if(s.top() != b)  flag[] = ;  
                    s.pop();  
                }  
                else  flag[] = ;  

                if(!q.empty())  
                {  
                    if(q.front() != b)  flag[] = ;  
                    q.pop();  
                }  
                else  flag[] = ;  
  
                if(!pq.empty())  
                {  
                    if(pq.top() != b)  flag[] = ;  
                    pq.pop();  
                }  
                else  flag[] = ;  
            }  
            }
            int count = ;
            for(int i =;i<;i++){
                count+=flag[i];
            }
            if(count ==) printf("impossible\n");
            else if(count<) printf("not sure\n");
            else if(flag[]==) printf("stack\n");
            else if(flag[]==) printf("queue\n");
            else if(flag[]==) printf("priority queue\n");
        }    
    return ;
}
View Code