Skip to content

Commit dd2f7de

Browse files
committed
bugs fixed
1 parent 9117c6d commit dd2f7de

File tree

3 files changed

+4
-6
lines changed

3 files changed

+4
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ But differently, the new `end_idxes` property in the `STree` indicates the index
7474

7575
So, back to the topic, how does this tree solve the longest common substring problem?
7676

77-
In the `Application.py`, the `lcs2` function find the lcs of two strings. The method `build_with_automatic_end` in the `STree` will concatenate a list of strings with the ending characters *"$"*, and construct a suffix tree. The `lcs2` function will traverse the tree and find the deepest non-leaf node that contains the leaf nodes of all string indexes in the string list.
77+
In the `Application.py`, the `lcs2` function find the lcs of two strings. The method `build_with_automatic_end` in the `STree` will concatenate a list of strings with the ending characters *"$"*, and construct a suffix tree. With the help of `end_idxes`, these *"$"* are different and they represent the end of different strings. `STree` will automatically treat these *"$"* as different from other characters, so the character *"$"* itself is just designed for readability of suffix tree printing. The `lcs2` function will traverse the tree and find the deepest non-leaf node that contains the leaf nodes of all string indexes in the string list.
7878

7979
Similarly, it can also be demonstrated using the following codes in `Application.py`
8080

STree.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def build(self, string, end_idxes=None):
2929
active_state, left = self._update(active_state, left, idx)
3030
active_state, left = self._canonize(active_state, left, idx)
3131
if self.end_idxes:
32-
self.root.transition['end'] = State(self.end_idxes[-1], float("inf"))
32+
self.root.transition['end{}'.format(self.end_idxes[-1])] = State(self.end_idxes[-1], float("inf"))
3333

3434
def build_with_automatic_end(self, strings):
3535
string = '$'.join(strings) + '$'
@@ -41,9 +41,7 @@ def __repr__(self):
4141
def state_desc(state: State):
4242
if state.left is None: return "⊥"
4343
if state.left == -1: return self.string
44-
return (self.string[state.left:] if state.right == float('inf') else self.string[
45-
state.left:state.right + 1]) + (
46-
" (end)" if state.right == float("inf") else "")
44+
return (self.string[state.left:] if state.right == float('inf') else self.string[state.left:state.right + 1]) + (" (end)" if state.right == float("inf") else "")
4745

4846
def suffix_link_desc(state: State, prefix=""):
4947
return prefix + state_desc(state.suffix_link) if state.suffix_link else ""
@@ -62,7 +60,7 @@ def _update(self, active_state, left, idx):
6260
is_end_point, split_state = self._test_and_split(active_state, left, idx - 1, self.string[idx])
6361
while not is_end_point:
6462
new_state = State(idx, float('inf'))
65-
split_state.transition[self.string[idx] if idx not in self.end_idxes else 'end'] = new_state
63+
split_state.transition[self.string[idx] if idx not in self.end_idxes else 'end{}'.format(idx)] = new_state
6664
if old_active_point != self.root:
6765
old_active_point.suffix_link = split_state
6866
old_active_point = split_state

__pycache__/STree.cpython-39.pyc

25 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)