当前位置:网站首页>卡特兰数(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)种组合方式。
边栏推荐
- 【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
- 【5G NR】UE注册流程
- JMeter performance automation test
- Kubesphere - build MySQL master-slave replication structure
- Create your own deep learning environment with CONDA
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- In depth learning
- [5g NR] UE registration process
- Kubesphere - build Nacos cluster
- 100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
猜你喜欢
(翻译)异步编程:Async/Await在ASP.NET中的介绍
2022 CISP-PTE(三)命令执行
Reinstalling the system displays "setup is applying system settings" stationary
Migrate data from Mysql to tidb from a small amount of data
23 design models
arcgis创建postgre企业级数据库
ruoyi接口权限校验
Scroll view specifies the starting position of the scrolling element
Summary of the design and implementation of the weapon system similar to the paladin of vitality
剖析虚幻渲染体系(16)- 图形驱动的秘密
随机推荐
Project summary --04
The mechanical hard disk is connected to the computer through USB and cannot be displayed
Cannot get value with @value, null
opencv
“我为开源打榜狂”第一周榜单公布,160位开发者上榜
[set theory] relational closure (relational closure solution | relational graph closure | relational matrix closure | closure operation and relational properties | closure compound operation)
ruoyi接口权限校验
Chapter 8. MapReduce production experience
Simple understanding of ThreadLocal
Difference between shortest path and minimum spanning tree
Zhiniu stock -- 03
About the difference between count (1), count (*), and count (column name)
SQL implementation merges multiple rows of records into one row
ThreadLocal的简单理解
Selenium ide installation recording and local project maintenance
表达式的动态解析和计算,Flee用起来真香
Kubesphere - build MySQL master-slave replication structure
Zhiniu stock project -- 05
Some thoughts on machine learning
远端rostopic的本地rviz调用及显示