Thinking of finding all legal stacks for a given stack
A few days ago to see an article about a given several elements, give a stack order, determine whether the order of the stack is legitimate, we can also be given a sequence of elements to find out all the legitimate out of the stack order, the problem is how to find the given element of all the permutation problem, before there is a blog post also for three number of full sequence, But the use of three for the loop, it is too young too simple, inefficient, and there is no practical, can not count several for the loop, which is too silly (650) this.width=650; "src=" http/ Img.baidu.com/hi/jx2/j_0070.gif "alt=" J_0070.gif "/>).
For judging the legality of the stack, I first write out the idea, as for the code implementation takes two days, to ask all the legitimate stack order there are two ways.
Scenario 1: One is to find out the arrangement of all the elements, and then bring each of them into the function of judging the legitimacy of the stack, thus selecting the legal.
Scenario 2: The second idea is to use the previous bracket matching idea, the stack is 0, the stack is 1, the premise of the stack is 0 is greater than the number of 1, and then all the possible preservation, is all the legitimate out of the stack order.
It is obvious that the scheme 2 is efficient, but the blogger for the second program of the implementation of the pressure, first of a simple implementation, the implementation of the second program as soon as possible to fill in (650) this.width=650; "Src=" Http://img.baidu.com/hi/face/i_ F07.gif "alt=" I_f07.gif "/>)
The program code is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace Std;
#pragma once
#include <iostream>
#include <stack>
using namespace Std;
BOOL Check_push_pop (int* Ppush, int* ppop, int length)
{
if (length <= 0 | | ppush = = NULL | | ppop = = NULL)
{
return false;
}
int in = 0;
int out = 0;
Stack<int> s;
S.push (Ppush[0]);
int index = 0; Number of elements pressed into
for (out = 0, out < length; out++)
(1) What the For loop controls, the record pops up a few elements
{
for (in = index; in <= length; in++)
ppush[1,2,3,4,5] ppop[4,5,3,2,1] control presses several elements into the
{
if ((S.empty ()) | | | s.top ()! = Ppop[out])
(2) Why do I have to do a stack operation when empty in the stack?
{
if (in = = length)
(3) All elements have been stacked at this time but no elements of the stack have been found, proving that the elements of the stack are illegal
{
return false;
}
S.push (Ppush[in]);
++index;
}
else//The element at the top of the stack is equal to the popup element and pops up directly
{
S.pop ();
Break Jump out of this layer for loop and make it traverse the next out stack element
}
}
}
return true;
}
void swap (int *a, int *b)//Swap two elements in an array
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void Perm (int *list, int i, int n,int& count,int* push)
{
int j = 0;
if (i = = N)
{
if (Check_push_pop (Push, list, n))
{
for (j = 0; J < N; j + +)
{
cout << List[j];
cout << "";
}
count++;
cout << Endl; Add
}
}
Else
{
for (j = i; J < N; j + +)
{
SWAP (list + I, list + j);
Perm (list, i + 1, n,count,push);
SWAP (list + I, list + j);
}
}
}
int main ()
{
int list[4] = {1, 2, 3, 4}; Elements into the stack
int push[4] = {1, 2, 3, 4};
cout << "Please input a number!" << Endl;
int n = 0;
int count = 0;
CIN >> N;
Perm (list, 0, N,count,push);
cout << Count << Endl;
System ("pause");
return 0;
}
The difficulty of the implementation of scenario 1 lies mainly in the use of recursion, the idea is to put each element in the array first, and then recursively take the second position as the first bit to continue recursion, until I equals N, printing out the subsequent process is to exchange from the back to the next, so that the whole arrangement. The fully arranged array is passed to the Check_push_pop () function to determine if the law is printed, otherwise it is not printed.
Option 2 is then served
Bo Master knowledge is still owed, if there is wrong place, please the Great God pointed out the error, 650) this.width=650; "src=" Http://img.baidu.com/hi/face/i_f32.gif "alt=" I_f32.gif "/> Bo Master must be careful analysis, as soon as possible to modify.
This article is from the "Qin-wang" blog, make sure to keep this source http://10810196.blog.51cto.com/10800196/1763997
Thinking of finding all legal stacks for a given stack