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

    I Can Guess the Data Structure!

    There is a bag-like data structure, supporting two operations:

    1 x

    Throw an element x into the bag.

    2

    Take out an element from the bag.

    Given a sequence of operations with return values, you're going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

    Input

    There are several test cases. Each test case begins with a line containing a single integer n (1<=n<=1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

    Output

    For each test case, output one of the following:

    stack

    It's definitely a stack.

    queue

    It's definitely a queue.

    priority queue

    It's definitely a priority queue.

    impossible

    It can't be a stack, a queue or a priority queue.

    not sure

    It can be more than one of the three data structures mentioned above.

    Sample Input

    6
    1 1
    1 2
    1 3
    2 1
    2 2
    2 3
    6
    1 1
    1 2
    1 3
    2 3
    2 2
    2 1
    2
    1 1
    2 2
    4
    1 2
    1 1
    2 1
    2 2
    7
    1 2
    1 5
    1 1
    1 3
    2 5
    1 4
    2 4
    

    Output for the Sample Input

    queue
    not sure
    impossible
    stack
    priority queue


    题目大意:有一个类似“包包”的数据结构,支持两种操作。1x:把元素x放进包包;2:从包包中拿出一个元素。给出一系列的操作以及返回值,你的任务是猜猜这个“包包”到底是什么。它可能是一个栈,队列,优先队列或者其他什么奇怪的东西。

    分析:只需要依次判断输入是否可能是栈,队列或优先队列,然后中合起来即可。注意到题目中说的“无错的返回”,因此在执行pop操作的时候要调用一下empty(),否则可能会异常退出。

    代码如下:
     1 #include<cstdio>
     2 #include<queue>
     3 #include<stack>
     4 #include<cstdlib>
     5 using namespace std;
     6 
     7 const int maxn = 1000 + 10;
     8 int n, t[maxn], v[maxn];
     9 
    10 int check_stack() {
    11   stack<int> s;
    12   for(int i = 0; i < n; i++) {
    13     if(t[i] == 2) {
    14       if(s.empty()) return 0;
    15       int x = s.top(); s.pop();
    16       if(x != v[i]) return 0;
    17     }
    18     else s.push(v[i]);
    19   }
    20   return 1;
    21 }
    22 
    23 int check_queue() {
    24   queue<int> s;
    25   for(int i = 0; i < n; i++) {
    26     if(t[i] == 2) {
    27       if(s.empty()) return 0;
    28       int x = s.front(); s.pop();
    29       if(x != v[i]) return 0;
    30     }
    31     else s.push(v[i]);
    32   }
    33   return 1;
    34 }
    35 
    36 int check_pq() {
    37   priority_queue<int> s;
    38   for(int i = 0; i < n; i++) {
    39     if(t[i] == 2) {
    40       if(s.empty()) return 0;
    41       int x = s.top(); s.pop();
    42       if(x != v[i]) return 0;
    43     }
    44     else s.push(v[i]);
    45   }
    46   return 1;
    47 }
    48 
    49 int main() {
    50   while(scanf("%d", &n) == 1) {
    51     for(int i = 0; i < n; i++) scanf("%d%d", &t[i], &v[i]);
    52     int s = check_stack();
    53     int q = check_queue();
    54     int pq = check_pq();
    55     if(!s && !q && !pq) printf("impossible
    ");
    56     else if(s && !q && !pq) printf("stack
    ");
    57     else if(!s && q && !pq) printf("queue
    ");
    58     else if(!s && !q && pq) printf("priority queue
    ");
    59     else printf("not sure
    ");
    60   }
    61   return 0;
    62 }
     
  • 相关阅读:
    64_l2
    64_l1
    64_k2
    64_k1
    64_j2
    64_j1
    64_g6
    64_g5
    64_g4
    64_g3
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3201667.html
Copyright © 2011-2022 走看看