3

现在我知道以前有关于这个算法的问题,但是老实说我还没有遇到过简单的 java 实现。许多人在他们的 GitHub 个人资料中复制并粘贴了相同的代码,这让我很恼火。

因此,出于面试练习的目的,我计划使用不同的方法来设置和实施算法。

该算法往往非常非常具有挑战性。老实说,我不知道该怎么做。只是逻辑没有意义。我几乎花了 4 天的时间直接绘制该方法,但无济于事。

因此,请用您的智慧启发我们。

我主要是根据这些信息做算法Intuition behind the Aho-Corasick string matching algorithm

如果可以在这里实现自己的解决方案,那将是一个很大的好处。

但这是我真正陷入困境的以下不完整且不起作用的解决方案:

如果您对代码感到不知所措,那么主要问题在于 Aho-Corasick 的主要算法。我们已经很好地创建了字典树。

但问题是,既然我们有了 trie 结构,我们如何真正开始实施。

这些资源都没有帮助。

public class DeterminingDNAHealth {
  private Trie tree;
  private String[] dictionary;
  private Node FailedNode;


  private DeterminingDNAHealth() {

  }

  private void buildMatchingMachine(String[] dictionary) {
    this.tree = new Trie();
    this.dictionary = dictionary;

    Arrays.stream(dictionary).forEach(tree::insert);

  }

  private void searchWords(String word, String[] dictionary) {

    buildMatchingMachine(dictionary);

    HashMap < Character, Node > children = tree.parent.getChildren();

    String matchedString = "";

    for (int i = 0; i < 3; i++) {
      char C = word.charAt(i);

      matchedString += C;

      matchedChar(C, matchedString);

    }

  }

  private void matchedChar(char C, String matchedString) {


    if (tree.parent.getChildren().containsKey(C) && dictionaryContains(matchedString)) {

      tree.parent = tree.parent.getChildren().get(C);

    } else {

      char suffix = matchedString.charAt(matchedString.length() - 2);

      if (!tree.parent.getParent().getChildren().containsKey(suffix)) {
        tree.parent = tree.parent.getParent();

      }


    }
  }

  private boolean dictionaryContains(String word) {

    return Arrays.asList(dictionary).contains(word);

  }


  public static void main(String[] args) {

    DeterminingDNAHealth DNA = new DeterminingDNAHealth();

    DNA.searchWords("abccab", new String[] {
      "a",
      "ab",
      "bc",
      "bca",
      "c",
      "caa"
    });


  }
}

我已经设置了一个工作正常的 trie 数据结构。所以这里没问题

特里.java

public class Trie {
  public Node parent;
  public Node fall;

  public Trie() {
    parent = new Node('⍜');
    parent.setParent(new Node());
  }

  public void insert(String word) {...}

  private boolean delete(String word) {...}

  public boolean search(String word) {...}

  public Node searchNode(String word) {...}

  public void printLevelOrderDFS(Node root) {...}

  public static void printLevel(Node node, int level) {...}

  public static int maxHeight(Node root) {...}

  public void printTrie() {...}

}

节点也是一样的。

节点.java

public class Node {

  private char character;
  private Node parent;
  private HashMap<Character, Node> children = new HashMap<Character, Node>();
  private boolean leaf;

  // default case
  public Node() {}

  // constructor accepting the character
  public Node(char character) {
    this.character = character;
  }

  public void setCharacter(char character) {...}

  public char getCharacter() {...}

  public void setParent(Node parent) {...}

  public Node getParent() {...}

  public HashMap<Character, Node> getChildren() {...}

  public void setChildren(HashMap<Character, Node> children) {...}

  public void resetChildren() {...}

  public boolean isLeaf() {...}

  public void setLeaf(boolean leaf) {...}
}

4

2 回答 2

8

我通常每隔一年教授一门高级数据结构课程,在探索字符串数据结构时我们会介绍 Aho-Corasick 自动机。这里有一些幻灯片展示了如何通过优化几个较慢的算法来开发算法。

一般来说,我会将实现分为四个步骤:

  1. 建立特里。Aho-Corasick 自动机的核心是一个带有一些额外箭头的 trie。算法的第一步是构建这个 trie,好消息是它就像一个普通的 trie 构建一样进行。实际上,我建议您通过假装您只是在尝试而不做任何事情来预测后面的步骤来实现这一步。

  2. 添加后缀(失败)链接。算法中的这一步添加了重要的失败链接,匹配器在遇到无法用于跟随 trie 边缘的字符时使用这些链接。我对这些工作的最佳解释是在链接的讲座幻灯片中。该算法的这一步被实现为在树上的广度优先搜索遍历。在编写此代码之前,我建议您手动完成一些示例,以确保您了解一般模式。一旦你这样做了,这并不是特别棘手的编码。但是,在不完全了解其工作原理的情况下尝试编写代码将使调试成为一场噩梦!

  3. 添加输出链接。在此步骤中,您可以添加用于报告在 trie 中给定节点处匹配的所有字符串的链接。它是通过对 trie 进行第二次广度优先搜索来实现的,而且我对它的工作原理的最佳解释是在幻灯片中。好消息是,这一步实际上比后缀链接构造更容易实现,因为您将更熟悉如何进行 BFS 以及如何上下树。同样,除非您可以轻松地手动完成,否则不要尝试对其进行编码!您不需要最小代码,但您不想在调试您不理解的高级行为的代码时陷入困境。

  4. 实现匹配器。这一步还不错!您只需沿着 trie 从输入中读取字符,在每一步输出所有匹配项,并在遇到卡住且无法向下前进时使用失败链接。

我希望这能给你一个更模块化的任务分解,以及整个过程应该如何工作的参考。祝你好运!

于 2017-10-25T17:32:29.533 回答
5

通过阅读一些源代码,您不会很好地理解Aho-Corasick 字符串匹配算法。而且你不会找到一个简单的实现,因为该算法是不平凡的。

原始论文Efficient String Matching: An Aid to Bibliographic Search写得很好,也很平易近人。我建议你下载那个PDF,仔细阅读,想一想,再读一遍。研究论文。

您可能还会发现阅读其他人对算法的描述很有用。有很多很多页面都有文字描述、图表、Powerpoint 幻灯片等。

在尝试实施之前,您可能希望至少花一两天时间研究这些资源。因为如果你在没有完全理解它是如何工作的情况下尝试实现它,你将会迷失方向,而你的实现会展示它。该算法并不完全简单,但它非常平易近人。

如果您只想要一些代码,这里有一个很好的实现:https ://codereview.stackexchange.com/questions/115624/aho-corasick-for-multiple-exact-string-matching-in-java 。

于 2017-10-25T15:21:44.890 回答