1 / 7

作业一

作业一. 130321033 、张鑫 2014-05-09. 问题描述. 字典序问题. 对问题的理解和分析. A~Z 序列共 26 个,其实质就是数学中的组合 C(1,26) 同理, AA~ZZ 就是 C(2,26)=26*25/2!=325 BCD~BYZ 就是 C(2,26-2)=24*23/2!=276 ABD=(A~Z)+(AB~YZ)+(D-B)=26+325+2 EGH=(A~Z)+(AB~YZ)+(ABC~AYZ)+(BCD~BYZ)+(CDE~CYZ)+(EFG~EFZ)+(G~H). 算法与数据结构的选取. 1 、先算出前 n 位的组合值

elani
Download Presentation

作业一

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 作业一 130321033、张鑫 2014-05-09

  2. 问题描述 • 字典序问题

  3. 对问题的理解和分析 A~Z序列共26个,其实质就是数学中的组合C(1,26) 同理,AA~ZZ就是C(2,26)=26*25/2!=325 BCD~BYZ就是C(2,26-2)=24*23/2!=276 ABD=(A~Z)+(AB~YZ)+(D-B)=26+325+2 EGH=(A~Z)+(AB~YZ)+(ABC~AYZ)+(BCD~BYZ)+(CDE~CYZ)+(EFG~EFZ)+(G~H)

  4. 算法与数据结构的选取 1、先算出前n位的组合值 2、算出长度为n的组合的值,这里借用数组来实现。如果标记为0,则C(len-1,26-(ch-’A’+1))。Len表示字符串长度,ch表示字符串中的字符。如果标记为1,则len-1,继续循环,直到长度为1为止。 3、最后一位减去前一位

  5. 核心代码

  6. 算法复杂度分析 • 值是直接计算出来的,所以算法复杂度为O(1) • 这里耗时主要在计算组合上,如果先全部乘起来,再算阶层,最后相除的话,考虑到长度为26的情况该是个很大的数,所以这里可以优化为边乘边除。还有就是数学公式中C(m,n)=C(n-m,n)公式来优化。 • C(5,26)=26*25*24*23*22/1*2*3*4*5

  7. 谢谢!

More Related