stl演算法:next_permutation剖析 .

來源:互聯網
上載者:User

 

在標準庫演算法中,next_permutation應用在數列操作上比較廣泛.這個函數可以計算一組資料的全排列.但是怎麼用,原理如何,我做了簡單的剖析.

首先查看stl中相關資訊.
函數原型:

template<class BidirectionalIterator>
   bool next_permutation(
      BidirectionalIterator
_First,

      BidirectionalIterator
_Last
   );
template<class BidirectionalIterator, class BinaryPredicate>
   bool next_permutation(
      BidirectionalIterator
_First,

      BidirectionalIterator
_Last,
      BinaryPredicate _Comp
   );

兩個重載函數,第二個帶謂詞參數_Comp,其中只帶兩個參數的版本,預設指令動詞函數為"小於".

傳回值:bool類型

分析next_permutation函數執行過程:

假設數列 d1,d2,d3,d4……

範圍由[first,last)標記,調用next_permutation使數列逐次增大,這個遞增過程按照字典序。例如,在字母表中,abcd的下一單詞排列為abdc,但是,有一關鍵點,如何確定這個下一排列為字典序中的next,而不是next->next->next……

若當前調用排列到達最大字典序,比如dcba,就返回false,同時重新設定該排列為最小字典序。

返回為true表示產生下一排列成功。下面著重分析此過程:

根據標記從後往前比較相鄰兩資料,若前者小於(預設為小於)後者,標誌前者為X1(位置PX)表示將被替換,再次重後往前搜尋第一個不小於X1的資料,標記為X2。交換X1,X2,然後把[PX+1,last)標記範圍置逆。完成。

要點:為什麼這樣就可以保證得到的為最小遞增。

從位置first開始原數列與新數列不同的資料位元置是PX,並且新資料為X2。[PX+1,last)總是遞減的,[first,PX)沒有改變,因為X2>X1,所以不管X2後面怎樣排列都比原數列大,反轉[PX+1,last)使此子數列(遞增)為最小。從而保證的新數列為原數列的字典序排列next。

明白了這個原理後,看下面例子:

int main(){
 int a[] = {3,1,2};
do{
     cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
 while (next_permutation(a,a+3));
 return 0;
}

輸出:312/321         因為原數列不是從最小字典排列開始。

所以要想得到所有全排列

 int a[] = {3,1,2};   change to  int a[] = {1,2,3};

 

 

另外,庫中另一函數prev_permutation與next_permutation相反,由原排列得到字典序中上一次最近排列。

所以

int main(){
 int a[] = {3,2,1};
do{
     cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
 while (prev_permutation(a,a+3));
 return 0;
}

才能得到123的所有排列。

轉載於:http://blog.csdn.net/aipb2008/article/details/2227490

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.