[CC150v4] 20.8 Full Text Search (suffix tree) - Shuatiblog.com
This is a very classic question of string search, favored by Google and Facebook.
The solution is suffix tree (to be distinguished from trie, or prefix tree, which searched word by its prefix). Suffix tree is good for search a proportion of a long string. For example, using “bibs” to build a suffix tree like this:
X. Using Suffix Tree
http://www.shuatiblog.com/blog/2014/09/10/full-text-search/
https://github.com/yxjiang/algorithms/blob/master/src/main/java/algorithm/cc150/chapter18/Question8.java
后缀Trie的查找效率很优秀,如果你要查找一个长度为n的字符串,只需要O(n)的时间, 比较次数就是字符串的长度,相当给力。 但是,构造字符串S的后缀Trie却需要O(m2 )的时间, (m为S的长度),及O(m2 )的空间。
Ukkonen算法, 该算法可以在线性时间和空间下构造后缀树,而且它还是在线算法! 不过它实现起来就没那么简单了,尤其是3个加速部分,分分钟把人弄晕。 上面说到的生物序列算法课件对此也有不错的讲解,同时推荐Gusfield的书: Algorithms on strings, trees, and sequences。
一篇介绍后缀树的好文章:(附代码) Fast String Searching With Suffix Trees
三鲜对上文的翻译(原文找不到了,这是别人转三鲜的): 后缀树的构造方法-Ukkonen详解
下面一篇来自stackoverflow: Ukkonen’s suffix tree algorithm in plain English
上文的译文: 介绍后缀树
X. AC自动机
AC自动机算法是解决字符串多模式匹配的一个经典方法,时间复杂度为:O(m+kn+z), 其中:m是目标串S的长度,k是模式串个数,n是模式串平均长度,z是S 中出现的模式串数量。
Read full article from [CC150v4] 20.8 Full Text Search (suffix tree) - Shuatiblog.com
SolutionGiven a string s and an array of smaller strings T, design a method to search s for each small string in T.
This is a very classic question of string search, favored by Google and Facebook.
The solution is suffix tree (to be distinguished from trie, or prefix tree, which searched word by its prefix). Suffix tree is good for search a proportion of a long string. For example, using “bibs” to build a suffix tree like this:
X. Using Suffix Tree
http://www.shuatiblog.com/blog/2014/09/10/full-text-search/
https://github.com/yxjiang/algorithms/blob/master/src/main/java/algorithm/cc150/chapter18/Question8.java
public int[] searchAll(String s, String[] T) { | |
// write implementation here | |
SuffixTree tree = new SuffixTree(s); | |
List<Integer> list = new ArrayList<Integer>(); | |
for (String t : T) { | |
list.add(tree.search(t)); | |
} | |
int[] results = new int[list.size()]; | |
for (int i = 0; i < list.size(); ++i) { | |
results[i] = list.get(i); | |
} | |
return results; | |
} | |
public static class SuffixNode { | |
public char ch; | |
public Map<Character, SuffixNode> children; | |
public int index; // denotes the end index if a word terminated as this node | |
public SuffixNode(char ch) { | |
this.ch = ch; | |
this.index = -1; | |
this.children = new HashMap<Character, SuffixNode>(); | |
} | |
} | |
public static class SuffixTree { | |
public SuffixNode root; | |
public SuffixTree(String str) { | |
this.root = new SuffixNode('#'); | |
for (int i = 0; i < str.length(); ++i) { | |
insert(root, str.substring(i), i); | |
} | |
} | |
public void insert(SuffixNode curNode, String suffix, int index) { | |
if (suffix.length() == 0) { | |
return; | |
} | |
char ch = suffix.charAt(0); | |
SuffixNode node = curNode.children.get(ch); | |
if (node == null) { | |
node = new SuffixNode(ch); | |
// each node record the end index | |
node.index = index; | |
curNode.children.put(ch, node); | |
} | |
insert(node, suffix.substring(1), index + 1); | |
} | |
public int search(String s) { | |
return search(s, s, root); | |
} | |
public int search(String original, String s, SuffixNode curNode) { | |
if (s.length() == 0) { | |
return curNode.index + 1 - original.length(); | |
} | |
char ch = s.charAt(0); | |
SuffixNode node = curNode.children.get(ch); | |
if (node == null) { | |
return -1; | |
} | |
else { | |
return search(original, s.substring(1), node); | |
} | |
} | |
} |
Ukkonen算法, 该算法可以在线性时间和空间下构造后缀树,而且它还是在线算法! 不过它实现起来就没那么简单了,尤其是3个加速部分,分分钟把人弄晕。 上面说到的生物序列算法课件对此也有不错的讲解,同时推荐Gusfield的书: Algorithms on strings, trees, and sequences。
一篇介绍后缀树的好文章:(附代码) Fast String Searching With Suffix Trees
三鲜对上文的翻译(原文找不到了,这是别人转三鲜的): 后缀树的构造方法-Ukkonen详解
下面一篇来自stackoverflow: Ukkonen’s suffix tree algorithm in plain English
上文的译文: 介绍后缀树
X. AC自动机
AC自动机算法是解决字符串多模式匹配的一个经典方法,时间复杂度为:O(m+kn+z), 其中:m是目标串S的长度,k是模式串个数,n是模式串平均长度,z是S 中出现的模式串数量。
AC自动机也会先构造一棵Trie树,不同的是,它用模式串来构造Trie树。 然后遍历一次目标串S,即可求出哪些模式串出现在目标串S中。
Read full article from [CC150v4] 20.8 Full Text Search (suffix tree) - Shuatiblog.com