P1044 stack, P1044

Source: Internet
Author: User

P1044 stack, P1044
Background

Stack is a classic data structure in computers. Simply put, stack is a linear table that restricts the insertion and deletion at one end.

Stack has two most important operations: pop (an element pops up from the top of the stack) and push (putting an element into the stack ).

The importance of stacks is self-evident. Any course on data structures will introduce stacks. While reviewing the basic concepts of the stack, Ning thought of a question not mentioned in the book, and he could not give the answer himself, so he needed your help.

Description

Ning considers the problem that the depth of stack A is greater than n in an operand sequence, from 1, 2, to n (1 to 3 in the figure below.

You can perform either of the following operations,

1. Move a number from the first end of the operand sequence to the first end of the stack (corresponding to the push operation of the data structure stack)

  1. Move a number from the beginning of the stack to the end of the output sequence (corresponding to the pop operation of the data structure stack)

Using these two operations, a series of output sequences can be obtained from an operand sequence, as shown in the process of generating sequence 2 3 1 for 1 2 3.

(The original status is shown in)

Your program will calculate and output the given n from the operand sequence 1, 2 ,..., N the total number of output sequences that may be obtained after the operation.

Input/Output Format Input Format:

The input file contains only one integer n (1 ≤ n ≤ 18)

Output Format:

The output file has only one row, that is, the total number of possible output sequences.

Input and Output sample Input example #1:
3
Output sample #1:
5

This is a bare catlan number.
However, you can also use dp to do this,
Use dp [I] [j] to indicate the number of I solutions in the stack and j outside the stack.
Transfer equation:
Dp [j] [I] = max (dp [j] [I], dp [J-1] [I] + dp [j + 1] [I-1])
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int read(int & n) 7 { 8     char p='+';int x=0; 9     while(p<'0'||p>'9')10         p=getchar();11     while(p>='0'&&p<='9')12     x=x*10+p-48,p=getchar();13     n=x;14 }15 int dp[20][20];16 int main()17 {18     int ans=0;19     int n;read(n);20     for(int i=0;i<=n;i++)21     {22         for(int j=0;j<=n;j++)23         {24             if(i==0)25                 dp[j][0]=1;26             else if(j==0)27                 dp[0][i]=dp[1][i-1];28             else29                 dp[j][i]=max(dp[j][i],dp[j-1][i]+dp[j+1][i-1]);30         }31     }32     cout<<dp[0][n];33     return 0;34 }

 

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.