概念

AC 自动机(Aho-Corasick)算法。其实,Trie 树跟 AC 自动机之间的关系,就像单串匹配中朴素的串匹配算法,跟 KMP 算法之间的关系一样,只不过Trie 树跟 AC 自动机针对的是多模式串而已。所以,AC 自动机实际上就是在 Trie 树之上,加了类似 KMP 的 next 数组,只不过此处的 next 数组是构建在树上罢了
所以,AC 自动机的构建,包含两个操作:

  • 将多个模式串构建成 Trie 树;
  • 在 Trie 树上构建失败指针(相当于 KMP 中的失效函数 next 数组)。

假设沿 Trie 树走到 p 节点,即下图中的紫色节点,那 p 的失败指针就是从 root 走到紫色节点形成的字符串 abc,跟所有模式串前缀匹配的最长可匹配后缀子串,就是箭头指的 bc 模式串。将 p 节点的失败指针指向那个最长匹配后缀子串对应的模式串的前缀的最后一个节点,就是下图中箭头指向的节点。
image.png
计算每个节点的失败指针这个过程看起来有些复杂。其实把树中相同深度的节点放到同一层,那么某个节点的失败指针只出现在它所在层的上一层。像 KMP 算法那样,当要求某个节点的失败指针的时候,通过已经求得的、深度更小的那些节点的失败指针来推导。即可以逐层依次来求解每个节点的失败指针。所以,失败指针的构建过程,是一个按层遍历树的过程。
首先 root 的失败指针为 NULL,也就是指向自己。当已经求得某个节点 p 的失败指针之后,如何寻找它的子节点的失败指针呢?
假设节点 p 的失败指针指向节点 q,看节点 p 的子节点 pc 对应的字符,是否也可以在节点 q 的子节点中找到。如果找到了则将节点 pc 的失败指针指向节点 qc。
image.png
如果没找到,则令 q=q -> fail(fail 表示失败指针),继续上面的查找,直到 q 是 root 为止,如果还没有找到,就让节点 pc 的失败指针指向 root。
image.png

代码实现

  1. class TrieNode {
  2. constructor(value) {
  3. this.value = value //value为单个字符
  4. this.num = 1
  5. this.deep = 0//根节点默认0
  6. this.son = []
  7. this.isEnd = false
  8. }
  9. findNode(value) {
  10. for (let i = 0; i < this.son.length; i++) {
  11. const node = this.son[i]
  12. if (node.value == value) {
  13. return node
  14. }
  15. }
  16. return null
  17. }
  18. }
  19. class Trie {
  20. constructor() {
  21. this.root = new TrieNode(null)
  22. this.size = 1//一开始的时候只有根节点这一个节点
  23. }
  24. insert(str) {
  25. let node = this.root
  26. for (let c of str) {
  27. let snode = node.findNode(c)
  28. if (snode == null) {
  29. snode = new TrieNode(c)
  30. snode.deep = node.deep + 1
  31. node.son.push(snode)
  32. } else {
  33. snode.num++//有N个字符串经过它
  34. }
  35. node = snode
  36. }
  37. //如果当前的node已经是一个word,则不需要添加
  38. if (!node.isEnd) {
  39. this.size++
  40. node.isEnd = true
  41. }
  42. }
  43. has(str) {
  44. let node = this.root
  45. for (let c of str) {
  46. const snode = node.findNode(c)
  47. if (snode) {
  48. node = snode
  49. } else {
  50. return false
  51. }
  52. }
  53. return node.isEnd
  54. }
  55. }
  56. //构建字典树失败指针
  57. function build_ac_automation(root) {
  58. root.fail = null
  59. const queue = [root]
  60. let i = 0
  61. while (i < queue.length) {
  62. const temp = queue[i]
  63. for (let j = 0; j < temp.son.length; j++) {
  64. const node = temp.son[j]
  65. if (temp === root) {
  66. node.fail = root
  67. } else {
  68. node.fail = temp.fail.findNode(node.value) || root
  69. }
  70. queue.push(node)
  71. }
  72. i++
  73. }
  74. }
  75. // AC算法多字符查询
  76. function acSearch(arr, str) {
  77. //生成字典树
  78. const tr = new Trie()
  79. arr.forEach(function (item) {
  80. tr.insert(item)
  81. })
  82. //构造字典树的失败指针
  83. build_ac_automation(tr.root)
  84. let node = tr.root
  85. const data = []
  86. for (let i = 0; i < str.length; i++) {
  87. let cnode = node.findNode(str[i])
  88. //匹配不到字符,进入失败匹配,
  89. while (!cnode && node !== tr.root) {
  90. node = node.fail
  91. cnode = node.findNode(str[i])
  92. }
  93. if (cnode) {
  94. node = cnode
  95. }
  96. if (node.isEnd) {
  97. data.push({
  98. start: i + 1 - node.deep,
  99. len: node.deep,
  100. str: str.substr(i + 1 - node.deep, node.deep),
  101. num: node.num,
  102. })
  103. }
  104. }
  105. return data
  106. }
  107. // 测试
  108. const result = acSearch(['she', 'shr', 'her', 'her'], 'sher')
  109. console.log(result)
  110. /**
  111. * [ { start: 0, len: 3, str: 'she', num: 1 },
  112. { start: 1, len: 3, str: 'her', num: 2 } ]
  113. */

场景

实现一个高性能的敏感词过滤系统:就要用到多模式串匹配算法
单模式与多模式区别:尽管单模式串匹配算法也能完成多模式串的匹配工作。例如通过单模式串匹配算法(比如 KMP 算法)与用户输入的文字内容进行匹配。但是每个匹配过程都需要扫描一遍用户输入的内容。整个过程下来就要扫描很多遍用户输入的内容。如果敏感词很多,比如几千个,并且用户输入的内容很长,假如有上千个字符,那就需要扫描几千遍这样的输入内容,这种处理思路比较低效。多模式匹配算法只需要扫描一遍主串,就能在主串中一次性查找多个模式串是否存在,从而大大提高匹配效率。
具体做法:可以对敏感词字典进行预处理,构建成 Trie 树结构。这个预处理的操作只需要做一次,如果敏感词字典动态更新了,比如删除、添加了一个敏感词,只需要动态更新一下 Trie 树就可以。当用户输入一个文本内容后,把用户输入的内容作为主串,从第一个字符开始,在 Trie 树中匹配。当匹配到 Trie 树的叶子节点,或者中途遇到不匹配字符的时候,将主串的开始匹配位置后移一位,重新在 Trie 树中匹配。利用AC 自动机算法对多模式串 Trie 树进行改进,进一步提高 Trie 树的效率。