UVA 11995 I Can Guess the Data Structure!(ADT)

賈樹丙發表於2013-07-19

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\n");
56     else if(s && !q && !pq) printf("stack\n");
57     else if(!s && q && !pq) printf("queue\n");
58     else if(!s && !q && pq) printf("priority queue\n");
59     else printf("not sure\n");
60   }
61   return 0;
62 }

 

 

相關文章