码迷,mamicode.com
首页 > 其他好文 > 详细

uva 11995 I Can Guess the Data Structure!

时间:2017-10-07 23:38:07      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:print   code   优先   imp   uva   nbsp   scan   col   优先队列   

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\n");
108         else if (cnt <= 1) printf("not sure\n");
109         else
110         {
111             if (!f) printf("stack\n");
112             if (!ff) printf("queue\n");
113             if (!fff) printf("priority queue\n");
114         }
115     }
116 
117 
118 
119     return 0;
120 }

 

uva 11995 I Can Guess the Data Structure!

标签:print   code   优先   imp   uva   nbsp   scan   col   优先队列   

原文地址:http://www.cnblogs.com/kickit/p/7635938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!