English 中文(简体)
账务司方法
原标题:Division method for Hash Values

Suppose that a string of r characters is hashed into m slots by treating it as a radix-128 number and then using the division method. The number m is easily represented as a 32-bit computer word, but the string of r characters, treated as a radix-128 number, takes many words. How can we apply the division method to compute the hash value of the character string without using more than a constant number of words of storage outside the string itself?

最佳回答

rad中的任何一位数:

number=a0*r^0+a1*r^1+a2*r^2+...+a(n-1)*r^(n-1)

为了计算这一数目的数值,我们确实这样做。

(a0*r^0+a1*r^1+a2*r^2+...+a(n-1)*r^(n-1))%m

但注意到

(a0*r^0+a1*r^1+a2*r^2+...+a(n-1)*r^(n-1))%m
   = ((a0*r^0)%m + (a1*r^1)%m+(a2*r^2)%m+...+(a(n-1)*r^(n-1))%m)%m
   = (sum over 0<=i<n: (ai*r^i)%m)%m

因此,在计算(i^ri)%值和累积数额时,你只能重复一度。

《刑法》(在沙里)

def hash_code(s,radix,mod):
        pwr=1 # radix^0=1
        answer=0
        for index,character in enumerate(s):
            answer=(answer+(ord(character)*pwr)%mod)%mod
            pwr=(pwr*radix)%mod # radix^(i+1)=radix*radix^i
        return answer

在每次作业后,再使用%的运营商来避免过度流入(尽管这在沙捞中是绝对必要的)。

问题回答

You can use Horner s method/rule.

y = 0
for i = (n - 1) downto 0
    y = (ai + 128y) mod m
return y




相关问题
XML-RPC Standard and XML Data Type

I was looking at XML-RPC for a project. And correct me if I m wrong, but it seems like XML-RPC has no XML datatype. Are you supposed to pass as a string? or something else? Am I missing something? ...

Is it exists any "rss hosting" with API for creating feeds

I am creating a desktop app that will create some reports. I want to export these reports as RSS or ATOM feeds. I can easily create feeds with Rome lib for Java. But I have no idea how to spread them. ...

Improving Q-Learning

I am currently using Q-Learning to try to teach a bot how to move in a room filled with walls/obstacles. It must start in any place in the room and get to the goal state(this might be, to the tile ...

High-traffic, Highly-secure web API, what language? [closed]

If you were planning on building a high-traffic, very secure site what language would you use? For example, if you were planning on say building an authorize.net-scale site, that had to handle tons ...

Def, Void, Function?

Recently, I ve been learning different programming langages, and come across many different names to initalize a function construct. For instance, ruby and python use the def keyword, and php and ...

热门标签