lpzhang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 测试程序与结果

posted on 2015-10-16 23:38  lpzhang  阅读(280)  评论(0编辑  收藏  举报