uva 11995 - I Can Guess the Data Structure!

Problem I

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


STL的应用,学习一下,做法就是先把输入的数据,存起来,然后用栈,队列,优先队列都试一下,看是不是满足就可以了。
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <stack>
 5 #include <queue>
 6 #include <iostream>
 7 using namespace std;
 8 struct data{
 9   int v; int k; 
10   friend bool operator < (data a, data b){
11     return a.v < b.v;
12   }
13 }s[1000+10];
14 int n;
15 int cstack(){
16   stack<int> qq;
17   for (int i = 0; i < n; ++i){
18     if (s[i].k == 1) qq.push(s[i].v);
19     if (s[i].k == 2) {
20       if (qq.empty() == 1) return 0;
21       if (qq.top() == s[i].v) {
22         qq.pop(); continue;
23       }
24       else return 0;
25     }
26   }
27   return 1;
28 }
29 int cqueue(){
30   queue<int> qq;
31   for (int i = 0; i < n; ++i){
32     if (s[i].k == 1) qq.push(s[i].v);
33     if (s[i].k == 2){
34       if (qq.empty() == 1) return 0;
35       if (qq.front() == s[i].v) {
36         qq.pop(); continue;
37       }
38       else return 0;
39     }
40   }
41   return 1;
42 }
43 int cpriqueue(){
44   priority_queue<data> qq;
45   for (int i = 0; i < n; ++i){
46     if (s[i].k == 1) qq.push(s[i]);
47     if (s[i].k == 2) {
48       if (qq.empty() == 1) return 0;
49       if (qq.top().v == s[i].v) {
50         qq.pop(); continue;
51       }
52       else return 0;
53     }
54   }
55   return 1;
56 }
57 int main(void){
58 #ifndef ONLINE_JUDGE
59   freopen("11995.in", "r", stdin);
60 #endif
61   while (~scanf("%d", &n)){
62     for (int i = 0; i < n; ++i){
63       scanf("%d%d", &s[i].k, &s[i].v);
64     }
65     int st = cstack(), qu = cqueue(), pri = cpriqueue();
66     if (st + qu + pri > 1) printf("not sure\n");
67     else if (st) printf("stack\n");
68     else if (qu) printf("queue\n");
69     else if (pri) printf("priority queue\n");
70     else printf("impossible\n");
71   }
72 
73   return 0;
74 }

关键是学会STL的使用,然后,还有结构体里面的友元函数,用来优先队列实现里面判断大小,注意,参数列表不能用 data &a, data &b会报错,看来C++语法还得复习一下啊……

posted on 2013-03-16 14:30  aries__liu  阅读(680)  评论(0编辑  收藏  举报