康託展開和逆康託展開

康託展開

  康託展開的公式是 X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0! 其中,ai爲當前未出現的元素中是排在第幾個(從0開始)。

  這個公式可能看着讓人頭大,最好舉個例子來說明一下。

      例如,有一個數組 s = ["A", "B", "C", "D"],它的一個排列 s1 = ["D", "B", "A", "C"],現在要把 s1 映射成 X。n 指的是數組的長度,也就是4,所以 X(s1) = a4*3! + a3*2! + a2*1! + a1*0!

      關鍵問題是 a4、a3、a2 和 a1 等於啥?
      a4 = "D" 這個元素在子數組 ["D", "B", "A", "C"] 中是第幾大的元素。"A"是第0大的元素,"B"是第1大的元素,"C" 是第2大的元素,"D"是第3大的元素,所以 a4 = 3。
      a3 = "B" 這個元素在子數組 ["B", "A", "C"] 中是第幾大的元素。"A"是第0大的元素,"B"是第1大的元素,"C" 是第2大的元素,所以 a3 = 1。
     a2 = "A" 這個元素在子數組 ["A", "C"] 中是第幾大的元素。"A"是第0大的元素,"C"是第1大的元素,所以 a2 = 0。
     a1 = "C" 這個元素在子數組 ["C"] 中是第幾大的元素。"C" 是第0大的元素,所以 a1 = 0。(因爲子數組只有1個元素,所以a1總是爲0)
     所以,X(s1) = 3*3! + 1*2! + 0*1! + 0*0! = 20


A B C | 0
A C B | 1
B A C | 2
B C A | 3
C A B | 4
C B A | 5


通過康託逆展開生成全排列

  如果已知 s = ["A", "B", "C", "D"],X(s1) = 20,能否推出 s1 = ["D", "B", "A", "C"] 呢?
  因爲已知 X(s1) = a4*3! + a3*2! + a2*1! + a1*0! = 20,所以問題變成由 20 能否唯一地映射出一組 a4、a3、a2、a1?如果不考慮 ai 的取值範圍,有
3*3! + 1*2!   + 0*1!   + 0*0! = 20
2*3! + 4*2!   + 0*1!   + 0*0! = 20
1*3! + 7*2!   + 0*1!   + 0*0! = 20
0*3! + 10*2! + 0*1!   + 0*0! = 20
0*3! + 0*2!   + 20*1! + 0*0! = 20
等等。但是滿足 0 <= ai <= n-1 的只有第一組。可以使用輾轉相除的方法得到 ai,如下圖所示:


知道了a4、a3、a2、a1的值,就可以知道:

s1[0] 是子數組["A", "B", "C", "D"]中第3大的元素 "D"

s1[1] 是子數組 ["A", "B", "C"] 中第1大的元素"B"

s1[2] 是子數組 ["A", "C"] 中第0大的元素"A"

s[3] 是子數組 ["C"] 中第0大的元素"C"

所以s1 = ["D", "B", "A", "C"]。


這樣我們就能寫出一個函數 Permutation3(),它可以返回  s 的第 m 個排列。

前面的內容從http://archive.cnblogs.com/a/2026276/轉載

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdlib>
using namespace std;
class cantor{
public:
	int n;//字符串的長度
	string s;
	int pos;//字符串在全排列中的字典位置,從0開始
	vector<int>num;//所有的字符
	cantor(string s):s(s){n=s.size();}
	cantor(int n,int pos):n(n),pos(pos){
		int i;
		for(i=0;i<n;i++)
			num.push_back(i);
	}
	int fac(int);
	void encode();
	void decode();
	
};
int cantor::fac(int num){
	if(num==0) return 1;
		else return num*fac(num-1);
}
void cantor::encode(){
	int i,j,count;
	vector<int>vec(n);
	for(i=0;i<n;i++){
		count=0;
		for(j=i;j<n;j++)
			if(s[i]>s[j]) count++;	
		vec[n-i-1]=count;
				}
	pos=0;
	for(i=0;i<s.size();i++)
		pos+=vec[i]*fac(i);	
}
void cantor::decode(){
	int i;
	div_t divresult;
	for(i=n-1;i>=0;i--){
		divresult=div(pos,fac(i));求餘數與除數
		s.push_back(num[divresult.quot]+'0');
		num.erase(num.begin()+divresult.quot);
		pos=divresult.rem;
			}
}
int main(){
	cantor test(4,2);
	test.decode();
	cout<<test.s<<endl;
}

代碼轉載自http://blog.csdn.net/zhongkeli/article/details/6966805
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章