Translation of title
Guess the data structure
Question Description:
You have a data structure similar to a "package" that supports two operations, as shown in the table below. 1x: Put element x into package 2: Take an element out of package and give a series of operations and return values. Your task is to guess what the "package" is. It could be a stack < FIFO), a queue (FIFO), a priority queue (large integer FIFO) or something weird.
Input:
The first action is an integer n (1 < n < 1 000). The following N lines are either a type 1 command or a type 2 command followed by an integer x(1 < x < 100). This integer x means that after executing the command of type 2, the package returns x without error. The size of the input file does not exceed 1MB.
Output:
Output a line. There are five possible outputs.
Stack: It must be a stack
Queue: It must be a queue
Priority queue: must be a priority queue
impossible: Must not be the three above
not sure: At least two are possible.
Input examples:
5 1 5 1 3 1 2 2 2 2 3
Output example:
stack
Title Description
Input and Output Format
Input format:
Output format:
Input and Output Samples
Input sample #1:copy
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 sample #1:copy
queue not sure impossible stack priority queue
Analysis:
Simple simulation will do.
#include<vector> #include<cstdio> #include<stack> #include<queue> using namespace std; struct Data_Struct { //vc store instruction pair sequence vector<pair<int,int> > vc; //Constructor Data_Struct(int n) { for(int i=0;i<n;i++) { int x,y; scanf("%d%d",&x,&y); vc.push_back(make_pair(x,y)); } } //Determine whether it might be a stack bool is_stack() { stack<int> S; for(int i=0;i<vc.size();i++) { int x=vc[i].first, y=vc[i].second; if(x==1) S.push(y); else if(x==2) { if(S.empty() || S.top()!=y ) return false; S.pop(); } } return true; } //Determine whether it might be a queue bool is_queue() { queue<int> Q; for(int i=0;i<vc.size();i++) { int x=vc[i].first, y=vc[i].second; if(x==1) Q.push(y); else if(x==2) { if(Q.empty() || Q.front()!=y ) return false; Q.pop(); } } return true; } //Determine whether a priority queue is possible bool is_priority_queue() { priority_queue<int> Q; for(int i=0;i<vc.size();i++) { int x=vc[i].first,y=vc[i].second; if(x==1) Q.push(y); else { if(Q.empty() || Q.top()!=y )return false; Q.pop(); } } return true; } //Output integration results void solve() { int S=is_stack(), Q=is_queue(), PQ=is_priority_queue(); if(S+Q+PQ==0) printf("impossible\n"); else if(S+Q+PQ==1) { if(S) printf("stack\n"); else if(Q) printf("queue\n"); else if(PQ) printf("priority queue\n"); } else if(S+Q+PQ>1) printf("not sure\n"); } }; int main() { int n; while(scanf("%d",&n)==1) { Data_Struct ds(n); ds.solve(); } return 0; }