您的位置:首页 > 理论基础 > 数据结构算法

3334-数据结构实验之栈与队列七:出栈序列判定

2017-10-19 19:49 204 查看
#include <bits/stdc++.h>

using namespace std;
typedef int ElemType;

class Stack{
private:
ElemType *up;
ElemType *base;
ElemType length;
public:
Stack(){
base = new ElemType;
up = base;
length = 0;
}
void push(ElemType x){
*up++ = x;
length++;
}
void pop(){
up--;
length--;
}
ElemType top(){
return *(up - 1);
}
ElemType size(){
return length;
}
bool empty(){
return 0 == length;
}
};

int main()
{
int res[11230];
int arr[11230];
int n;
while(cin >> n)
{
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
int T;
cin >> T;
while(T--)
{
for(int i = 0; i < n; i++)
{
cin >> res[i];
}
Stack Q;
int p = 0;
for(int i = 0; i < n; i++)
{
Q.push(arr[i]);
while(!Q.empty() && Q.top() == res[p])
{
Q.pop();
p++;
}
}
if(Q.empty())
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: