Pet*_*r T 8 algorithm search data-structures
我有一组位模式,并希望找到集合中与给定输入匹配的元素的索引.位模式包含"不关心"位,即x-es,它匹配0和1.
示例 位模式集是
index abcd
0 00x1
1 01xx
2 100x
3 1010
4 1x11
Run Code Online (Sandbox Code Playgroud)
然后,尝试匹配0110应返回索引1和1011应返回索引4.
如何通过元素线性搜索更快地解决这个问题?我想可以制作一种二叉树,但是,创建这样一棵树的智能方法是什么?是否存在针对此类问题的其他有效数据结构/算法,主要是在查询效率和存储要求方面.
我有两种不同的情况需要解决这个问题
更新 x-es更有可能出现在某些位位置,也就是说,某些位位置将由x-es支配,而其他位将主要为零/ 1.
我认为,模式容器的解决方案将是一个特定的有序树。
该节点中的匹配不应通过叶子完成,而应通过级别完成。一个级别将是一个父级的一组子级。
Take first level of the children as current level
Take the first child on the level for current
read currentnode.position
check the appropriate position in the matched string against child value.
If it fits, go higher up the tree.
If it doesn't fit, go to next child.
If we are out of children on the level, go down the tree.
Run Code Online (Sandbox Code Playgroud)
这里模式添加和二进制字符串匹配的复杂度都是log(n)。如果有 a% 的 x'es,则时间大约会缩短 a%,这与@Qiang Jin 的解决方案相反。搜索多分支树比仅搜索三分支树更快。
我将该树实现为列表的层次结构。
归档时间: |
|
查看次数: |
1508 次 |
最近记录: |