This problem is quite simple.
Some places to be aware of:
1, Impossible: All the mark amount is false
2, not sure: there are 2 cases at the same time or there are three kinds of situations.
Problem II Can Guess the Data structure!
There is a BAG-LIKE data structure, supporting and operations:
1 x
Throw an element x into the bag.
2
Take out a 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 is several test cases. Each test case is begins with a line containing a single integer n (1<=n<=1000). Each of the next n lines are either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get the An element x without error. The value of x is always a positive an 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
61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4
Output for the Sample Input
Queuenot sureimpossiblestackpriority Queue
Rujia Liu ' s Present 3: A Data Structure Contest celebrating the 100th anniversary of Tsinghua University
Special thanks:yiming Li
Note:please make sure to test your program with the gift I/O files before submitting!
Simulate the three data structures directly, and note the judgment when the current data structure is empty.
The code is as follows:
/* * uva_11995.cpp * * Created on:2014 December 28 * author:administrator * * #include <iostream> #include <cstdi o> #include <queue> #include <stack> #include <cstring>using namespace std;const int maxn = 1010;int n ; int id[maxn];int X[maxn];bool isstack () {stack<int> s;int i;for (i = 0; i < n; ++i) {if (id[i] = = 1) {S.push (x[i]); }else{if (S.empty () = true) {int val = s.top (); S.pop (); if (x[i]! = val) {return false;}} Else{return false;}}} return true;} BOOL Isqueue () {queue<int> q;int i;for (i = 0; i < n; ++i) {if (id[i] = = 1) {Q.push (x[i]);} Else{if (Q.empty () = true) {int val = Q.front (); Q.pop (); if (x[i]! = val) {return false;}} Else{return false;}}} return true;} BOOL Ispriority_queue () {priority_queue<int> q;int i;for (i = 0; i < n; ++i) {if (id[i] = = 1) {Q.push (x[i]);} Else{if (Q.empty () = true) {int val = q.top (); Q.pop (); if (x[i]! = val) {return false;}} Else{return false;}}} return true;} int main () {while (scanf ("%d", &n)! = EOF) {memset (id,-1,sizeof (ID)); memset (x,-1,sizeof (x)); int i;for (i = 0; i < n; ++i) {scanf ("%d%d", &id[i],&x[i]);} BOOL St = Isstack (), BOOL q = Isqueue (), bool PQ = Ispriority_queue (), if (st = = False && Q = = False && PQ = = False) {printf ("impossible\n");} else if ((st = = true && Q = = True && PQ = true) | | (st = = true && Q = = True && PQ = = False) | | (st = = true && Q = = False && PQ = true) | | (St = = False && Q = = True && PQ = = True)) {printf ("not sure\n");} else if (st = = true && Q = = False && PQ = False) {printf ("stack\n");} else if (q = = True && St = = False && PQ = = False) {printf ("queue\n");} else if (PQ = = True && St = = False && Q = = False) {printf ("priority queue\n");}} return 0;}
(DS "algorithm Primer classic") UVA 11995 I Can Guess the Data structure! (Determine what kind of data structure)