n elements into the stack, how many kinds of stack order?

Source: Internet
Author: User

Original: http://blog.csdn.net/zyearn/article/details/7758716

Recently in reviewing data structure, see the stack, found 1 elements into the stack, there are 1 kinds of stack order, 2 elements into the stack, 2 kinds of stack order, 3 elements into the stack, there are 5 kinds of stack order, then a very natural problem is n elements into the stack, how many kinds of stack order?

I'm ashamed to say that I didn't think about it when I learned the data structure. Recently in the dynamic planning, so the "sub-problem" These 3 words have been wandering in my mind, so when solving this problem I also use a similar "sub-problem" method, plainly speaking is the recursive formula.

We put the number of n elements out of the stack as f (n), then for the three-to-one, we can easily conclude:

F (1) = 1//or 1

F (2) = 2//i.e. 12, 21

F (3) = 5//i.e. 123, 132, 213, 321, 231

Then we consider F (4), we give 4 elements to a,b,c,d, then consider: element A can only appear in position 1th, position 2nd, position 3rd and position 4th (very easy to understand, altogether 4 locations, such as ABCD, element A is in position 1th).

Analysis:

1) If element A is in position 1th, then only a stack is possible, immediately out of the stack, then there are elements B, C, d wait for operation, is the sub-problem F (3);

2) If element A is in position 2nd, then there must be an element in the first stack than a, that is, f (1) possible order (only B), and the remaining C, D, that is, F (2), according to the multiplication principle, the total number of orders is F (1) * f (2);

3) If element A is in position 3rd, then there must be two elements in the stack than 1, that is, F (2) possible order (only B, C), and D, that is f (1),

According to the multiplication principle, the total order number is F (2) * f (1);

4) if element A in position 4th, then must be a advanced stack, and finally out of the stack, then the order of the elements B, C, D is the solution of this small problem, that is, F (3);

Combine all cases, i.e. f (4) = f (3) + F (2) * F (1) + f (1) * F (2) + f (3);

To be normalized, we define f (0) = 1, and f (4) can be re-written as:

F (4) = f (0) *f (3) + F (1) *f (2) + F (2) * F (1) + F (3) *f (0)

Then we generalize to N, the idea of promotion and n=4 is exactly the same, so we can get:

F (n) = f (0) *f (n-1) + f (1) *f (n-2) + ... + f (n-1) *f (0)

That

But this is just a recursive formula (if implemented programmatically, a one-dimensional array needs to be maintained, with a time complexity of O (n^2)).  How do you turn it into a general formula, with only an O (1) complexity? So search on the internet, there really is such a formula: C (2n,n)/(N+1) (c (2n,n) for 2n), and a name called Catalan number. Attached to the wiki link, written in too detail: Http://en.wikipedia.org/wiki/Catalan_number Now the question is: How to find out from the above recursive formula C (2n,n)/(n+1)? Interested friends welcome message to discuss! 2013.6.4 Update According to the idea of netizens u010896627, I abstract the next question, in the knowledge asked a question, one of the answers to the "discounted method", from the geometry of the "n elements into the stack number of stack order" the answer to this question is C (2n,n)-B ( 2n,n-1), Catalan number is a simple simplification. We recommend that you take a look.

n elements into the stack, how many kinds of stack order?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.