HDU2222 Keywords Search(AC自动机)

本文介绍了一个基于AC自动机的应用案例,通过实现多模式字符串匹配算法解决图像检索系统中的关键词匹配问题。具体包括AC自动机的数据结构设计、插入关键字、构建失败指针以及查询等关键步骤。

Problem Description

In the modern time, Search engine came into the life of everybody like
Google, Baidu, etc. Wiskey also wants to bring this feature to his
image retrieval system. Every image have a long description, when
users type some keywords to find the image, the system will match the
keywords with description of image and show the image which the most
keywords be matched. To simplify the problem, giving you a description
of image, and some keywords, you should tell me how many keywords will
be match.

Input

First line will contain one integer means how many cases will follow
by. Each case will contain two integers N means the number of keywords
and N keywords follow. (N <= 10000) Each keyword will only contains
characters ‘a’-‘z’, and the length will be not longer than 50. The
last line is the description, and the length will be not longer than
1000000.

Output

Print how many keywords are contained in the description.

Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3

思路

给你给了n个字符串,然后接下来有一个串,问在这个串中,那n个串出现过几次

AC自动机:Aho-Corasick
automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一。一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过

也就是AC自动机的基本题型,关于AC自动机的介绍:
AC自动机

kuangbinAC自动机小结
采用kuangbin的模板,在建立fail指针的时候进行一个优化,把空节点变成根节点节省时间

第一次用我原本写字典树的风格超时了,也不知道为什么,可以当做模板,存一下
两份代码的思想是一样的,就是写的格式不一样

代码1(kuangbin风格)

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+20;
struct dicTree
{
    int next[N][26],fail[N],sum[N];
    int root,sz;
    int newnode()
    {
        for(int i=0; i<26; i++)
            next[sz][i]=-1;
        sum[sz++]=0;
        return sz-1;
    }
    void init()
    {
        sz=0;
        root=newnode();
    }
    void insert(char *s)
    {
        int len=strlen(s);
        int now=root;
        for(int i=0; i<len; i++)
        {
            int to=s[i]-'a';
            if(next[now][to]==-1)
                next[now][to]=newnode();
            now=next[now][to];
        }
        sum[now]++;
    }
    void build()
    {
        queue<int>q;
        fail[root]=root;
        for(int i=0; i<26; i++)
        {
            if(next[root][i]==-1)
                next[root][i]=root;
            else
            {
                fail[next[root][i]]=root;
                q.push(next[root][i]);
            }
        }
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            for(int i=0; i<26; i++)
            {
                if(next[now][i]==-1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    q.push(next[now][i]);
                }
            }
        }
    }
    int query(char *s)
    {
        int len=strlen(s);
        int now=root;
        int res=0;
        for(int i=0; i<len; i++)
        {
            int to=s[i]-'a';
            now=next[now][to];
            int temp=now;
            while(temp!=root)
            {
                res+=sum[temp];
                sum[temp]=0;
                temp=fail[temp];
            }
        }
        return res;
    }

} ac;
char s[1000000+20];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        ac.init();
        while(n--)
        {
            scanf("%s",s);
            ac.insert(s);
        }
        ac.build();
        scanf("%s",s);
        printf("%d\n",ac.query(s));
    }
}

代码2(我的风格。。TLE)

#include <bits/stdc++.h>
using namespace std;
const int N=5e5+20;
struct dicTree
{
    struct node
    {
        int next[26];
        int sum;
        int fail;
    } T[N];
    int root,sz;
    int newnode()
    {
        for(int i=0; i<26; i++)
            T[sz].next[i]=-1;
        T[sz].sum=0;
        T[sz++].fail=0;
        return sz-1;
    }
    void init()
    {
        sz=0;
        root=newnode();
    }
    void insert(char *s)
    {
        int len=strlen(s);
        int now=root;
        for(int i=0; i<len; i++)
        {
            int to=s[i]-'a';
            if(T[now].next[to]==-1)
                T[now].next[to]=newnode();
            now=T[now].next[to];
        }
        T[now].sum++;
    }
    void build()
    {
        queue<int>q;
        T[root].fail=root;
        for(int i=0; i<26; i++)
        {
            if(T[root].next[i]==-1)
                T[root].next[i]=root;
            else
            {
                T[T[root].next[i]].fail=root;
                q.push(T[root].next[i]);
            }
        }
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            for(int i=0; i<26; i++)
            {
                if(T[now].next[i]==-1)
                    T[now].next[i]=T[T[now].fail].next[i];
                else
                {
                    T[T[now].next[i]].fail=T[T[now].fail].next[i];
                    q.push(T[now].next[i]);
                }
            }
        }
        return;
    }
    int query(char *s)
    {
        int len=strlen(s);
        int now=root,temp;
        int res=0;
        for(int i=0; i<len; i++)
        {
            int to=s[i]-'a';
            now=T[now].next[to];
            temp=now;
            while(temp!=root)
            {
                res+=T[temp].sum;
                T[temp].sum=0;
                temp=T[temp].fail;
            }
        }
        return res;
    }
} ac;
char s[1000000+20];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        ac.init();
        while(n--)
        {
            scanf("%s",s);
            ac.insert(s);
        }
        ac.build();
        scanf("%s",s);
        printf("%d\n",ac.query(s));
    }
}
分布式微服务企业级系统是一个基于Spring、SpringMVC、MyBatis和Dubbo等技术的分布式敏捷开发系统架构。该系统采用微服务架构和模块化设计,提供整套公共微服务模块,包括集中权限管理(支持单点登录)、内容管理、支付中心、用户管理(支持第三方登录)、微信平台、存储系统、配置中心、日志分析、任务和通知等功能。系统支持服务治理、监控和追踪,确保高可用性和可扩展性,适用于中小型企业的J2EE企业级开发解决方案。 该系统使用Java作为主要编程语言,结合Spring框架实现依赖注入和事务管理,SpringMVC处理Web请求,MyBatis进行数据持久化操作,Dubbo实现分布式服务调用。架构模式包括微服务架构、分布式系统架构和模块化架构,设计模式应用了单例模式、工厂模式和观察者模式,以提高代码复用性和系统稳定性。 应用场景广泛,可用于企业信息化管理、电子商务平台、社交应用开发等领域,帮助开发者快速构建高效、安全的分布式系统。本资源包含完整的源码和详细论文,适合计算机科学或软件工程专业的毕业设计参考,提供实践案例和技术文档,助力学生和开发者深入理解微服务架构和分布式系统实现。 【版权说明】源码来源于网络,遵循原项目开源协议。付费内容为本人原创论文,包含技术分析和实现思路。仅供学习交流使用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值