双数组 Trie 问题

发布于 2024-11-07 04:40:33 字数 282 浏览 0 评论 0原文

我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

默嘫て 2024-11-14 04:40:33

想象一下您将 2D 数组折叠为 1D 数组:

int arr2D[2][3];
int arr1D[2 * 3]; // # of rows * # of columns

// access element [1][2] of 2D array, i.e., the last element
int elem2D = arr2D[1][2];
// access element [1][2] of 1D array, i.e., the last element
int elem1D = arr1D[1 * 3 + 2];
// =========================================================
lets visualize the array access:
arr2D => x/y 0  1  2
          0 [N][N][N]
+1 on x > 1 [N][N][N]
+2 on y ---------- ^
y_len  =>   ^-------^ 3 elements
so the access happens with x * y_len + y
                           1 *   3   + 2
now for the 1D array
we want the second row, so we go with 1 * 3
(0-based access, y_len = 3)
and then we want the 3rd element, so + 2
(again, 0-based access)
arr1D =>  x  0  1  2 
            [N][N][N]
             3  4  5
1 * 3 = 3 > [N][N][N]
      + 2 = 5 ---- ^

我希望我没有让这变得太复杂(即使我认为我做了......)。 :)

Imagine you collaps a 2D array into a 1D array:

int arr2D[2][3];
int arr1D[2 * 3]; // # of rows * # of columns

// access element [1][2] of 2D array, i.e., the last element
int elem2D = arr2D[1][2];
// access element [1][2] of 1D array, i.e., the last element
int elem1D = arr1D[1 * 3 + 2];
// =========================================================
lets visualize the array access:
arr2D => x/y 0  1  2
          0 [N][N][N]
+1 on x > 1 [N][N][N]
+2 on y ---------- ^
y_len  =>   ^-------^ 3 elements
so the access happens with x * y_len + y
                           1 *   3   + 2
now for the 1D array
we want the second row, so we go with 1 * 3
(0-based access, y_len = 3)
and then we want the 3rd element, so + 2
(again, 0-based access)
arr1D =>  x  0  1  2 
            [N][N][N]
             3  4  5
1 * 3 = 3 > [N][N][N]
      + 2 = 5 ---- ^

I hope I didn't make this too complicated (even though I think I did...). :)

潜移默化 2024-11-14 04:40:33

字符是“从零开始的”,即“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.

潜移默化 2024-11-14 04:40:33

您应该将代码编辑为以下内容:

check[base[s] + c] = s
next[base[s] + c] = t 

在状态 s 接受字符 c 并转换为状态 t 的情况下:s -c-> t
关键点是base[s]的含义。
由于一个状态可以接受多个字符:

| |c1|c2|c3|
|--|--|--|--|
| s| x| y| z|
问题在于如何表达 s-c1->xs-c2->ys-c3->z< /代码>。传统上,map 是一种很好的做法:

class State {
    Map<Character, State> mappings;
}

但是如果您使用数组来表达映射呢?
base[s]next 数组中的基索引,从中添加偏移量 c
base[s] + cnext 中的最终索引,用于存储从状态 s 和字符 c 映射的下一个状态代码>.请注意,字符c实际上是编程语言中的整数。

You should edit your code to the following:

check[base[s] + c] = s
next[base[s] + c] = t 

In the scenario that state s accepts a character c and transformed into state t: 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, a map is a good practice:

class State {
    Map<Character, State> mappings;
}

But what if you use an array to express the mappings?
base[s] is a base index in next array, from which an offset c is added.
base[s] + c is the final index in next to store the next state mapped from state s and character c. Note that character c is actually an integer in programming language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文