2013-12-19 70 views
0

我正在尝试想出一个我想到的问题的解决方案。我有26个字符的排列数,有6个可能的点,分别为26^6 = 308 915 776.我试图设法让每个数字映射到一个独特的组合,并能够从组合中来回移动编号。将唯一组合映射到数字

An example: 
1 = aaaaaa 
2 = aaaaab 
27 = aaaaba 

是否可以写一个多项式时间算法,将两个和/或是否有什么我试图做任何有效的实例之间进行转换。

回答

2

这只是基地转换我的朋友。

由于您未指定语言,因此以下代码为伪代码,其数组索引和字符串索引从0开始,赋值为:=。

如果你让 'A' 为0, 'Z' 是25,然后再从底部26转换为10进制:

total:= 0 
loop index from 0 to 5 
    temp:= 'z' - input[index] // Left to right. Single base 26 digit to base 10 
    total:= 26 * total + temp // Shift left and add the converted digit 
    increment index and goto loop start 

要返回字母(基地26)也很容易:

result:= '' 
loop index from 0 to 5 
    temp:= 'a' + input mod 26 // Input modulus 26 is the base 26 digit to add next 
    result:= temp + result // Append current result to the new base 26 digit 
    input:= input div 26 // Divide input by 26, throw away the remainder 
    increment index and goto loop start 

如果你想全A为1,然后添加一个从底部26转换为10进制,并从基地10转换为基座26个人之前减去1后,我让全部的为0。

1

你可以通过指针映射成一个双:

char *example = "abcdef"; 

double d = 0; 
char *p = (char *)&d; 
for (int i=0; i<6; i++) 
    p[i] = example[i]; 

// d is your code 

它是不是很漂亮,并允许不是100%,但它的作品。