康托展开(Cantor expansion)及逆康托展开

康托展开(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,132 两个排列小于 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;
}

如有错误还请指出。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A91A981E

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值