当前位置:网站首页>卡特兰数(Catalan)的应用场景
卡特兰数(Catalan)的应用场景
2022-07-03 06:33:00 【今天也要写bug、】
卡特兰数
卡特兰数的推导:
卡特兰数(Catalan)

应用场景
假设有n个左括号和n个右括号,他们有多少种合法的组合。
一共有C(2n,n)种组合,假设这些组合为A集合。
对于不合法的组合,一定有一个前缀,其右括号数量比左括号数量多1个,比如())(()中前缀()),而后缀一定是右括号比左括号少1一个((),我们将后缀反转(左括号变右括号,右括号变左括号),这样就变成了整个组合中右括号为n+1,左括号为n-1。反转后一共有C(2n,n-1)种组合,假设他们为B集合。
A中不合法的组合可以通过反转推出B集合中所有的情况,而B集合也可以通过反转推出A中不合法的组合。因此A中不合法的数量就等于C(2n,n-1)。
综上,合法的组合一共有C(2n,n)-C(2n,n-1)种。n个数有多少种合法的进出栈方式。
任何一个前缀出栈数量一定是少于进栈数量,因此这个问题就转化成了上面的括号问题。其组合方式也是C(2n,n)-C(2n,n-1)种。一共有n个节点,有多少种组成二叉树的方式。
有0个节点,方式为空树,组成二叉树的方式1种。
有1个节点,树为它自己,1种。
有2个节点,根+左或者根+右,2种。
有n个节点,选一个节点为头节点;头节点左边0个节点,右边n-1个节点;头结点左边1个节点,右边n-2个节点;头结点左边2个节点,右边n-3个节点。。。 可以推出k(n)=k(0)*k(n-1)+k(1)*k(n-2)+....+k(n-2)*k(1)+k(n-1)*k(0),也就是卡特兰数C(2n,n)-C(2n,n-1)种组合方式。
边栏推荐
- Mysql database binlog log enable record
- Page text acquisition
- Kubesphere - build Nacos cluster
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- Selenium - by changing the window size, the width, height and length of different models will be different
- Project summary --04
- Pytorch exercise items
- Support vector machine for machine learning
- 简易密码锁
- 数值法求解最优控制问题(一)——梯度法
猜你喜欢

Creating postgre enterprise database by ArcGIS

IE browser flash back, automatically open edge browser

SQL implementation merges multiple rows of records into one row

Yolov1 learning notes

. Net program configuration file operation (INI, CFG, config)

Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)

Advanced technology management - do you know the whole picture of growth?

Selenium - by changing the window size, the width, height and length of different models will be different

远端rostopic的本地rviz调用及显示

Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
随机推荐
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
Selenium - 改变窗口大小,不同机型呈现的宽高长度会不一样
Code management tools
代码管理工具
Use abp Zero builds a third-party login module (I): Principles
PMP notes
远端rostopic的本地rviz调用及显示
Yolov3 learning notes
Know flex box
【系统设计】邻近服务
A letter to graduating college students
2022年华东师范大学计科考研复试机试题-详细题解
Unittest attempt
Migrate data from Mysql to tidb from a small amount of data
SQL实现将多行记录合并成一行
Operation principle of lua on C: Foundation
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
[system design] proximity service
The mechanical hard disk is connected to the computer through USB and cannot be displayed