双数组 Trie 问题
我试图从 http://linux.thai.net 理解双数组 Trie 实现/~thep/datrie/datrie.html 但我不明白以下部分。
check[base[s] + c] = s
base[s] + c = t
这里添加c
意味着什么。
谁能用简单的语言解释算法。
I am trying to understand Double-Array Trie implementation from http://linux.thai.net/~thep/datrie/datrie.html
But I do not understand following part.
check[base[s] + c] = s
base[s] + c = t
What does adding c
means here.
Can anybody explain algorithm in simple language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
想象一下您将 2D 数组折叠为 1D 数组:
我希望我没有让这变得太复杂(即使我认为我做了......)。 :)
Imagine you collaps a 2D array into a 1D array:
I hope I didn't make this too complicated (even though I think I did...). :)
字符是“从零开始的”,即“a”是0,“b”是1,“c”是2,等等。查找“a”意味着将 0 添加到当前节点的基地址并检查该索引处的所有者。如果该所有者是当前节点,则 trie 中的当前节点存在一个以“a”开头的字符串,并且该索引处的基地址是下一次查找的基地址。
Characters are "zero based", i.e., 'a' is 0, 'b' is 1, 'c' is 2, et cetera. Looking up "a" would mean to add that 0 to the base address of the current node and check the owner at that index. If that owner is the current node, there is a string starting with "a" from the current node in the trie, and the base address at that index is the base address for the next lookup.
您应该将代码编辑为以下内容:
在状态
s
接受字符c
并转换为状态t
的情况下:s -c-> t
。关键点是
base[s]
的含义。由于一个状态可以接受多个字符:
| |c1|c2|c3|
|--|--|--|--|
| s| x| y| z|
问题在于如何表达
s-c1->x
、s-c2->y
、s-c3->z< /代码>。传统上,
map
是一种很好的做法:但是如果您使用数组来表达映射呢?
base[s]
是next
数组中的基索引,从中添加偏移量c
。base[s] + c
是next
中的最终索引,用于存储从状态s
和字符c
映射的下一个状态代码>.请注意,字符c
实际上是编程语言中的整数。You should edit your code to the following:
In the scenario that state
s
accepts a characterc
and transformed into statet
:s - c -> t
.The key point is what
base[s]
means.As a state may accept multiple characters:
| |c1|c2|c3|
|--|--|--|--|
| s| x| y| z|
The problem is how you express these mappings of
s-c1->x
,s-c2->y
,s-c3->z
. Traditionally, amap
is a good practice:But what if you use an array to express the mappings?
base[s]
is a base index innext
array, from which an offsetc
is added.base[s] + c
is the final index innext
to store the next state mapped from states
and characterc
. Note that characterc
is actually an integer in programming language.