効果の対象に
、「パッケージ」(一部のデータ構造)にいくつかのデータを入力し、入力「スタック」、「キュー」、「優先キュー」に対応し、これらのデータは、どのようなデータ構造に従っている「パック」を決定する部分から除去、「わからない」「不可能」。
分析
この質問は、簡単なシミュレーション問題であり、我々は、出力データ構造のマッチングを探して、あるデータのシミュレーションのためのSTLカプセル化されたスタック、キューと優先キューを使用する必要があります。数は入力の数よりも大きくすることができる、それアウト数は、この質問ピットという空のスタック場合は、実行優先順位キューとキューポップ()エラー操作は、ランタイムの原因となります。
対応するSTLはじめに
1.スタック(スタック)
スタック「は、以下のリストの末尾にのみ挿入および欠失を定義し、制限された直線状であるLIFOの原則を」。以下の操作は、STLの一般的なスタックの関数です。
#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.キュー(キュー)
のキューはまた、限定されるもので直線状であり、ヘッダのみ欠失で定義されている、手術台の挿入端部は、「フォローFIFO」原理。
#include <queue> //header file
queue<int> q; //define a queue
q.empty();
q.push((int)value);
q.front(); //returns the header element
q.pop();
前記優先待ち行列(PRIORITY_QUEUE)
プライオリティキューに、要素が優先されます。最も高い優先度を持つ要素にアクセスすると、削除する最初の要素である場合には、「従う最も先進的な、ファーストアウト(最初の最大のうち、中)の原則」、通常はヒープ達成するためのデータ構造を。中にカプセル化されたC ++ STL PRIORITY_QUEUEデフォルト値が比較的大きい数が高い優先度を持っている私たちは優先順位を変更する必要がある場合は、その後のことができます。次の方法で:
//方法一
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;
一般的に使用される機能の一部PRIORITY_QUEUE:
#include<queue>
priority_queue<int> pq;
s.empty();
s.push((int)value);
s.top(); //returns the top element of the heap
s.pop();
4.消去
消去は、より多くのSTLコンテナについて、それは彼らがいることを残念である空のように設定し、地図持ってクリア()関数のようにしていない、我々は唯一この方法では、次のような手動でフラッシュ操作することができます。
while(!s.empty())
s.pop();
コードは以下の通りです
#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;
}
参考資料
https://blog.csdn.net/apro1066/article/details/81814154
https://blog.csdn.net/weixin_36888577/article/details/79937886