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

    https://vjudge.net/problem/UVA-11995

    题意:

    现在有一个未知的数据结构,给出n个操作,如果说操作为1的话,给出一个x放入这个结构,如果说操作为2的话,表示从里面拿出来了一个数x。

    现在问是否有一种数据结构符合这组数据的操作,给定的数据结构有栈,队列,优先队列。

    思路:

    由于有了现成的stl,那么直接模拟算算是否符合就行了,注意容器要判断容器是否为空。

    代码:

      1 #include <stdio.h>
      2 #include <stack>
      3 #include <queue>
      4 using namespace std;
      5 
      6 stack<int> s;
      7 queue<int> q;
      8 priority_queue<int> pq;
      9 
     10 struct node
     11 {
     12     int op;
     13     int x;
     14 } a[1005];
     15 
     16 int main()
     17 {
     18     int n;
     19 
     20     while (scanf("%d",&n) != EOF)
     21     {
     22         while (!s.empty()) s.pop();
     23         while (!q.empty()) q.pop();
     24         while (!pq.empty()) pq.pop();
     25 
     26         for (int i = 0;i < n;i++)
     27         {
     28             scanf("%d%d",&a[i].op,&a[i].x);
     29         }
     30 
     31         bool f = 0,ff = 0,fff = 0;
     32 
     33         for (int i = 0;i < n;i++)
     34         {
     35             if (a[i].op == 1)
     36             {
     37                 s.push(a[i].x);
     38             }
     39             else
     40             {
     41                 int tmp;
     42 
     43                 if (s.empty())
     44                 {
     45                     f = 1;break;
     46                 }
     47                 else if (!s.empty()) tmp = s.top(),s.pop();
     48 
     49                 if (tmp != a[i].x)
     50                 {
     51                     f = 1;break;
     52                 }
     53             }
     54         }
     55 
     56         for (int i = 0;i < n;i++)
     57         {
     58             if (a[i].op == 1)
     59             {
     60                 q.push(a[i].x);
     61             }
     62             else
     63             {
     64                 if (q.empty())
     65                 {
     66                     ff = 1;break;
     67                 }
     68                 else if (!q.empty())
     69                 {
     70                     int tmp = q.front();q.pop();
     71 
     72                     if (tmp != a[i].x)
     73                     {
     74                         ff = 1;break;
     75                     }
     76                 }
     77             }
     78         }
     79 
     80         for (int i = 0;i < n;i++)
     81         {
     82             if (a[i].op == 1) pq.push(a[i].x);
     83             else
     84             {
     85                 if (pq.empty())
     86                 {
     87                     fff = 1;break;
     88                 }
     89                 else if (!pq.empty())
     90                 {
     91                     int tmp = pq.top();pq.pop();
     92 
     93                     if (tmp != a[i].x)
     94                     {
     95                         fff = 1;break;
     96                     }
     97                 }
     98             }
     99         }
    100 
    101         int cnt = 0;
    102 
    103         if (f) cnt++;
    104         if (ff) cnt++;
    105         if (fff) cnt++;
    106 
    107         if (cnt == 3) printf("impossible
    ");
    108         else if (cnt <= 1) printf("not sure
    ");
    109         else
    110         {
    111             if (!f) printf("stack
    ");
    112             if (!ff) printf("queue
    ");
    113             if (!fff) printf("priority queue
    ");
    114         }
    115     }
    116 
    117 
    118 
    119     return 0;
    120 }
  • 相关阅读:
    IPv6基础介绍
    SNMP(Simple Network Mnagement Protocol)——简单网络管理协议详解
    GRE(Generic Routing Encapsulation)——通用路由封装协议详解
    NAT(Network Address Translation)网络地址转换详解
    PPPoE(Point to Point Protocol over Ethernet)——以太网上的点对点协议详解
    链路聚合详解——Link Aggregation
    MongoDB快速copy笔记
    MongoDB导入导出和踩过的坑
    Linux离线安装RabbitMQ
    VSCode 开发、运行和调试
  • 原文地址:https://www.cnblogs.com/kickit/p/7635938.html
Copyright © 2011-2022 走看看