Generating Permutations全排列

Problem

        generating all permutations of the numbers 1, 2, . . . , n.

The first algorithm
Idea

          1[all the permutations of the numbers 2,3,,n]

          2[all the permutations of the numbers 1,3,,n]

           ……

                        n[all the permutations of the numbers 1,2,,n-1]

  1. 首先,我们创建一个数组P[],用来存储排列结果。数组P的长度为n,表示排列中包含的数字范围从1到n。
  2. 我们从1到n的循环,将数组P[]初始化为1到n的有序排列。
  3. 接着,调用perml(1)这个过程,开始生成排列。
  4. 过程perml(m)的目标是生成从m到n的所有排列。
  5. 如果m等于n,表示我们已经完成了整个排列,将当前的排列输出。
  6. 否则,我们从m到n的循环,每次交换P[m]和P[j]的值,然后递归调用perml(m+1),直到达到最后一个数字n。
  7. 当递归回溯到上一层时,再次交换P[m]和P[j]的值,以确保在下一轮迭代中能够重新考虑所有可能的排列。
  8. 最终,当m等于n时,输出P[1..n]表示一种排列
Time Complexity:  Q ( nn !)

        Since there are n! permutations, Step 1 of Procedure perm1 takes nn! to output all permutations.

        Now we count the number of iterations of the for loop. In the first call to Procedure perm1, m = 1. Hence, the for loop is executed n times plus the number of times it is executed in the recursive call perm1(2). When n = 1, the number of iterations is zero, and the number of iterations f(n) can be expressed by the recurrence

The second algorithm
n Idea

           n   [x xx x] ,

            [´] n [x  x x]

      ……

           [ x x   x  x] n.

       First, we put n in P[1] and generate all the permutations of the first n − 1 numbers using the subarray P[2..n]. Next, we put n in P[2] and generate all the permutations of the first n − 1 numbers using the subarrays P[1] and P[3..n]. Then, we put n in P[3] and generate all the permutations of the first n−1 numbers using the subarrays P[1..2] and P[4..n]. This continues until finally we put n in P[n] and generate all the permutations of the first n 1 numbers using the subarray P[1..n − 1]. Initially, all n entries of P[1..n] contain 0’s.

  1. 首先,我们创建一个数组P[],用来存储排列结果。数组P的长度为n,表示排列中包含的数字范围从1到n。
  2. 我们从1到n的循环,将数组P[]初始化为0。
  3. 接着,调用perm2(n)这个过程,开始生成排列。
  4. 过程perm2(m)的目标是生成从m到0的所有排列。
  5. 如果m等于0,表示我们已经完成了整个排列,将当前的排列输出。
  6. 否则,我们从1到n的循环,对于每个位置j,如果P[j]等于0,表示该位置还没有被填入数字,则我们将数字m填入该位置。
  7. 然后,递归调用perm2(m-1),继续填入下一个数字。
  8. 在递归返回之前,将填入的数字重新置为0,以便下一轮迭代使用。
  9. 当所有位置都填满数字后,输出当前排列。
  10. 最终,当m等于0时,输出P[1..n]表示一种排列。
Time Complexity:  Q ( nn !)

        Since there are n! permutations, Step 1 of Procedure perm2 takes nn! to output all permutations.

        Now we count the number of iterations of the for loop. The for loop is executed n times in every call perm2(m) plus the number of times it is executed in the recursive call perm2(m − 1). When Procedure perm2 is invoked by the call perm2(m) with m > 0, the array P contains exactly m zeros, and hence the recursive call perm2(m − 1) will be executed exactly m times. When m = 0, the number of iterations is zero, and the number of iterations can be expressed by the recurrence

### 如何使用JavaCC生成解析器 Java Compiler Compiler (JavaCC) 是一种工具,可以用来生成基于给定语法文件的解析器。它是一种LL(k)类型的自顶向下解析器生成器[^1]。以下是关于如何利用JavaCC来生成解析器的具体方法。 #### 准备工作 首先需要下载并安装JavaCC软件包。可以从其官方网站或其他可信资源获取最新版本的JavaCC。确保环境变量配置正确以便能够在命令行中调用`javacc`命令[^1]。 #### 创建语法文件 编写一个描述目标语言结构的`.jj`文件。此文件定义了词法规则和语法规则。例如: ```java PARSER_BEGIN(MyParser) public class MyParser { public static void main(String[] args) throws ParseException { MyParser parser = new MyParser(System.in); parser.Start(); } } PARSER_END(MyParser) TOKEN : { < INTEGER : "[0-9]+" > } void Start() : {} { (<INTEGER>)+ } ``` 这段代码片段展示了一个简单的例子,其中定义了一个接受整数序列输入的解析器[^1]。 #### 使用JavaCC生成解析器 通过执行如下命令行指令来运行JavaCC工具以生成解析器类: ```bash javacc MyGrammar.jj ``` 这会依据指定的`.jj`文件生成一系列Java源码文件,这些文件共同构成了能够解析符合所定义语法字符串的解析器[^1]。 如果希望扩展功能或者定制化行为,则可考虑集成JJTree作为预处理器的一部分流程[^2]^。 JJTree允许开发者构建抽象语法树(AST),从而更方便地操作复杂的表达式或数据结构[^3]。 #### 配置选项优化性能 为了提高效率,在某些情况下可能想要调整默认设置比如关闭部分静态检查项。可以通过修改`.options`参数实现这一点;具体而言,“SANITY_CHECKS”是一个布尔型选项,默认开启(true),当设为false时能减少不必要的验证过程加快编译速度但是也可能隐藏潜在错误风险因此需谨慎对待[^4]。 ### 总结 综上所述,借助于强大的JavaCC框架及其配套组件(JJTree等),我们可以轻松高效地开发出自定义需求下的各类解析解决方案。无论是基础应用还是高级特性支持都提供了极大的灵活性与便利性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值