康托展开

2017-07-06  本文已影响0人  Gitfan

原博客
  康托展开的公式是 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"]。

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdlib>
using namespace std;
class Cantor
{
private:
    string str;//要康托展开的字符串
    char *indexStr;//str排序后的字符串
    int len;//字符串长度
    int *fact;//阶乘
    void Init();
    void toCharArray();

public:
    Cantor(string str):str(str),len(str.length())
    {
        Init();
    };
    int encode(string ss);
    string decode(int val);
};
void Cantor::Init()
{
    fact=new int[len];
    fact[0]=fact[1]=1;
    for(int i=2;i<len;i++)
    {
        fact[i]=i*fact[i-1];
    }
    toCharArray();
}
void Cantor::toCharArray()
{
    indexStr=new char[len];
    for(int i=0;i<len;i++)
    {
        indexStr[i]=str[i];
    }
    sort(indexStr,indexStr+len);
}
int Cantor::encode(string ss)//返回字符串在全排列中的字典位置,从0开始
{
    int cnt,pos=0;
    for(int i=0;i<len;i++)
    {
        cnt=0;
        for(int j=i;j<len;j++)
        {
            if(ss[i]>ss[j]) cnt++;
        }
        pos+=cnt*fact[len-i-1];
    }
    return pos;
}
string Cantor::decode(int val)//通过字符串在全排列中的字典位置找到相应的字符串
{
    div_t divResult;//求商和余数的数据结构
    string order,res;
    vector<int> num;
    for(int i=0;i<len;i++)
    {
        num.push_back(i);
    }
    for(int i=len-1;i>=0;i--)
    {
        divResult=div(val,fact[i]);
        order.push_back(num[divResult.quot]);
        num.erase(num.begin()+divResult.quot);
        val=divResult.rem;
    }
    for(int i=0;i<len;i++)
    {
        res.push_back(indexStr[order[i]]);
    }
    return res;
}
int main(){
    Cantor cantor("ABCD");

    cout<<cantor.encode("DBAC")<<endl;
    cout<<cantor.decode(cantor.encode("DBAC"))<<endl;

    cout<<cantor.encode("BCAD")<<endl;
    cout<<cantor.decode(cantor.encode("BCAD"))<<endl;
}

上一篇下一篇

猜你喜欢

热点阅读