#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;
/*
j=0
for i = 0 i<len(stack_out)
s.push(stack_in[i])
while s.top == stack_out[j] && !s.empty
s.pop
j++
endwhile
endfor
*/
int input[] = {1,2,3,4,5};
int output[] = {4,5,1,2,3};
int isPopOrder( int* in, int* out,int inlen, int outlen)
{
if (in == NULL || out == NULL || inlen != outlen)
{
return 1;
}
stack<int> s1;
int j = 0;
int i = 0;
for (; i< inlen; i++)
{
s1.push(in[i]);
while (!s1.empty() && s1.top() == out[j])
{
s1.pop();
j++;
}
}
if (s1.empty())
{
return 0;
}
else
{
return 1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int ret = isPopOrder(input, output, 5,5);
if (ret == 0)
{
cout<<"match"<<endl;
}
else
{
cout<<"not match"<<endl;
}
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
/*
j=0
for i = 0 i<len(stack_out)
s.push(stack_in[i])
while s.top == stack_out[j] && !s.empty
s.pop
j++
endwhile
endfor
*/
int input[] = {1,2,3,4,5};
int output[] = {4,5,1,2,3};
int isPopOrder( int* in, int* out,int inlen, int outlen)
{
if (in == NULL || out == NULL || inlen != outlen)
{
return 1;
}
stack<int> s1;
int j = 0;
int i = 0;
for (; i< inlen; i++)
{
s1.push(in[i]);
while (!s1.empty() && s1.top() == out[j])
{
s1.pop();
j++;
}
}
if (s1.empty())
{
return 0;
}
else
{
return 1;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int ret = isPopOrder(input, output, 5,5);
if (ret == 0)
{
cout<<"match"<<endl;
}
else
{
cout<<"not match"<<endl;
}
return 0;
}