# 判断一个序列是不是栈的输出序列

```
#include <iostream>
/*
* author: w397090770
* Email:wyphao.2007@163.com
* 仅用于学习交流之用
**/
#include <vector>
#include <stack>

using namespace std;

bool IsPopOrder(const vector<int> & Push, const vector<int> & Pop){
if(Push.size() != Pop.size()){
return false;
}

stack<int>temp;
int tempIndex = 0;
int tempIndex2 = 0;
int Size = Pop.size();

while(tempIndex2 < Size){
for(; tempIndex < Size; tempIndex++){
if(Push[tempIndex] == Pop[tempIndex2]){
tempIndex2++;
tempIndex++;
break;
}
temp.push(Push[tempIndex]);
}

if(!temp.empty() && temp.top() != Pop[tempIndex2]){
for(; tempIndex < Size; tempIndex++){
if(Push[tempIndex] == Pop[tempIndex2]){
tempIndex2++;
tempIndex++;
break;
}
temp.push(Push[tempIndex]);
}
}else if(!temp.empty()){
temp.pop();
tempIndex2++;
}

if(!temp.empty() && tempIndex >= Size && temp.top() != Pop[tempIndex2]){
return false;
}else{
while(!temp.empty() && temp.top() == Pop[tempIndex2]){
temp.pop();
tempIndex2++;
}
}

}

if(temp.empty() && tempIndex >= Size){
return true;
}

return false;
}

int main(){
vector<int> Push, Pop;
//1 1
//1 2
//1, 2, 3, 4, 5 3, 5, 4, 1, 2
//1, 2, 3, 4, 5 4, 3, 5, 1, 2
//1, 2, 3, 4, 5 3, 5, 4, 2, 1
//1, 2, 3, 4, 5 4, 5, 3, 2, 1
//1, 2, 3, 4, 5 3, 5, 4, 2, 1
Push.push_back(1);
Push.push_back(2);
Push.push_back(3);
Push.push_back(4);
Push.push_back(5);
Push.push_back(6);
Push.push_back(7);

Pop.push_back(2);
Pop.push_back(1);
Pop.push_back(4);
Pop.push_back(3);
Pop.push_back(7);
Pop.push_back(5);
Pop.push_back(6);

cout << IsPopOrder(Push, Pop) << endl;
return 0;
}
```