- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设文件 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
是棘手的部分。您可以将其分成两个循环:
或者您可以使用模运算将值“包装”在 i - m < 0
周围。 :
int valueToSutract = array[(i - m) % n];
关于c# - 实现递归哈希算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409929/
背景: 我最近一直在使用 JPA,我为相当大的关系数据库项目生成持久层的轻松程度给我留下了深刻的印象。 我们公司使用大量非 SQL 数据库,特别是面向列的数据库。我对可能对这些数据库使用 JPA 有一
我已经在我的 maven pom 中添加了这些构建配置,因为我希望将 Apache Solr 依赖项与 Jar 捆绑在一起。否则我得到了 SolarServerException: ClassNotF
interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle {
我希望可用于 Java 的对象/关系映射 (ORM) 工具之一能够满足这些要求: 使用 JPA 或 native SQL 查询获取大量行并将其作为实体对象返回。 允许在行(实体)中进行迭代,并在对当前
好像没有,因为我有实现From for 的代码, 我可以转换 A到 B与 .into() , 但同样的事情不适用于 Vec .into()一个Vec . 要么我搞砸了阻止实现派生的事情,要么这不应该发
在 C# 中,如果 A 实现 IX 并且 B 继承自 A ,是否必然遵循 B 实现 IX?如果是,是因为 LSP 吗?之间有什么区别吗: 1. Interface IX; Class A : IX;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在阅读标准haskell库的(^)的实现代码: (^) :: (Num a, Integral b) => a -> b -> a x0 ^ y0 | y0 a -> b ->a expo x0
我将把国际象棋游戏表示为 C++ 结构。我认为,最好的选择是树结构(因为在每个深度我们都有几个可能的移动)。 这是一个好的方法吗? struct TreeElement{ SomeMoveType
我正在为用户名数据库实现字符串匹配算法。我的方法采用现有的用户名数据库和用户想要的新用户名,然后检查用户名是否已被占用。如果采用该方法,则该方法应该返回带有数据库中未采用的数字的用户名。 例子: “贾
我正在尝试实现 Breadth-first search algorithm , 为了找到两个顶点之间的最短距离。我开发了一个 Queue 对象来保存和检索对象,并且我有一个二维数组来保存两个给定顶点
我目前正在 ika 中开发我的 Python 游戏,它使用 python 2.5 我决定为 AI 使用 A* 寻路。然而,我发现它对我的需要来说太慢了(3-4 个敌人可能会落后于游戏,但我想供应 4-
我正在寻找 Kademlia 的开源实现C/C++ 中的分布式哈希表。它必须是轻量级和跨平台的(win/linux/mac)。 它必须能够将信息发布到 DHT 并检索它。 最佳答案 OpenDHT是
我在一本书中读到这一行:-“当我们要求 C++ 实现运行程序时,它会通过调用此函数来实现。” 而且我想知道“C++ 实现”是什么意思或具体是什么。帮忙!? 最佳答案 “C++ 实现”是指编译器加上链接
我正在尝试使用分支定界的 C++ 实现这个背包问题。此网站上有一个 Java 版本:Implementing branch and bound for knapsack 我试图让我的 C++ 版本打印
在很多情况下,我需要在 C# 中访问合适的哈希算法,从重写 GetHashCode 到对数据执行快速比较/查找。 我发现 FNV 哈希是一种非常简单/好/快速的哈希算法。但是,我从未见过 C# 实现的
目录 LRU缓存替换策略 核心思想 不适用场景 算法基本实现 算法优化
1. 绪论 在前面文章中提到 空间直角坐标系相互转换 ,测绘坐标转换时,一般涉及到的情况是:两个直角坐标系的小角度转换。这个就是我们经常在测绘数据处理中,WGS-84坐标系、54北京坐标系
在软件开发过程中,有时候我们需要定时地检查数据库中的数据,并在发现新增数据时触发一个动作。为了实现这个需求,我们在 .Net 7 下进行一次简单的演示. PeriodicTimer .
二分查找 二分查找算法,说白了就是在有序的数组里面给予一个存在数组里面的值key,然后将其先和数组中间的比较,如果key大于中间值,进行下一次mid后面的比较,直到找到相等的,就可以得到它的位置。
我是一名优秀的程序员,十分优秀!