NOIP普及组2003 栈



Description

宁宁考虑的是这样一个问题:一个操作数序列,1,2,...,n(图示为1到3的情况),栈A的深度大于n。

现在可以进行两种操作:

将一个数,从操作数序列的头端移到栈的头端(对应数据结构栈的push操作)
将一个数,从栈的头端移到输出序列的尾端(对应数据结构栈的pop操作)

使用这两种操作,由一个操作数序列就可以得到一系列的输出序列,下图所示为由1 2 3生成序列2 3 1的过程。

你的程序将对给定的n,计算并输出由操作数序列1,2,…,n经过操作可能得到的输出序列的总数。


Input

输入文件只含一个整数n。

Output

输出文件只有11行,即可能输出序列的总数目。

Hint

1≤n≤18。

Solution

卡特兰数模板题(?)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 55
using namespace std;
int f[maxn];
int n;
inline void Set_Catalan(){
    f[0]=1,f[1]=1;
    for(int i=2;i<=18;i++){
        for(int j=1;j<=i;j++){
            f[i]+=f[j-1]*f[i-j];
        }
    }
}
int main(){
    scanf("%d",&n);
    Set_Catalan();
    printf("%d\n",f[n]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/virtual-north-Illya/p/10327413.html