介绍
trie,又称前缀树或字典树,是一种有序树,用来保存关联数组,其中的键通常是字符串。是一种高效的存储和查找字符串的数据结构。
Presume we have a string S
(length is m) and a pattern P
(length is n) given as follow:
1 | index: 1 2 3 4 5 6 7 8 9 10 11 12 |
Now we want to find out is there a sub-string in S
that matchs P
exactly, which in this case is S[6:12]
.
That is what KMP algorithm does.