gpt4 book ai didi

c# - 实现递归哈希算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:48:33 25 4
gpt4 key购买 nike

假设文件 A 有字节:

2
5
8
0
33
90
1
3
200
201
23
12
55

我有一个简单的散列算法,我在其中存储最后三个连续字节的总和:

2   
5
8 - = 8+5+2 = 15
0
33
90 - = 90+33+0 = 123
1
3
200 - = 204
201
23
12 - = 236

所以我可以将文件 A 表示为 15, 123, 204, 236

假设我将该文件复制到新计算机 B 并做了一些小修改,文件 B 的字节为:

255 
2
5
8
0
33
90
1
3
200
201
23
12
255
255

“注意不同之处在于文件开头多了一个字节,末尾多了 2 个字节,但其余部分非常相似”

所以我可以执行相同的算法来确定文件的某些部分是否相同。请记住,文件 A 由哈希码 15, 123, 204, 236 表示,让我们看看文件 B 是否提供了一些哈希码!

所以在文件 B 上我必须每 3 个连续字节执行一次

int[] sums; // array where we will hold the sum of the last bytes


255 sums[0] = 255
2 sums[1] = 2+ sums[0] = 257
5 sums[2] = 5+ sums[1] = 262
8 sums[3] = 8+ sums[2] = 270 hash = sums[3]-sums[0] = 15 --> MATHES FILE A!
0 sums[4] = 0+ sums[3] = 270 hash = sums[4]-sums[1] = 13
33 sums[5] = 33+ sums[4] = 303 hash = sums[5]-sums[2] = 41
90 sums[6] = 90+ sums[5] = 393 hash = sums[6]-sums[3] = 123 --> MATHES FILE A!
1 sums[7] = 1+ sums[6] = 394 hash = sums[7]-sums[4] = 124
3 sums[8] = 3+ sums[7] = 397 hash = sums[8]-sums[5] = 94
200 sums[9] = 200+ sums[8] = 597 hash = sums[9]-sums[6] = 204 --> MATHES FILE A!
201 sums[10] = 201+ sums[9] = 798 hash = sums[10]-sums[7] = 404
23 sums[11] = 23+ sums[10] = 821 hash = sums[11]-sums[8] = 424
12 sums[12] = 12+ sums[11] = 833 hash = sums[12]-sums[9] = 236 --> MATHES FILE A!
55 sums[13] = 55+ sums[12] = 888 hash = sums[13]-sums[10] = 90
255 sums[14] = 255+ sums[13] = 1143 hash = sums[14]-sums[11] = 322
255 sums[15] = 255+ sums[14] = 1398 hash = sums[15]-sums[12] = 565

所以通过查看该表,我知道文件 B 包含文件 A 的字节加上其他字节,因为哈希码匹配。

我展示这个算法的原因是因为它是 n 阶的,换句话说,我能够计算出最后 3 个连续字节的哈希值,而无需遍历它们!

如果我在哪里有一个更复杂的算法,比如对最后 3 个字节进行 md5,那么它将是 n^3 阶,这是因为当我遍历文件 B 时,我将必须有一个内部 for 循环来计算最后三个字节的散列。

所以我的问题是:

我怎样才能改进保持 n 阶的算法。那只是计算一次哈希。如果我使用现有的哈希算法,如 md5,我将不得不在算法中放置一个内部循环,这将显着增加算法的阶数。

请注意,可以用乘法而不是加法来做同样的事情。但柜台显着增长非常快。也许我可以结合乘法和加法和减法...

编辑

此外,如果我用谷歌搜索:

递归哈希函数 in-gram

出现了很多信息,我认为那些算法很难理解...

我必须为一个项目实现这个算法,这就是我重新发明轮子的原因……我知道有很多算法。

我考虑的另一个替代解决方案是执行相同的算法加上另一个强大的算法。所以在文件 A 上,我将每 3 个字节加上每 3 个字节的 md5 执行相同的算法。在第二个文件中,如果第一个算法成立,我将只执行第二个算法....

最佳答案

编辑:

我越是思考你所说的“递归”是什么意思,我就越怀疑我之前提出的解决方案是否是你应该实现的以做任何有用的事情。

您可能想要 implement a hash tree algorithm ,这是一个递归操作。

为此,您对列表进行哈希处理,将列表一分为二,然后递归到这两个子列表中。当您的列表大小为 1 或所需的最小哈希大小时终止,因为每个递归级别都会使您的总哈希输出的大小加倍。

伪代码:

create-hash-tree(input list, minimum size: default = 1):
initialize the output list
hash-sublist(input list, output list, minimum size)
return output list

hash-sublist(input list, output list, minimum size):
add sum-based-hash(list) result to output list // easily swap hash styles here
if size(input list) > minimum size:
split the list into two halves
hash-sublist(first half of list, output list, minimum size)
hash-sublist(second half of list, output list, minimum size)

sum-based-hash(list):
initialize the running total to 0

for each item in the list:
add the current item to the running total

return the running total

我认为整个算法的运行时间是O(hash(m)); m = n * (log(n) + 1) , 与 hash(m)通常是线性时间。

存储空间类似于O(hash * s); s = 2n - 1 ,散列通常是恒定大小。

请注意,对于 C#,我会将输出列表设为 List<HashType> ,但我会将输入列表设为 IEnumerable<ItemType>节省存储空间,并使用 Linq 快速“拆分”列表,而无需分配两个新的子列表。

原文:

我想你可以把它变成 O(n + m)执行时间处理时间;其中 n是列表的大小,m是运行计数的大小,n < m (否则所有总和将相等)。

双端队列

内存消耗将是堆栈大小加上大小 m用于临时存储。

为此,请使用双端队列和运行总计。将新遇到的值插入列表,同时添加到运行总计,以及当队列达到大小时 m , 从列表中弹出并从运行总计中减去。

这是一些伪代码:

initialize the running total to 0

for each item in the list:
add the current item to the running total
push the current value onto the end of the dequeue
if dequeue.length > m:
pop off the front of the dequeue
subtract the popped value from the running total
assign the running total to the current sum slot in the list

reset the index to the beginning of the list

while the dequeue isn't empty:
add the item in the list at the current index to the running total
pop off the front of the dequeue
subtract the popped value from the running total
assign the running total to the current sum slot in the list
increment the index

这不是递归,而是迭代。

这个算法的运行看起来像这样(对于 m = 3 ):

value   sum slot   overwritten sum slot
2 2 92
5 7 74
8 15 70
0 15 15
33 46
90 131
1 124
3 127
200 294
201 405
23 427
12 436
55 291

带索引

您可以通过对最后一个 m 求和来移除队列并重新分配任何槽位值开始,并使用索引的偏移量而不是弹出出队,例如array[i - m] .

这不会减少您的执行时间,因为您仍然需要两个循环,一个用于建立运行计数,另一个用于填充所有值。但它只会将您的内存使用量减少到堆栈空间(有效 O(1) )。

这是一些伪代码:

initialize the running total to 0

for the last m items in the list:
add those items to the running total

for each item in the list:
add the current item to the running total
subtract the value of the item m slots earlier from the running total
assign the running total to the current sum slot in the list

m slots earlier是棘手的部分。您可以将其分成两个循环:

  • 从列表末尾开始索引,减去m,加上i
  • 索引从 i 减 m

或者您可以使用模运算将值“包装”在 i - m < 0 周围。 :

int valueToSutract = array[(i - m) % n];

关于c# - 实现递归哈希算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com