AC自动机实现

程序功能
求文本串中出现了多少模式串

[链接](http://acm.hdu.edu.cn/showproblem.php?pid=2222)
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxkind = 26;
char text[1000005],pattern[55];

struct Node
{
    Node *fail;
    Node *next[maxkind];
    int cnt;
    Node() { fail = NULL; memset(next,0,sizeof(next)); cnt = 0; }
};

struct myACMachine
{
    Node *root;
    myACMachine()
    {
        root = new Node();
    }
    ~myACMachine()
    {
        destroy(root);
        delete root;
    }
    void destroy(Node *rt)
    {
        if(!rt) return;
        for(int i=0; i<26; i++)
            destroy(rt->next[i]);
    }
    void Insert(char *str)
    {
        int len = strlen(str);
        Node *p = root;
        for(int i=0; i<len; i++)
        {
            int index = str[i] - 'a';
            if(p->next[index] == NULL) p->next[index] = new Node();
            p = p->next[index];
        }
        p->cnt++;
    }
    void getACMachine()
    {
        queue<Node*>Q;
        Q.push(root);
        while(!Q.empty())
        {
            Node *father = Q.front(); Q.pop();
            for(int i = 0; i < 26; i++)
            {
                Node *son = father->next[i];
                if(son != NULL)
                {
                    Node *temp = father->fail;
                    while(temp != NULL && temp->next[i] == NULL)
                        temp = temp->fail;
                    if(temp == NULL)
                        son->fail = root;
                    else
                        son->fail = temp->next[i];
                    Q.push(son);
                }
            }
        }
    }
    int query(char *text)
    {
        int len = strlen(text),ans = 0;
        Node *p = root;
        for(int i=0; i<len; i++)
        {
            int index = text[i] - 'a';
            while(p != NULL && p->next[index] == NULL)
                p = p->fail;
            if(p == NULL) p = root;
            else
            {
                p = p->next[index];
                Node *temp = p;
                while(temp!=root && temp->cnt)
                {
                    ans += temp->cnt;
                    temp->cnt = 0;
                    temp = temp->fail;
                }
            }
        }
        return ans;
    }
};


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        myACMachine ac;
        int n;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",pattern);
            ac.Insert(pattern);
        }
        ac.getACMachine();
        scanf("%s",text);
        printf("%d\n",ac.query(text));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是AC自动机在Java中的实现代码: ```java import java.util.LinkedList; import java.util.Queue; class ACNode { public char data; public ACNode[] children = new ACNode[26]; public boolean isEndingChar = false; public int length = -1; public ACNode fail; public ACNode(char data) { this.data = data; } } class ACAutomaton { private ACNode root; public ACAutomaton() { root = new ACNode('/'); } public void insert(String text) { ACNode p = root; for (int i = 0; i < text.length(); i++) { int index = text.charAt(i) - 'a'; if (p.children[index] == null) { ACNode newNode = new ACNode(text.charAt(i)); p.children[index] = newNode; } p = p.children[index]; } p.isEndingChar = true; p.length = text.length(); } public void buildFailurePointer() { Queue<ACNode> queue = new LinkedList<>(); root.fail = null; queue.add(root); while (!queue.isEmpty()) { ACNode p = queue.remove(); for (int i = 0; i < 26; i++) { ACNode pc = p.children[i]; if (pc == null) continue; if (p == root) { pc.fail = root; } else { ACNode q = p.fail; while (q != null) { ACNode qc = q.children[pc.data - 'a']; if (qc != null) { pc.fail = qc; break; } q = q.fail; } if (q == null) { pc.fail = root; } } queue.add(pc); } } } public int match(String text) { ACNode p = root; int n = text.length(); for (int i = 0; i < n; i++) { int index = text.charAt(i) - 'a'; while (p.children[index] == null && p != root) { p = p.fail; } p = p.children[index]; if (p == null) p = root; ACNode tmp = p; while (tmp != root) { if (tmp.isEndingChar) { return i - tmp.length + 1; } tmp = tmp.fail; } } return -1; } } ``` 使用方法: ```java ACAutomaton ac = new ACAutomaton(); ac.insert("he"); ac.insert("she"); ac.insert("his"); ac.insert("hers"); ac.buildFailurePointer(); System.out.println(ac.match("ushers")); ``` 输出:2 这个例子中,AC自动机匹配到了字符串"she"的结尾,所以返回2。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值