The main idea stack must meet the advanced post-out rules, such as:
Press-in sequence 1,2,3,4,5
Out Stack sequence 4,3,5,1,2
Sets a max value that represents the maximum value of the index number of the pressed sequence that is currently out of the stack
such as when 4 out of the stack, the current Max is 4, when the 3 out of the stack, to see if the 3,4 is out of the stack, if the stack is correct
When the 1 out of the stack, the current Max is 5, to check the time of the heap, when the 2 is not out of the stack think that the stack sequence does not conform to the advanced
#include <iostream>
#include <map>
#include <vector>
#include <memory.h>
using namespace Std;
Class Solution {
Map<int,int> Inmap;
Public
BOOL Ispoporder (vector<int> pushv,vector<int> POPV) {
int max = 0;
The stack sequence and the number of stacks are not equal
if (popv.size ()! = Pushv.size ())
{
return false;
}
if (popv.size () = = 0)
{
return true;
}
When the stack sequence is as large as the number of stacks, the elements of the two stack are equal.
if (popv.size () = = 1)
{
if (popv[0]! = pushv[0])
{
return false;
} else {
return true;
}
}
if (popv.size () = = 2)
{
if ((Popv[0] ==pushv[0] && popv[1] = = pushv[1]) | | (Popv[0] = = Pushv[1] && pushv[0] = popv[1]))
{
return true;
} else {
return false;
}
}
for (int i = 0;i < Pushv.size (); i++)
{
Inmap.insert (Map<int,int>::value_type (pushv[i],i));
}
BOOL Array[pushv.size ()];
memset (array, false, sizeof (array));
int one = Inmap.find (popv[0])->second;
int Inmap.find (popv[1])->second;
Array[one] = true;
Array[two] = true;
if (one > both)
{
max = one;
} else {
max = both;
}
for (int i = 2;i< popv.size (); i++)
{
Map<int,int>::iterator currentindex = Inmap.find (Popv[i]);
Currentindex->first is key
if (Currentindex->second > Max)
{
max = currentindex->second;
}
Array[currentindex->second] = true;
for (int j = currentindex->second; J <= Max; j + +)
{
if (array[j] = = False)
{
return false;
}
}
}
return true;
}
};
int main ()
{
int inarray[5] = {1,2,3,4,5};
int outarray[5] = {4,5,3,2,1};
int outarray[5] = {4,3,5,1,2};
vector<int> PUSHV = vector<int> (&inarray[0], &inarray[5]);
vector<int> POPV = vector<int> (&outarray[0], &outarray[5]);
for (int i = 0;i <pushv.size (); i++)
{
cout<<pushv[i];
}
cout<<endl;
Solution solution = solution ();
Cout<<solution. Ispoporder (PUSHV, POPV);
return 0;
}
Determine if the stack sequence is likely to be a stack sequence of a stack sequence, C + +