AC自动机模板

每个节点只访问一次。

//子树节点是在插入时new的,
//寻找失配指针中使用的队列是直接调用STL的
const int kind = 26;
struct node
{
    node *fail;
    node *next[kind];
    int count;//记录当前前缀是完整单词出现的个数
    node()
    {
        fail = NULL;
        count = 0;
        memset(next,NULL,sizeof(next));
    }
};

void insert(char *str,node *root)
{
    node *p=root;
    int i=0,index;
    while(str[i])
    {
        index = str[i]-'a';
        if(p->next[index]==NULL)
            p->next[index]=new node();
        p=p->next[index];
        i++;
    }
    p->count++;

}


//寻找失败指针
void build_ac_automation(node *root)
{
    int i;
    queue<node *>Q;
    root->fail = NULL;
    Q.push(root);
    while(!Q.empty())
    {
        node *temp = Q.front();//q[head++];//取队首元素
        Q.pop();
        node *p = NULL;
        for(i=0; i<kind; i++)
        {
            if(temp->next[i]!=NULL)//寻找当前子树的失败指针
            {
                p = temp->fail;
                while(p!=NULL)
                {
                    if(p->next[i]!=NULL)//找到失败指针
                    {
                        temp->next[i]->fail = p->next[i];
                        break;
                    }
                    p = p->fail;
                }

                if(p==NULL)//无法获取,当前子树的失败指针为根
                    temp->next[i]->fail = root;

                Q.push(temp->next[i]);
            }
        }
    }
}


//询问str中包含n个关键字中多少种即匹配
int query(node *root)
{
    int i = 0,cnt = 0,index,len;
    len = strlen(str);
    node *p = root;
    while(str[i])
    {
        index = str[i]-'a';
        while(p->next[index]==NULL&&p!=root)//失配
            p=p->fail;
        p=p->next[index];
        if(p==NULL)//失配指针为根
            p = root;

        node *temp = p;
        while(temp!=root&&temp->count!=-1)//寻找到当前位置为止是否出现病毒关键字
        {
            //if(temp->count!=0)
            cnt+=temp->count;
            temp->count=-1;
            temp=temp->fail;
        }
        i++;
    }
    return cnt;
}

无限次访问的模板


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int kind = 256;

struct node
{
    node *fail;
    node *next[kind];
    int count;
    bool visit;
    node()
    {
        fail = NULL;
        count = 0;
        visit = false;
        memset(next,NULL,sizeof(next));
    }
};
node* query_temp_que[1111];
void insert(unsigned char *str,node *root,int slen)
{
    node *p=root;
    int i,index;
    for (i=0;i<slen;i++)
    {
        index = (int)str[i];
        if(p->next[index]==NULL) p->next[index]=new node();
        p=p->next[index];
    }
    p->count++;
}

//寻找失败指针
void build_ac_automation(node *root)
{
    int i;
    queue<node *>Q;
    root->fail = NULL;
    Q.push(root);
    while(!Q.empty())
    {
        node *temp=Q.front();//q[head++];//取队首元素
        Q.pop();
        node *p=NULL;
        for(i=0; i<kind; i++)
        {
            if(temp->next[i]!=NULL)//寻找当前子树的失败指针
            {
                p=temp->fail;
                while(p!=NULL)
                {
                    if(p->next[i]!=NULL)//找到失败指针
                    {
                        temp->next[i]->fail=p->next[i];
                        break;
                    }
                    p=p->fail;
                }
                if(p==NULL)//无法获取,当前子树的失败指针为根
                    temp->next[i]->fail=root;
                Q.push(temp->next[i]);
            }
        }
    }
}

//询问str中包含n个关键字中多少种即匹配
int query(unsigned char *str,node *root,int slen)
{
    int i,cnt = 0,index;
    int head,tail;
    head=tail=0;
    node *p = root;
    for (i=0;i<slen;i++)
    {
        index = (int)str[i];
        while(p->next[index]==NULL&&p!=root) p=p->fail;
        p=p->next[index];
        if(p==NULL) p = root;
        node *temp = p;
        while(temp!=root&&!temp->visit)//寻找到当前位置为止是否出现病毒关键字
        {
            cnt+=temp->count;
            temp->visit=true;
            query_temp_que[tail++]=temp;
            temp=temp->fail;
        }
    }
    while (head<tail)
    {
        node* cur=query_temp_que[head++];
        cur->visit=false;
    }
    return cnt;
}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值