标签:
java实现的AC自动机算法
1 构建TrieTree
package com.grayzlp.ac; import java.util.HashSet; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; public class TrieTree{ private Node root; public void addWord(String ch, int id){ addWord(root, ch, id); } //构建失败指针 public void buildFailNodeBFS(){ Queue<Node> queue=new ArrayBlockingQueue<Node>(100); for(int i=0;i<26;i++){ if(root.childNodes[i]!=null){ root.childNodes[i].failNode=root; queue.add(root.childNodes[i]); } } while(queue.isEmpty()==false){ Node cur=queue.poll(); Node failNode=null; for(int i=0;i<26;i++){ if(cur.childNodes[i]==null){ continue; } failNode=cur.failNode; while(failNode!=null){ int site=cur.childNodes[i].nodeChar-‘a‘; if(failNode.childNodes[site]!=null){ cur.childNodes[i].failNode=failNode.childNodes[site]; break; } failNode=failNode.failNode; } if(failNode==null){ cur.childNodes[i].failNode=root; } queue.add(cur.childNodes[i]); } } } private void addWord(Node node, String str, int id) { if(str.length()==0){ return; } char ch=str.charAt(0); int site=ch-‘a‘; if(node.childNodes[site]==null){ Node cur=new Node(); cur.nodeChar=ch; node.childNodes[site]=cur; } String next=str.substring(1); if(next.length()==0){ node.childNodes[site].freq++; node.childNodes[site].numList.add(id); } addWord(node.childNodes[site], next, id); } public TrieTree() { root=new Node(); } public HashSet<Integer> searchAC(String str){ HashSet<Integer> set=new HashSet<Integer>(); return searchAC(root,str,set); } private HashSet<Integer> searchAC(Node node, String str, HashSet<Integer> set) { int length=str.length(); Node head=node; for(int i=0;i<length;i++){ char ch=str.charAt(i); int index=ch-‘a‘; while(head.childNodes[index]==null&&head!=root){ head=head.failNode; } head=head.childNodes[index]; if(head==null){ head=root; } Node temp=head; while(temp!=root){ for (Integer num : temp.numList) { set.add(num); } temp=temp.failNode; } } return set; } class Node{ public Node[] childNodes; public int freq; public char nodeChar; public Node failNode; public HashSet<Integer> numList; public Node() { childNodes=new Node[26]; freq=0; numList=new HashSet<Integer>(); } } }
2 测试程序与结果
标签:
原文地址:http://www.cnblogs.com/lpzhang/p/4886692.html