第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

懶惰地生成排列

懶惰地生成排列

我正在尋找一種算法來(lái)生成集合的排列,以便可以在Clojure中列出它們的惰性列表。即,我想遍歷一系列排列,在我請(qǐng)求之前不會(huì)計(jì)算每個(gè)排列,并且不必將所有排列立即存儲(chǔ)在內(nèi)存中。或者,我正在尋找一種算法,給定特定集合,該算法將返回該集合的“下一個(gè)”排列,以這種方式,在其自己的輸出上重復(fù)調(diào)用該函數(shù)將循環(huán)遍歷原始集合的所有排列,一些訂單(順序無(wú)關(guān)緊要)。有這樣的算法嗎?我見(jiàn)過(guò)的大多數(shù)排列生成算法都傾向于一次全部生成它們(通常是遞歸生成),而這些算法并不能擴(kuò)展到非常大的集合。用Clojure(或另一種功能語(yǔ)言)實(shí)現(xiàn)將很有幫助,但我可以從偽代碼中弄清楚。
查看完整描述

3 回答

?
侃侃無(wú)極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

假設(shè)我們?cè)谟懻撆帕兄档淖值漤樞?,則可以使用兩種通用方法:


將元素的一個(gè)排列轉(zhuǎn)換為下一個(gè)排列(如ShreevatsaR發(fā)布),或者

從0向上n計(jì)數(shù)n,直接計(jì)算th排列。

對(duì)于那些不像本地人那樣講c ++的人(如我;-),可以從下面的偽代碼實(shí)現(xiàn)方法1,假設(shè)索引的零從零開(kāi)始在索引的左側(cè)為數(shù)組(用其他結(jié)構(gòu)代替) ,例如列表,是“作為練習(xí)留下的;”-):


1. scan the array from right-to-left (indices descending from N-1 to 0)

1.1. if the current element is less than its right-hand neighbor,

     call the current element the pivot,

     and stop scanning

1.2. if the left end is reached without finding a pivot,

     reverse the array and return

     (the permutation was the lexicographically last, so its time to start over)

2. scan the array from right-to-left again,

   to find the rightmost element larger than the pivot

   (call that one the successor)

3. swap the pivot and the successor

4. reverse the portion of the array to the right of where the pivot was found

5. return

這是一個(gè)從CADB當(dāng)前排列開(kāi)始的示例:


1. scanning from the right finds A as the pivot in position 1

2. scanning again finds B as the successor in position 3

3. swapping pivot and successor gives CBDA

4. reversing everything following position 1 (i.e. positions 2..3) gives CBAD

5. CBAD is the next permutation after CADB

對(duì)于第二種方法(直接計(jì)算nth排列),請(qǐng)記住存在元素的N!排列N。因此,如果要排列N元素,則第一個(gè)(N-1)!排列必須以最小的元素開(kāi)始,接下來(lái)的(N-1)!排列必須以第二個(gè)最小的元素開(kāi)始,依此類推。這導(dǎo)致以下遞歸方法(再次使用偽代碼,從0開(kāi)始對(duì)排列和位置進(jìn)行編號(hào)):


To find permutation x of array A, where A has N elements:

0. if A has one element, return it

1. set p to ( x / (N-1)! ) mod N

2. the desired permutation will be A[p] followed by

   permutation ( x mod (N-1)! )

   of the elements remaining in A after position p is removed

因此,例如,發(fā)現(xiàn)ABCD的第13個(gè)置換如下:


perm 13 of ABCD: {p = (13 / 3!) mod 4 = (13 / 6) mod 4 = 2; ABCD[2] = C}

C followed by perm 1 of ABD {because 13 mod 3! = 13 mod 6 = 1}

  perm 1 of ABD: {p = (1 / 2!) mod 3 = (1 / 2) mod 2 = 0; ABD[0] = A}

  A followed by perm 1 of BD {because 1 mod 2! = 1 mod 2 = 1}

    perm 1 of BD: {p = (1 / 1!) mod 2 = (1 / 1) mod 2 = 1; BD[1] = D}

    D followed by perm 0 of B {because 1 mod 1! = 1 mod 1 = 0}

      B (because there's only one element)

    DB

  ADB

CADB

順便說(shuō)一下,元素的“刪除”可以由布爾值的并行數(shù)組表示,該數(shù)組指示哪些元素仍然可用,因此不必在每個(gè)遞歸調(diào)用上創(chuàng)建新的數(shù)組。


因此,要遍歷ABCD的排列,只需從0到23(4!-1)計(jì)數(shù)并直接計(jì)算對(duì)應(yīng)的排列即可。


查看完整回答
反對(duì) 回復(fù) 2019-12-09
  • 3 回答
  • 0 關(guān)注
  • 708 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)