I Can Guess the Data Structure!
Time limit: 1.000 seconds
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
考查基本数据结构定义,实现判断即可。
/* * @author Panoss */ #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<ctime> #include<stack> #include<queue> #include<list> using namespace std; #define DBG 0 #define fori(i,a,b) for(int i = (a); i < (b); i++) #define forie(i,a,b) for(int i = (a); i <= (b); i++) #define ford(i,a,b) for(int i = (a); i > (b); i--) #define forde(i,a,b) for(int i = (a); i >= (b); i--) #define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i]) #define mset(a,v) memset(a, v, sizeof(a)) #define mcpy(a,b) memcpy(a, b, sizeof(a)) #define dout DBG && cerr << __LINE__ << " >>| " #define checkv(x) dout << #x"=" << (x) << " | "<<endl #define checka(array,a,b) if(DBG) { dout << #array"[] | " << endl; forie(i, a, b) cerr << "[" << i << "]=" << array[i] << " |" << ((i - (a)+1) % 5 ? " " : " "); if (((b)-(a)+1) % 5) cerr << endl; } #define redata(T, x) T x; cin >> x #define MIN_LD -2147483648 #define MAX_LD 2147483647 #define MIN_LLD -9223372036854775808 #define MAX_LLD 9223372036854775807 #define MAX_INF 18446744073709551615 inline int reint() { int d; scanf("%d", &d); return d; } inline long relong() { long l; scanf("%ld", &l); return l; } inline char rechar() { scanf(" "); return getchar(); } inline double redouble() { double d; scanf("%lf", &d); return d; } inline string restring() { string s; cin >> s; return s; } struct Cmd { int cmd; int x; }; vector<Cmd> C; bool is_stack() { stack<int> S; fori(i,0,C.size()) { if(C[i].cmd == 1) S.push(C[i].x); else { if(S.empty()) return false; int value = S.top(); S.pop(); if(value != C[i].x) return false; } } return true; } bool is_queue() { queue<int> Q; fori(i,0,C.size()) { if(C[i].cmd == 1) Q.push(C[i].x); else { if(Q.empty()) return false; int value = Q.front(); Q.pop(); if(value != C[i].x) return false; } } return true; } bool is_pri_queue() { priority_queue<int> Q; fori(i,0,C.size()) { if(C[i].cmd == 1) Q.push(C[i].x); else { if(Q.empty()) return false; int value = Q.top(); Q.pop(); if(value != C[i].x) return false; } } return true; } int main() { int n; while(scanf("%d",&n)==1) { C.clear(); forie(i,1,n) { Cmd command; scanf("%d%d",&command.cmd,&command.x); C.push_back(command); } bool is_st = is_stack(); bool is_qu = is_queue(); bool is_pri_qu = is_pri_queue(); if(!is_st&&!is_qu&&!is_pri_qu) puts("impossible"); else if(is_st&&is_qu&&is_pri_qu) puts("not sure"); else if((is_st&&is_qu&&!is_pri_qu)||(is_st&&!is_qu&&is_pri_qu)||(!is_st&&is_qu&&is_pri_qu)) puts("not sure"); else if(is_st&&!is_qu&&!is_pri_qu) puts("stack"); else if(!is_st&&is_qu&&!is_pri_qu) puts("queue"); else if(!is_st&&!is_qu&&is_pri_qu) puts("priority queue"); } return 0; }