//============================================================================
// Name : 100题之29栈的push,pop序列.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int find(int number,const int* push,int length)
{
for(int i=1;i<=length;i++)
{
if(number==push[i])
return i;
}
return -1;
}
bool IsPossiblePopOrder(const int *push,const int *pop,int length)
{
bool *type=new bool[length+1];
for(int popi=1;popi<=length;popi++)
{
type[popi]=false;
}
int pushi=1;
for(int popi=1;popi<=length;popi++)
{
int j=find(pop[popi],push,length);
cout<<"j is"<<j<<" pushi is"<<pushi<<endl;
if(pushi<j)
{
pushi=j;
}
else if(pushi>j)
{
while(type[pushi]==true)
pushi--;
cout<<"pushi now is "<<pushi<<endl;
if(push[pushi]==pop[popi])
{
type[pushi]=true;
continue;
}
else
return false;
}
type[j]=true;
}
delete []type;
return true;
}
int main() {
int length=5;
int *push=new int[length+1];
int *pop=new int[length+1];
push[1]=1;
push[2]=2;
push[3]=3;
push[4]=4;
push[5]=5;
pop[1]=4;
pop[2]=3;
pop[3]=5;
pop[4]=2;
pop[5]=1;
cout<<IsPossiblePopOrder(push,pop,length);
return 0;
}