// 栈的压入、弹出顺序
// a表示压入顺序,b表示弹出顺序。
bool IsStackOrder(int a[],int b[],int n)
{
if(a==NULL||b==NULL||n<=0)
throw exception("Invalid input!");
if(n==1)
return true;
vector<int> vec;
int i,j;
j=-1;
for(i=0;i<n;i++)
{
if(!vec.empty()&&b[i]==vec.back())// 栈顶的情况
{
vec.erase(vec.end()-1);
}
else
{
j++;
while(j<n&&a[j]!=b[i]){vec.push_back(a[j]);j++;} // 在后面的序列当中找到了
if(j==n)
return false;
}
}
if(i==n&&vec.empty())
return true;
else
return false;
}
void main()
{
int a[5]={1,2,3,4,5};
int b[5]={3,5,4,2,1};
cout<<IsStackOrder(a,b,5)<<endl;
system("pause");
}