UVA 11995 - I Can Guess the Data Structure!

时间限制:1.000秒

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


  给你一个未知的数据结构,支持两个操作:
</pre><pre name="code" class="plain">1 x:向这个数据结构内放入一个元素
2 x:从这个数据结构中拿出一个元素,返回值是x
  输入是多组数据,每组操作数目最多为1000,数据结构是栈(stack)、队列(queue)、优先队列(priority_queue)的一种,求给出的是哪一种数据结构,结果可能为:
stack:确定数据结构是栈
queue:确定数据结构是队列.
priority queue:确定数据结构是优先队列
impossible:不是这三种数据结构中的任何一种,或者这种数据结构不存在
not sure:不能够确定是这三种数据结构中的哪一种,存在多种可能

  题意也比较简单,单纯地模拟即可。

#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       

using namespace std;

int main() {
    int n;
    while(~scanf("%d", &n)) {
        stack
       
       
        
         a; queue
        
        
          b; priority_queue 
         
           c; bool ok_a = true, ok_b = true, ok_c = true; int cmd, d; for(int i = 0; i != n; ++i) { scanf("%d%d", &cmd, &d); if(cmd == 1) { if(ok_a) a.push(d); if(ok_b) b.push(d); if(ok_c) c.push(d); } else { if(ok_a) { if(a.empty()) ok_a = false; else { if(d != a.top()) ok_a = false; else a.pop(); } } if(ok_b) { if(b.empty()) ok_b = false; else { if(d != b.front()) ok_b = false; else b.pop(); } } if(ok_c) { if(c.empty()) ok_c = false; else { if(d != c.top()) ok_c = false; else c.pop(); } } } } int num = 0; if(ok_a) ++num; if(ok_b) ++num; if(ok_c) ++num; if(!num) printf("impossible\n"); else if(num > 1) printf("not sure\n"); else { if(ok_a) printf("stack\n"); else if(ok_b) printf("queue\n"); else if(ok_c) printf("priority queue\n"); } } } 
          
        
       
       
      
      
     
     
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值