1. 程式人生 > 其它 >康託展開(Cantor expansion)及逆康託展開

康託展開(Cantor expansion)及逆康託展開

技術標籤:演算法演算法c++

康託展開(Cantor expansion)及逆康託展開



康託展開


康託展開的定義

康託展開是一個全排列到一個自然數的雙射,常用於構建雜湊表時的空間壓縮。 康託展開的實質是計算當前排列在所有由小到大全排列中的順序,因此是可逆的。

應用方面: 求集合全排列中某一狀態的字典序

例如,給定集合 S e t = { 1 , 2 , 3 } Set = \{1,2,3\} Set={1,2,3} ,需要求排列 213 213 213 的字典序大小。易得,在 S e t Set Set 的全排列中, 有 123 , 132 123, 132 123,1

32 兩個排列小於 213 213 213 的字典序,所以 213 213 213 的康託展開值是 2 2 2,序號為3。

排列序號康託展開值
1231 0 × 2 ! + 0 × 1 ! + 0 × 0 ! 0\times2!+0\times1!+0\times0! 0×2!+0×1!+0×0!
1322 0 × 2 ! + 1 × 1 ! + 0 × 0 ! 0\times2!+1\times1!+0\times0! 0×2!+1×1!+0×0!
2133 1 × 2 ! + 0 × 1 ! + 0 × 0 ! 1\times2!+0\times1!+0\times0!
1×2!+0×1!+0×0!
2314 1 × 2 ! + 1 × 1 ! + 0 × 0 ! 1\times2!+1\times1!+0\times0! 1×2!+1×1!+0×0!
3125 2 × 2 ! + 0 × 1 ! + 0 × 0 ! 2\times2!+0\times1!+0\times0! 2×2!+0×1!+0×0!
3216 2 × 2 ! + 1 × 1 ! + 0 × 0 ! 2\times2!+1\times1!+0\times0!
2×2!+1×1!+0×0!

計算公式

X = a n × ( n − 1 ) ! + a n − 1 × ( n − 2 ) ! + ⋅ ⋅ ⋅ + a 2 × 1 ! + a 1 × 0 ! X = a_n\times (n-1)!+a_{n-1}\times(n-2)!+···+a_2\times1!+a_1\times0! X=an×(n1)!+an1×(n2)!++a2×1!+a1×0!
  其中, a i a_i ai 為第 i i i 位後的 i − 1 i-1 i1 位中比第 i i i的個數( i i i 為從右往左)。

舉例說明

  已知 S e t = { 1 , 2 , 3 , 4 , 5 } Set = \{1,2,3,4,5 \} Set={1,2,3,4,5} ,求 n = = 34152 n==34152 n==34152 S e t Set Set 全排列從小到大排序後在第幾位。

35142 35142 35142 中,不論從左向右還是從右向左開始建立(不是 i i i 的順序, i i i 都是從右向左),我們都可以得到以下這張表。

n i n_i ni34152
i i i54321
a i a_i ai22010

從左向右為例解釋:

  • n 5 = 3 n_5 = 3 n5=3 ,在後面4位中,有 1 1 1 2 2 2 小於 n 5 n_5 n5,於是 a 5 = 2 a_5=2 a5=2
  • n 4 = 4 n_4 = 4 n4=4 ,在後面3位中,有 1 1 1 2 2 2 小於 n 4 n_4 n4,於是 a 4 = 2 a_4=2 a4=2
  • n 3 = 1 n_3 = 1 n3=1 ,在後面2位中,沒有數小於 n 3 n_3 n3,於是 a 3 = 0 a_3=0 a3=0
  • n 2 = 5 n_2 = 5 n2=5 ,在後面1位中,有 2 2 2 小於 n 2 n_2 n2,於是 a 2 = 1 a_2=1 a2=1
  • n 1 = 2 n_1 = 2 n1=2 ,在後面0位中,沒有數小於 n 1 n_1 n1,於是 a 1 = 0 a_1=0 a1=0

根據公式可得:

在這裡插入圖片描述

所以 34152 34152 34152 前面的排列共有 61 61 61 個,結果就是 61 + 1 = 62 61+1=62 61+1=62

  • 要注意康託展開值是某一排列之前有多少排列。

C/C++語言實現程式碼

#include <bits/stdc++.h>
using namespace std;

int cantor(int n)
{
    const int factorial[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320}; //階乘預處理
    stringstream ss;
    ss << n;
    string s = ss.str();
    int num = s.length(), x = 0, temp;
    for (int i = 0; i < num; i++)
    {
        temp = 0;
        for (int j = i + 1; j < num; j++)
            if (s[i] > s[j])
                temp++;
        x += temp * factorial[num - i - 1];
    }
    return x + 1;
}

int main(int argc, char *argv[])
{
    int n;

    cin >> n;
    cout << cantor(n) << endl;

    return 0;
}

逆康託展開


  逆康託展開只需要將康託展開的過程反過來即可,將序號減一後,依次對階乘作商取餘。程式碼如下:

#include <bits/stdc++.h>
using namespace std;

int decantor(int n, vector<int> v)
{
    const int factorial[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320}; //階乘預處理
    int num = v.size();
    stringstream ss;
    n--;
    for (int i = 0; i < num; i++)
    {
        int temp = n / factorial[num - i - 1];
        n %= factorial[num - 1 - i];
        ss << v[temp];
        v.erase(v.begin() + temp);
    }
    return atoi(ss.str().c_str());
}

int main(int argc, char *argv[])
{
    int n, temp;
    vector<int> vec;
    vec.clear();

    cout << "input index: ";
    cin >> n;
    cout << "input set, end with -1:" << endl;
    while (cin >> temp, temp != -1)
        vec.push_back(temp);
    sort(vec.begin(), vec.end());

    cout << decantor(n, vec) << endl;
    return 0;
}

如有錯誤還請指出。