Do not search, output stack sequence statistics, search sequence statistics

Source: Internet
Author: User

Do not search, output stack sequence statistics, search sequence statistics

1627: Time Limit for output stack sequence statistics: 1 Sec memory limit: 128 MB

Description

Stack is a common data structure, where n elements wait for the top side of the stack to enter the stack, and the other side of the stack is the output stack sequence. You already know that there are two stack operations: push and pop. The former is to add an element to the stack, and the latter is to pop up the top element of the stack. Now we need to use these two operations to obtain a series of output sequences from one operation sequence. Please program to find the given n, calculate and output the sequence of numbers 1, 2 ,..., N, the total number of output sequences that may be obtained after a series of operations.

 

Input

An integer n (1 <= n <= 15)

 

Output

An integer, that is, the total number of possible output sequences.

 

Sample Input
3
Sample output
5
Prompt Source

Search

Although this question is a search, it can be done using catlan numbers.

Obviously, searching is very troublesome.

As for why, this formula exists.

# Include <iostream> # include <cstdio> using namespace std; int main () {int n, f [20] = {0}; scanf ("% d ", & n); f [0] = 1; f [1] = 1; for (int I = 2; I <= n; I ++) for (int j = 0; j <I; j ++) f [I] + = f [j] * f [i-j-1]; printf ("% d \ n ", f [n]); return 0;}/* Make h (0) = 1, h (1) = 1, and the catalan number satisfies the recursive formula: h (n) = h (0) * h (n-1) + h (1) * h (n-2) +... + h (n-1) h (0) (n> = 2) for example: h (2) = h (0) * h (1) + h (1) * h (0) = 1*1 + 1*1 = 2 h (3) = h (0) * h (2) + h (1) * h (1) + h (2) * h (0) = 1*2 + 1*1 + 2*1 = 5 */

 

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.