//思路:
//模拟栈, 让栈顶元素和pop序列比较,若相等则出栈, 若不等则入栈
//结果:若栈空,则是,非空则不是。
//模拟栈, 让栈顶元素和pop序列比较,若相等则出栈, 若不等则入栈
//结果:若栈空,则是,非空则不是。
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define NSIZ 1200
int IsPushPop(int push[], int pop[], int n)
{
int i = 0, j = 0;
stack<int > st;
st.push(push[i]);
while(i++ < n)
{
while(!st.empty() && j < n)
{
if (st.top() == pop[j])
{
++j;
st.pop();
}
else
{
break;
}
}
if (i < n)
{
st.push(push[i]);
}
}
return st.size() == 0? 1: 0;
}
int main()
{
int push[] = {1,2 , 3, 4, 5};
int pop[] = {4, 3, 5, 1, 2};
int n = sizeof(push)/sizeof(push[0]);
printf("%s\n", IsPushPop(push, pop, n) == 0?"NO":"YES");
return 0;
}