Description
We know an inbound stack sequence and try to find all possible outbound stack sequence numbers. For example, if the inbound stack sequence is 1, 2, 3, there are 5 possible outbound stack sequences: 1 2 3, 1 3 2, 2 1, 2 3, 3, 2 1.
Analysis
It is easy to calculate the number of stack sequences. Many articles have analyzed this question. The final answer is the catlan number. That is to say, the total number of the n elements in the output stack sequence is C (2n, n)-C (2n, n-1) = C (2n, N)/(n + 1 ). In the blog post, the Catalan number provides several methods for solving the catlan number.
Can I write a program to find the number of sequences that come out of the stack without analyzing the generic formula? The answer is yes. The following program can solve the number of stack sequences. The Code comes from a blog on the network and cannot remember the source, so it cannot be specified. Please forgive me.
# Include <stdio. h>/* sum is used as the global variable to record the total number of possible cases */INT sum = 0;/** Description: recursive calculation of the total number of possible output stack sequences, no return value * parameter: * In -- number of elements currently stored in the stack * Wait -- number of elements not currently in the stack * out -- number of elements already in the stack * num -- total number of elements ** /void F (INT in, int wait, int out, int num) {/* if all elements are out of the stack, it indicates a new situation. The total number is plus one */If (out = num) sum ++;/* otherwise, the new State will be recursively derived */else {/* derivative method 1: Let an element go to the stack */If (wait> 0) F (in + 1, wait-1, out, num);/* Derivative Method 2: Let an element go out of the stack */If (in> 0) f (in-1, wait, out + 1, num );}}
/* Use the main function to call */INT main () {/* A total of n elements * // int n = 5; int N; scanf ("% d ", & N);/* at the beginning, the stack had 0 elements, n elements waiting, and 0 elements leaving the stack. There were n elements in total */F (0, n, 0, n); printf ("% d \ n", sum); Return 0 ;}
Extension
The above only calculates the number of all outbound stacks. What if all outbound stack sequences are required? For example, if the given inbound stack sequence is 1 2 3, all outbound stack sequences are required. The following content is transferred from http://blog.csdn.net/lzshlzsh/article/details/5910682
As shown in: each node may have two types of operations: Stack entry and stack exit. Go to the left subtree, and go to the right subtree.When the "unprocessed" element is empty, the output sequence is determined.(As shown in green node ). Note that the output sequence and stack status must be maintained during rollback.
# Include <iostream> # include <vector> # include <iomanip> using namespace STD; void func (int A [], Int J, int N, vector <int> & STK, vector <int> & que) {static int CNT = 0; If (j> = N) {cout <++ CNT <":"; for (unsigned int K = 0; k <que. size (); k ++) {// print out the stack sequence cout <SETW (5) <que [k];} For (int K = STK. size ()-1; k> = 0; k --) {// print the sequence in the stack. Note that cout is printed from the back to the front <SETW (5) <STK [k] ;}cout <Endl; retu Rn;} STK. push_back (A [J]);/* inbound stack */func (A, J + 1, n, STK, que);/* left subtree */STK. pop_back ();/* return to the current node */If (! STK. empty () {/* output stack */que. push_back (STK. back (); STK. pop_back (); func (A, J, N, STK, que);/* right subtree */STK. push_back (que [Que. size ()-1]); que. pop_back () ;}} int main () {int A [] = {1, 2, 3}; vector <int> STK; vector <int> que; func (, 0, sizeof (a)/sizeof (INT), STK, que); Return 0 ;}