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

数据结构实验之栈七:出栈序列判定

2016-08-01 20:17 162 查看
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
typedef struct node
{
int *data;
int top;
int bottom;
}Stack;
Stack S;
void initstack(Stack &S)
{
S.data=new int [maxn];
S.bottom=0;
}
void push(Stack &S, int key)
{
S.data[++S.top]=key;
}
void pop(Stack &S)
{
S.top--;
}
void clean(Stack &S)
{
S.top=S.bottom-1;
}
int pu[maxn], po[maxn];
int main()
{
ios::sync_with_stdio(false);
Stack S;
initstack(S);
int n, m;
cin>>n;
for(int i=0;i<n;i++)
cin>>pu[i];
cin>>m;
while(m--)
{
clean(S);
for(int i=0;i<n;i++)
cin>>po[i];
int a=0, b=0;
while(a<n)
{
if(pu[a]==po[b])
{
a++;
b++;
}
else
{
push(S, pu[a]);
a++;
}
}
int flag=1;
while(S.top>=0)
{
if(S.data[S.top]==po[b])
{
b++;
pop(S);
}
else
{
flag=0;
break;
}
}
if(flag)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: