【发布时间】:2018-04-05 21:17:23
【问题描述】:
现在我知道之前有关于这个算法的问题,但老实说我还没有遇到过简单的 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 数据结构。所以这里没问题
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() {...}
}
Node 也是如此。
Node.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) {...}
}
【问题讨论】:
-
"老实说,我还没有遇到过 简单 java 实现" --- "算法往往是 非常非常具有挑战性。”——也许这就是原因。
-
因此,我希望您能帮助实施一种更简单的方法,以便大多数人都能理解,对吧? @安德烈亚斯
-
“逻辑就是没有意义。” --- 如果你不懂算法,你就没有办法编写代码。
-
我确实理解它背后的理论。但是实现令人困惑@Andreas
-
抱歉,您给自己的挑战超出了您的能力范围,所以现在您想让 我们 为您编写代码吗?要编写一个复杂算法的简单实现,这可能是不可能的,否则有人可能已经做到了?这不是代码编写服务。在别处寻找。
标签: java algorithm data-structures trie aho-corasick