English 中文(简体)
Bijective “Integer <-> String” function
原标题:Bijective "Integer <-> String" function

我试图为这一问题找到最佳解决办法。 在[0......N]范围内,我有一套明确的非否定性分类。 我需要能够代表本套中的每一份数字,并能够将这种str倒数改为原来的数字。 因此,这应该是双管齐下的职能。

Additional requirements are:

  1. String representation of a number should obfuscate original number at least to some degree. So primitive solution like f(x) = x.toString() will not work.
  2. String length is important: the less the better.
  3. If one knows the string representation of K, I would like it to be non-trivial (to some degree) to guess the string representation of K+1.

For p.1 & p.2 the obvious solution is to use something like Base64 (or whatever BaseXXX to fit all the values) notation. But can we fit into p.3 with minimal additional effort? Common sense tells me that I additionally need a bijective "String <-> String" function for BaseXXX values. Any suggestions? Or maybe there s something better than BaseXXX to use to fit all 3 requirements?

问题回答

This method meets requirements 1-3, but it is perhaps a bit too computationally expensive:

  1. find a prime p > N+2, not too much larger
  2. find a primitive root g modulo p, that is, a number whose multiplicative order modulo p is p-1
  3. for 0 <= k <= N, let enc(k) = min {j > 0 : g^j == (k+2) (mod p)}
  4. f(k) = enc(k).toString()

编制一个长度表<代码>M。 该表应绘制0至M-1的地图,以随机顺序标明短体。 用表格中的插图表示数字。 直截了当的反射。

有了<代码>M=26,你只能用一封信给每位数字。 或采用<代码>M=256,每位数使用一个星号。

Not even remotely a good cryptographic approach!

因此,你需要说明,混淆了原来的编号,但允许在知道地(K)时确定地(K+1)?

How about just doing f(x) = (x + a).toString(), where a is secret? Then an outside user can t determine x from f(x), but they can be confident that if they have a string "1234", say, for an unknown x then "1235" maps to x+1.

第1页和第3页略为矛盾,也含糊不清。

我将提议使用X射线表示。

17 => 0x11
123123 => 1E0F3




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签