UVA-11995 (STL + analog) is attached to explain

Topic Portal

Subject to the effect

To a "package" (some data structures) enter some data, and then removed from a portion of the determined "pack" These data are in accordance with what kind of data structure, corresponding to the input "stack", "queue", "priority queue", "not sure", "impossible".

analysis

This question is a simple simulation problem, we need to use the STL encapsulated stack, queue and given priority queue for data simulation, that is, looking for matching the output data structure. This question pits that: it out number the number may be greater than the number of inputs, if a stack of empty, execution priority queue and the queue pop () Error operation causes the Runtime .

Corresponding STL Introduction

1. Stack (Stack)
stack is a restricted linear form, defining only insertions and deletions at the end of the list, follow the " LIFO principle". The following operation is a function of the STL common stack:

#include <stack>    //header file
stack<int> s;      //define a stack
s.empty();         //return value is Boolean type to determine whether the stack is empty
s.push((int)value);//Insert variables into the stack
s.top();    	   //returns the top element of the stack
s.pop();		   //delete the top element of the stack

2. The queue (Queue)
queue is also a limited linear form, is defined only in the header deletion, insertion end of the operating table, follow the " FIFO " principle.

#include <queue>    //header file
queue<int> q;       //define a queue
q.empty();
q.push((int)value);
q.front();          //returns the header element
q.pop();

3. The priority queue (The priority_queue)
in the priority queue, the element is given priority. When accessing the element with the highest priority is the first element to delete, follow the " most advanced, first-out (first in, largest out) principle", usually heap data structure to achieve. c ++ STL encapsulated in the priority_queue default value is relatively large number have higher priority , if we need to change the priority, then you can in the following ways:

//方法一
priority_queue<int, vector<int>, greater<int> > qq;

//方法二, Define structure overload () operator
struct cmp{
	bool operator() (const int a,const int b) const{
        return a > b; //Numbers with small values have high priority
    }
};
priority_queue<int, vector<int>, cmp> cq;

Some priority_queue of commonly used functions:

#include<queue>
priority_queue<int> pq;
s.empty();         
s.push((int)value);
s.top();    	   //returns the top element of the heap
s.pop();

4. erase
erase more about STL containers, it is a pity that they did not like the set and the map has clear () function so empty , we can only be manually flush operation similar to the following in this way.

   while(!s.empty())
   		s.pop();

code show as below

#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
stack<int> s;
queue<int> q;
priority_queue<int> pq;
bool sflag,qflag,pqflag;
int n,a,b;
void init()  //Initialization function
{
    sflag =qflag = pqflag = true;
    while(!s.empty())s.pop();
    while(!q.empty())q.pop();
    while(!pq.empty())pq.pop();
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie();
    while(cin>>n)
    {
        init();
        while(n--)
        {
            cin>>a>>b;
            if(a == 1)
            {
                if(sflag)s.push(b);
                if(qflag)q.push(b);
                if(pqflag)pq.push(b);
            }
            else
            {
                if(s.empty() || q.empty() || pq.empty())
                    sflag =qflag = pqflag = false;
                if(sflag)
                {
                    if(s.top() != b)
                        sflag = false;
                    s.pop();
                }
                if(qflag)
                {
                    if(q.front() != b)
                        qflag = false;
                    q.pop();
                }
                if(pqflag)
                {
                    if(pq.top() != b)
                        pqflag = false;
                    pq.pop();
                }
            }
        }
        if(!sflag && !qflag && !pqflag)
            cout<<"impossible"<<endl;
        else if((sflag && qflag) || (sflag && pqflag) || (qflag && pqflag))
            cout<<"not sure"<<endl;
        else if(sflag)
            cout<<"stack"<<endl;
        else if(qflag)
            cout<<"queue"<<endl;
        else
            cout<<"priority queue"<<endl;
    }
	return 0;
}

Reference material

https://blog.csdn.net/apro1066/article/details/81814154
https://blog.csdn.net/weixin_36888577/article/details/79937886

Published 59 original articles · won praise 103 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42292229/article/details/96735688