字符串数和背部算法 [英] String to Number and back algorithm

查看:125
本文介绍了字符串数和背部算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很难(对我来说)我希望大家能帮助我。我有一些文字,我需要把它转移到一个号码,但它必须是唯一的,就像文字是独一无二的。

This is a hard one (for me) I hope people can help me. I have some text and I need to transfer it to a number, but it has to be unique just as the text is unique.

例如: 这个词'小猫咪'能够产生12432,但只有两个字猫咪产生一个号码。该文本可以是任何东西,并适当数量应给予。

For example: The word 'kitty' could produce 12432, but only the word kitty produces that number. The text could be anything and a proper number should be given.

一个问题的结果整数必须我一个32位无符号整数,这意味着尽可能多的为2147483647。我不介意,如果有一个文本长度的限制,但我希望它可以尽可能大。

One problem the result integer must me a 32-bit unsigned integer, that means the largest possible number is 2147483647. I don't mind if there is a text length restriction, but I hope it can be as large as possible.

我的尝试。你有字母AZ和0-9这样一个角色可以有1-36之间的数字。但是,如果A = 1和B = 2,文本是一个(1)B(2),并添加它,你会得到3的结果,这个问题是文本BA产生相同的结果,所以这algoritm不会的工作。

My attempts. You have the letters A-Z and 0-9 so one character can have a number between 1-36. But if A = 1 and B = 2 and the text is A(1)B(2) and you add it you will get the result of 3, the problem is the text BA produces the same result, so this algoritm won't work.

任何想法,我指出了正确的方向或者是不可能的呢?

Any ideas to point me in the right direction or is it impossible to do?

推荐答案

您的想法是一般理智,只需要开发了一些。

Your idea is generally sane, only needs to be developed a little.

F(C)在范围<$ C $函数转换字符 C 来的唯一号码C> [0..M-1] 。然后你就可以计算出结果数量整个字符串是这样的。

Let f(c) be a function converting character c to a unique number in range [0..M-1]. Then you can calculate result number for the whole string like this.

f(s[0]) + f(s[1])*M + f(s[2])*M^2 + ... + f(s[n])*M^n

您可以很容易地证明这个数字将是唯一的特定字符串(你可以得到的字符串从后面的数字)。

You can easily prove that number will be unique for particular string (and you can get string back from the number).

显然,你不能使用很长的字符串在这里(最多6个字符的情况下),因为 36 ^ N 生长快。

Obviously, you can't use very long strings here (up to 6 characters for your case), as 36^n grows fast.

这篇关于字符串数和背部算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆