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 */