Keywords Search(hdu-2222,ac自动机模板题)

本文详细介绍了一种基于AC自动机的关键词搜索算法,用于快速匹配大量文本中的关键词。通过构建AC自动机,该算法能高效地处理多个关键词在长文本中的搜索任务,特别适用于搜索引擎和图像检索系统中的关键词匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 79890    Accepted Submission(s): 27827

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

ac:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#define MAXN 1000005
using namespace std;

const int N=10100;
char str[MAXN];
int num,n;
struct node
{
    int son[30];
    int fail,cnt;
}ac[N*60];

queue<int> que;

void init(int x)
{
    ac[x].cnt=0;
    ac[x].fail=0;
    memset(ac[x].son,0,sizeof(ac[x].son));
}

void trie(char ctr[])
{
    int len=strlen(ctr);
    int x=0;
    for(int i=0;i<len;i++)
    {
        int t=ctr[i]-'a'+1;
        if(!ac[x].son[t])
        {
            num++;
            init(num);
            ac[x].son[t]=num;
        }
        x=ac[x].son[t];
    }
    ac[x].cnt++;
}

void buildac()
{
    while(!que.empty())
        que.pop();
    for(int i=1;i<=26;i++)
        if(ac[0].son[i])
            que.push(ac[0].son[i]);
    while(!que.empty())
    {
        int x=que.front();
        que.pop();
        int fail=ac[x].fail;
        for(int i=1;i<=26;i++)
        {
            int y=ac[x].son[i];
            if(y)
            {
                ac[y].fail=ac[fail].son[i];
                que.push(y);
            }
            else ac[x].son[i]=ac[fail].son[i];
        }
    }
}

int find1(char ctr[])
{
    int len=strlen(ctr);
    int x=0,ans=0;
    for(int i=0;i<len;i++)
    {
        int t=ctr[i]-'a'+1;
        while(x&&!ac[x].son[t])
            x=ac[x].fail;
        x=ac[x].son[t];
        int p=x;
        while(p&&ac[p].cnt!=-1)
        {
            ans+=ac[p].cnt;
            ac[p].cnt=-1;
            p=ac[p].fail;
        }
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        num=0;
        init(0);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",&str);
            trie(str);
        }
        buildac();
        scanf("%s",str);
        printf("%d\n",find1(str));
    }
}

下面是kuangbin大佬的模板:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=26;
const int MAXN=500005;
struct Trie{
    int next[MAXN][N],fail[MAXN],end[MAXN];
    int root;
    int tot;
    int newnode()
    {
        for(int i=0;i<N;i++)
                next[tot][i]=-1;
        end[tot++]=0;
        return tot-1;
    }
    void init()
    {
        tot=0;
        root=newnode();
    }
    
    void insert(char buf[])
    {
        int len=strlen(buf);
        int now=root;
        for(int i=0;i<len;i++)
        {
            int k=buf[i]-'a';
            if(next[now][k]==-1)    
                next[now][k]=newnode();
            now=next[now][k];
        }
        end[now]++;
    }
    
    void build()
    {
        queue<int> que;
        fail[root]=root;
        for(int i=0;i<N;i++)
            if(next[root][i]==-1)
                next[root][i]=root;
            else
            {
                fail[next[root][i]]=root;
                que.push(next[root][i]);
            }
        
        while(!que.empty())
        {
            int now = que.front();
            que.pop();
            for(int i=0;i<N;i++)
                if(next[now][i]==-1)
                    next[now][i]=next[fail[now]][i];
                else
                {
                    fail[next[now][i]]=next[fail[now]][i];
                    que.push(next[now][i]);
                }
        }
    }
    
    int query(char buf[])
    {
        int len=strlen(buf);
        int now=root;
        int res=0;
        for(int i=0;i<len;i++)
        {
            now=next[now][buf[i]-'a'];
            int temp=now;
            while(temp!=root)
            {
                res+=end[temp];
                end[temp]=0;//模式串只在主串中匹配一次就可以了
                temp=fail[temp];
            }
        }
        return res;
    }
    
};
Trie ac;
char buf[MAXN+MAXN];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ac.init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",buf);
            ac.insert(buf);
        }
        ac.build();
        scanf("%s",buf);
        printf("%d\n",ac.query(buf));
    }
    
    return 0;
}

 

转载于:https://www.cnblogs.com/wangtao971115/p/10358226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值