@y20070316
2017-03-25T01:40:56.000000Z
字数 2459
阅读 1273
学习笔记 SAM
本文是网上某博文[1]的学习笔记。
A suffix automaton A for a string s is a minimal finite automaton that recognizes the suffixes of s. This means that A is a directed acyclic graph with an initial node i, a set of terminal nodes, and the arrows between nodes are labeled by letters of s. To test whether w is a suffix of s it is enough to start from the initial node of i and follow the arrows corresponding to the letters of w. If we are able to do this for every letter of w and end up in a terminal node, w is a suffix of s.
对于一个字符串 ,我们对它建一个 SAM 。
SAM 是一个 DAG , SAM 的每一条边有一个 symbol 。
SAM 有一个起点 和若干个终端 。
对于 的每一个子串,我们可以从起点 开始,经过若干条边,所有边上的 symbol 串起来,得到该子串。

给定字符串 与 ,定义 表示出现在 中的 的结束位置的下标的集合。
对于两个串 ,若 ,则称 与 是 endpos-equivalent 的。
设 表示从 出发,走到 所得到的所有的子串的集合。对上图观察,发现:
所以,根据 的定义,将 的所有substrings进行分类。那么,所有endpos-equivalent的substrings放到一个节点。
我们考虑任意两个子串 与 ,它们的 的关系,从而确定点要如何分类。
不妨设 。
当 是 的suffix时, 。反之,当 时,则存在一个位置 ,所以 ,而 ,所以 是 的prefix。
当 不是 的suffix时,必有 。
综上,有
接下来考虑,对于同一个节点,它的两个Substrings 和 有什么性质。
为了研究性质,我们先研究特殊化的情况。我们设 为 中最长的, 为 中最短的。 , 。
由于 ,根据之前的结论,有 是 的suffix。同理,对于任意 ,有 为 的suffix。
我们还可以得到一个更好的结论。对于任意 的suffix, ,必有。 这是因为, ,由于 ,所以 ,根据每个节点的 相同的定义,得证。
这也就意味着,一个点存的必然是一段连续的suffix。
给定 State ,定义 ,其中 。
SAM 会把 的suffix集合 切分成若干个区间,每个区间可以视为一个 State ,通过State's Suffix Link连接。

给定一个 State 和 symbol ,定义 ,表示从 State 到 State 可以通过 symbol 。
如果存在 , State 与 State 有什么关系?
根据定义,直接有
反向思考。对于一个 State ,定义它的前驱的集合为 。则有:

由于 中的是连续的,所以构成它的也要是连续的。所以一定是连续的。所以通过 State's Suffix Link进行连接。上图就是最好的例子。
