algorithm - 递归方法 : How can we generate all possibilities on braces?

标签 algorithm recursion data-structures catalan

我们如何生成大括号上的所有可能性?

N个值(value)已经给了我们,我们要产生所有的可能性。

示例:

1)如果N == 1,那么只有一种可能()。

2) 如果N==2,那么可能性是(()), ()()

3) 如果N==3,那么可能性是((())), (())(),()()(), ()(()) ...

注意:左右大括号应该匹配。我的意思是 )( 对于 N==1 是无效的

我们可以使用递归方法解决这个问题吗?

最佳答案

来自维基百科-

A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's (see also Dyck language). For example, the following are the Dyck words of length 6:

XXXYYY     XYXXYY     XYXYXY     XXYYXY     XXYXYY.

Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, Cn counts the number of expressions containing n pairs of parentheses which are correctly matched:

((()))     ()(())     ()()()     (())()     (()())

另见 http://www.acta.sapientia.ro/acta-info/C1-1/info1-9.pdf

Abstract. A new algorithm to generate all Dyck words is presented, which is used in ranking and unranking Dyck words. We emphasize the importance of using Dyck words in encoding objects related to Catalan numbers. As a consequence of formulas used in the ranking algorithm we can obtain a recursive formula for the nth Catalan number.

关于algorithm - 递归方法 : How can we generate all possibilities on braces?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313921/

相关文章:

有效地将十进制移位应用于所有数字的算法

algorithm - 返回获取数组左值或右值的最大值

javascript - Promise & Recursion,如何在没有回调的情况下处理找到的项目?

ruby-on-rails - 用于循环第 n 个嵌套深度的递归函数

无法理解for循环中的递归

java - 高效获取 LinkedHashSet 中最近插入的元素

data-structures - Rust 中的树遍历 vs Borrow Checker

algorithm - 了解 Prime XOR 的社论 - HackerRank

c++ - 如何创建 DAWG?

algorithm - 如何编写用于生成集合的所有子集的迭代算法?